0% found this document useful (0 votes)
9 views30 pages

Python Lists and Tuples Explained

The document explains lists and tuples in Python, highlighting their characteristics, operations, and differences. Lists are mutable linear data structures with zero-based indexing, while tuples are immutable and require a comma for single elements. It also covers nested structures, iteration methods, and implications of list assignment and copying.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views30 pages

Python Lists and Tuples Explained

The document explains lists and tuples in Python, highlighting their characteristics, operations, and differences. Lists are mutable linear data structures with zero-based indexing, while tuples are immutable and require a comma for single elements. It also covers nested structures, iteration methods, and implications of list assignment and copying.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Lists & Tuples

What Is a List?
• A list is a linear data structure, thus its elements have a
linear ordering.
• Example
• a list storing the average temperature for each day of a
given week, in which each item in the list is identified by its
index value .
• The location at index 0 stores the temperature for Sunday,
the location at index 1 stores the temperature for Monday,
and so on. It is customary in programming languages to
begin numbering sequences of items with an index value of
0 rather than 1. This is referred to as zero-based indexing .
Common List Operations
• Operations commonly performed on lists
include retrieve, update, insert, remove, and
append.
• A list traversal is a means of accessing, one-
by-one, the elements of a list.
Lists (Sequences) in Python
• Python List Type
• A list in Python is a mutable, linear data structure of variable
length, allowing mixed-type elements.
• Mutable means that the contents of the list may be altered.
• Lists in Python use zerobased indexing. Thus, all lists have index
values 0 ... n-1, where n is the number of elements in the list.
• Lists are denoted by a comma-separated list of elements within
square brackets as shown below,
• [1, 2, 3] ['one', 'two', 'three'] ['apples', 50, True]
• An empty list is denoted by an empty pair of square brackets,
[].
Tuples
• A tuple in Python is an immutable linear data
structure, denoted by a comma-separated list
of elements within parentheses, allowing
mixed-type elements.
• nums = (10, 20, 30)
• student = ('John Smith', 48, 'Computer
Science', 3.42)
• Another difference between tuples and lists is that tuples of one
element must include a comma following the element.
Otherwise, the parenthesized element will not be made into a
tuple, as shown below,
• CORRECT WRONG
• >>> (1,) >>> (1)
• (1) 1
• An empty tuple is represented by a set of empty parentheses, ().
• The elements of tuples are accessed the same as lists, with
square brackets
• >>>nums[0] >>>student[0]
• 10 'John Smith'
• Any attempt to alter a tuple is invalid. Thus, delete, update,
insert, and append operations are not defined on tuples.
Sequences
• A sequence in Python is a linearly ordered set
of elements accessed by an index number.
Lists,tuples, and strings are all sequences.
Strings, like tuples, are immutable ; therefore,
they cannot be altered.
• We give sequence operations common to
strings, lists, and tuples
Sequence Operations in Python
Nested Lists
• Lists and tuples can be nested within each other to construct arbitrarily complex
data structures.
• Lists and tuples can contain elements of any type, including other sequences.
Thus, lists and tuples can be nested to create arbitrarily complex data structures.
• Below is a list of exam grades for each student in a given class,
• class_grades = [ [85, 91, 89], [78, 81, 86], [62, 75, 77], ...]
• In this list, for example,
• class_grades[0] equals [85, 91, 89],
• and class_grades[1] equals [78, 81, 86].
• Thus, the following would access the first exam grade of the first student in the list,
• student1_grades = class_grades[0]
• student1_exam1 = student1_grades[0]
• However, there is no need for intermediate variables student1_grades and
student1_
• exam1. The exam grade can be directly accessed as follows,
• class_grades[0][0] ➝ [85, 91, 89][0] ➝ 85
• To calculate the class average on the first
exam, a while loop can be constructed that
iterates over the first grade of each student’s
list of grades,
• sum = 0
• k=0
• while k < len(class_grades):
• sum = sum + class_grades[k][0]
• k=k+1
• average_exam1 = sum/float(len(class_grades))
• If we wanted to produce a new list containing
the exam average for each student in the class,
we could do the following,
• exam_avgs = []
• k=0
• while k < len(class_grades):
• avg = (class_grades[k][0] + class_grades[k][1] + \
class_grades[k][2]) / 3.0
• exam_avgs.append(avg)
• k=k+1
A Chinese Zodiac Program
• This program utilizes the following
programming features:
• ➤ tuples ➤ datetime module
Iterating Over Lists (Sequences) in Python

• For Loops
• A for statement is an iterative control
statement that iterates once for each element
in a specified sequence of elements.
• From the Python Shell, enter the following and observe the results.
• >>>for k in [4, 2 ,3, 1]:
print(k)
???
• >>> for k in ['Apple', 'Banana', 'Pear']:
print(k)
???
• >>>for k in (4, 2, 3, 1):
print(k)
???
• >>>for k in 'Apple':
print(k)
???
The Built-in range Function
• Python provides a built-in range function that
can be used for generating a sequence of
integers that a for loop can iterate over, as
shown below.
• sum = 0
• for k in range(1, 11):
• sum = sum + k
• From the Python Shell, enter the following and observe the results.
• >>>for k in range(0, 11):
print(k)
???
• >>>for k in range(2, 102, 2):
print(k)
???
• >>>for k in range[0, 11]:
print(k)
???
• >>> for k in range(10, 2 1, 2 2):
print(k)
???
Iterating Over List Elements vs. List Index
Values
While Loops and Lists (Sequences)
• Enter and execute the following Python code
and observe the results.
• k=0
• sum = 0
• nums = range(100)
• while k < len(nums) and sum < 100:
• sum = sum + nums[k]
• k=k+1
• print('The fi rst', k, 'integers sum to 100 or
greater')
Assigning and Copying Lists
• This has important implications. For example, if an
element of list1 is changed, then the corresponding
element of list2 will change as well,
• >>> list1 = [10, 20, 30, 40]
• >>> list2 = list1
• >>> list1[0] = 5
• >>> list1
• [5, 20, 30, 40] change made in list1
• >>> list2
• [5, 20, 30, 40] change in list1 causes a change in list2
• Knowing that variables list1 and list2 refer to the same list
explains this behaviour. This issue does not apply to strings and
tuples, since they are immutable and therefore cannot be
modified.
• When needed, a copy of a list can be made as given below,
• list2 = list(list1)
• In this case, we get the following results,
• >>> list1 = [10, 20, 30, 40]
• >>> list2 = list(list1)
• >>> list1[0] = 5
• >>> list1
• [5, 20, 30, 40] change made in list1
• >>> list2
• [10, 20, 30, 40] change in list1 does NOT cause any change in list2
List Comprehensions

You might also like