A GUIDE TO WORKING WITH THE PYTHON INTERPRETER. -----1--------------------------------------------------------------- This file has a few conventions. The symbol '>>>' represents the interactive python prompt. A line such as >>> x=4.5 means that 'x=4.5' was typed at the prompt, and then the ENTER key was pressed. A sequence of lines such as >>> x 4.5 >>> means that 'x' was typed at the prompt, then the python interpreter displayed '4.5' and finally returned the prompt. Anything on a line following a '#' symbol is ignored by the python interpreter. In the stuff that follows you'll see lines like this: >>> x=4.5 # This defines the variable 'x' to be equal to 4.5 If you're following along on your interpreter, you can ignore the '#' and anything following it. You would just type >>> x=4.5 and then hit ENTER. ----2---------------------------------------------------------------- Before trying things out, let me try to give you some feel for how to think about things when you're using python. First off, in python, everything is an 'object'. The 'type' of an 'object' is essentially determined by what 'attributes' it has. A real world example may help. A pig and a pigeon are not of the same type: the ability to fly is an attribute that a pigeon has which a pig - thank goodness - does not. One difference between python and the real world is that, in python, the attributes of an object are themselves objects. Try not to think about it. The common python types and some examples are integer 42 float 3.14159 string 'Cleveland rocks.' tuple (42, 2.801, 'arggh', 2) list [42, 2.801, 'arggh', 2] dictionary {'a': 23, 'b': 7.7, 11: 4, 12: 'oof'} function max ----3---------------------------------------------------------------- One final note before we get down to business. The assignment operator in python is '='. So >>> x=4.5 creates a new variable with name 'x' and value '4.5'. Try this sequence of commands: >>> x=4.5 >>> x*2 >>> x**2 >>> x/2 >>> x//2 Can you figure out what the operators '*', '**', '/' and '//' are? So what should the following commands do? >>> 2*x >>> 2**x >>> 2/x >>> 2//x ----4---------------------------------------------------------------- The most import data types for us will be string, list, and dictionary. We're going to play around with them a bit, but first you should realize another thing about python objects: some of their attributes are functions (called 'methods'), and some methods return a value, while others do not, but do something to the object instead. First we'll define a string variable named 'pfui', and try out some of its methods. A string method always returns a value; it NEVER does anything to the original string. >>> pfui = "This is not one of your better days Archie." >>> pfui >>> pfui.upper() >>> pfui.lower() >>> pfui.count('t') >>> pfui.lower().count('t') To see a complete list of the methods of 'pfui' type >>> dir(pfui) Index 'slicing' allows us to retrieve parts of 'pfui' in various ways. Things to try: >>> pfui[1] >>> pfui[8] >>> pfui[1:8] >>> pfui[:8] >>> pfui[8:] >>> pfui[3:25:2] >>> pfui[::2] >>> pfui[33,4,-1] >>> pfui[::-1] And just how many letters does 'pfui' have? >>> len(pfui) ----5---------------------------------------------------------------- We can make a list out of a string. >>> Lui = list(pfui) >>> Lui We could have done the same thing using 'list comprehension': >>> Lui = [ c for c in pfui ] >>> Lui A list has a different set of methods than a string. Also, a list method will sometimes modify the original list and return 'None' - python's version of nothing. Some methods to try: >>> Lui.sort() >>> Lui >>> Lui.reverse() >>> Lui >>> Lui.append(56) >>> Lui >>> Lui.append(('hoo','haw')) >>> Lui >>> Lui.extend(('hoo','haw')) >>> Lui Slicing works for lists too. >>> Lui[1] >>> Lui[8] >>> Lui[1:8] >>> Lui[:8] >>> Lui[8:] >>> Lui[3:25:2] >>> Lui[::2] >>> Lui[33,4,-1] >>> Lui[::-1] ----6---------------------------------------------------------------- Lists and strings work and play well together. >>> urf = "Now what Bob?" >>> Lrf = list(urf) >>> "xx".join(Lrf) Note that the following line just returns a new string that is the same as the original 'urf'. >>> "".join(list(urf)) ----7---------------------------------------------------------------- Dictionaries are just lists with custom made indices. There are a lot of different ways to build a dictionary. Here's one: >>> dewey = {'yy':34, 34:4.75, 'top'='bottom'} >>> dewey.keys() >>> dewey.values() >>> dewey.items() >>> dewey['yy'] >>> dewey[34] >>> dewey['top'] >>> dewey['bottom'] This last command should have led to your first(?) error message. >>> dewey['bottom']=78 >>> dewey['bottom'] ----8---------------------------------------------------------------- Looping through a string, list, or dictionary is easy to do. >>> for x in pfui: x ... # Hit ENTER again >>> for x in Lui: x ... # Hit ENTER again >>> for x in dewey: x ... # Hit ENTER again >>> for x in dewey: dewey[x] ... # Hit ENTER again ----9---------------------------------------------------------------- Importing objects from any of the 'modules' supplied wit python is easy. One of the modules we will often find of use is the 'string' module. Here is one way to use it. >>> import string Now let's see what new definitions we have at our disposal. >>> dir(string) Some of these objects are just strings. Take a look. >>> string.digits >>> string.punctuation >>> string.letters >>> string.ascii_uppercase >>> string.ascii_lowercase >>> string.printable >>> string.whitespace Most - if not all - of the functions supplied by the string module correspond to string methods. For example, the following two commands will do the same thing: >>> pfui.count('e') >>> string.count(pfui,'e') ----10--------------------------------------------------------------- Defining your own functions is also easy to do, but the definitions should be made in a file, and not in the interpreter. This is not strictly necessary, but it will make your life much easier. These functions can then be 'imported' just like any of the functions that come with python. The only catch is that the file containing your definitions must be on the python 'path'. To see what directories are on the python path use the following two commands. >>> import sys >>> sys.path Two ways (there are others) to ensure that the directory containing your file are on the python path are: (1) open a command shell and change to the directory containing the file you want to use then start up python by typing 'python' at the shell prompt; (2) use 'IDLE' to edit a file in the same directory as the file you want to use (it can be the same file) and hit the key - this will run the file you are editing and put its directory on the python path. Suppose that we have the directory containing the file 'hobby.py' on the python path. I've defined two functions named 'sport' and 'sp' in this file. Both 'sport' and 'sp' take a single integer as input. Here are a few ways to use the functions. >>> from hobby import sport >>> sport(0) >>> sport(1) >>> from hobby import sport as sp >>> sp(0) >>> sp(1) >>> from hobby import * >>> sport(0) >>> sport(1) >>> sp(0) >>> sp(1) >>> import hobby >>> hobby.sport(0) >>> hobby.sport(1) >>> import hobby as nob >>> nob.sport(0) >>> nob.sport(1) Do you understand what's going on here? ---------------------------------------------------------------------