Bird
Raised Fist0
NumPydata~10 mins

np.power() and np.square() in NumPy - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to square each element in the array using np.square().

NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
squared = np.[1](arr)
print(squared)
Drag options to blanks, or click blank then click option'
Aabs
Bpower
Csqrt
Dsquare
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.sqrt() which computes the square root instead.
Using np.abs() which computes absolute values.
2fill in blank
medium

Complete the code to raise each element in the array to the power of 3 using np.power().

NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
cubed = np.power(arr, [1])
print(cubed)
Drag options to blanks, or click blank then click option'
A3
B4
C2
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2 which squares instead of cubes.
Using 0 which results in all ones.
3fill in blank
hard

Fix the error in the code to correctly square the array elements using np.power().

NumPy
import numpy as np
arr = np.array([2, 3, 4])
squared = np.power(arr, [1])
print(squared)
Drag options to blanks, or click blank then click option'
A1
Barr
C2
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 which returns the original array.
Using 0 which returns ones.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their squared lengths as values, only for words longer than 3 characters.

NumPy
import numpy as np
words = ['data', 'science', 'ai', 'ml']
squared_lengths = {word: [1] for word in words if len(word) [2] 3}
print(squared_lengths)
Drag options to blanks, or click blank then click option'
Anp.square(len(word))
Bnp.power(len(word), 3)
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.power(len(word), 3) which cubes instead of squares.
Using <= which selects shorter words.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their squared lengths as values, only for words longer than 2 characters.

NumPy
import numpy as np
words = ['cat', 'dog', 'a', 'elephant']
squared_lengths = { [1]: [2] for word in words if len(word) [3] 2}
print(squared_lengths)
Drag options to blanks, or click blank then click option'
Aword.upper()
Bnp.square(len(word))
C>
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using word.lower() instead of upper().
Using < instead of > for filtering.
Using len(word) without np.square().

Practice

(1/5)
1. What does the np.square() function do in NumPy?
easy
A. It raises each element of an array to the power of 2.
B. It calculates the square root of each element in an array.
C. It multiplies two arrays element-wise.
D. It returns the sum of squares of array elements.

Solution

  1. Step 1: Understand the purpose of np.square()

    The function np.square() takes each element in an array and raises it to the power of 2.
  2. Step 2: Compare with other options

    It does not calculate square roots, multiply arrays, or sum squares; it only squares each element.
  3. Final Answer:

    It raises each element of an array to the power of 2. -> Option A
  4. Quick Check:

    np.square(x) = x² [OK]
Hint: Remember: square means power of 2, not root or sum [OK]
Common Mistakes:
  • Confusing square with square root
  • Thinking it sums squares instead of element-wise operation
  • Mixing with multiplication of arrays
2. Which of the following is the correct syntax to square all elements in a NumPy array arr using np.power()?
easy
A. np.power(arr)
B. np.power(2, arr)
C. np.power(arr, 2)
D. np.power(arr, arr)

Solution

  1. Step 1: Understand np.power() syntax

    The function np.power(base, exponent) raises each element in base to the exponent.
  2. Step 2: Apply to square elements

    To square elements of arr, use np.power(arr, 2). Other options misuse the order or miss the exponent.
  3. Final Answer:

    np.power(arr, 2) -> Option C
  4. Quick Check:

    np.power(arr, 2) = arr squared [OK]
Hint: np.power(base, exponent) order matters: base first [OK]
Common Mistakes:
  • Swapping base and exponent
  • Omitting the exponent argument
  • Using the array as exponent incorrectly
3. What is the output of the following code?
import numpy as np
arr = np.array([1, 2, 3])
result = np.power(arr, 3)
print(result)
medium
A. [1 6 9]
B. [1 8 27]
C. [3 6 9]
D. [1 4 9]

Solution

  1. Step 1: Understand np.power(arr, 3)

    This raises each element of arr to the power of 3.
  2. Step 2: Calculate each element

    1³=1, 2³=8, 3³=27, so the result is [1, 8, 27].
  3. Final Answer:

    [1 8 27] -> Option B
  4. Quick Check:

    np.power([1,2,3],3) = [1,8,27] [OK]
Hint: Power 3 means cube each element [OK]
Common Mistakes:
  • Calculating square instead of cube
  • Adding elements instead of powering
  • Confusing element-wise with sum
4. Identify the error in the following code snippet:
import numpy as np
arr = np.array([2, 4, 6])
result = np.square(arr, 2)
print(result)
medium
A. np.square() takes only one argument, but two were given.
B. np.square() cannot be used on arrays.
C. The array must be converted to a list before squaring.
D. The code should use np.power(arr, 2) instead.

Solution

  1. Step 1: Check np.square() function signature

    np.square() accepts only one argument: the array or number to square.
  2. Step 2: Analyze the code error

    The code passes two arguments, which causes a TypeError.
  3. Final Answer:

    np.square() takes only one argument, but two were given. -> Option A
  4. Quick Check:

    np.square(x) needs 1 argument [OK]
Hint: np.square() needs exactly one argument [OK]
Common Mistakes:
  • Passing extra arguments to np.square()
  • Thinking np.square() needs base and exponent
  • Confusing np.square() with np.power()
5. You have a NumPy array data = np.array([1, -2, 3, -4]). You want to create a new array where each element is squared, but negative values should be squared and then multiplied by -1 to keep their sign effect. Which code correctly achieves this?
hard
A. np.square(np.abs(data))
B. np.square(data)
C. np.power(data, 2)
D. np.where(data < 0, -np.square(data), np.square(data))

Solution

  1. Step 1: Understand the requirement

    Negative values should be squared and then multiplied by -1; positive values just squared.
  2. Step 2: Analyze each option

    np.where(data < 0, -np.square(data), np.square(data)) uses np.where to apply -np.square() for negatives and np.square() for positives, matching the requirement exactly.
  3. Final Answer:

    np.where(data < 0, -np.square(data), np.square(data)) -> Option D
  4. Quick Check:

    Conditional square with sign handled = np.where(data < 0, -np.square(data), np.square(data)) [OK]
Hint: Use np.where for conditionally applying functions [OK]
Common Mistakes:
  • Multiplying sign before squaring
  • Using np.sign(data) directly without condition
  • Confusing absolute value with sign handling