-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionary.py
More file actions
149 lines (36 loc) · 1.89 KB
/
Copy pathDictionary.py
File metadata and controls
149 lines (36 loc) · 1.89 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
56
57
58
59
60
61
62
63
64
# dictionary = a collection of {key:value} pairs. A dictionary is ordered and changeable. No duplicates
capitals = {"USA": "Washington D.C",
"India": "New Delhi",
"China": "Beijing",
"Russia": "Moscow"}
# To see attributes and methods of a dictionary,use the dir function; print(dir(capitals))
# For an indepth description, use the help function; print(help(capitals))
# To get a value from a dictionary, type the key;
# print(capitals.get("China"))
# If python doesn't get the key, it will return the value "None". PS: It can be used within an if statement i.e
if capitals.get("Canada"):
print("That capital exists")
else:
print("That capital doesn't exist")
# To update (add to) a dictionary use the .update function. It can also be used to update an already existing key:value pair i.e
capitals.update({"Germany": "Berlin", "Nigeria": "Abuja"})
print(capitals)
# To remove values from a dictionary, use the .pop function. Also using the .popitem function, you don't have to pass in a key value, it pops the last key value pair in the dictionary
capitals.pop("China")
capitals.update({"Germany": "Berlin", "Nigerai": "Abuja"})
capitals.popitem()
# To completely erase a dictionary, use the .clear function
#capitals.clear()
# To get just the keys in a dictionary without their values, use .keys() and additionally, they can be used in loops (they are iterable)
keys = capitals.keys()
for key in capitals.keys():
print(key)
# To get just the values within your dictionary use the .values() funtion
values = capitals.values()
for value in capitals.values():
print(value)
#When you use the .items() function, it returns a 2D list of tuples
items = capitals.items()
for key, value in capitals.items():
print(f"{key}: {value}")
#print(items)