-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy path5_dictionaries.py
68 lines (43 loc) · 1.02 KB
/
5_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
57
58
59
60
61
62
63
64
65
66
67
68
'''
Definition
A dictionary is a collection of key/value pairs, very similar to lists except you use keys instead of indexes to access the values in it.
Syntax:
d = { key1: val1, key2: val2, ... }
A key can be of any immutable data type.
'''
#d = { [1,2] : True }
#print(d)
d = { (1,2) : True }
print(d)
emails = {} or dict()
emails = { "David" : "[email protected]" }
#Accessing
emails = { "David" : "[email protected]" }
print (emails["David"])
print (emails["Sam"])
print (emails.get("Sam", None))
#Add
emails["Brian"] = "[email protected]"
emails["Tom"] = "[email protected]"
#Modify
emails["David"] = "[email protected]"
#Remove
del emails["David"]
#update
new_emails = { "David" : "[email protected]", "Sam" : "[email protected]" }
emails.update(new_emails)
#pop, popitem
element = emails.pop("Sam")
print(element)
emails["Sam"] = "[email protected]"
element = emails.popitem()
print(element)
#items, keys and values
emails.items()
emails.keys()
emails.values()
#len
len(email)
#clear
emails.clear()
print(emails)