-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsimple_dict.py
50 lines (32 loc) · 1016 Bytes
/
simple_dict.py
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
45
46
47
48
49
50
import pprint
pp = pprint.PrettyPrinter(indent=4)
# below is a Python dictionary - note how it differs in structure from
# a Python list - a dictionary stores key-value PAIRS
cat = { 'eyes': 'green',
'fur': 'brown',
'name': 'Furball',
'nose': 'pink',
'tail': 'short' }
# add a new item to the dictionary
cat.update({'ears':'pointy'})
# change an item in the dictionary - same as adding a new item
cat.update({'fur':'orange'})
print("\n")
print(cat)
print("\n")
# we imported pprint (not normally needed for dictionaries)
# so we could see this difference in printing in Terminal
pp.pprint(cat)
print("\n")
print('Print the value of "nose":')
print(cat['nose'])
print("\n")
print('Print the value of "name":')
print(cat['name'])
print("\n")
print("The cat's name is " + cat['name'] + " and the cat's nose is " + cat['nose'] + ".")
print("\n")
new_name = input("Give the cat a new name: ")
cat.update({'name':new_name})
print("The cat's name is now " + cat['name'] + ".")
print("\n")