Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Syntax: Dictionaries

check

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

  1. Create a dictionary named book with the keys "title", "author", and "year". Assign appropriate values to each key.

  2. Print the book dictionary.

HINT*: Click here for a hint
*Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
> {'title': '1984', 'author': 'George Orwell', 'year': 1949}
SOLUTION*: Click here for an example solution
*Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
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.