Thuta Learning
BasicProgrammingbeginner

Python Dictionaries

Relax. We'll talk through this in plain words — no textbook voice.

Dictionary vs Other Collections
Dictionary က key:value pairs အနေနဲ့ data သိမ်းပုံကို comparison ပုံစံနဲ့မြင်နိုင်ပါတယ်။

🐍 Lesson 15: Python Dictionaries

1. What is a dictionary?

In short → A dictionary is a collection that stores data as key:value pairs, and it's ordered (Python 3.7+) and changeable.

More formally → A dictionary is a collection of key-value pairs that is ordered and changeable.

2. Dictionary Methods (the ones you'll use most)

  • get() → retrieves a value by key
  • keys() → all the keys
  • values() → all the values
  • items() → all the key-value pairs

3. Summary

✅ Dictionary = key:value pairs

✅ Keys must be unique

✅ Access data with keys(), values(), items()

✅ Manage it with get(), pop(), update()

python
# ===== 1. Create a Dictionary =====
person = {
    "name": "Kyaw Kyaw",
    "age": 30,
    "city": "Yangon"
}
print(f"Dictionary: {person}")
print(f"Type: {type(person)}")

# ===== 2. Access Items =====
print(f"\n===== Accessing =====")
print(f"Name: {person['name']}")
print(f"Age (using get): {person.get('age')}")

# ===== 3. Modify Dictionary =====
print(f"\n===== Modifying =====")
person["age"] = 32  # Update value
print(f"Updated age: {person}")

person["email"] = "kyaw@example.com"  # Add new key-value
print(f"Added email: {person}")

# ===== 4. Remove Items =====
del person["city"]
print(f"After delete 'city': {person}")

# ===== 5. Dictionary Methods =====
print(f"\n===== Methods =====")
print(f"Keys: {list(person.keys())}")
print(f"Values: {list(person.values())}")
print(f"Items: {list(person.items())}")

# ===== 6. Loop Through Dictionary =====
print(f"\n===== Looping =====")
for key in person:
    print(f"{key}: {person[key]}")

# ===== 7. Length =====
print(f"\nLength: {len(person)}")
You should see
Dictionary: {'name': 'Kyaw Kyaw', 'age': 30, 'city': 'Yangon'} Type: ===== Accessing ===== Name: Kyaw Kyaw Age (using get): 30 ===== Modifying ===== Updated age: {'name': 'Kyaw Kyaw', 'age': 32, 'city': 'Yangon'} Added email: {'name': 'Kyaw Kyaw', 'age': 32, 'city': 'Yangon', 'email': 'kyaw@example.com'} After delete 'city': {'name': 'Kyaw Kyaw', 'age': 32, 'email': 'kyaw@example.com'} ===== Methods ===== Keys: ['name', 'age', 'email'] Values: ['Kyaw Kyaw', 32, 'kyaw@example.com'] Items: [('name', 'Kyaw Kyaw'), ('age', 32), ('email', 'kyaw@example.com')] ===== Looping ===== name: Kyaw Kyaw age: 32 email: kyaw@example.com Length: 3
Python Dictionaries | Thuta Learning