What does element-wise arithmetic mean when working with arrays?
Element-wise arithmetic means performing operations on each pair of corresponding elements from two arrays separately, like adding or multiplying each element one by one.
Click to reveal answer
beginner
How do you add two arrays element-wise in Python using NumPy?
You simply use the + operator between two NumPy arrays of the same shape, for example: array1 + array2 adds each element of array1 to the corresponding element of array2.
Click to reveal answer
intermediate
What happens if you try element-wise operations on arrays of different shapes?
If the shapes are not compatible, NumPy will raise an error. But if shapes are compatible by broadcasting rules, it will perform element-wise operations by stretching the smaller array.
Click to reveal answer
beginner
Show an example of element-wise multiplication of two arrays.
Example:<br><code>import numpy as np<br>a = np.array([1, 2, 3])<br>b = np.array([4, 5, 6])<br>c = a * b # c is array([4, 10, 18])</code>
Click to reveal answer
beginner
Why is element-wise array arithmetic useful in data science?
It allows fast and simple calculations on datasets, like scaling, combining, or transforming data without writing loops, making code cleaner and faster.
Click to reveal answer
What does the expression array1 + array2 do in NumPy if both arrays have the same shape?
AConcatenates the two arrays
BAdds each element of array1 to the corresponding element of array2
CMultiplies each element of array1 by the corresponding element of array2
DRaises an error
✗ Incorrect
When two arrays have the same shape, the + operator adds their elements one by one.
What will happen if you try to add arrays of shapes (3,) and (2,)?
ARaises a shape mismatch error
BElement-wise addition with broadcasting
CConcatenates the arrays
DPerforms dot product
✗ Incorrect
Arrays with incompatible shapes that cannot be broadcasted will cause an error.
Which operator is used for element-wise multiplication of two NumPy arrays?
A-
B+
C@
D*
✗ Incorrect
The * operator multiplies arrays element-wise.
If a = np.array([1, 2, 3]) and b = np.array([4, 5, 6]), what is a * b?
A[5, 7, 9]
BError
C[4, 10, 18]
D[1, 2, 3, 4, 5, 6]
✗ Incorrect
Element-wise multiplication multiplies each pair: 1*4=4, 2*5=10, 3*6=18.
Why is element-wise arithmetic preferred over loops in data science?
AIt is faster and more concise
BIt requires less memory
CIt is slower but easier to read
DIt avoids using arrays
✗ Incorrect
Element-wise operations use optimized code and avoid explicit loops, making them faster and cleaner.
Explain what element-wise arithmetic is and give an example using Python arrays.
Think about how you add or multiply numbers one by one in two lists.
You got /3 concepts.
Describe what happens when you try to perform element-wise operations on arrays with incompatible shapes.
Consider what happens if you try to add a list of 3 numbers to a list of 2 numbers.
You got /3 concepts.
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
Step 1: Understand element-wise operation
Element-wise means doing the operation on each pair of matching positions in two arrays.
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.
Final Answer:
Adding each number in one array to the corresponding number in the other array -> Option B
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
Step 1: Recall NumPy addition syntax
NumPy supports element-wise addition using the + operator directly between arrays.
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.
Final Answer:
c = a + b -> Option A
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
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.
Step 2: Calculate each element
1*4=4, 2*5=10, 3*6=18, so the result is [4, 10, 18].
Final Answer:
[4 10 18] -> Option C
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
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.
Step 2: Understand error cause
Since sizes differ and no broadcasting applies here, addition fails with a ValueError.
Final Answer:
Arrays have different sizes, so element-wise addition is not possible -> Option D
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:
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
Step 1: Calculate total sales element-wise
Add store1 and store2 arrays element-wise using + operator to get daily totals.
Step 2: Apply 10% tax
Multiply the total sales array by 1.10 to add 10% tax to each day's total.
Final Answer:
total = store1 + store2
final = total * 1.10
print(final) -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.