Bird
Raised Fist0
NumPydata~10 mins

np.sqrt() for square roots in NumPy - Step-by-Step Execution

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
Concept Flow - np.sqrt() for square roots
Start with array or number
↓
Call np.sqrt() function
↓
Calculate square root element-wise
↓
Return new array with roots
↓
End
The np.sqrt() function takes a number or array and returns the square root of each element.
Execution Sample
NumPy
import numpy as np
arr = np.array([4, 9, 16])
roots = np.sqrt(arr)
print(roots)
Calculate square roots of each element in the array [4, 9, 16].
Execution Table
StepInput ValueActionOutput Value
14Calculate sqrt(4)2.0
29Calculate sqrt(9)3.0
316Calculate sqrt(16)4.0
4[4,9,16]Apply sqrt element-wise[2.0, 3.0, 4.0]
5Print rootsOutput array[2.0 3.0 4.0]
💡 All elements processed, square roots calculated and printed.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
arrundefined[4, 9, 16][4, 9, 16][4, 9, 16][4, 9, 16]
rootsundefinedundefinedundefined[2.0, 3.0, 4.0][2.0, 3.0, 4.0]
Key Moments - 2 Insights
Why does np.sqrt() return a float even if the input is an integer?
Square roots often are not whole numbers, so np.sqrt() returns floats to keep decimal precision, as shown in execution_table steps 1-3.
What happens if np.sqrt() is given a negative number?
np.sqrt() returns nan or a warning for negative inputs because square root of negative numbers is not defined in real numbers. This is not shown here but important to know.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output of sqrt(9) at step 2?
A3.0
B9
C4.5
D2.0
💡 Hint
Check the 'Output Value' column at step 2 in the execution_table.
At which step is the entire array of square roots created?
AStep 3
BStep 4
CStep 1
DStep 5
💡 Hint
Look for the step where sqrt is applied element-wise to the whole array.
If the input array had a negative number, what would likely happen to the output?
AIt would return a complex number array
BIt would ignore the negative number
CIt would return nan or warning for that element
DIt would return zero for that element
💡 Hint
Recall the key moment about negative inputs and np.sqrt() behavior.
Concept Snapshot
np.sqrt(x) computes the square root of x.
Input can be a number or numpy array.
Returns float results element-wise for arrays.
Negative inputs cause nan or warnings.
Useful for math and data analysis.
Full Transcript
This visual shows how np.sqrt() works step-by-step. We start with an array of numbers. Each number is processed to find its square root, which is usually a float. The results are collected into a new array. Finally, the array of roots is printed. This helps understand how numpy handles element-wise math functions.

Practice

(1/5)
1. What does the np.sqrt() function do in NumPy?
easy
A. Calculates the square root of a number or each element in an array
B. Calculates the square of a number or each element in an array
C. Calculates the logarithm of a number or each element in an array
D. Calculates the exponential of a number or each element in an array

Solution

  1. Step 1: Understand the function purpose

    np.sqrt() is designed to find the square root of numbers or arrays element-wise.
  2. Step 2: Compare with other options

    Options B, C, and D describe different mathematical operations (square, logarithm, exponential) which are not what np.sqrt() does.
  3. Final Answer:

    Calculates the square root of a number or each element in an array -> Option A
  4. Quick Check:

    Square root = np.sqrt() [OK]
Hint: Remember: sqrt means square root, not square or log [OK]
Common Mistakes:
  • Confusing square root with square
  • Thinking it calculates logarithm
  • Assuming it calculates exponential
2. Which of the following is the correct syntax to find the square root of 16 using NumPy?
easy
A. np.square(16)
B. np.sqrt(16)
C. np.sqrt16()
D. sqrt(np.16)

Solution

  1. Step 1: Recall correct function usage

    The correct syntax to find the square root of a number is np.sqrt(number).
  2. Step 2: Check each option

    np.sqrt(16) uses np.sqrt(16) which is correct. np.square(16) uses np.square(16) which squares the number, not square root. Options C and D have invalid syntax.
  3. Final Answer:

    np.sqrt(16) -> Option B
  4. Quick Check:

    Correct syntax = np.sqrt(value) [OK]
Hint: Use np.sqrt() with parentheses around the number [OK]
Common Mistakes:
  • Using np.square() instead of np.sqrt()
  • Missing parentheses after sqrt
  • Incorrect function name or syntax
3. What is the output of the following code?
import numpy as np
arr = np.array([4, 9, 16])
result = np.sqrt(arr)
print(result)
medium
A. Error: np.sqrt() cannot take arrays
B. [16. 81. 256.]
C. [2. 3. 4.]
D. [1.414 3.0 4.0]

Solution

  1. Step 1: Understand input array and function

    The array contains [4, 9, 16]. Applying np.sqrt() computes the square root of each element.
  2. Step 2: Calculate square roots element-wise

    Square roots are sqrt(4)=2, sqrt(9)=3, sqrt(16)=4, so the result is [2. 3. 4.]
  3. Final Answer:

    [2. 3. 4.] -> Option C
  4. Quick Check:

    Square roots of [4,9,16] = [2,3,4] [OK]
Hint: np.sqrt() works element-wise on arrays [OK]
Common Mistakes:
  • Confusing square root with square
  • Expecting a single number output
  • Thinking np.sqrt() cannot handle arrays
4. The following code raises a RuntimeWarning. What is the problem?
import numpy as np
arr = np.array([-4, 9, 16])
result = np.sqrt(arr)
print(result)
medium
A. np.sqrt() cannot handle negative numbers and raises a RuntimeWarning
B. The array syntax is incorrect
C. np.sqrt() requires a list, not a NumPy array
D. The print statement is missing parentheses

Solution

  1. Step 1: Identify the input causing error

    The array contains a negative number -4. Square root of negative numbers is not defined for real numbers.
  2. Step 2: Understand np.sqrt() behavior on negatives

    By default, np.sqrt() raises a RuntimeWarning and returns NaN for negative numbers. This causes unexpected output.
  3. Final Answer:

    np.sqrt() cannot handle negative numbers and raises a RuntimeWarning -> Option A
  4. Quick Check:

    Negative input to sqrt causes RuntimeWarning [OK]
Hint: Square root of negative numbers causes RuntimeWarnings unless complex dtype is used [OK]
Common Mistakes:
  • Thinking array syntax is wrong
  • Assuming np.sqrt() works on negatives by default
  • Ignoring warning messages
5. You have a NumPy array arr = np.array([1, 4, 9, 16, 25]). You want to create a new array that contains the square roots of only the elements greater than 10. Which code correctly does this?
hard
A. np.sqrt(arr[arr < 10])
B. np.sqrt(arr) > 10
C. np.sqrt(arr) if arr > 10 else arr
D. np.sqrt(arr[arr > 10])

Solution

  1. Step 1: Filter elements greater than 10

    Use boolean indexing arr > 10 to select elements 16 and 25.
  2. Step 2: Apply np.sqrt() on filtered elements

    Apply np.sqrt() on the filtered array arr[arr > 10] to get square roots of 16 and 25, which are 4 and 5.
  3. Final Answer:

    np.sqrt(arr[arr > 10]) -> Option D
  4. Quick Check:

    Filter first, then sqrt on filtered [OK]
Hint: Filter array first, then apply np.sqrt() [OK]
Common Mistakes:
  • Applying sqrt before filtering
  • Using incorrect conditional syntax
  • Filtering with wrong comparison operator