Python Dictionaries

Hey friends 👋 welcome back, In the last lesson, we learned about tuples and sets, Today, we are going to learn about dictionaries, which store data in key-value pairs, Dictionaries are useful when you want to connect a piece of information (the key) to another piece of information (the value),

Creating a Dictionary

You can create a dictionary using curly braces {} and separating keys and values with :, Example:

student = { "name": "Yusuf", "age": 14, "grade": "B" } print(student)

Output:

{'name': 'Yusuf', 'age': 14, 'grade': 'B'}

Accessing Values

You can access a value using its key, Example:

print(student["name"]) print(student["age"])

Output:

Yusuf 14

Modifying Values

You can change the value of a key, Example:

student["grade"] = "A" print(student)

Output:

{'name': 'Yusuf', 'age': 14, 'grade': 'A'}

Adding and Removing Items

You can add new items or remove existing ones, Example:

student["height"] = 1.6 # add a new key-value del student["age"] # remove a key-value print(student)

Output:

{'name': 'Yusuf', 'grade': 'A', 'height': 1.6}

Looping Through a Dictionary

You can loop through keys, values, or both, Example:

for key, value in student.items(): print(key, ":", value)

Output:

name : Yusuf grade : A height : 1.6

Practice Time

Create a Python file called dictionaries.py and try these exercises, 1, Create a dictionary with three key-value pairs and print one value, 2, Change one value in your dictionary and print it, 3, Add a new key-value pair and print the dictionary, 4, Loop through your dictionary and print all keys and values

Great job, You just learned how to store and manage related information in Python using dictionaries, In the next lesson, we will explore Python Modules and Libraries, where we will learn how to use code written by others to make our programs more powerful, We will soon be launching our YouTube channel, where we are going to post step-by-step video tutorials on Python and other tech skills, These videos will make learning easier and more visual, helping beginners follow along confidently.