Bird
Raised Fist0
Data Analysis Pythondata~10 mins

Array arithmetic (element-wise) in Data Analysis Python - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Array arithmetic (element-wise)
Start with two arrays
↓
Select operation: +, -, *, /
↓
Apply operation element-wise
↓
Create new array with results
↓
Output result array
We take two arrays and apply the chosen arithmetic operation to each pair of elements, creating a new array of results.
Execution Sample
Data Analysis Python
import numpy as np
arr1 = np.array([2, 4, 6])
arr2 = np.array([1, 3, 5])
result = arr1 + arr2
print(result)
This code adds two arrays element-wise and prints the resulting array.
Execution Table
Steparr1 Elementarr2 ElementOperationResult Element
1212 + 13
2434 + 37
3656 + 511
End---Result array complete: [3, 7, 11]
💡 All elements processed, element-wise addition complete.
Variable Tracker
VariableStartAfter 1After 2After 3Final
arr1[2, 4, 6][2, 4, 6][2, 4, 6][2, 4, 6][2, 4, 6]
arr2[1, 3, 5][1, 3, 5][1, 3, 5][1, 3, 5][1, 3, 5]
result[][3][3, 7][3, 7, 11][3, 7, 11]
Key Moments - 3 Insights
Why does the operation add elements one by one instead of adding whole arrays at once?
The operation is element-wise, meaning each element in arr1 is paired with the element at the same position in arr2. This is shown in the execution_table where each step adds corresponding elements.
What happens if the arrays have different lengths?
Element-wise operations require arrays to be the same length. If lengths differ, Python's numpy will raise an error. This example assumes equal length arrays as shown in the variable_tracker.
Is the original array changed after the operation?
No, the original arrays arr1 and arr2 stay the same. The result is stored in a new array, as seen in variable_tracker where arr1 and arr2 remain unchanged.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result element at step 2?
A5
B4
C7
D11
💡 Hint
Check the 'Result Element' column at step 2 in the execution_table.
At which step does the result array first contain two elements?
AAfter step 2
BAfter step 1
CAfter step 3
DAt the end
💡 Hint
Look at the 'result' variable in variable_tracker after each step.
If arr2 was [1, 3] instead of [1, 3, 5], what would happen?
AThe operation would complete normally with shorter result.
BAn error would occur due to different array lengths.
CThe missing elements would be treated as zero.
DThe operation would ignore extra elements in arr1.
💡 Hint
Recall the key moment about array length compatibility for element-wise operations.
Concept Snapshot
Array arithmetic (element-wise):
- Applies operations (+, -, *, /) between arrays element by element.
- Arrays must be the same length.
- Produces a new array with results.
- Original arrays stay unchanged.
- Useful for quick, parallel calculations.
Full Transcript
This visual execution shows how element-wise array arithmetic works in Python using numpy. We start with two arrays of the same length. Each step adds the elements at the same position from both arrays. The result is stored in a new array. The original arrays remain unchanged. The process continues until all elements are processed, producing the final result array. This method requires arrays to be the same length, otherwise an error occurs.

Practice

(1/5)
1. What does element-wise addition of two arrays mean in simple terms?
easy
A. Combining the arrays end to end without any math
B. Adding each number in one array to the corresponding number in the other array
C. Multiplying each number in one array by the total sum of the other array
D. Adding all numbers in both arrays together as a single sum

Solution

  1. Step 1: Understand element-wise operation

    Element-wise means doing the operation on each pair of matching positions in two arrays.
  2. Step 2: Apply to addition

    For addition, each number in the first array is added to the number in the same position in the second array.
  3. Final Answer:

    Adding each number in one array to the corresponding number in the other array -> Option B
  4. Quick Check:

    Element-wise addition = add matching elements [OK]
Hint: Think pair by pair addition, not total sums [OK]
Common Mistakes:
  • Confusing element-wise with total sum
  • Thinking arrays are joined instead of added
  • Mixing addition with multiplication
2. Which of the following is the correct syntax to add two NumPy arrays a and b element-wise?
easy
A. c = a + b
B. c = a.add(b)
C. c = add(a, b)
D. c = a & b

Solution

  1. Step 1: Recall NumPy addition syntax

    NumPy supports element-wise addition using the + operator directly between arrays.
  2. Step 2: Check options

    c = a + b uses a + b, which is correct. c = a.add(b) is invalid syntax. c = add(a, b) requires importing add function explicitly. c = a & b uses bitwise AND, not addition.
  3. Final Answer:

    c = a + b -> Option A
  4. Quick Check:

    Use + for element-wise addition in NumPy [OK]
Hint: Use + operator for element-wise addition in NumPy [OK]
Common Mistakes:
  • Using bitwise operators instead of +
  • Trying to call add as a method on array
  • Forgetting to import add function if used
3. What is the output of this code?
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
z = x * y
print(z)
medium
A. [4 5 6]
B. [5 7 9]
C. [4 10 18]
D. [1 2 3 4 5 6]

Solution

  1. Step 1: Understand element-wise multiplication

    Multiplying two arrays with * multiplies each element in the first array by the corresponding element in the second array.
  2. Step 2: Calculate each element

    1*4=4, 2*5=10, 3*6=18, so the result is [4, 10, 18].
  3. Final Answer:

    [4 10 18] -> Option C
  4. Quick Check:

    Element-wise multiply = multiply matching elements [OK]
Hint: Multiply arrays element by element with * operator [OK]
Common Mistakes:
  • Adding instead of multiplying
  • Concatenating arrays instead of multiplying
  • Confusing output format with list instead of array
4. The following code throws an error. What is the main reason?
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5])
z = x + y
print(z)
medium
A. You forgot to convert arrays to lists first
B. You must use np.add() instead of + operator
C. The print statement syntax is incorrect
D. Arrays have different sizes, so element-wise addition is not possible

Solution

  1. Step 1: Check array sizes

    Array x has 3 elements, y has 2 elements. Element-wise operations require arrays to be the same size or broadcastable.
  2. Step 2: Understand error cause

    Since sizes differ and no broadcasting applies here, addition fails with a ValueError.
  3. Final Answer:

    Arrays have different sizes, so element-wise addition is not possible -> Option D
  4. Quick Check:

    Array sizes must match for element-wise ops [OK]
Hint: Check array lengths before element-wise operations [OK]
Common Mistakes:
  • Assuming + works regardless of size
  • Thinking np.add fixes size mismatch
  • Blaming print syntax for error
5. You have two arrays representing daily sales for two stores:
store1 = np.array([10, 20, 30, 40])
store2 = np.array([5, 15, 25, 35])

You want to find the total sales per day by adding these arrays element-wise. Then, you want to apply a 10% tax on the total sales. Which code correctly does this?
hard
A. total = store1 + store2 final = total * 1.10 print(final)
B. total = store1 + store2 final = total + 0.10 print(final)
C. total = store1 * store2 final = total * 1.10 print(final)
D. final = (store1 + store2) / 1.10 print(final)

Solution

  1. Step 1: Calculate total sales element-wise

    Add store1 and store2 arrays element-wise using + operator to get daily totals.
  2. Step 2: Apply 10% tax

    Multiply the total sales array by 1.10 to add 10% tax to each day's total.
  3. Final Answer:

    total = store1 + store2 final = total * 1.10 print(final) -> Option A
  4. Quick Check:

    Add then multiply by 1.10 for tax [OK]
Hint: Add arrays first, then multiply by 1.10 for tax [OK]
Common Mistakes:
  • Adding 0.10 instead of multiplying by 1.10
  • Multiplying arrays instead of adding
  • Dividing instead of multiplying for tax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard