Chapter 6 pfe
Chapter 6 pfe
1
Creating Lists
values = [32, 54, 67.5, 29, 35, 80, 115, 44.5, 100, 65]
2
Accessing List Elements
3
Mutable and Immutable
• There are two differences between lists and strings. Lists can
hold values of any type, whereas strings are sequences of
characters. Moreover, strings are immutable— you cannot
change the characters in the sequence. But lists are mutable.
You can replace one list element with another, like this:
values[5] = 87
4
out-of-range error or a
bounds error
• values[0], the first element
• values[1], the second element
• values[2], the third element
• ...
• values[9], the tenth element
• In this list, an index can be any integer ranging from 0 to 9.
• There is no values[10] in a list with ten elements – the index can
range from 0 to 9. To avoid out-of-range errors, you will want to
know how many elements are in a list.
• You can use the len function to obtain the length of the list; that
is, the number of elements:
numElements = len(values)
5
List Iteration
for i in range(10) :
print(i, values[i])
•The variable i iterates through the integer values 0 through 9,
which is appropriate because there is no element corresponding to
values[10].
•Instead of using the literal value 10 for the number of elements in
the list, it is a
•good idea to use the len function to create a more reusable loop:
for i in range(len(values)) :
print(i, values[i])
•If you don’t need the index values, you can iterate over the
individual elements using a for loop in the form:
for element in values :
print(element)
6
List References
• When you copy a list variable into another, both variables refer
to the same list. The second variable is an alias for the first
because both variables reference the same list.
scores = [10, 9, 7, 4, 5]
values = scores # Copying list reference
• You can modify the list through the following assignment
operation:
scores[3] = 10
print(values[3])
7
Self Check
8
Reverse Subscripts
9
List Operations
• In contrast, Python has a rich set of
operations that make list processing
quite convenient.
• Appending Element
• Here we start out with an empty list:
friends = [] # ---1
• A new element can be appended to the
end of the list with the append
method:
friends.append("Harry") 2# ---2
• The size, or length, of the list increases
after each call to the append method.
Any number of elements can be added
to a list:
friends.append("Emily") # ---3
friends.append("Bob")
friends.append("Cari")
10
List Operations
• Inserting an Element
• You have just seen how to add
a new element to the end of a
list using the list method
append. Sometimes, however,
a new element has to be
inserted at a specific position in
the list.
friends = ["Harry", "Emily",
"Bob", "Cari"] 1
• suppose we want to insert the
string "Cindy" into the list
following the first element,
which contains the string
"Harry". The statement
friends.insert(1, "Cindy") 2
• achieves this task
friends.insert(5, "Bill")
11
List Operations
• Finding an Element
• Let’s assume we have the following list declaration
friends = ["Harry", "Emily", "Bob", "Cari", "Emily"]
• If you simply want to know whether an element is present in a
list, use the in operator:
if "Cindy" in friends :
print("She's a friend")
• Often, you want to know the position at which an element
occurs.
n = friends.index("Emily") # Sets n to 1
12