Lecture 13 Python
Lecture 13 Python
Lecture 13
Fall 2023/24
Reading assignment:
Chapter 13 of the textbook (Classes and functions)
Sections 14.1~14.5 in Chapter 14 of the textbook
(Classes and methods)
12/19/2024 2
DICTIONARIES ASTU
Use a dictionary!
12/19/2024 3
ASTU
medals = {}
medals = {“United States”: (46, 29, 29),
“China”: (38, 27, 23),
“Great Britain”:(29, 17, 19),
“Rep. of Korea”: (13, 8, 7)
“Australia”: (7, 16, 12)}
>>>medals[“United States”]
(46, 29, 29)
>>>medals[“Rep. of Korea”]
(13, 8, 7)
>>>medals[1]
KeyError: 1
12/19/2024 4
ASTU
Creating a dictionary
txt = (“one”, “two”, “three”, “four”, “five”)
num = (1, 2, 3, 4, 5)
dict1 = {}
for i in range(len(txt)):
dict1[txt[i]] = num[i]
print (dict1)
12/19/2024 5
ASTU
12/19/2024 6
ASTU
12/19/2024 7
ASTU
Traversing a dictionary
>>>dict1={'one': 1,'two': 2,'three': 3,'four': 4,'five': 5}
>>>for key in dict1:
print (key, dict1[key])
one 1
two 2
three 3
four 4
five 5
12/19/2024 8
ASTU
Converting to a list
12/19/2024 9
ASTU
Case study
stg = “maintain”
m a i n t
1 2 2 2 1
12/19/2024 10
ASTU
stg = “maintain”
count = {}
for c in stg:
if c not in count:
count[c] = 1
else:
count[c] += 1
print (count)
12/19/2024 11
ASTU
12/19/2024 13
ASTU
Default parameters
def create_sun(radius = 30, color = “yellow”):
sun = Circle(radius)
sun.setFillColor(color)
sun.setBorderColor(color)
sun.moveTo(100, 100)
return sun
sun = create_sun()
star = create_sun(2) OK !
moon = create_sun(28, “red”)
moon =create_sun(“red”) Wrong !
12/19/2024 14
ASTU
12/19/2024 15
FILES ASTU
Creating a file
f = open("cseg1101/planets.txt", “w")
for i in range(8): Mercury
planet = input (“Enter a planet”) Venus
Earth
f.write(planet + “\n”)
Mars
f.close() Jupiter
Saturn
f.writelines((“Mercury\n”, “Venus\n”, … , Uranus
“Neptune”)) Neptune
12/19/2024 16
ASTU
line separator
How to get rid of white space?
>>>s.strip(), len(s.strip())
(‘Mercury’, 7)
12/19/2024 17
ASTU
12/19/2024 18
ASTU
f = open(“cseg1101/planets.txt”, “r”)
count = 0
in_file = False Apply lower() to
for line in f: line.strip().
count += 1
if line.strip().lower() == “earth”
in_file = True
break Get out of the loop.
if in_file:
print (“Earth is in line”, str(count) + “.”)
12/19/2024 20
ASTU
Appending a line
12/19/2024 21
FORMATTING ASTU
Format string
12/19/2024 22
ASTU
………………………
for name, money in lst:
print (“%-11s %6d” % (name, money))
2 spaces
left-aligned
12/19/2024 23
ASTU
x1 = input(“x1 = “) “60”
x2 = input(“x2 = “) “150”
val = int(x1) + int(x2) 210
print (str(val) + “ is “ + x1 + “ + “ + x2 )
210 is 60 + 150
val x1 x2
print (“%d is %s + %s”% (val, x1, x2))
place holders
12/19/2024 24
ASTU
Format operators
format string % (arg0, arg1, …..)
Every element in the tuple has its corresponding place
holder in the format string.
Place holders
%d integers in decimal
%s strings
%g floats
%f floats
%.5g # of significant digits is 5
%.5f # of digit after the decimal point
12/19/2024 25
ASTU
12/19/2024 26
STRING METHODS ASTU
in operator
>>>”abc” in “01234abcdefg”
True
>>>”fgh” in “01234abceefg”
False
A bit different from the
usage for tuples & lists
12/19/2024 27
ASTU
12/19/2024 28
ASTU
12/19/2024 29
ASTU
12/19/2024 30