Data Structures
Dictionaries
A dictionary stores key: value pairs, like a real dictionary stores words and meanings.
student = {"name": "Sara", "marks": 92, "city": "Lahore"}
print(student["name"]) # Sara
print(student.get("age", "N/A")) # N/A (safe default)
student["marks"] = 95 # update
student["course"] = "Python" # add
del student["city"] # remove
for key, value in student.items():
print(key, "->", value)Output
Sara N/A name -> Sara marks -> 95 course -> Python
| Method | Returns |
|---|---|
keys() | All keys |
values() | All values |
items() | (key, value) pairs |
get(k, default) | Value or a default |
update(dict) | Merge another dict in |