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
In addition to basic data types that store numerical values and strings, Python defines three data types for storing more complex data: the
list (a sequence of related data), the
tuple (pronounced
too-ple; a list whose elements may not be modified) and a
dictionary (a list of values that are accessed through their associated keys). These data types are high-level implementations of simple data structures that enable Python programmers to manipulate many types of data quickly and easily. Some Python modules (e.g.,
Cookie and
cgi) use these data types to provide simple access to their underlying data structures. Figure 35.7 is a program that illustrates tuples, lists and dictionaries.
Fig. 35.7 Tuples, lists and dictionaries.
|
|
|
|
5 aTuple = ( 1, "a", 3.0 )
|
6 firstItem = aTuple[ 0 ]
|
7 secondItem = aTuple[ 1 ]
|
8 thirdItem = aTuple[ 2 ]
|
|
10 print "The first item in the tuple is", firstItem
|
11 print "The second item in the tuple is", secondItem
|
12 print "The third item in the tuple is", thirdItem
|
|
|
15 firstItem, secondItem, thirdItem = aTuple
|
16 print "The first item in the tuple is", firstItem
|
17 print "The second item in the tuple is", secondItem
|
18 print "The third item in the tuple is", thirdItem
|
|
|
|
22 print "Used the += statement on the tuple"
|
|
|
|
26 print "The raw tuple data is:", aTuple
|
27 print "The items in the tuple are:"
|
|
|
|
|
|
|
|
|
|
|
|
|
40 print "The raw list data is:", aList
|
|
|
|
44 print "Added an item to the list using the += statement"
|
|
|
|
48 print "The items in the list are:"
|
|
|
|
|
|
|
|
|
57 aDictionary = { 1 : "January", 2 : "February", 3 : "March",
|
58 4 : "April", 5 : "May", 6 : "June", 7 : "July",
|
59 8 : "August", 9 : "September", 10 : "October",
|
|
61 aDictionary[ 12 ] = "December"
|
|
63 print "The raw dictionary data is:", aDictionary
|
64 print "\nThe entries in the dictionary are:"
|
|
66 for item in aDictionary.keys():
|
67 print "aDictionary[ ", item, " ] = ", aDictionary[ item ]
|
The first item in the tuple is 1
The second item in the tuple is a
The third item in the tuple is 3.0
The first item in the tuple is 1
The second item in the tuple is a
The third item in the tuple is 3.0
Used the += statement on the tuple
The raw tuple data is: (1, 'a', 3.0, 4)
The items in the tuple are:
The raw list data is: [0, 2, 3, 5]
Added an item to the list using the += statement
The items in the list are:
The raw dictionary data is: {12: 'December', 11: 'November', 10: 'October', 9: 'September', 8: 'August', 7: 'July', 6: 'June', 5: 'May', 4: 'April', 3: 'March', 2: 'February', 1: 'January'}
The entries in the dictionary are:
aDictionary[ 12 ] = December
aDictionary[ 11 ] = November
aDictionary[ 10 ] = October
aDictionary[ 9 ] = September
aDictionary[ 8 ] = August
aDictionary[ 2 ] = February
aDictionary[ 1 ] = January
|