Learn programming concepts interactively
# Python Dictionary Operations Example# Initialize dictionary with current key-value pairs{ = true"name": "Alice","age": 25,"city": "NYC"}# Current state: 3 key-value pairs# Dictionary length: len(my_dict) = 3# Key Constraints: Keys must be immutable (strings, numbers, tuples)# Valid keys: 'name', 42, (1, 2), True# Invalid keys: ['list'], {'dict': 'value'}# === BASIC OPERATIONS ===# Access value by key# my_dict["name"] returns "Alice"# my_dict["age"] returns 25# my_dict["city"] returns "NYC"# === DICTIONARY METHODS ===# Get all keys, values, and itemsmy_dict.keys() # dict_keys(['name', 'age', 'city']) = truemy_dict.values() # dict_values(["Alice", 25, "NYC"]) = truemy_dict.items() # dict_items([(key, value), ...]) = true# Safe access with default valuesmy_dict.get("key", "default") # Returns "default" if key not found = true# === NESTED DICTIONARIES ===# Multi-level dictionary access{ = true"user": {"name":"Alice","age":25},"settings": {"theme":"dark","notifications":true}}# Access nested values# nested_dict["user"]["name"] returns the name# nested_dict.get("user", {}).get("age", 0) for safe access# === PERFORMANCE NOTES ===# Dictionary lookup is O(1) average case due to hashing# Much faster than list search which is O(n)# Display dictionaryprint(f"Dictionary: {my_dict}")print(f"Size: {len(my_dict)} key-value pairs")
No operations yet
Start by adding or modifying elements