sequences of values.
Line 5 creates multiple-subscript tuple table2 and provides six values in three sub tuples (i.e., tuples-within-tuples) The first subtuple (row) contains two elements with
60 return total / len( setOfGrades )
62 63 # main program 64 grades = [ [ 77, 68, 86, 73 ], 65 [ 96, 87, 89, 81 ], 66 [ 70, 90, 86, 81 ] ] 67 68 printGrades( grades )
69 print "\n\nLowest grade:", minimum( grades ) 70 print "Highest grade:", maximum( grades ) 71 print "\n"
72
73 # print average for each student 74 for i in range( len( grades ) ):
75 print "Average for student", i, "is", average( grades[ i ] )
Fig. 5.21 Fig. 5.21 Fig. 5.21
Function printGrades uses the list grades and variables students (number of rows in the list) and exams (number of columns in the list). The function loops through list
grades, using nested for structures to print out the grades in tabular format. The outer
for structure (lines 19–25) iterates over i (i.e., the row subscript), the inner for structure (lines 22–23) over j (i.e., the column subscript).
Functions minimum and maximum loop through list grades, using nested for structures. Function minimum compares each grade to variable lowScore. If a grade is less than lowScore, lowScore is set to that grade (line 36). When execution of the nested structure is complete, lowScore contains the smallest grade in the double-sub- scripted list. Function maximum works similarly to function minimum.
Function average takes one argument—a single-subscripted list of test results for a particular student. When line 75 invokes average, the argument is grades[ i ], which specifies that a particular row of the double-subscripted list grades is to be passed to
average. For example, the argument grades[ 1 ] represents the four values (a single- subscripted list of grades) stored in the second row of the double-subscripted list grades. Remember that, in Python, a double-subscripted list is a list with elements that are single- subscripted lists. Function average calculates the sum of the list elements, divides the total by the number of test results and returns the floating-point result.
In the above example, we demonstrated how to use double-subscripted lists. However, when we need to compute pure numerical problems (i.e., multi-dimensional arrays), the basic Python language cannot handle them efficiently. In this case, a package called NumPy should be used. The NumPy (numerical python) package contains modules that handle arrays, and it provides multi-dimensional array objects for efficient computation. For more information on NumPy, visit sourceforge.net/projects/numpy.
Chapters 2–5 introduced the basic-programming techniques of Python. In Chapter 6, Introduction to the Common Gateway Interface (CGI), we will use these techniques to design Web-based applications. In Chapters 7–9, we will introduce object-oriented pro- gramming techniques that will allow us to build complex applications in the latter half of the book.
The list is:
[0] [1] [2] [3] grades[0] 77 68 86 73 grades[1] 96 87 89 81 grades[2] 70 90 86 81 Lowest grade: 68 Highest grade: 96
Average for student 0 is 76.0 Average for student 1 is 88.25 Average for student 2 is 81.75
Fig. 5.21 Fig. 5.21 Fig. 5.21
SUMMARY
• Data structures hold and organize information (data).
• Sequences, often called arrays in other languages, are data structures that store related data items. Python supports three basic sequence data types: a string, a list and a tuple.
• A sequence element may be referenced by writing the sequence name followed by the element’s position number in square brackets ([]). The first element in a sequence is the zeroth element. • Sequences can be accessed from the end of the sequence by using negative subscripts.
• The position number more formally is called a subscript (or an index), which must be an integer or an integer expression. If a program uses an integer expression as a subscript, Python evaluates the expression to determine the location of the subscript.
• Some types of sequences are immutable—the sequence cannot be altered (e.g., by changing the value of one of its elements). Python strings and tuples are immutable sequences.
• Some sequences are mutable—the sequence can be altered. Python lists are mutable sequences. • The length of the sequence is determined by the function call len( sequence ).
• To create an empty string, use the empty quotes (i.e., "", '',""" """ or ''' ''')
• To create an empty list, use empty square brackets (i.e., []). To create a list that contains a se- quence of values, separate the values with commas, and place the values inside square brackets. • To create an empty tuple, use the empty parentheses (i.e., ()). To create a tuple that contains a
sequence of values, simply separate the values with commas. Tuples also can be created by sur- rounding the tuple values with parentheses; however, the parentheses are optional.
• Creating a tuple is sometimes referred to as packing a tuple.
• When creating a one-element tuple—called a singleton—write the value, followed by a comma (,). • In practice, Python programmers distinguish between tuples and lists to represent different kinds
of sequences, based on the context of the program.
• Although lists are not restricted to homogeneous data types, Python programmers typically use lists to store sequences of homogeneous values—values of the same data type. In general, a pro- gram uses a list to store homogeneous values for the purpose of looping over these values and per- forming the same operation on each value. Usually, the length of the list is not predetermined and may vary over the course of the program.
• The += augmented assignment statement can insert a value in a list. When the value to the left of the += symbol is a sequence, the value to the right of the symbol must be a sequence also. • The for/in structure iterates over a sequence. The for structure starts with the first element in
the sequence, assigns the value of the first element to the control variable and executes the body of the for structure. Then, the for structure proceeds to the next element in the sequence and performs the same operations.
• If a program attempts to access a nonexistent index, the program exits and displays an “out-of- range” error message. This error can be caught as an exception.
• Tuples store sequences of heterogeneous data. Each data piece in a tuple represents a part of the total information represented by the tuple. Usually, the length of the tuple is predetermined and does not change over the course of a program’s execution. A program usually does not iterate over a sequence, but accesses the parts of the tuple the program needs to perform its task.
• If a program attempts to modify a tuple, the program exits and displays an error message. • Sequences can be unpacked—the values stored in the sequence are assigned to various identifiers.
Unpacking is a useful programming shortcut for assigning values to multiple variables in a single statement.
• When unpacking a sequence, the number of variable names to the left of the = symbol must equal the number of elements in the sequence to the right of the symbol.
• Python provides the slicing capability to obtain contiguous regions of a sequence.
• To obtain a slice of the ith element through the jth element, inclusive, use the expression se-
quence[ i:j + 1 ].
• The dictionary is a mapping construct that consists of key-value pairs. Dictionaries (called hashes or associative arrays in other languages), can be thought of as unordered collections of values where each value is accessed through its corresponding key.
• To create an empty dictionary, use empty curly braces (i.e., {}).
• To create a dictionary with values, use a comma-separated sequence of key-value pairs, inside curly braces. Each key-value pair is of the form key : value.
• Python dictionary keys must be immutable values, like strings, numbers or tuples, whose elements are immutable. Dictionary values can be of any Python data type.
• Dictionary values are accessed with the expression dictionaryName[ key ].
• To insert a new key-value pair in a dictionary, use the statement dictionaryName[ key ] = value. • The statement dictionaryName[ key ] = value modifies the value associated with key, if the dictio-
nary already contains that key. Otherwise, the statement inserts the key-value pair into the dictionary. • Accessing a non-existent dictionary key causes the program to exit and to display a “key error”
message.
• A method performs the behaviors (tasks) of an object.
• To invoke an object’s method, specify the name of the object, followed by the dot (.) access op- erator, followed by the method invocation.
• List method append adds an items to the end of a list.
• List method count takes a value as an argument and returns the number of elements in the list that have that value. If the list contains no elements with the specified value, method count re- turns 0.
• Dictionary method items returns a list of tuples, where each tuple contains a key-value pair. Dic- tionary method keys returns an unordered list of the dictionary’s keys. Dictionary method val-
ues returns an unordered list of the dictionary’s values.
• Dictionary method copy returns a new dictionary that is a shallow copy of the original dictionary. In a shallow copy, the elements in the new dictionary are references to the elements in the original dictionary.
• If the programmer wants to create a copy—called a deep copy—that is independent of the original dictionary, Python provides module copy. Function copy.deepcopy returns a deep copy of it argument.
• In many programming languages, the two ways to pass arguments to functions are pass-by-value and pass-by-reference (also called pass-by-value and pass-by-reference).
• When an argument is passed by value, a copy of the argument’s value is made and passed to the called function.
• With by reference, the caller allows the called function to access the caller’s data directly and to modify that data.
• Unlike many other languages, Python does not allow programmers to choose between pass-by-val- ue and pass-by-reference to pass arguments. Python arguments are always passed by object refer- ence—the function receives references to the values passed as arguments. In practice, pass-by- object-reference can be thought of as a combination of pass-by-value and pass-by-reference.
• If a function receives a reference to a mutable object (e.g., a dictionary or a list), the function can modify the original value of the object. It is as if the object had been passed by reference. • If a function receives a reference to an immutable object (e.g., a number, a string or a tuple whose
elements are immutable values), the function cannot modify the original object directly. It is as if the object had been passed by value.
• To pass a list argument to a function, specify the name of the list without square brackets. • Although entire lists can be changed by a function, individual list elements that are numeric and
immutable sequence data types cannot be changed. To pass a list element to a function, use the subscripted name of the list element as an argument in the function call.
• Slicing creates a new sequence; therefore, when a program passes a slice to a function, the original sequence is not affected.
• Sorting data is the process of placing data into a particular order.
• By default, list method sort sorts the elements of a list in ascending order.
• Some sorting algorithms are simple to express and program, but are inefficient. Other algorithms are complex and sophisticated, but provide increased performance.
• Often, programmers work with large amounts of data stored in lists. It might be necessary to de- termine whether a list contains a value that matches a certain key value. The process of locating a particular element value in a list is called searching.
• Keyword in tests whether a sequence contains a particular value.
• List method index takes a search key as a parameter, searches through the list and returns the index of the first list value that matches the search key. If the list does not contain any value that matches the search key, the program displays an error message.
• Sequences can contain elements that are also sequences. Such sequences have multiple subscripts. A common use of multiple-subscripted sequences is to represent tables of values consisting of in- formation arranged in rows and columns.
• To identify a particular table element, we must specify two subscripts—by convention, the first identifies the element’s row, the second identifies the element’s column.
• Sequences that require two subscripts to identify a particular element are called double-subscript- ed sequences or two-dimensional sequences.
• Python does not support multiple-subscripted sequences directly, but allows programmers to spec- ify single-subscripted tuples and lists whose elements are also single-subscripted tuples and lists, thus achieving the same effect.
• A sequence with m rows and n columns is called an m-by-n sequence. It is more commonly know as two-dimensional sequence.
• The name of every element in a multiple-subscripted sequence is of the form a[ i ][ j ], where
a is the name of the sequence, and i and j are the subscripts that uniquely identify the row and column of each element in the sequence.
• To compute pure numerical problems (i.e., multi-dimensional arrays), use package NumPy (nu- merical Python). This package contains modules that handle arrays and provides multi-dimension- al array objects for efficient computation.
TERMINOLOGY
append method of list bracket operator ([])
array clear method of dictionary
SELF-REVIEW EXERCISES
5.1 Fill in the blanks in each of the following statements:
a) are “associative arrays” that consist of pairs.
b) The last element in a sequence can always be accessed with subscript . c) Statement creates a singleton aTuple.
d) Function returns the length of a sequence.
e) Selecting a portion of a sequence with the operator [:] is called . f) Dictionary method returns a list of key-value pairs.
comma (,) m-by-n sequence
copy method of dictionary mapping construct
count list method method
data structure method invocation
deep copy of a dictionary multiple-subscripted sequence
dictionary mutable sequence
dictionary method name (sequence)
double-subscripted sequence NumPy package (numerical Python) dot access operator (.) one-element tuple (singleton)
element out-of-range error message
empty curly braces {} packed
empty dictionary packing a tuple
empty list pass-by-object-reference
empty parentheses () pass-by-reference
empty quotes pass-by-value
empty square brackets [] popitem method of dictionary
empty string position number
empty tuple row
for structure search
get method of dictionary search key
hash sequence
has_key method of dictionary sequence slicing heterogeneous data (in tuples) sequence unpacking
histogram setdefault method of dictionary
homogeneous data (in lists) shallow copy of a dictionary
immutable sequence singleton
in keyword slice a sequence
index slicing operator ([:])
in-place sorting sort
index method of list sort list method
items method of dictionary subscript
iteritems method of dictionary table
iterkeys method of dictionary tuple
itervalues method of dictionary two-dimensional sequence
keys method of dictionary update method of dictionary
key value unpacked sequence
key-value pair value (sequence)
length (sequence) values dictionary method
list zeroth element
g) When an argument is passed , a copy of the argument’s value is made and passed to the called method.
h) Use the expression to obtain the ith element through the jth element of list
sequence, inclusive.
i) A sequence with m rows and n columns is called an .
j) List method returns the number of times a specified element occurs in a list.
5.2 State whether each of the following is true or false. If false, explain why. a) A sequence begins at subscript 1.
b) Strings and tuples are mutable sequences.
c) Each key-value pair in a dictionary has the form key : value. d) Using a tuple as a dictionary key is an error.
e) Dictionary values are accessed with the dot operator. f) Method insert adds one element to the end of a list. g) The += statement appends items into lists.
h) List method sort sorts the elements of a list in place.
i) If list method search finds a list value that matches the search key, it returns the sub- script of the list value.
j) Unlike other languages, Python does not allow the programmer to choose whether to pass each argument pass-by-value or pass-by-reference.
ANSWERS TO SELF-REVIEW EXERCISES
5.1 a) Dictionaries, key-value. b) -1. c) aTuple = 1,. d) len. e) slicing. f) items. g) pass-by- value. h) sequence[ i:j + 1]. i) m-by-n sequence. j) count.
5.2 a) False. The first element in every sequence has subscript 0. b) False. Strings and tuples are immutable sequences—their values cannot be altered. c) True. d) False. Dictionary keys must be im- mutable data types, such as tuples. e) False. Dictionary values are accessed with the expression dic-
tionaryName[ key ]. f) False. Method append adds one element to the end of a list. g) True.
h) True. i) False. If list method index finds a list value that matches the search key, it returns the subscript of the list value. j) True.
EXERCISES
5.3 Use a list to solve the following problem: Read in 20 numbers. As each number is read, print it only if it is not a duplicate of a number already read.
5.4 Use a list of lists to solve the following problem. A company has four salespeople (1 to 4) who sell five different products (1 to 5). Once a day, each salesperson passes in a slip for each differ- ent type of product sold. Each slip contains:
a) The salesperson number. b) The product number.
c) The number of that product sold that day.
Thus, each salesperson passes in between 0 and 5 sales slips per day. Assume that the information from all of the slips for last month is available. Write a program that will read all this information for last month’s sales and summarize the total sales by salesperson by product. All totals should be stored in list sales. After processing all the information for last month, display the results in tabu- lar format, with each of the columns representing a particular salesperson and each of the rows rep- resenting a particular product. Cross-total each row to get the total sales of each product for last month; cross-total each column to get the total sales by salesperson for last month. Your tabular printout should include these cross-totals to the right of the totaled rows and at the bottom of the totaled columns.
5.5 (The Sieve of Eratosthenes) A prime integer is any integer greater than 1 that is evenly divis- ible only by itself and 1. The Sieve of Eratosthenes is a method of finding prime numbers. It operates as follows:
a) Create a list with all elements initialized to 1 (true). List elements with prime subscripts will remain 1. All other list elements will eventually be set to zero.
b) Starting with list element 2, every time a list element is found whose value is 1, loop through the remainder of the list and set to zero every element whose subscript is a mul- tiple of the subscript for the element with value 1. For list subscript 2, all elements be- yond 2 in the list that are multiples of 2 will be set to zero (subscripts 4, 6, 8, 10, etc.); for list subscript 3, all elements beyond 3 in the list that are multiples of 3 will be set to zero (subscripts 6, 9, 12, 15, etc.); and so on.
When this process is complete, the list elements that are still set to 1 indicate that the subscript is a prime number. These subscripts can then be printed. Write a program that uses a list of 1000 ele- ments to determine and print the prime numbers between 2 and 999. Ignore element 0 of the list.
5.6 (Bubble Sort) Sorting data (i.e. placing data into some particular order, such as ascending or descending) is one of the most important computing applications. Python lists provide a sort meth- od. In this exercise, readers implement their own sorting function, using the bubble-sort method. In the bubble sort (or sinking sort), the smaller values gradually “bubble” their way upward to the top of the list like air bubbles rising in water, while the larger values sink to the bottom of the list. The pro- cess that compares each adjacent pair of elements in a list in turn and swaps the elements if the second element is less than the first element is called a pass. The technique makes several passes through the list. On each pass, successive pairs of elements are compared. If a pair is in increasing order, bubble sort leaves the values as they are. If a pair is in decreasing order, their values are swapped in the list.