capypad
0 day streak
python / beginner
Snippet

Storing Data with Dictionaries

Dictionaries allow you to store data in key-value pairs, making it easy to look up information by a specific label.

snippet.py
python
1
2
3
user = {"name": "Alice", "score": 42}
print(user["name"])
print(user.get("score"))
Breakdown
1
user = {"name": "Alice", "score": 42}
Creates a dictionary with two keys: 'name' and 'score'.
2
print(user["name"])
Accesses the value associated with the key 'name' using square brackets.
3
print(user.get("score"))
Uses the .get() method to safely retrieve a value from the dictionary.