Repository navigation
Expand file tree
/
Copy pathDictionary.py
More file actions
71 lines (37 loc) · 1.63 KB
/
Copy pathDictionary.py
File metadata and controls
71 lines (37 loc) · 1.63 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
# A dictionary is a collection of {key: value} pairs
# They are ordered and changeable, but do not allow duplicates
capitals = {"UK": "London",
"USA": "Washington D.C.",
"India": "New Delhi"}
# To see all the atributes and methos of the dictionary we can write the following lines print (dir(capitals)
# This one shows helpfull commands too print (help(capitals))
#To get one of the values from the dictionary:
print(capitals.get("USA"))
#To check if the value exists in the dictionary, we can use an if statement
if capitals.get == ("UK"):
print("That capitlas exists")
else:
print("That capital does not exist")
# Using the update method we can add a new value key
capitals.update({"Germany": "Berlin"})
#Using the pop methos will remove specific values
capitals.pop("UK")
#Using the variant of the pop method "popitem" we can remove the latest value of the dictionary
capitals.popitem()
#Using the method clear will clear the dictionary
capitals.clear()
# Using the method keys will return only the keys of the dictionary, in this example i will put on a variable
# Also, technically keys is an object meaning that it will re-assemble a list
keys = capitals.keys()
# Keys can be used in a for loop, meaning you can iterate over every key
for key in capitals.keys():
print(key)
# To get all the values in the dictionary we have the values method
values = capitals.values()
#We can use this with a for loop as well
for value in capitals.values():
print(value)
# The items method return a 2d tupple
items = capitals.items()
for key, value in capitals.items():
print(f"{key}:{value}")