-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path45_Access_Dictionary_Items.py
More file actions
54 lines (43 loc) · 1.09 KB
/
45_Access_Dictionary_Items.py
File metadata and controls
54 lines (43 loc) · 1.09 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
# Python - Access Dictionary Items
print("Python - Access Dictionary Items:")
# Accessing items
print("\nAccessing items:")
print("Example 1:")
d1 = {"name": "Zaber", "age": 29, "id": 1}
print(d1["name"])
print(d1["age"])
print("Example 2:")
print(d1.get("id"))
# Get keys
print("\nGet keys:")
print("Example 1:")
print(d1.keys())
print("Example 2:")
car = {"brand": "toyota", "model": "x", "year": 1980}
print(car.keys())
car["color"] = "white"
print(car.keys())
# Get values
print("\nGet values:")
print("Example 1:")
laptop = {"brand": "Lenovo", "color": "black", "year": 2014}
print(laptop.values())
print("Example 2:")
laptop["OS"] = "Windows"
print(laptop.values())
laptop["year"] = 2013
print(laptop.values())
# Get items
print("\nGet items:")
print("Example 1:")
player = {"name": "John", "height": 5.6, "hair": "black"}
print(player.items())
print("Example 2:")
player["weight"] = 60
print(player.items())
# Checking if a key is available
print("\nChecking if a key is available:")
game1 = {"event": "chess", "time": 14.00}
print(game1)
if "time" in game1:
print("Yes, \"time\" key is found.")