Bird
Raised Fist0
NumPydata~10 mins

Element-wise arithmetic in NumPy - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add two numpy arrays element-wise.

NumPy
import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
result = arr1 [1] arr2
print(result)
Drag options to blanks, or click blank then click option'
A/
B-
C+
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causes subtraction.
Using '*' multiplies elements instead of adding.
Using '/' divides elements instead of adding.
2fill in blank
medium

Complete the code to multiply two numpy arrays element-wise.

NumPy
import numpy as np

arr1 = np.array([2, 4, 6])
arr2 = np.array([3, 5, 7])
result = arr1 [1] arr2
print(result)
Drag options to blanks, or click blank then click option'
A*
B-
C+
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' adds elements instead of multiplying.
Using '-' subtracts elements instead of multiplying.
Using '/' divides elements instead of multiplying.
3fill in blank
hard

Fix the error in the code to subtract two numpy arrays element-wise.

NumPy
import numpy as np

arr1 = np.array([10, 20, 30])
arr2 = np.array([1, 2, 3])
result = arr1 [1] arr2
print(result)
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' adds elements instead of subtracting.
Using '*' multiplies elements instead of subtracting.
Using '/' divides elements instead of subtracting.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths if length is greater than 3.

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: [1] for word in words if len(word) [2] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word' instead of 'len(word)' for the dictionary value.
Using '<' instead of '>' in the condition.
Not using len() to get word length.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their lengths if length is greater than 4.

NumPy
words = ['apple', 'bat', 'carrot', 'dog']
result = [1] [2] for word in words if len(word) [3] 4
print(result)
Drag options to blanks, or click blank then click option'
A{word.upper():
Blen(word)
C>
D[word.upper() for word in words]
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list comprehension instead of a dictionary comprehension.
Not converting words to uppercase for keys.
Using '<' instead of '>' in the condition.

Practice

(1/5)
1. What does element-wise arithmetic mean in numpy?
easy
A. Adding arrays only if they have the same number of elements
B. Applying operations to each pair of elements in arrays individually
C. Multiplying the sum of all elements in two arrays
D. Combining arrays into a single list

Solution

  1. Step 1: Understand element-wise operation

    Element-wise arithmetic means applying the operation to each corresponding element in two arrays.
  2. Step 2: Compare options

    Only Applying operations to each pair of elements in arrays individually correctly describes this as applying operations to each pair of elements individually.
  3. Final Answer:

    Applying operations to each pair of elements in arrays individually -> Option B
  4. Quick Check:

    Element-wise = operation on each element pair [OK]
Hint: Think: operation applies to each element pair separately [OK]
Common Mistakes:
  • Confusing element-wise with whole-array operations
  • Thinking arrays must be identical length only
  • Assuming element-wise means concatenation
2. Which of the following is the correct syntax to add two numpy arrays a and b element-wise?
easy
A. np.sum(a, b)
B. a.add(b)
C. a + b
D. a.append(b)

Solution

  1. Step 1: Check numpy addition methods

    The + operator performs element-wise addition in numpy.
  2. Step 2: Evaluate options

    a.add(b) is invalid because numpy arrays do not have an add method. np.sum(a, b) computes sums, not element-wise addition. a.append(b) is a list method and errors on arrays.
  3. Final Answer:

    a + b -> Option C
  4. Quick Check:

    + operator adds arrays element-wise [OK]
Hint: Use + for element-wise addition [OK]
Common Mistakes:
  • Using methods that don't exist on numpy arrays
  • Trying to add arrays with Python list methods
  • Confusing element-wise addition with matrix multiplication
3. What is the output of this code?
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
print(x * y)
medium
A. [4 10 18]
B. [5 7 9]
C. [1 2 3 4 5 6]
D. Error: arrays must be same shape

Solution

  1. Step 1: Understand element-wise multiplication

    Multiplying two numpy arrays multiplies each element pair: 1*4=4, 2*5=10, 3*6=18.
  2. Step 2: Calculate output array

    The result is an array: [4, 10, 18].
  3. Final Answer:

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

    Element-wise multiply: [1*4, 2*5, 3*6] = [4 10 18] [OK]
Hint: Multiply arrays element-wise: multiply each pair [OK]
Common Mistakes:
  • Adding instead of multiplying elements
  • Expecting concatenation of arrays
  • Thinking multiplication sums elements
4. Identify the error in this code snippet:
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5])
print(x + y)
medium
A. No error, output is [5 7 3]
B. Syntax error in print statement
C. Missing import statement for numpy
D. Arrays have incompatible shapes for element-wise addition

Solution

  1. Step 1: Check array shapes

    Array x has shape (3,), y has shape (2,). They are not compatible for element-wise addition.
  2. Step 2: Understand numpy broadcasting rules

    Broadcasting requires shapes to be compatible; here they differ and cause an error.
  3. Final Answer:

    Arrays have incompatible shapes for element-wise addition -> Option D
  4. Quick Check:

    Shape mismatch causes error in element-wise addition [OK]
Hint: Check array shapes before element-wise operations [OK]
Common Mistakes:
  • Assuming arrays auto-align despite different lengths
  • Ignoring broadcasting rules
  • Thinking Python lists behave like numpy arrays
5. Given two arrays:
a = np.array([[1, 2], [3, 4]])
b = np.array([10, 20])

What is the result of a + b and why?
hard
A. [[11 22] [13 24]] because b is broadcasted to each row of a
B. [[10 20] [10 20]] because b replaces a
C. [[1 2] [3 4]] because addition is not possible
D. Error due to shape mismatch

Solution

  1. Step 1: Analyze shapes of arrays

    a is shape (2,2), b is shape (2,). Numpy broadcasts b across rows of a.
  2. Step 2: Perform element-wise addition with broadcasting

    Add b to each row of a: first row [1+10, 2+20], second row [3+10, 4+20].
  3. Final Answer:

    [[11 22] [13 24]] -> Option A
  4. Quick Check:

    Broadcasting adds b to each row of a [OK]
Hint: Broadcast smaller array to match larger shape for element-wise ops [OK]
Common Mistakes:
  • Expecting error due to different shapes
  • Thinking b replaces a instead of broadcasting
  • Ignoring numpy broadcasting rules
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