0
0
Pythonprogramming~15 mins

Formatting structured data in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Formatting structured data
📖 Scenario: You work in a small shop and keep track of product prices in a dictionary. You want to show the prices neatly formatted with a dollar sign and two decimal places.
🎯 Goal: Create a dictionary of products and prices, then format the prices as strings with a dollar sign and two decimals using dictionary comprehension.
📋 What You'll Learn
Create a dictionary called products with these exact entries: 'apple': 0.4, 'banana': 0.25, 'cherry': 0.15
Create a variable called formatted_prices using dictionary comprehension to format each price as a string with a dollar sign and two decimal places
Print the formatted_prices dictionary
💡 Why This Matters
🌍 Real World
Formatting prices neatly is important for receipts, price tags, and online stores to make prices clear and professional.
💼 Career
Many jobs in retail, web development, and data processing require formatting and displaying structured data clearly.
Progress0 / 4 steps
1
Create the products dictionary
Create a dictionary called products with these exact entries: 'apple': 0.4, 'banana': 0.25, 'cherry': 0.15
Python
Need a hint?

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

2
Prepare for formatting
Create a variable called formatted_prices to hold the formatted prices. Initialize it using dictionary comprehension that will format prices as strings with a dollar sign and two decimals.
Python
Need a hint?

Use f"${price:.2f}" inside the comprehension to format the price with two decimals and a dollar sign.

3
Format the prices using dictionary comprehension
Use dictionary comprehension with for product, price in products.items() to create formatted_prices where each price is formatted as a string with a dollar sign and two decimal places using f"${price:.2f}".
Python
Need a hint?

Dictionary comprehension creates a new dictionary by looping over products.items() and formatting each price.

4
Print the formatted prices
Write print(formatted_prices) to display the dictionary with formatted prices.
Python
Need a hint?

Use the print() function to show the final dictionary.