0
0
Pythonprogramming~15 mins

Basic dictionary comprehension in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Basic dictionary comprehension
๐Ÿ“– Scenario: You work in a small bookstore. You have a list of books with their prices. You want to create a new dictionary that shows the prices after a discount.
๐ŸŽฏ Goal: Create a dictionary using dictionary comprehension to apply a discount to each book price.
๐Ÿ“‹ What You'll Learn
Create a dictionary called books with given book titles and prices.
Create a variable called discount to hold the discount rate.
Use dictionary comprehension to create a new dictionary called discounted_books with discounted prices.
Print the discounted_books dictionary.
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Applying discounts to product prices is common in stores and online shops.
๐Ÿ’ผ Career
Understanding dictionary comprehension helps in data processing and automation tasks in many programming jobs.
Progress0 / 4 steps
1
Create the initial dictionary of books
Create a dictionary called books with these exact entries: 'Python Basics': 40, 'Data Science': 50, 'Machine Learning': 60
Python
Need a hint?

Use curly braces {} to create a dictionary with keys and values separated by colons.

2
Set the discount rate
Create a variable called discount and set it to 0.1 to represent a 10% discount
Python
Need a hint?

Just assign the number 0.1 to the variable discount.

3
Create discounted prices with dictionary comprehension
Use dictionary comprehension to create a new dictionary called discounted_books where each price is reduced by the discount. Use for title, price in books.items() in the comprehension
Python
Need a hint?

Use the format {key: value_expression for key, value in dictionary.items()}.

4
Print the discounted books dictionary
Write print(discounted_books) to display the dictionary with discounted prices
Python
Need a hint?

Use the print function to show the dictionary.