0
0
Pythonprogramming~5 mins

Why dictionaries are used in Python

Choose your learning style9 modes available
Introduction
Dictionaries help you store and find data quickly by using keys instead of just numbers. They make it easy to organize information like a real-life address book.
When you want to look up a phone number by a person's name.
When you need to store settings with names and values, like 'volume' and 10.
When you want to count how many times each word appears in a text.
When you want to group data by categories, like students by their grade.
When you want to quickly check if something exists using a name or label.
Syntax
Python
my_dict = {
    'key1': 'value1',
    'key2': 'value2'
}
Keys are unique names used to find values quickly.
Values can be any type: numbers, text, lists, or even other dictionaries.
Examples
A dictionary storing names as keys and phone numbers as values.
Python
phone_book = {'Alice': '123-456', 'Bob': '987-654'}
A dictionary storing settings with names and numbers.
Python
settings = {'volume': 10, 'brightness': 70}
An empty dictionary ready to store key-value pairs.
Python
empty_dict = {}
Sample Program
This program creates a dictionary of contacts and shows how to get and add phone numbers using names as keys.
Python
contacts = {
    'John': '555-1234',
    'Jane': '555-5678',
    'Doe': '555-0000'
}

print("John's number is", contacts['John'])

# Add a new contact
contacts['Alice'] = '555-9999'
print("Alice's number is", contacts['Alice'])
OutputSuccess
Important Notes
Dictionaries are very fast for looking up data compared to lists.
Keys must be immutable types like strings or numbers, not lists or other dictionaries.
You can change values or add new key-value pairs anytime.
Summary
Dictionaries store data with unique keys for quick access.
They are useful for organizing related information like names and phone numbers.
You can add, change, or find data easily using keys.