Deitel & Associates, Inc. Logo

Back to
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

© 2004
pages: 1420

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 (Continued)
By default, the print statement writes a newline character (e.g., a carriage return) at the end of its output; however, the comma in line 30 tells Python not to print the newline character. In the next iteration of the for loop, the print statement writes text to the screen on the same line as the previous print statement. Lines 32-33 print a new line and a blank line to the screen, respectively, after all the elements in the tuple have been displayed
Line 36 creates a list that contains elements 1, 2 and 3. Python lists are similar to tuples, except that Python lists are mutable (they may be altered). Line 37 demonstrates this fact by assigning the value 0 to the element in the list at index 0. Line 38 adds an element to the end of a list by calling list method append. Lists also support several other methods (Fig. 35.8).
The output from the statement in line 40 shows how the print statement handles a variable that is a list. Line 43 adds the integer 4 to variable aList, using the += statement. The value on the right side of the += statement must be a list (or another sequence, such as a string or tuple). In this case, the list contains one element. The for statement (lines 50-51) prints each element of the list to the screen.
Fig. 35.8 Python list methods.
append( item )
Inserts item at the end of the list.
count( item )
Returns the number of occurrences of item in the list.
extend( newList )
Inserts newList at the end of the list.
index( item )
Returns the index of the first occurrence of item in the list. If the element is not in the list, a ValueError exception occurs. [Note: We discuss exceptions in Section 35.5]
insert( index, item )
Inserts item at position index.
pop( [index] )
Removes and returns the last element in the list. If the optional parameter index is specified, removes and returns the element at position index.
remove( item )
Removes the first occurrence of item from the list. If item is not in the list, a ValueError exception occurs.
Reverses the items in the list.
sort( [function] )
Sorts the items in the list. Optional parameter function is a comparison function that may be user defined.
Lines 57-60 create a Python dictionary. Each entry in a dictionary has two parts-a key and a value-and a dictionary consists of a set of zero or more comma-separated key-value pairs. A value in a dictionary is manipulated using its key. The key must be of an immutable data type (e.g., a number, a string or a tuple that contains only immutable data types); dictionary values may be any data type. Each key-value pair takes the form key : value.
Line 61 illustrates how to add a new element to a dictionary by using the [] operator. Because a value must be accessed using its corresponding key, each key in a dictionary must be unique. For example, the statements
month = { 11 : "November" }
month[ 11 ] = "Nov."
would create a dictionary and then change the value associated with key 11 from "November" to the abbreviation "Nov.".
Lines 66-67 use a for loop to print each key-value pair in variable aDictionary. Method keys returns an unordered list of all the keys in the dictionary. Dictionaries also support several other methods (Fig. 35.9). The for loop iterates over each key and prints the key and its corresponding value. Each value in the dictionary is accessed using the [] operator (line 67).
Fig. 35.9 Python dictionary methods.
Deletes all items from the dictionary.
Creates a copy of the dictionary.
get( key [, falseValue] )
Returns the value associated with key. If key is not in the dictionary and if falseValue is specified, returns the specified value.
has_key( key )
Returns 1 if key is in the dictionary; returns 0 if key is not in the dictionary.
Returns a list of tuples that are key-value pairs.
Returns a list of keys in the dictionary.
setdefault( key [, falseValue] )
Behaves similarly to method get. If key is not in the dictionary and falseValue is specified, inserts the key and the specified value into the dictionary.
update( otherDictionary )
Adds all the key-value pairs from otherDictionary to the current dictionary.
Returns a list of values in the dictionary.
Other Python Tutorials:
Introduction to Python
Pytthon Basic Data Types, Control Statements and Functions
Return to Tutorial Index
Page 1 | 2 | 3