Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Internet & World Wide Web How to Program, 3/e
Internet & World Wide Web How to Program, 3/e

ISBN:
0-13-145091-3
© 2004
pages: 1420

Order
Amazon logo

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.
Download the Code Examples
[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.
1   # Fig. 35.7: fig35_07.py
2   # A program that illustrates tuples, lists and dictionaries.
3   
4   # tuples
5   aTuple = ( 1, "a", 3.0 ) # create tuple
6   firstItem = aTuple[ 0 ] # first tuple item
7   secondItem = aTuple[ 1 ] # second tuple item
8   thirdItem = aTuple[ 2 ] # third tuple item
9
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
13   print
14
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
19   print
20
21   aTuple += ( 4, )
22   print "Used the += statement on the tuple"
23   print
24
25   # print the tuple
26   print "The raw tuple data is:", aTuple
27   print "The items in the tuple are:"
28
29   for item in aTuple: # print each item
30      print item,
31
32   print # end previous line
33   print # blank line
34
35   # lists
36   aList = [ 1, 2, 3 ] # create list
37   aList[ 0 ] = 0 # change first element of list
38   aList.append( 5 ) # add item to end of list
39
40   print "The raw list data is:", aList # print list data
41   print
42
43   aList += [ 4 ] # add an item to the end of the list
44   print "Added an item to the list using the += statement"
45   print
46
47   # print each item in the list
48   print "The items in the list are:"
49
50   for item in aList:
51      print item,
52
53   print # end previous line
54   print # blank line
55
56   # dictionaries
57   aDictionary = { 1 : "January", 2 : "February", 3 : "March",
58                   4 : "April", 5 : "May", 6 : "June", 7 : "July",
59                   8 : "August", 9 : "September", 10 : "October",
60                   11 : "November" }
61   aDictionary[ 12 ] = "December" # add item to dictionary
62
63   print "The raw dictionary data is:", aDictionary
64   print "\nThe entries in the dictionary are:"
65
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:
1 a 3.0 4
The raw list data is: [0, 2, 3, 5]
Added an item to the list using the += statement
The items in the list are:
0 2 3 5 4
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[ 7 ] = July
aDictionary[ 6 ] = June
aDictionary[ 5 ] = May
aDictionary[ 4 ] = April
aDictionary[ 3 ] = March
aDictionary[ 2 ] = February
aDictionary[ 1 ] = January

 
Other Python Tutorials:
Introduction to Python
Pytthon Basic Data Types, Control Statements and Functions
Return to Tutorial Index
Page 1 | 2 | 3