Python Dictionaries
PythonDictionaries store data as key-value pairs. Think of it like a real dictionary — word (key) → meaning (value).
Key Points
- Use curly braces
{}withkey: valuepairs - Keys must be unique and immutable (strings, numbers)
- Access values using
dict[key]ordict.get(key) - Very fast lookups — used everywhere in real projects
- Common methods:
keys(),values(),items()
Example — PYTHON
# Creating a dictionary
student = {
"name": "Rahul",
"age": 20,
"city": "Pune",
"marks": 85
}
# Accessing values
print(student["name"])
print(student.get("city"))
# Adding / Updating
student["email"] = "[email]"
student["marks"] = 90
print(student)
# Looping through dictionary
for key, value in student.items():
print(f"{key}: {value}")
# Dictionary of students
students = {
"Rahul": 85,
"Priya": 92,
"Amit": 78
}
# Find topper
topper = max(students, key=students.get)
print(f"Topper: {topper} ({students[topper]} marks)")
# Check if key exists
if "Priya" in students:
print(f"Priya scored {students['Priya']}") Result
Rahul
Pune
{'name': 'Rahul', 'age': 20, 'city': 'Pune', 'marks': 90, 'email': '[email]'}
name: Rahul
age: 20
city: Pune
marks: 90
email: [email]
Topper: Priya (92 marks)
Priya scored 92
Pune
{'name': 'Rahul', 'age': 20, 'city': 'Pune', 'marks': 90, 'email': '[email]'}
name: Rahul
age: 20
city: Pune
marks: 90
email: [email]
Topper: Priya (92 marks)
Priya scored 92