forked from WebJournal/journaldev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_dictionary_examples.py
More file actions
55 lines (44 loc) · 1 KB
/
python_dictionary_examples.py
File metadata and controls
55 lines (44 loc) · 1 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
45
46
47
48
49
50
51
52
53
54
55
my_dictionary = {} # init empty dictionary
# init dictionary with some key-value pair
another = {
# key : value,
'man': 'Bob',
'woman': 'Alice',
'other': 'Trudy'
}
# print initial dictionaries
print(my_dictionary)
print(another)
# insert value
my_dictionary['day'] = 'Thursday'
another['day'] = 'Thursday'
my_dictionary['color'] = 'Blue'
another['color'] = 'Blue'
# print updated dictionaries
print('Updated Dictionaries:')
print(my_dictionary)
print(another)
# update values
my_dictionary['day'] = 'Friday'
another['day'] = 'Friday'
my_dictionary['color'] = 'Black'
another['color'] = 'Black'
# print updated dictionaries
print('After Update:')
print(my_dictionary)
print(another)
# printing a single element
print(my_dictionary['day'])
print(another['color'])
# add to dictionary
d = {'a': 1, 'b': 2}
print(d)
d['a'] = 100 # existing key, so overwrite
d['c'] = 3 # new key, so add
d['d'] = 4
print(d)
if 'c' not in d.keys():
d['c'] = 300
if 'e' not in d.keys():
d['e'] = 5
print(d)