Internet & World Wide Web How to Program, 3/e
 |
ISBN:
0-13-145091-3
© 2004
pages: 1420
Order

 |
Python defines several data structures that help programmers store and manipulate data quickly and easily. This tutorial presents Python's built-in data structures for lists (
list and
tuple) and key-value pairs (
dictionary). The tutorial is intended for students and developers who are familiar with basic Python programming, or for people who have read our prior Python articles,
Introduction to Python and
Python Basic Data Types, Control Statements and Functions.
[Note: This tutorial is an excerpt (Section 35.3) of Chapter 35, Python, from our textbook
Internet & World Wide Web How to Program, 3/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., INTERNET & WORLD WIDE WEB HOW TO PROGRAM, 3/E, ©2004, pp.1246-1251. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]
35.3 Tuples, Lists and Dictionaries
(Continued)
Line 5 creates a tuple with elements
1,
"a" and
3.0. Tuples are created as a comma-separated list of values inside parentheses. A tuple is used most often to contain combinations of many data types (e.g., strings, integers, other tuples). Lines 6-8 use the
[] operator to access specific elements through an index. The first element in a tuple has index
0.
Tuple element contents are
immutable-they cannot be modified. So the statement
produces a runtime error similar to
Traceback (innermost last):
File "<interactive input>", line 1, in ?
TypeError: object doesn't support item assignment
Common Programming Error 35.5
35.5 Attempting to change an immutable data structure is a syntax error.
Attempting to access a value at a nonexistent element is also an error. The statement
produces a runtime error similar to
Traceback (innermost last):
File "<interactive input>", line 1, in ?
IndexError: tuple index out of range
because
aTuple does not have a tenth element.
Common Programming Error 35.6
35.6 Trying to access an out-of-range element (i.e., an element at an index that does not exist) produces a runtime error.
Line 15
unpacks the items of the tuple into three variables. This statement produces the same results as lines 6-8. Line 21 has the effect of adding an element to the end of variable
aTuple. The right-hand side of the
+= statement must be a tuple; therefore, we must specify a
one-element tuple, or
singleton, on the right-hand side of the statement. The value
( 4, ) is a one-element tuple. The comma after the tuple element value is mandatory, because the value
( 4 ) is an integer.
Because tuples are immutable, the
+= statement actually creates a new tuple that combines the tuple on the left side of the
+= sign (i.e.,
aTuple) with the tuple on the right side of the
+= sign (i.e.,
( 4, )) to create a new tuple. The new tuple is stored in variable
aTuple.
The output of line 26 shows how the
print statement handles a variable that is a tuple. Lines 29-30 use a
for loop to print each element in variable
aTuple.
The statement in line 29 assigns the first element in
aTuple (i.e.,
aTuple[ 0 ]) to variable
item. Line 30 then prints the value of variable
item to the screen. The
for loop iterates over each element in the tuple, assigns the element to variable
item and executes the code in line 30.