A Very Brief Introduction to PythonAnd its Data-Types
![]() Python and its Data-Types
IntroductionThe Python programming language is an Open Source, cross-platform, high level, dynamic, interpreted [1] language. The Python 'philosophy' emphasises readability, clarity and simplicity, whilst maximising the power and expressiveness available to the programmer. The ultimate compliment to a Python programmer is not that his code is clever, but that it is elegant. For these reasons Python is an excellent 'first language', while still being a powerful tool in the hands of the seasoned and cynical programmer. Python is a very flexible language. It is widely used for many different purposes. Typical uses include :
This is a brief introduction to the Python and its basic datatypes. It may someday become a full Python tutorial. If you have any specific questions to do with Python programming, you should try the Python Newsgroup, which is generally a friendly place. For further resources, try the following sites : The Interactive InterpreterYou can download Python for almost every platform from the Python website. Linux and Mac OS X will almost certainly already have Python installed, but it may not be the latest version. We recommend using the latest stable version of Python, which at the time of writing is Python 2.5. After installing Python, launch the interpreter. On a Linux platform you should be able to do this by typing python at a command prompt. On windows you should double click on the Python icon. This will launch the interactive interpreter. It will look something like this : Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> The interactive interpreter allows you to type in individual Python commands, and see the results straight away. It is a great way to experiment, but also a useful tool. Type x = 3, then hit enter, followed by print x followed by enter. You should see the following results in your interpreter 'session' : Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> x = 3 >>> print x 3 >>> Creating and Running ProgramsIn order to be a useful language we have to be able to create programs and run them. Python programs are just text files that contain instructions for the Python interpreter. You can create them with any text editor (including notepad), or a programmers text editor called an IDE: Integrated Development Environment. IDEs for Python will often have useful built in tools like syntax checkers, debuggers, code browsers and much more. Most Python installs include a simple IDE called IDLE, which is very handy. CommentsAny line which starts with a hash (#) is a comment. It will be skipped by the interpreter. There is no multiline comment in Python, you should start every line with a hash instead. Comments in code are good (so long as you keep them up to date), but nothing like so important as making your code readable. VariablesIn the interactive interpreter session we actually created a very simple Python program. We created a variable called 'x' and associated it with the value 3, the number. We then printed this value to the screen. In almost any programming language, the 'variable' is the most basic concept. Variables are the way that we can store values and work with them. We might process these values and save them to a file, or perhaps send them across the internet, but variables are the way we keep track of them. The line of code x = 3 is a statement which means it does a job. The job it does is to assign the value 3 to the variable 'x'. In Python terminology we say that it binds the name 'x' to the number three. This idea of variables being names bound to objects (or names which 'reference' objects) is important in Python. A variable name in Python can be almost anything, so long as it obeys certain rules :
Basic Variable TypesJust as variables are a basic concept in programming, so are the different types of values that a variable can have. We call these 'data-types', because they are different types of ways we can store data. In programming the word 'type' has a specific meaning. It is a precise way of categorising different sorts of objects. Every object has a 'type'. The most basic data types are listed below. It is often possible to convert between these different types (for example converting the text '10' into the integer value 10), but that is a subject for another day. Numbers
Warning In current versions of Python, dividing an integer by an integer will return an integer. You can get unexpected results like : >>> 7/2 3 To get the answer you expect, one of the numbers must be a floating point number : >>> 7.0/2 3.5 There is also a complex number basic datatype in Python. They aren't very common, but can be extremely useful for certain types of maths. They are written : 3i + 4j StringsA string is any piece of text. Python does not distinguish between a string that is a single character or a large block of text. Because of the many ways that Python provides for working with strings, it is an ideal language to do text processing with. The name 'string' probably comes from the fact that programming languages often treat text as a sequence of characters 'stringed' together. Python has two different string-types. The basic string type (which I have called 'normal strings' below) store the text as a sequence of bytes (numbers) with one byte per character. These should only really be used if you know your text will only have ASCII values in it. Unfortunately they are terribly convenient to use, so many programs just use this sort of string. They can also be used for storing binary data in. They are sometimes called byte-strings, which is quite a good description of what they do. Unicode strings store text internally using the unicode standard. They are slightly more complicated, because you must know the 'encoding' the text is stored in whenever you read the text in or save it out. In the long run this can save a great deal of confusion. Below is an overview of the two different string types, and how to include them in your programs.
Python strings, like the other built-in types, have lots of useful methods for doing string-type-things with them. Refer to the string methods in the Python docs for a full list. Other Useful ValuesThere are a few other useful values which can be very handy.
These values turn up a great deal when programming. You can include them in your program by writing them literally : x = None y = True z = False More Complex Data TypesIn order to work with information, you will often want to group data together and work on it in that group. Python contains lots of powerful concepts for doing this, including fancy things like iterators, generators and list comprehensions. The most fundamental way of grouping data together is to use 'data-structures'. These are objects that store your values together. With Python you are free to create your own custom data structures, but it has four very powerful ones built in. Objects that store data for you are called containers. All these four data-types (the list, the tuple, the set and the dictionary) have lots of different operations you can perform on them. Many of these are built-in to them and are called 'methods'. This introduction will not cover these, instead it will just introduce you to them and how you include them in your programs. The ListA list stores a group of items one after the other. It keeps them in order for you. You can add or remove items as you wish, sort your list or search through it to see if it contains a specific value. You can start with an empty list and add things to it, or use a pre-populated list. Items in your list can be any object, including other lists. This means that you can have lists of lists, and use them to create complex nested structures. They are a very useful fundamental building block. A list is written using square brackets, with commas between the items : # w is an empty list w = [] # x has only one member x = [3.0] # y is a list of numbers y = [1, 2, 3, 4, 5] # z is a list of strings # and other lists z = ['first', [], 'second', [1, 2, 3, 4], 'third'] We access the members of a list by 'indexing' into it. The index must be an integer representing the position of item. The first item is in position zero. You can index a variable by placing square brackets after the variable with the index in. If you try to access a position that does not exist (for example the fifth item of a list with only four members), you will get an IndexError. You can access from the end of the list, by using negative indexes. Position -1 is the last item, -2 is the one before the last etc. >>> x = [1, 2, 3, 4, 5] >>> print x[0] 1 >>> print x[4] 5 >>> print x[-1] 5 >>> print x[-5] 1 >>> print x[10] Traceback (most recent call last): File "<stdin>", line 1, in ? IndexError: list index out of range Lists have lots of methods for working with them. They also have special syntax for working on parts of a list. This is called slicing. See the page on sequence types for details. TuplesTuples are a lot like lists in that they are used to store sequences of information. Unlike lists they can't be altered after they have been created. Because the interpreter knows the length and types of the members of a tuple, working with them can be more efficient than using a list. You can index and slice tuples, but not change them. You come across tuples 'implicitly' in many places in Python, for example functions that return multiple values return them as a tuple. Tuples are created using parentheses instead of square brackets : >>> x = (1, 2, 3,4) >>> x (1, 2, 3, 4) >>> x[0] 1 >>> x = (1,) >>> x (1,) >>> x[0] 1 >>> x = () >>> x () >>> x[0] Traceback (most recent call last): File "<stdin>", line 1, in ? IndexError: tuple index out of range You may have noticed that to create a tuple with only one member you need to add a comma into the mix: (1,). (This: (1) isn't a tuple.) To create an empty tuple (there must be a reason why you'd want to do that sometime), do this (), or even this: tuple(). By convention in Python, tuples are used when the elements are heterogeneous (different types of objects), lists are used for homogeneous collections. This is a distinction observed more in theory than in practise. DictionariesDictionaries are an extremely useful and flexible datatype. In fact Python is built on them. Dictionaries store values with keys, so you can look them up with the key. You can use them to build complex nested data-structures or just for storing values. Like the other data-types we've looked at so far, Python has built in syntax for creating dictionaries : >>> x = {'key1': 'value1', 'key2': 'value2'} >>> print x {'key2': 'value2', 'key1': 'value1'} You can retrieve the values by indexing the dictionary, using the same syntax as for list; except using the key instead of the position. (Dictionaries are unordered by the way.) >>> x = {'key1': 'value1', 'key2': 'value2'} >>> print x['key1'] value1 You don't have to use strings as keys, you can use any object that doesn't change (any 'immutable' object). This means that strings and numbers are in and dictionaries and lists are out. This is another place where tuples come in handy. >>> attemptedKey = {} >>> x = {attemptedKey: 'some value to store'} Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: dict objects are unhashable >>> anotherTry = (None, 2) >>> x = {anotherTry: 'some value to store'} >>> print x {(None, 2): 'some value to store'} See the Mapping Types Reference for a list of methods available on dictionary objects. SetsThe set is an extremely useful data type. It was introduced in Python 2.3 and became a built-in in Python 2.4. Sets contain unique members. They are very efficient if you want to test if a value is in the set (test for membership). It is a lot faster than checking if an object is in a list. Unlike the other data-types we've seen, there is no syntax for using them. To create a set, you use the set constructor. See below : >>> x = set() >>> x set([]) >>> x.add(1) >>> x set([1]) >>> x.add(1) >>> x set([1]) >>> x.add(2) >>> 1 in x True >>> 2 in x True >>> 3 in x False >>>
For buying techie books, science fiction, computer hardware or the latest gadgets: visit The Voidspace Amazon Store. If you're looking for a new techie job, try the Voidspace Tech Job Board. This is part of the Hidden Network of technology and programming jobs.
Last edited Fri Feb 15 13:42:08 2008. Counter... |
|||
|
Blogads
Follow me on: Tech Jobs |