📖 Scenario: You are working with scientific data and want to use NumPy to perform a simple computation.NumPy is a Python library that helps with math and science tasks.
🎯 Goal: Learn how to import NumPy and use it to calculate the square root of a number.
📋 What You'll Learn
Create a variable with a number
Import the numpy library's sqrt function
Use sqrt to compute the square root of the number
Print the result
💡 Why This Matters
🌍 Real World
Scientists and engineers often use NumPy to perform math calculations on data.
💼 Career
Knowing how to use NumPy is useful for data scientists and researchers working with scientific data.
Progress0 / 4 steps
1
Create a variable with a number
Create a variable called number and set it to 16.
SciPy
Hint
Use = to assign the value 16 to the variable number.
2
Import the sqrt function from NumPy
Import the sqrt function from numpy.
SciPy
Hint
Use from numpy import sqrt to import the square root function.
3
Calculate the square root
Create a variable called result and set it to the square root of number using sqrt(number).
SciPy
Hint
Use result = sqrt(number) to compute the square root.
4
Print the result
Print the value of result.
SciPy
Hint
Use print(result) to show the square root.
Practice
(1/5)
1. What is the main purpose of the SciPy library in Python?
easy
A. To manage databases
B. To create web applications
C. To perform scientific and mathematical computations easily
D. To design user interfaces
Solution
Step 1: Understand SciPy's role
SciPy is designed for scientific and mathematical computations. Options for managing databases, creating web applications, and designing user interfaces do not match.
Final Answer:
To perform scientific and mathematical computations easily -> Option C
Quick Check:
SciPy = scientific math library [OK]
Hint: SciPy is for science and math, not web or UI [OK]
Common Mistakes:
Confusing SciPy with web frameworks
Thinking SciPy manages databases
Assuming SciPy is for UI design
2. Which of the following is the correct way to import the SciPy square root function?
easy
A. from scipy import sqrt
B. from scipy.special import sqrt
C. from scipy import math.sqrt
D. from scipy import sqrt as sp_sqrt
Solution
Step 1: Recall SciPy import syntax
sqrt is in scipy.special, so from scipy.special import sqrt works. Direct from scipy import sqrt fails as sqrt is not in top-level scipy (A,D); math.sqrt is separate module (B).
Final Answer:
from scipy.special import sqrt -> Option B
Quick Check:
from scipy.special import sqrt [OK]
Hint: Use scipy.special for sqrt [OK]
Common Mistakes:
Importing sqrt directly from scipy
Using incorrect module paths
Trying to import math.sqrt from scipy
3. What is the output of this code?
import numpy as np
from scipy import special
x = np.array([0, 1, 2])
y = special.expit(x)
print(y)
Hint: expit = sigmoid function, check values at 0,1,2 [OK]
Common Mistakes:
Confusing expit with exponential
Expecting raw input array as output
Mixing up output with other functions
4. Identify the error in this code snippet:
from scipy import sqrt
result = sqrt(16)
print(result)
medium
A. print statement syntax is incorrect
B. sqrt function requires two arguments
C. Missing numpy import for sqrt
D. sqrt is not directly in scipy, import from scipy.special
Solution
Step 1: Check import correctness
sqrt is not in top-level scipy (needs scipy.special); function takes one argument; no numpy required; print is correct.
Final Answer:
sqrt is not directly in scipy, import from scipy.special -> Option D
Quick Check:
Import path error = sqrt is not directly in scipy, import from scipy.special [OK]
Hint: sqrt is in scipy.special, not scipy directly [OK]
Common Mistakes:
Assuming sqrt is in scipy root module
Thinking sqrt needs multiple arguments
Confusing print syntax with Python 2
5. You want to compute the Bessel function of the first kind for values [0, 1, 2] using SciPy. Which code snippet correctly does this?
hard
A. from scipy.special import jv
import numpy as np
x = np.array([0,1,2])
result = jv(0, x)
print(result)
B. import scipy
x = [0,1,2]
result = scipy.bessel(0, x)
print(result)
C. from scipy import bessel
x = [0,1,2]
result = bessel.jv(0, x)
print(result)
D. from scipy.special import jn
x = [0,1,2]
result = jn(x, 0)
print(result)
Solution
Step 1: Identify correct function and import
Bessel function of first kind: jv(order, x) from scipy.special. Code with from scipy.special import jv, np.array x, jv(0, x) works; others have wrong imports/functions/arguments.
Final Answer:
from scipy.special import jv
import numpy as np
x = np.array([0,1,2])
result = jv(0, x)
print(result) -> Option A
Quick Check:
jv(0, x) from scipy.special [OK]
Hint: Use jv(order, x) from scipy.special for Bessel function [OK]
Common Mistakes:
Using wrong function names like bessel or jn incorrectly
Swapping arguments order in jn or jv
Not importing numpy for array input
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.