We use np.sqrt() to find the square root of numbers or each number in a list. It helps us quickly get the root values without doing the math by hand.
np.sqrt() for square roots in NumPy
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
NumPy
np.sqrt(x)
x can be a single number or a list/array of numbers.
The function returns the square root of each element in x.
Examples
NumPy
import numpy as np result = np.sqrt(16) print(result)
NumPy
import numpy as np arr = np.array([1, 4, 9, 16]) result = np.sqrt(arr) print(result)
NumPy
import numpy as np result = np.sqrt(0) print(result)
Sample Program
This program calculates the square root of each number in the list and prints both the original numbers and their square roots.
NumPy
import numpy as np # Calculate square roots of a list of numbers numbers = np.array([25, 36, 49, 64, 81]) square_roots = np.sqrt(numbers) print("Original numbers:", numbers) print("Square roots:", square_roots)
Important Notes
If you pass a negative number, np.sqrt() will return nan (not a number) because square root of negative numbers is not defined in real numbers.
You can use np.sqrt() on numpy arrays for fast calculations on many numbers at once.
Summary
np.sqrt() finds the square root of numbers or arrays.
It works on single numbers and lists/arrays of numbers.
Useful in many real-life math and data science problems.
Practice
1. What does the
np.sqrt() function do in NumPy?easy
Solution
Step 1: Understand the function purpose
np.sqrt()is designed to find the square root of numbers or arrays element-wise.Step 2: Compare with other options
Options B, C, and D describe different mathematical operations (square, logarithm, exponential) which are not whatnp.sqrt()does.Final Answer:
Calculates the square root of a number or each element in an array -> Option AQuick 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
Solution
Step 1: Recall correct function usage
The correct syntax to find the square root of a number isnp.sqrt(number).Step 2: Check each option
np.sqrt(16) usesnp.sqrt(16)which is correct. np.square(16) usesnp.square(16)which squares the number, not square root. Options C and D have invalid syntax.Final Answer:
np.sqrt(16) -> Option BQuick 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
Solution
Step 1: Understand input array and function
The array contains [4, 9, 16]. Applyingnp.sqrt()computes the square root of each element.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.]Final Answer:
[2. 3. 4.] -> Option CQuick 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
Solution
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.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.Final Answer:
np.sqrt() cannot handle negative numbers and raises a RuntimeWarning -> Option AQuick 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
Solution
Step 1: Filter elements greater than 10
Use boolean indexingarr > 10to select elements 16 and 25.Step 2: Apply np.sqrt() on filtered elements
Applynp.sqrt()on the filtered arrayarr[arr > 10]to get square roots of 16 and 25, which are 4 and 5.Final Answer:
np.sqrt(arr[arr > 10]) -> Option DQuick 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
