Vectorized operations vs loops
📖 Scenario: Imagine you have a list of daily sales numbers for a small store. You want to calculate the total sales and also increase each day's sales by 10% to see the effect of a price increase.
🎯 Goal: You will create a list of sales numbers, set a percentage increase, use a loop and then vectorized operations with NumPy to increase sales, and finally print the results to compare.
📋 What You'll Learn
Create a list of daily sales numbers called
sales with these exact values: 100, 150, 200, 130, 170Create a variable called
increase_percent and set it to 10Use a
for loop with variable i to increase each sale in sales by increase_percent percent and store results in a new list called increased_sales_loopUse NumPy vectorized operations to increase
sales by increase_percent percent and store results in a NumPy array called increased_sales_vectorizedPrint both
increased_sales_loop and increased_sales_vectorized to compare the results💡 Why This Matters
🌍 Real World
Retail stores and businesses often need to quickly update prices or sales data and analyze the effects. Vectorized operations help do this faster and with simpler code.
💼 Career
Data analysts and scientists use vectorized operations in Python with libraries like NumPy to handle large datasets efficiently, making their work faster and more reliable.
Progress0 / 4 steps