Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Why dictionaries are used
๐ Scenario: Imagine you run a small library. You want to keep track of books and their authors quickly. You need a way to find the author of any book easily.
๐ฏ Goal: You will create a dictionary to store book titles and their authors. Then, you will look up authors by book titles to see why dictionaries are useful.
๐ What You'll Learn
Create a dictionary called library with exact book-author pairs
Create a variable called book_to_find with a specific book title
Use dictionary access to get the author of book_to_find
Print the author name
๐ก Why This Matters
๐ Real World
Libraries, stores, and many apps use dictionaries to quickly find information by a unique key like a book title or product code.
๐ผ Career
Knowing how to use dictionaries helps you work with data efficiently, a key skill in programming jobs like web development, data analysis, and software engineering.
Progress0 / 4 steps
1
Create the library dictionary
Create a dictionary called library with these exact entries: 'The Hobbit': 'J.R.R. Tolkien', '1984': 'George Orwell', 'Pride and Prejudice': 'Jane Austen'
Python
Hint
Use curly braces {} to create a dictionary. Each entry has a book title as key and author as value.
2
Choose a book to find
Create a variable called book_to_find and set it to the string '1984'
Python
Hint
Use = to assign the string '1984' to the variable book_to_find.
3
Find the author using the dictionary
Create a variable called author and set it to the value from library for the key book_to_find
Python
Hint
Use square brackets [] with the dictionary name and key variable to get the value.
4
Print the author
Write a print statement to display the value of the variable author
Python
Hint
Use print(author) to show the author's name on the screen.
Practice
(1/5)
1. Why do programmers use dictionaries in Python? my_dict = {'name': 'Alice', 'age': 25}
easy
A. To create loops automatically
B. To store data with unique keys for quick access
C. To perform mathematical calculations faster
D. To store data in a fixed order only
Solution
Step 1: Understand dictionary structure
Dictionaries store data as key-value pairs, where each key is unique.
Step 2: Identify the main use
This structure allows quick access to values using keys, unlike lists which use indexes.
Final Answer:
To store data with unique keys for quick access -> Option B
Quick Check:
Dictionaries = unique keys + fast access [OK]
Hint: Dictionaries use keys to find data fast [OK]
Common Mistakes:
Thinking dictionaries store data in order only
Confusing dictionaries with lists for calculations
Believing dictionaries create loops automatically
2. Which of the following is the correct way to create a dictionary in Python?
easy
A. my_dict = ('key1', 'value1', 'key2', 'value2')
B. my_dict = ['key1', 'value1', 'key2', 'value2']
C. my_dict = {'key1': 'value1', 'key2': 'value2'}
D. my_dict = 'key1': 'value1', 'key2': 'value2'
Solution
Step 1: Recognize dictionary syntax
Dictionaries use curly braces {} with key:value pairs separated by commas.
Step 2: Compare options
my_dict = {'key1': 'value1', 'key2': 'value2'} uses correct syntax with braces and colons; others use lists, tuples, or invalid syntax.
Final Answer:
my_dict = {'key1': 'value1', 'key2': 'value2'} -> Option C
Quick Check:
Curly braces + key:value pairs = dictionary [OK]
Hint: Dictionaries use curly braces and colons [OK]