This section is set pieces of information that I keep referring to. Initially each piece was in a relevant section, but I kept reordering sections, and I'd wind up talking about list[] before I explained how they're used.
Here's a documentation template for a function
""" name: fencepost(d) author: Fred Flintstone (C) 2008 license: GPL v3 parameters: d: int or string; length of fence returns: number of fence posts, int modules: imports math method: assumes a distance of 10 between fence posts. number of fenceposts=(length of fence/10)+1 """ |
Here's a code fragment that generates a random number
>>> import random >>> random.randint(0,10) 0 >>> random.randint(0,10) 6 |
random is a module (code that's not part of the base python code, but can be imported to add extra functionality). In the above example, the randint() function was called twice, generating two different numbers.
Note | |
---|---|
Because random is a module, you need an import random statement at the top of the section of code containing randint(): i.e. if randint() is in main(), then the import statement must be at/near the top of main(); if randint() is in a function, then import must be at/near the top of the function. The import random and the randint() call must be in the same scope. If you forget the import random statement, then python will complain about random being undefined. For ease of maintenance, all import statements in any scope should be together. |
randint() generates a random integer using the same syntax as range(). Being an intelligent and reasonable person, what would you expect the range of random numbers to be from the above code fragment [366] ? The answer python gives is [367] .
I've said that writeable global variables are a bad idea, because some other function, perhaps written later, can modify them, without you realising that it's happening.
I've also used what are generally called "user defined variables" (which are really constants and not variable at all). These are also global variables, which describe fixed numbers for the program (e.g. max number of counters in the game, max/min number of counters you can pick up). You may be able to change some/all of these variables, to get a different sort of game, but you won't change these variables during a run of the program: you'll edit your code instead (you may not be able to change the variable and have a sensible run of the program).
Why are user defined variables OK but global variables not OK? In principle, user defined variables are read-only, and you hope that no-one changes them, but python has no mechanism to enforce this. Other languages (e.g. C++, Ada) enforce the read-only property (e.g. C++ has a keyword const as part of the variable's declaration) and not declaring a variable to be const when it is, is regarded as sloppy programming on a level comparable to driving without seat belts, or not wearing a bicycle helmet (people may refuse to read your code till you've fixed this, because of the likelihood that these variables are being modified).
Other numbers like π will be imported, and yet other numbers (like the "1" in the formula for the number of fenceposts in a 10 section fence) will be in the code, because they are part of the formula and are not variables.
Here's the list of global and user variables that you're likely to find
In python, a data type list[] holds a series of entries, of one type or of mixed type (e.g. all floats; all integers; or a mixture of various types such as strings, integers, floats and objects). (see An Introduction to Python Lists - http://effbot.org/zone/python-list.html).
Note | |
---|---|
The list data type is not a primitive data type, but is an object data type, in particular a list object. We will learn about objects later, probably not in this course, but for the moment you can regard objects as data types built on primitive data types. A banana object would have price (real), color (string), number of days since picking (int), supplier (string), barcode (object). We could make a submarine object, which would be specified by the length (real), weight (real), name (string e.g. "USS Turtle"), date of commissioning (date object, itself various integers), the number of crew (int) amount of food left (lists of food types) and the number of ice cream makers (int). A navy object would include the number of submarine objects, ship objects, and dingey objects. A naval battle game would need at least two navy objects. For more thoughts on the differences and advantages of procedural and object oriented programming (OOP), two of the major styles of imperative programming, see the section comparison procedural and OOP programming. |
Typical operations with lists are adding an item, usually called push() (but in python called append()) and removing an item, called pop().
Here are some examples:
list=[1,2,3,4,5] #initialise a list with content for item in list: #retrieve and print entries in a list print item list=range(0,10) #range() creates a list list=[] #initialise an empty list item=1 #find something to put in the list list.append(item) #add item to end of list item=2 list.append(item) list #print list len(list) #number of items in list variable=list.pop() #remove last entry from list and assign it to variable variable #show the value of variable list #show the value of list len(list) list.pop() #remove last entry from list, but don't assign the value to any variable len(list) |
Here's the code in interactive mode
pip:/src/da/python_class/class_code# python Python 2.4.4 (#2, Mar 30 2007, 16:26:42) [GCC 3.4.6] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> list=range(0,10) >>> list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> list=[1,2,3,4,5] >>> list [1, 2, 3, 4, 5] >>> list=[] >>> item=1 >>> list.append(item) >>> item=2 >>> list.append(item) >>> list [1, 2] >>> len(list) 2 >>> for item in list: ... print item ... 1 2 >>> variable=list.pop() >>> variable 2 >>> list [1] >>> len(list) 1 >>> list.pop() 1 >>> len(list) 0 |
Try these in interactive mode:
Here's my code [368]
The entries in a list can be anything, including a list. Let's put a list into a list of lists.
# functionally the same as previous code list_of_lists=[] #initialise list_of_lists[] and list[] list=[] list.append(1) list.append(2) list for item in list: print item #now add this list to a list of lists list_of_lists.append(list) #list_of_lists[] contains a single item, list[] list_of_lists #print list_of_lists, what do you expect for output?C #lets add some more items to list_of_lists[] (the items being themselves a list[]) list=[] #why do I reinitialise list[]? list.append(3) list.append(4) list_of_lists.append(list) #how many items are in list_of_lists[]? #Output the items that are in list_of_lists[] list_of_lists #How do you output the items that are in the lists that are in list_of_lists[]? |
Here's the code run interactively
dennis:# python Python 2.4.3 (#1, Apr 22 2006, 01:50:16) [GCC 2.95.3 20010315 (release)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> list=[] >>> list.append(1) >>> list.append(2) >>> list [1, 2] >>> list_of_lists=[] >>> list_of_lists.append(list) >>> list_of_lists [[1, 2]] >>> list=[] >>> list.append(3) >>> list.append(4) >>> list_of_lists.append(list) >>> list_of_lists [[1, 2], [3, 4]] >>> len(list_of_lists) 2 >>> list=[] >>> list.append(5) >>> list.append(6) >>> list_of_lists.append(list) >>> len(list_of_lists) 3 >>> list_of_lists [[1, 2], [3, 4], [5, 6]] >>> for list in list_of_lists: ... print list ... [1, 2] [3, 4] [5, 6] >>> for list in list_of_lists: ... print list ... for item in list: ... print item ... [1, 2] 1 2 [3, 4] 3 4 [5, 6] 5 6 >>> for list in list_of_lists: ... for item in list: ... print item ... 1 2 3 4 5 6 |
Try these examples at the interactive prompt:
Here's my code [369]