forked from adaptives/python-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_dictionary.py
More file actions
executable file
·44 lines (34 loc) · 1.17 KB
/
python_dictionary.py
File metadata and controls
executable file
·44 lines (34 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/python
# Filename : python_dictionary.py
#A Python dictionary is a collection which contains key value pairs, where the
#key must be an immutable object
def print_dict(the_dict, msg="Contents of the dict"):
print msg
for key, value in the_dict.items():
print key, value
pop = {'China' : 1337722000,
'India' : 1162210000,
'United States' : 306261000}
print 'The population of India is ', pop['India']
#We have 3 countried let's add 2 more
pop['Indonesia'] = 229976124
pop['Brazil'] = 191080000
print_dict(pop, 'The 5 highest populated countries in the world are (unsorted)')
#Let's say we decide to keep only top 3
del(pop['Indonesia'])
del(pop['Brazil'])
print_dict(pop, 'The 3 highest populated countries in the world are (unsorted)')
class SimpleMutableClass:
def __init__(self, num):
self.num = num
smc1 = SimpleMutableClass(4)
#maps can only have immutable objects as keys, we try putting a mutable object
illegal_dict = {smc1 : 4}
#We are able to create and print the map
print illegal_dict[smc1]
#Lets mutate the key
smc1.num = 6
#Mutation not reflected in the dict
assert illegal_dict[smc1] == 4
#But the change did happen
assert smc1.num == 6