-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_dictionaries.py
More file actions
40 lines (32 loc) · 1 KB
/
05_dictionaries.py
File metadata and controls
40 lines (32 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
#!/usr/bin/env python3
def dictionaries():
"""10_dictionaries.py - Dictionaries examples"""
print("=== DICTIONARIES ===")
# Dictionary creation and access
person = {
"name": "Bob",
"age": 28,
"city": "New York",
"skills": ["Python", "JavaScript"]
}
print("Name: ", person["name"])
print("Age (safe get): ", person.get("age"))
print("Country (safe): ", person.get("country", "Not found"))
# Adding / updating keys
person["age"] = 29
person["job"] = "Developer"
print("After update: ", person)
# Looping through dictionary
print("\nAll keys:")
for key in person.keys():
print(" -", key)
print("\nKey-Value pairs:")
for key, value in person.items():
print(f" {key}: {value}")
# Dictionary comprehension
squares_dict = {x: x * x for x in range(6)}
print("\nDict comprehension: ", squares_dict)
def main():
dictionaries()
if __name__ == '__main__':
main()