
Syntax: Dictionaries
Python collections of named values.
Python Dictionaries
Python's dictionaries are a crucial data structure that allow pairing of keys with values, making it efficient to organize and retrieve information.
Creating Dictionaries in Python
A dictionary in Python is defined using curly brackets ({}). Inside these brackets, keys and values are declared in pairs using a colon (:).
user = {
"name": "Bob",
"email": "[email protected]",
"age": 35
}
print(user)
> {'name': 'Bob', 'email': '[email protected]', 'age': 35}In the code above, we have created a dictionary user which has three keys: "name", "email", and "age". Each key has an associated value.
Practice: Creating a Dictionary
-
Create a dictionary named
bookwith the keys"title","author", and"year". Assign appropriate values to each key. -
Print the
bookdictionary.
HINT*: Click here for a hint
> {'title': '1984', 'author': 'George Orwell', 'year': 1949}SOLUTION*: Click here for an example solution
book = {
"title": "1984",
"author": "George Orwell",
"year": 1949
}
print(book)
Fantastic! You've now grasped the fundamental concept of creating dictionaries in Python. In future lessons, we will explore how to access, modify, and perform various operations on these dictionaries.