✅ Python Dictionaries 📚
Dictionaries are one of the most important data structures in Python, especially in data science and real-world datasets. They store data in key–value pairs.
🔹 1. What is a Dictionary?
A dictionary stores data in key:value format.
✅ Example:
student = { "name": "Rahul", "age": 22, "course": "Data Science" }
print(student)
Output:
{'name': 'Rahul', 'age': 22, 'course': 'Data Science'}✔ Uses curly brackets {}
🔹 2. Access Dictionary Values
Use the key to access values.
student = { "name": "Rahul", "age": 22 }
print(student["name"])
Output:
Rahul🔹 3. Add New Elements
student = { "name": "Rahul", "age": 22 }
student["city"] = "Delhi"
print(student)
Output:
{'name': 'Rahul', 'age': 22, 'city': 'Delhi'}🔹 4. Modify Values
student["age"] = 23
🔹 5. Remove Elements
student.pop("age")
🔹 6. Important Dictionary Methods
⭐
✅ Get Method:
print(student.get("name"))
Output:
Rahul✅ Keys Method:
print(student.keys())
Output:
dict_keys(['name', 'age'])✅ Values Method:
print(student.values())
Output:
dict_values(['Rahul', 22])✅ Items Method:
print(student.items())
Output:
dict_items([('name', 'Rahul'), ('age', 22)])🔹 7. Loop Through Dictionary
student = { "name": "Rahul", "age": 22 }
for key, value in student.items():
print(key, value)
Output:
name Rahul
age 22
🎯 Today’s Goal
✔ Understand key–value pairs
✔ Access dictionary values
✔ Add or update data
✔ Loop through dictionary
👉 Dictionaries are widely used in APIs, JSON data, and machine learning datasets.
Double Tap ♥️ For More