Lec 7
Lec 7
Formatting operator:
format_string % (arg0, arg1, .... )
Tuple has one element for each place holder in the format_string. Place holders are:
%d for integers in decimal
%g for float
%.2f for float with fixed precision (2 digits after period)
%s for anything (like str(x))
To create a set object, we can use curly braces or the set() function.
We can also change the value via the key in the dictionary.
>>> majors["PH"] = "Physics"
>>> majors["PH"]
'Physics'
The objects that are returned from keys(), values() and items() are
not list objects.
They have elements like lists, but they cannot be modified and do not have an
append()method.
CS101 Copyright (c) School of Computing, KAIST 15
Dictionary methods
>>> majors.values()
dict_values(['Computer Science', 'Physics', 'Mechanical
Engineering', 'Electrical Engineering', 'Mathematical
Sciences'])
>>> majors.items()
dict_items([('CS','Computer Science'), ('PH','Physics'),
('ME','Mechanical Engineering'), ('EE','Electrical Engineering'),
('MAS','Mathematical Sciences')])
To loop over both keys and values in a dictionary, we can use items()
>>> for key, value in majors.items():
... print("%s is %s." % (key, value))
CS is Computer Science.
PH is Physics.
ME is Mechanical Engineering.
EE is Electrical Engineering.
MAS is Mathematical Sciences.
CS101 Copyright (c) School of Computing, KAIST 18
List, Set and Dictionary
Result:
Running time for list: 78.066966 sec
Running time for set: 0.010978 sec
CS101 Copyright (c) School of Computing, KAIST 20
Copy and paste