List comprehension vs loop
๐ Scenario: You work in a small bakery. Every day, you record the number of cookies sold in a list. You want to find out how many cookies you sold each day after giving a 10% discount on the price per cookie.
๐ฏ Goal: Build a program that calculates the discounted cookie prices using both a for loop and a list comprehension. Compare the results.
๐ What You'll Learn
Create a list called
cookie_prices with exact values: 2.0, 2.5, 3.0, 2.75, 3.25Create a variable called
discount_rate and set it to 0.10Use a
for loop with variable price to create a list called discounted_prices_loop with prices after discountUse a list comprehension with variable
price to create a list called discounted_prices_comp with prices after discountPrint both
discounted_prices_loop and discounted_prices_comp๐ก Why This Matters
๐ Real World
Calculating discounts on product prices is common in stores and online shops to attract customers.
๐ผ Career
Understanding loops and list comprehensions helps in data processing tasks, which are common in programming jobs.
Progress0 / 4 steps