0
0
Data Analysis Pythondata~30 mins

Vectorized operations vs loops in Data Analysis Python - Hands-On Comparison

Choose your learning style9 modes available
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, 170
Create a variable called increase_percent and set it to 10
Use 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_loop
Use NumPy vectorized operations to increase sales by increase_percent percent and store results in a NumPy array called increased_sales_vectorized
Print 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
1
Create the sales data list
Create a list called sales with these exact values: 100, 150, 200, 130, 170
Data Analysis Python
Hint

Use square brackets [] to create a list and separate numbers with commas.

2
Set the increase percentage
Create a variable called increase_percent and set it to 10
Data Analysis Python
Hint

Just assign the number 10 to the variable increase_percent.

3
Increase sales using a for loop
Use a for loop with variable i to increase each sale in sales by increase_percent percent. Store the results in a new list called increased_sales_loop
Data Analysis Python
Hint

Use range(len(sales)) to loop over indexes, calculate increased value, and add to the new list.

4
Increase sales using NumPy vectorized operations and print results
Import NumPy as np. Use NumPy vectorized operations to increase sales by increase_percent percent and store the result in a NumPy array called increased_sales_vectorized. Then print both increased_sales_loop and increased_sales_vectorized
Data Analysis Python
Hint

Use np.array(sales) to convert list to array, then multiply by 1 + increase_percent / 100. Use print() to show both results.