What Is a Dictionary?
A Python dictionary stores values in key → value pairs. It’s one of the most important and most used data types in Python.
person = {
"name": "Kaloyan",
"age": 12,
"country": "Bulgaria"
}
Accessing Values
print(person["name"]) # Kaloyan
print(person["age"]) # 12
Adding and Changing Values
person["age"] = 13
person["city"] = "Sofia"
Removing Keys
del person["city"]
Looping Through a Dictionary
Keys Only
for key in person:
print(key)
Keys and Values
for key, value in person.items():
print(key, "=", value)
Check If a Key Exists
if "name" in person:
print("Name exists!")
Dictionary Length
print(len(person))
Nested Dictionaries
students = {
"kal": {"age": 12, "grade": 6},
"ivo": {"age": 13, "grade": 7}
}
print(students["kal"]["grade"])
When to Use Dictionaries?
- Storing structured data
- Fast lookup using keys
- Configs, settings, JSON, APIs
- User accounts, profiles, metadata
Full Example — User Profile
user = {
"username": "CodeTweakrs",
"premium": False,
"projects": ["SysDash", "SysTweak", "CT-Cleaner"]
}
print("User:", user["username"])
print("Projects:", ", ".join(user["projects"]))
What’s Next?
Next you’ll learn about HTML semantic tags — the proper way to structure web pages for clarity, SEO, and accessibility.