πŸ“˜ Lesson 10: Dictionaries in Python

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}

πŸ“Œ Explanation
pop() removes a key-value pair.


πŸ”Ή 7. Dictionary Length

Example 6

student = {
"name": "Sameer",
"age": 22,
"city": "Hyderabad"
}print(len(student))

Output

3

πŸ“Œ Explanation
len() 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

MethodPurpose
keys()Returns keys
values()Returns values
items()Returns key-value pairs
pop()Removes item
update()Updates dictionary

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *