-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path04_dictionary_basics.py
More file actions
37 lines (31 loc) · 1 KB
/
Copy path04_dictionary_basics.py
File metadata and controls
37 lines (31 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
# 04_dictionary_basics.py
# Concept: Dictionaries - Key-Value Mapping
# 1. Creating a Dictionary
player = {
"name": "Krish",
"level": 20,
"inventory": ["sword", "shield", "potion"],
"is_online": True
}
print(f"Initial Player: {player}")
# 2. Accessing values
print(f"Player Name: {player['name']}")
# Safe access using .get() (avoids KeyError if key doesn't exist)
print(f"Health: {player.get('health', 100)}")
# 3. Modifying and Adding values
player["level"] = 21 # Update existing
player["class"] = "Warrior" # Add new
print(f"Updated Player: {player}")
# 4. Removing items
removed_val = player.pop("is_online")
print(f"Removed is_online: {removed_val}")
# 5. Iterating through a dictionary
print("\n--- Player Stats ---")
for key, value in player.items():
print(f"{key.capitalize()}: {value}")
# 6. Nested Dictionary
studio_data = {
"Studio_A": {"project": "Alpha", "team": 12},
"Studio_B": {"project": "Beta", "team": 8}
}
print(f"\nStudio A Project: {studio_data['Studio_A']['project']}")