Python
πΉ 1. What is a Dictionary?
A dictionary is a collection of keyβvalue pairs.
Example structure:
key : value
Example:
name : Sameer
age : 25
city : Hyderabad
In Python dictionaries are written using curly brackets {}.
β Stores data as key : value
β Ordered (Python 3.7+)
β Changeable (mutable)
β No duplicate keys
πΉ 2. Creating a Dictionary
Example 1
student = {
"name": "Sameer",
"age": 22,
"city": "Hyderabad"
}print(student)
Output
{'name': 'Sameer', 'age': 22, 'city': 'Hyderabad'}
π Explanation
"name","age","city"β keys"Sameer",22,"Hyderabad"β values
πΉ 3. Accessing Dictionary Values
Use the key name to access values.
Example 2
student = {
"name": "Ayesha",
"age": 21,
"city": "Warangal"
}print(student["name"])
print(student["age"])
Output
Ayesha
21
π Explanation
The value is retrieved using the key.
πΉ 4. Changing Dictionary Values
Example 3
student = {
"name": "Saddam",
"age": 24
}student["age"] = 25print(student)
Output
{'name': 'Saddam', 'age': 25}
π Explanation
Dictionaries are mutable, so values can be updated.
πΉ 5. Adding New KeyβValue Pairs
Example 4
student = {
"name": "Chandini",
"age": 23
}student["city"] = "Hyderabad"print(student)
Output
{'name': 'Chandini', 'age': 23, 'city': 'Hyderabad'}
πΉ 6. Removing Items from Dictionary
Example 5
student = {
"name": "Gousya",
"age": 22,
"city": "Warangal"
}student.pop("city")print(student)
Output
{'name': 'Gousya', 'age': 22}
π Explanationpop() removes a key-value pair.
πΉ 7. Dictionary Length
Example 6
student = {
"name": "Sameer",
"age": 22,
"city": "Hyderabad"
}print(len(student))
Output
3
π Explanationlen() counts the number of keys.
πΉ 8. Loop Through Dictionary
Example 7: Loop Through Keys
student = {
"name": "Sameer",
"age": 22,
"city": "Hyderabad"
}for key in student:
print(key)
Output
name
age
city
Example 8: Loop Through Values
student = {
"name": "Ayesha",
"age": 21,
"city": "Warangal"
}for value in student.values():
print(value)
Output
Ayesha
21
Warangal
Example 9: Loop Through KeyβValue Pairs
student = {
"name": "Saddam",
"age": 24,
"city": "Hyderabad"
}for key, value in student.items():
print(key, ":", value)
Output
name : Saddam
age : 24
city : Hyderabad
πΉ 9. Checking if Key Exists
Example 10
student = {
"name": "Sameer",
"age": 22
}print("name" in student)
print("city" in student)
Output
True
False
π Explanation
Checks if a key exists in dictionary.
πΉ 10. Nested Dictionary
A dictionary inside another dictionary.
Example 11
students = {
"student1": {
"name": "Sameer",
"age": 22
},
"student2": {
"name": "Ayesha",
"age": 21
}
}print(students["student1"]["name"])
Output
Sameer
πΉ 11. Real-Life Example
Employee Information
employee = {
"name": "Gousya",
"salary": 30000,
"department": "IT"
}print("Employee Name:", employee["name"])
print("Salary:", employee["salary"])
print("Department:", employee["department"])
Output
Employee Name: Gousya
Salary: 30000
Department: IT
πΉ 12. Common Dictionary Methods
| Method | Purpose |
|---|---|
keys() | Returns keys |
values() | Returns values |
items() | Returns key-value pairs |
pop() | Removes item |
update() | Updates dictionary |

Leave a Reply