0
0
Pythonprogramming~30 mins

Dictionary iteration in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Dictionary iteration
๐Ÿ“– Scenario: You work in a small bookstore. You have a list of books with their prices stored in a dictionary. You want to check each book and its price to prepare a price list for customers.
๐ŸŽฏ Goal: Learn how to go through each item in a dictionary using a for loop and display the book names with their prices.
๐Ÿ“‹ What You'll Learn
Create a dictionary with book names as keys and prices as values
Create a variable to count how many books cost more than a certain price
Use a for loop with variables book and price to iterate over the dictionary items
Print the book names and prices in a readable format
Print the count of books that cost more than the given price
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Bookstores and shops often keep product prices in dictionaries or similar data structures. Iterating over these helps prepare price lists or reports.
๐Ÿ’ผ Career
Knowing how to iterate over dictionaries is essential for data processing, reporting, and many programming tasks in software development and data analysis.
Progress0 / 4 steps
1
Create the books dictionary
Create a dictionary called books with these exact entries: 'Python Basics': 29.99, 'Data Science 101': 39.95, 'Machine Learning': 49.50, 'Deep Learning': 59.99, 'AI for Everyone': 24.99
Python
Need a hint?

Use curly braces {} to create a dictionary. Put book names as keys and prices as values separated by colons.

2
Set the price threshold
Create a variable called price_threshold and set it to 40. This will help us count books costing more than this price.
Python
Need a hint?

Just write price_threshold = 40 on a new line.

3
Count expensive books using a for loop
Create a variable called count_expensive and set it to 0. Then use a for loop with variables book and price to iterate over books.items(). Inside the loop, increase count_expensive by 1 if price is greater than price_threshold.
Python
Need a hint?

Start with count_expensive = 0. Then write for book, price in books.items():. Inside the loop, use an if to check price and add 1 to count_expensive.

4
Print the books and count
Use a for loop with variables book and price to iterate over books.items(). Inside the loop, print the book and price in this format: Book: {book}, Price: ${price} using an f-string. After the loop, print Number of books costing more than $40: {count_expensive}.
Python
Need a hint?

Use a for loop to print each book and price with print(f"Book: {book}, Price: ${price}"). Then print the count with print(f"Number of books costing more than $40: {count_expensive}").