bestbarcoder.com

pythonhtp1_05.fm Page 156 Saturday, December 8, 2001 9:35 AM in Microsoft Office Integrating QR Code 2d barcode in Microsoft Office pythonhtp1_05.fm Page 156 Saturday, December 8, 2001 9:35 AM

pythonhtp1_05.fm Page 156 Saturday, December 8, 2001 9:35 AM generate, create none none on none projects USS Code 93 Lists, Tuples and Dictionaries 5 . Outline 5.1 5.2 5.

3 5.4 Introduction Sequences Creating Sequences Using Lists and Tuples 5.4.

1 5.4.2 5.

4.3 5.4.

4 5.5 5.6 5.

7 5.8 5.9 5.

10 Using Lists Using Tuples Sequence Unpacking Sequence Slicing. Dictionari none for none es List and Dictionary Methods =References and Reference Parameters Passing Lists to Functions Sorting and Searching Lists Multiple-Subscripted Sequences. Summary Terminology Self-Review Exercises Answers to Self-Review Exercises Exercises 5.1 Introduction This chapt er introduces Python s data-handling capabilities that use data structures. Data structures hold and organize information (data). Many types of data structures exist, and each type has features appropriate for certain tasks.

Sequences, often called arrays in other languages, are data structures that store (usually) related data items. Python supports three basic sequence data types: the string, the list and the tuple. Mappings, often called associative arrays or hashes in other languages, are data structures that store data in key-value pairs.

Python supports one mapping data type: the dictionary. This chapter discusses Python s sequence and mapping types in the context of several examples. 22, Data Structures, introduces some high-level data structures (linked lists, queues, stacks and trees) that extend Python s basic data types.

. 5.2 Sequences A sequence is a series of contiguous values that often are related. We already have encountered sequences in several programs: Python strings are sequences, as is the value returned by function range a Python built-in function that returns a list of integers. In this section, we discuss sequences in detail and explain how to refer to a particular element, or location, in the sequence.

Figure 5.1 illustrates sequence c, which contains 12 integer elements. Any element may be referenced by writing the sequence name followed by the element s position number in square brackets ([]).

The first element in every sequence is the zeroth element. Thus, in sequence c, the first element is c[ 0 ], the second element is c[ 1 ], the sixth element of sequence c is c[ 5 ]. In general, the ith element of sequence c is c[ i - 1 ].

. pythonhtp1_05.fm Page 157 Saturday, December 8, 2001 9:35 AM 5 . Lists, Tuples and Dictionaries Name of sequence (c). c[ 0 ] c[ none none 1 ] c[ 2 ] c[ 3 ] c[ 4 ] c[ 5 ] c[ 6 ] c[ 7 ] c[ 8 ] c[ 9 ] c[ 10 ] c[ 11 ]. -45 6 0 72 1543 -89 0 62 -3 1 6453 78. c[ -12 ] c none none [ -11 ] c[ -10 ] c[ -9 ] c[ -8 ] c[ -7 ] c[ -6 ] c[ -5 ] c[ -4 ] c[ -3 ] c[ -2 ] c[ -1 ]. Position number of the element within sequence c Fig. 5.1 Sequence with elements and indices. Sequences none none also can be accessed from the end. The last element is c[ -1 ], the second to last element is c[ -2 ] and the ith-from-the-end is c[ -i ]. Sequences follow the same naming conventions as variables.

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 index. For example, if variable a equals 5 and variable b equals 6, then the statement.

print c[ a + b ]. prints the none for none value of c[ 11 ]. Integer expressions used as subscripts can be useful for iterating over a sequence in a loop. Python lists and dictionaries are mutable they can be altered.

For example, if sequence c in Fig. 5.1 were mutable, the statement.

c[ 11 ] = 0. modifies t none for none he value of element 11 by assinging it a new value of 0 to replace the original value of 78.. pythonhtp1_05.fm Page 158 Saturday, December 8, 2001 9:35 AM Lists, Tuples and Dictionaries 5 . On the oth none for none er hand, some types of sequences are immutable they cannot be altered (e.g., by changing element values).

Python strings and tuples are immutable sequences. For example, if the sequence c were immutable, the statement. c[ 11 ] = 0. would be i none for none llegal. Let us examine sequence c in detail. The sequence name is c.

The length of the sequence is determined by the function call len( c ). It is useful to know a sequence s length, because referring to an element outside the sequence results in an out-ofrange error. Most of the errors discussed in this chapter can be caught as exceptions.

[Note: We discuss exceptions in 12, Exception Handling.] Sequence c contains 12 elements, namely c[ 0 ], c[ 1 ], , c[ 11 ]. The range of elements also can be referenced by c[ -12 ], c[ -11 ], .

.., c[ -1 ].

In this example, c[ 0 ] contains the value -45, c[ 1 ] contains the value 6, c[ -9 ] contains the value 72 and c[ -2 ]contains the value 6453. To calculate the sum of the values contained in the first three elements of sequence c and assign the result to variable sum, we would write. sum = c[ 0 ] + c[ 1 ] + c[ 2 ]. To divide none none the value of the seventh element of sequence c by 2 and assign the result to the variable x, we would write. x = c[ 6 ] / 2.
Copyright © bestbarcoder.com . All rights reserved.