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
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
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
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
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
Hint
Use the print() function to show the final dictionary.
Practice
(1/5)
1. What does the indent parameter do in json.dumps() when formatting structured data?
easy
A. Adds spaces and new lines to make the output easier to read
B. Converts data into a binary format
C. Removes all spaces and new lines for compact output
D. Encrypts the JSON data for security
Solution
Step 1: Understand the purpose of json.dumps()
This function converts Python data into a JSON string.
Step 2: Role of indent parameter
The indent parameter adds spaces and new lines to format the JSON string nicely for readability.
Final Answer:
Adds spaces and new lines to make the output easier to read -> Option A
Quick Check:
indent = readable JSON [OK]
Hint: Remember: indent means pretty print with spaces [OK]
Common Mistakes:
Thinking indent compresses JSON
Confusing indent with encryption
Assuming indent changes data content
2. Which of the following is the correct syntax to format a Python dictionary data as a JSON string with indentation of 4 spaces?
easy
A. json.dumps(data, indent=4)
B. json.dumps(data, indent='4')
C. json.dumps(data, indent=True)
D. json.dumps(data, space=4)
Solution
Step 1: Check the correct parameter type for indent
The indent parameter expects an integer number of spaces, not a string or boolean.
Step 2: Validate each option
json.dumps(data, indent=4) uses indent=4 correctly. json.dumps(data, indent='4') uses a string '4' which is invalid. json.dumps(data, indent=True) uses boolean True which is invalid. json.dumps(data, space=4) uses a wrong parameter name space.
Final Answer:
json.dumps(data, indent=4) -> Option A
Quick Check:
Indent value must be integer [OK]
Hint: Use integer for indent, not string or boolean [OK]
Common Mistakes:
Passing indent as a string instead of integer
Using wrong parameter name like 'space'
Passing boolean instead of number
3. What is the output of this code?
import json
data = {'name': 'Alice', 'age': 30}
print(json.dumps(data, indent=2))
medium
A. {"name": "Alice", "age": 30}
B. {
"name": "Alice",
"age": 30
}
C. {name: Alice, age: 30}
D. SyntaxError
Solution
Step 1: Understand json.dumps with indent=2
The function converts the dictionary to a JSON string with 2 spaces indentation for each nested level.
Step 2: Check the output format
The output will have new lines and spaces, keys and string values in double quotes, and numeric values as is.
Final Answer:
{
"name": "Alice",
"age": 30
} -> Option B
Quick Check:
Indent=2 adds spaces and new lines [OK]
Hint: Indent adds new lines and spaces for readability [OK]
Common Mistakes:
Expecting compact JSON without spaces
Using single quotes instead of double quotes
Confusing Python dict print with JSON string
4. The following code throws an error. What is the mistake?
import json
data = {'x': 1, 'y': 2}
print(json.dumps(data, indent=2.0))
medium
A. json.dumps requires a second argument for separators
B. The data dictionary keys must be strings only
C. json.dumps cannot format dictionaries
D. The indent parameter should be an integer, not a float
Solution
Step 1: Identify the error cause
The indent parameter is given as a float 2.0 instead of an integer 2.
Step 2: Understand parameter type requirements
json.dumps expects indent to be an integer number of spaces for formatting, passing a float causes a TypeError.
Final Answer:
The indent parameter should be an integer, not a float -> Option D
Quick Check:
Indent must be int, not float [OK]
Hint: Check indent type: must be integer, not float [OK]
Common Mistakes:
Passing indent as float instead of int
Thinking keys must be strings for json.dumps
Expecting separators argument is mandatory
5. You have a list of dictionaries representing users: