Lecture 11 Python
Lecture 11 Python
Lecture 11
Fall 2023/24
Tuples
(1, 3, 5, 7, 9)
(“red”, “green”, “blue”)
(777, “a lucky number”)
Strings
Lists
Dictionary
2
ASTU
OUTLINE
Reading assignment
Chapters 9 and 11 of the textbook
3
ASTU
PREVIEW: TUPLES AND STRINGS
Tuples
A tuple is an ordered sequence of data elements. It is
an immutable object. In other words, a tuple can never
be changed unless it is re-created.
Tup0 = ()
tup1 = (1, 2, 3, 4, 5)
tup2 = (1.57, 3.14, 9.02)
tup3 = (“one”, “two”, “three”)
tup4 = (1, 3.14, “two”., 2, (5, 6))
4
ASTU
Cont …
A singleton tuple
Is (2) a tuple ? Well, ……
5
ASTU
Cont …
Selection Slicing
>>>tp1 = (1, 2, 3, 4, 5) >>>tp1[1:3]
>>>tp1 (2, 3)
(1, 2, 3, 4, 5) >>>tp1[:3]
>>>tp1[0] (1, 2, 3)
1 >>>tp1[1:]
>>>tp1[2] (2, 3, 4, 5)
3 >>>tp1[:]
>>>tp1[-1] (1, 2, 3, 4, 5)
5 >>>tp1
>>>tp1[-2] (1, 2, 3, 4, 5)
4
6
ASTU
Strings
A string is an ordered sequence of characters. It is an
immutable objects.
st1 = “abcedghijklmnopqrstuvwxyz”
st2 = “0123456789”
St3 = “cce20003 is fantastic !”
7
ASTU
Cont…
Selection Slicing
8
ASTU
LISTS
Top 10 Countries in 2012 Summer Olympic Medals
Rank NOC Gold Silver Bronze Total
2 China 38 27 23 88
3 Great Britain 29 17 19 65
4 Russia 24 25 32 81
5 Rep. of Korea 13 8 7 28
6 Germany 11 19 14 44
7 France 11 11 12 34
8 Italy 8 9 11 28
9 Hungary 8 4 6 18
10 Australia 7 16 12 35
9
ASTU
Cont …
How can we store this much data in Python?
Should we use 4 x 10 variables?
10
ASTU
Cont …
A list is an ordered sequence of data elements.
It is a mutable object. In other words, existing
elements can be replaced and deleted, and new
elements can be added.
To create a list, enclose the values in square brackets []
list0 = []
list1 = [1, 2, 3, 4, 5]
list2 = [1.57, 3.14, 9.02]
list3 = [“one”, “two”, “three”]
list4 = [1, 3.14, “two”., 2, (5, 6), [7,8]]
11
ASTU
Cont …
A list is an object of type list.
We can access the elements of a list using an integer in
dex. The first element is at index 0, the second at index
1, and so on:
>>> countries[0]
‘United States'
>>> countries[9]
‘Australia'
>>> gold[5]
11
12
ASTU
Cont …
Negative indices start at the end of the list:
>>> countries[-1]
‘Australia'
>>> countries[-5]
‘Germany’
The length of a list is given by len:
>>> len(countries)
10
The empty list is written [] and has length zero.
13
ASTU
Cont …
Now we can store the 2012 Summer Olympic Medal
Table using lists:
14
ASTU
Cont…
Selection, slicing, and concatenation are allowed in lists
as in tuples:
>>>nobles.append(“radon”)
>>>nobles
[“helium”, “neon”, “argon”, “krypton”, “xenon”, “radon”]
>>>len(nobles)
6
17
ASTU
Cont…
>>>nobles.insert(1, “nitrogen”)
>>>nobels
[“helium”, “nitrogen”, “neon”, “argon”, “krypton”, “xenon”,
“radon”]
>>>len(nobles)
7
18
ASTU
Cont…
>>>nobles.remove(“nitrogen”)
>>>nobles
[“helium”, “neon”, “argon”, “krypton”, “xenon”, “radon”]
>>>len(nobles)
6
or
>>>gas = nobles.pop(1)
>>>nobles
[“helium”, “neon”, “argon”, “krypton”, “xenon”, “radon”]
>>>gas
‘nitrogen’
>>>len(nobles)
6 19
ASTU
Cont…
>>>nobles.sort()
[“argon”, “helium”, “krypton”, “neon”, “radon”, “xenon”]
>>>nobles.reverse()
[“xenon”, “radon”, “neon”, “kryton”, “helium”, “argon”]
20
ASTU
Cont…
Other methods for lists
lst.pop() : Remove the last element of lst and return it.
lst.index(v): Return the index of the first element of lst
which is equal to v.
lst.count(v): Count the number of elements in lst, which
is equal to v.
lst.extend(K): Append all elements in a sequence K to lst.
21
ASTU
Cont…
>>>countries
[“United States”, “China”, “Great Britain”, “Russia”, “Rep. of
Korea”, “Germany”, ”France”, “Italy”, ”Hungary”, “Australia”]
>>>golds
[46, 38, 29, 24, 13, 11, 11, 8, 8, 7] 195
>>>silvers
[ 29, 27, 17, 25, 8, 19, 11, 9, 4, 16] 165
>>>bronzes
{29, 23, 19, 32, 7, 14, 12, 11, 6, 12] 165
22
ASTU
Cont…
Built-in Functions on lists: min, max, len and sum
len returns length of a list.
sum returns the sum of the elements.
max returns the largest element.
min returns the smallest element:
>>>min(golds), max(golds), sum(golds)
(7 46 195)
>>>min(silvers), max(silvers), sum(silvers)
(4, 29, 165)
>>>min(bronzes), max(bronzes), sum(bronzes)
(6, 32, 165)
23
Traversing a list ASTU
24
ASTU
Cont …
If we want to modify elements, we need the index:
>>> lst = list(range(1, 11))
>>> for i in range(len(lst)):
... lst[i] = lst[i] ** 2
>>> lst
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
25
ASTU
Traversing several lists
Creating a list for total numbers of medals by nations
>>>totals = []
>>>for i in range (len(countries)):
medals= golds[i] + silvers[i] + bronzes[i]
totals.append((medals, countries[i]))
>>>print totals
26
Sorting ASTU
27
ASTU
Cont…
Printing top five countries in their total numbers of
medals
>>>total[0:5]
[(104, 'United States'), (88, 'China'), (81, 'Russia'),
(65, 'Great Britain'), (44, 'Germany')
28
ASTU
Cont…
Unpacking
30
ASTU
Ranking
Printing the top five countries in lexicographical ranking
table = []
for i in range(len(countries)):
table.append((golds[i], silvers[i], bronzes[i], countries[i]))
31
ASTU
Cont…
>>>table.sort()
>>>top_five = table[-5:] What is it?
>>>top_five .reverse()
>>>for g, s, b, nation in top_five:
print g, s, b, nation
46 29 29 United States
38 27 23 China
29 17 19 Great Britain
24 25 32 Russia
13 8 7 Rep. of Korea
32
List methods ASTU
33
ASTU
Cont…
a “banana”
a [1, 2, 3]
b b [1, 2, 3]
34
Aliasing ASTU
An object can have more than one name. This is called aliasin
g. We have to be careful when working with mutable objects:
>>>list1 = [“A”, “B”, “C”] >>>list1 = [“A”, “B”, “C”]
>>>list 2 = List 1 >>>list2 = [“A”, “B”, “C”]
>>>list2 .append(“D”) >>>list2 .append(“D”)
>>>list2 >>>list2
[‘A’, ‘B’, ‘C’, ‘D’] [‘A’, ‘B’, ‘C’, ‘D’]
>>>list1[1] = “X” >>>list1[1] = “X”
>>>list2 >>>list2
[‘A’, ‘X’, ‘C’, ‘D’] [‘A’, ‘B’, ‘C’, ‘D’] Why?
>>>list1 is list2 >>>list1 is list2
True False
list1 [“A”, “X”, “C”]
list1 [‘A’, ‘X’, ‘C’, ‘D’]
list2 [‘A’, ‘B’, ‘C’, ‘D’]
list2
35
ASTU
Type conversion
Type conversion
tuples lists strings tuples, lists
36
ASTU
SUMMARY: STRINGS, TUPLES, & LISTS
37