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:
Output:
Accessing Values
You can access a value using its key, Example:
Output:
Modifying Values
You can change the value of a key, Example:
Output:
Adding and Removing Items
You can add new items or remove existing ones, Example:
Output:
Looping Through a Dictionary
You can loop through keys, values, or both, Example:
Output:
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.
0 Comments