Menu
Coddy logo textTech

Creating a Dictionary

Part of the Logic & Flow section of Coddy's Python journey — lesson 8 of 78.

To create a dictionary in Python, you use curly braces {} and specify the key-value pairs within them. Each key-value pair is written as key: value, and multiple pairs are separated by commas.

Here's how you can create a dictionary:

# Creating a dictionary of country capitals
country_capitals = {
    "USA": "Washington, D.C.",
    "France": "Paris",
    "Japan": "Tokyo"
}

# Creating a dictionary of employee information
employee = {
    "name": "John Doe",
    "age": 30,
    "position": "Software Engineer"
}

# Creating an empty dictionary
empty_dict = {}

In the first example, country_capitals is a dictionary with country names as keys and their capitals as values. In the second example, employee is a dictionary containing information about an employee. The third example shows how to create an empty dictionary, which can be useful when you want to add items to it later.

challenge icon

Challenge

Easy

Create a function named create_student_dict that takes three parameters: name, age, and major. The function should return a dictionary where the keys are "name", "age", and "major", and the values are the corresponding values passed to the function.

For example, calling create_student_dict("Alice", 20, "Computer Science") should return a dictionary with the following key-value pairs:

  • Key: "name", Value: "Alice"
  • Key: "age", Value: 20
  • Key: "major", Value: "Computer Science"

Cheat sheet

To create a dictionary in Python, use curly braces {} with key-value pairs separated by commas:

# Dictionary with key-value pairs
country_capitals = {
    "USA": "Washington, D.C.",
    "France": "Paris",
    "Japan": "Tokyo"
}

# Dictionary with different data types
employee = {
    "name": "John Doe",
    "age": 30,
    "position": "Software Engineer"
}

# Empty dictionary
empty_dict = {}

Each key-value pair is written as key: value, where keys are typically strings and values can be any data type.

Try it yourself

def create_student_dict(name, age, major):
    # Write code here
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow