Chapter 07+08
Chapter 07+08
Lists and
Tuples
Copying Lists
Processing Lists
Two-Dimensional Lists
Tuples
Sequences
Sequence: an object that contains
multiple items of data
The items are stored in sequence one after
another
Python provides different types of
sequences, including lists and tuples
The difference between these is that a list is
mutable and a tuple is immutable
Introduction to Lists
List: an object that contains multiple data
items
Element: An item in a list
Format: list = [item1, item2, etc.]
Can hold items of different types
print function can be used to display an
entire list
list() function can convert certain types of
objects to lists
Introduction to Lists (cont’d.)
The Repetition Operator and
Iterating over a List
Repetition operator: makes multiple copies
of a list and joins them together
The * symbol is a repetition operator when
applied to a sequence and an integer
Sequence is left operand, number is right
General format: list * n
You can iterate over a list using a for loop
Format: for x in list:
Indexing
Index: a number specifying the position
of an element in a list
Enables access to individual element in list
Index of first element in the list is 0, second
element is 1, and n’th element is n-1
Negative indexes identify positions relative to
the end of the list
The index -1 identifies the last element, -2
identifies the next to last element, etc.
The len function
def main():
# First, create an empty list.
name_list = []
...
def main():
# Create a two-dimensional list.
values = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
def main():
# Create a variable to use to hold the count.
# The variable must start with 0.
count = 0