Professional Documents
Culture Documents
Lecture 11
Fall 2015
Department of Computing
The School of EE & Computing
Adama Science & Technology University
ASTU
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 a
n immutable object. In other words, a tuple can never b
e 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
A singleton tuple
Is (2) a tuple ? Well, ……
5
ASTU
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 i
mmutable objects.
st1 = “abcedghijklmnopqrstuvwxyz”
st2 = “0123456789”
St3 = “cce20003 is fantastic !”
7
ASTU
Selection Slicing
8
ASTU
LISTS
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
How many variables to store the medal information for all count
ries ?
10
ASTU
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
12
ASTU
>>>nobles.append(“radon”)
>>>nobles
[“helium”, “neon”, “argon”, “krypton”, “xenon”, “radon”]
>>>len(nobles)
6
15
ASTU
>>>nobles.insert(1, “nitrogen”)
>>>nobels
[“helium”, “nitrogen”, “neon”, “argon”, “krypton”, “xenon”,
“radon”]
>>>len(nobles)
7
16
ASTU
>>>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 17
ASTU
>>>nobles.sort()
[“argon”, “helium”, “krypton”, “neon”, “radon”, “xenon”]
>>>nobles.reverse()
[“xenon”, “radon”, “neon”, “kryton”, “helium”, “argon”]
18
ASTU
19
ASTU
>>>countries
[“United States”, “China”, “Great Britain”, “Russia”, “Rep. of
Korea”, “Germany”, ”France”, “Italy”, ”Hungary”, “Australi
a”]
>>>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
20
ASTU
21
ASTU
22
ASTU
>>>totals.sort()
>>>totals
[(18, 'Hungary'), (28, 'Italy'), (28, 'Rep. of Korea'),
(34, 'France'), (35, 'Australia'), (44, 'Germany'), (65, 'Great B
ritain'), (81, 'Russia'), (88, 'China'), (104, 'United States')]
>>>totals.reverse()
>>>totals
[(104, 'United States'), (88, 'China'), (81, 'Russia'), (65, 'Grea
t Britain'), (44, 'Germany'), (35, 'Australia'), (34, 'France'), (2
8, 'Rep. of Korea'), (28, 'Italy'), (18, 'Hungary')]
23
ASTU
24
ASTU
Unpacking
26
ASTU
>>>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
27
ASTU
a “banana”
a [1, 2, 3]
b b [1, 2, 3]
28
ASTU
Aliasing
>>>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
30
ASTU
Type conversion
tuples lists strings tuples, lists
>>>tuple(lst)
(“a”, “b”, “c”, “d”, “e”)
>>>list(tpl)
[“a”, “b”, “c”, “d”, “e”]
31