Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Dictionaries: Storing Paired Information | Organizing and Summarizing Data
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python for Daily Tasks

bookDictionaries: Storing Paired Information

Dictionaries in Python are powerful tools for pairing related information. Unlike lists, which store items in a sequence, dictionaries use a key-value structure. This means each piece of data (the value) is associated with a unique identifier (the key). You can think of a dictionary as a real-world dictionary: you look up a word (the key) to find its definition (the value). In Python, dictionaries are created using curly braces {} and key-value pairs separated by colons. This structure is especially useful when you need to keep track of information that naturally comes in pairs, such as item names and their prices or tasks and their deadlines.

123456789
# Create a dictionary to store items and their prices item_prices = { "apple": 0.99, "banana": 0.59, "bread": 2.49, "milk": 1.99 } print(item_prices)
copy

To work with dictionaries, you need to know how to access, update, and add key-value pairs. To access the value for a specific key, use square brackets with the key name, like item_prices["apple"]. If you want to update the price of an existing item, assign a new value to its key. To add a new item, simply assign a value to a new key. These operations make dictionaries flexible for organizing and summarizing paired data in your daily tasks.

1234567
# Update the price of an existing item item_prices["banana"] = 0.69 # Add a new item to the dictionary item_prices["eggs"] = 2.99 print(item_prices)
copy

1. What is a key in a Python dictionary?

2. How do you access the value associated with a key?

3. Fill in the blanks to add a new key-value pair to a dictionary.

question mark

What is a key in a Python dictionary?

Select the correct answer

question mark

How do you access the value associated with a key?

Select the correct answer

question-icon

Fill in the blanks to add a new key-value pair to a dictionary.

=
{'laundry': 'Saturday', 'groceries': 'Sunday'}

Click or drag`n`drop items and fill in the blanks

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 2

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

How do I access the price of a specific item in the dictionary?

What happens if I try to access a key that doesn't exist in the dictionary?

Can you explain how to remove an item from the dictionary?

bookDictionaries: Storing Paired Information

Sveip for å vise menyen

Dictionaries in Python are powerful tools for pairing related information. Unlike lists, which store items in a sequence, dictionaries use a key-value structure. This means each piece of data (the value) is associated with a unique identifier (the key). You can think of a dictionary as a real-world dictionary: you look up a word (the key) to find its definition (the value). In Python, dictionaries are created using curly braces {} and key-value pairs separated by colons. This structure is especially useful when you need to keep track of information that naturally comes in pairs, such as item names and their prices or tasks and their deadlines.

123456789
# Create a dictionary to store items and their prices item_prices = { "apple": 0.99, "banana": 0.59, "bread": 2.49, "milk": 1.99 } print(item_prices)
copy

To work with dictionaries, you need to know how to access, update, and add key-value pairs. To access the value for a specific key, use square brackets with the key name, like item_prices["apple"]. If you want to update the price of an existing item, assign a new value to its key. To add a new item, simply assign a value to a new key. These operations make dictionaries flexible for organizing and summarizing paired data in your daily tasks.

1234567
# Update the price of an existing item item_prices["banana"] = 0.69 # Add a new item to the dictionary item_prices["eggs"] = 2.99 print(item_prices)
copy

1. What is a key in a Python dictionary?

2. How do you access the value associated with a key?

3. Fill in the blanks to add a new key-value pair to a dictionary.

question mark

What is a key in a Python dictionary?

Select the correct answer

question mark

How do you access the value associated with a key?

Select the correct answer

question-icon

Fill in the blanks to add a new key-value pair to a dictionary.

=
{'laundry': 'Saturday', 'groceries': 'Sunday'}

Click or drag`n`drop items and fill in the blanks

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 2
some-alt