-
Notifications
You must be signed in to change notification settings - Fork 5
/
29. dictionaries.py
58 lines (31 loc) · 1.07 KB
/
29. dictionaries.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
51
52
53
54
55
56
'''
One of the most useful data types in python is the python
dictionary.
If you are familiar with other languages, think of it like an associative
array.
The idea of the dictionary is to have what are called keys and values. Despite
being ordered if you print a dictionary out, there is no actual
order to dictionaries.
All keys are unique
So before we used two lists and assumed their association, searched for index,
and found information about 1 item in 1 list from another.
Now here, everything is contained in the same location, and makes more sense
Let us show an example:
'''
# Dictionary of names and ages.
exDict = {'Jack':15,'Bob':22,'Alice':12,'Kevin':17}
print(exDict)
# How old is Jack?
print(exDict['Jack'])
# We find a new person that we want to insert:
exDict['Tim'] = 14
print(exDict)
# Tim just had a birthday though!
exDict['Tim'] = 15
print(exDict)
# Then Tim died.
del exDict['Tim']
print(exDict)
# next we want to track hair color
exDict = {'Jack':[15,'blonde'],'Bob':[22, 'brown'],'Alice':[12,'black'],'Kevin':[17,'red']}
print(exDict['Jack'][1])