Key-and-value storage
Learn dictionaries โ a powerful way to store data using named keys instead of numbered indexes.
A dictionary stores data in key-value pairs. Instead of accessing items by position (like lists), you access them by name. It is like having a real dictionary โ you look up a word (key) and find its meaning (value).
๐ก Think of it like this:
Think of a dictionary like a contact list in your phone. You look up someone's name (the key) and find their phone number (the value). You do not need to know what position they are in โ you just search by name.
Dictionaries are perfect for storing related information. A game character might have keys like "name", "health", "level", and "score". Dictionaries keep this data organized and easy to access.
Where you see this in real life:
Python dictionaries are also called "dicts" for short. They are one of the most used data types in Python because they are so fast at looking things up!
A dictionary stores data in key-value pairs. Instead of accessing items by position (like lists), you access them by name. It is like a real dictionary โ you look up a word (key) and find its meaning (value).
Dictionaries use curly braces {}and each pair is written as key: value.
Click โถ Run to see the output!You can add new keys or change existing ones just by assigning a value!
Click โถ Run to see the output!You can loop through keys, values, or both!
Click โถ Run to see the output!๐ List
Access by position (index)
names[0] โ "Alice"
๐ Dictionary
Access by name (key)
pet["name"] โ "Whiskers"
How do you access the value for key 'name' in a dictionary called 'person'?