Dictionaries: 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)
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)
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.
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
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?
Fantastisk!
Completion rate forbedret til 5.56
Dictionaries: 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)
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)
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.
Takk for tilbakemeldingene dine!