0
0
NumPydata~20 mins

np.linspace() for evenly spaced arrays in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Linspace Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of np.linspace() with default endpoint
What is the output of this code?
import numpy as np
arr = np.linspace(0, 1, 5)
print(arr)
NumPy
import numpy as np
arr = np.linspace(0, 1, 5)
print(arr)
A[0. 0.25 0.5 0.75 1. ]
B[0. 0.2 0.4 0.6 0.8 ]
C[0. 0.25 0.5 0.75]
D[0. 0.2 0.4 0.6 0.8 1.0]
Attempts:
2 left
💡 Hint
Remember np.linspace includes the endpoint by default.
data_output
intermediate
1:30remaining
Number of elements in np.linspace() output
How many elements are in the array created by this code?
import numpy as np
arr = np.linspace(10, 20, 11)
NumPy
import numpy as np
arr = np.linspace(10, 20, 11)
print(len(arr))
A10
B20
C9
D11
Attempts:
2 left
💡 Hint
The third argument is the number of points to generate.
Predict Output
advanced
2:00remaining
Effect of endpoint=False in np.linspace()
What is the output of this code?
import numpy as np
arr = np.linspace(0, 1, 5, endpoint=False)
print(arr)
NumPy
import numpy as np
arr = np.linspace(0, 1, 5, endpoint=False)
print(arr)
A[0. 0.2 0.4 0.6 0.8]
B[0. 0.25 0.5 0.75 1. ]
C[0. 0.2 0.4 0.6 0.8 1.0]
D[0. 0.25 0.5 0.75]
Attempts:
2 left
💡 Hint
Setting endpoint=False excludes the stop value.
🧠 Conceptual
advanced
1:30remaining
Understanding np.linspace() step size
If you run np.linspace(2, 8, 4), what is the step size between consecutive elements?
A3
B1.5
C2
D6
Attempts:
2 left
💡 Hint
Step size = (stop - start) / (number_of_points - 1)
🔧 Debug
expert
2:00remaining
Identify the error in np.linspace() usage
What error does this code raise?
import numpy as np
arr = np.linspace(5, 1, -3)
print(arr)
NumPy
import numpy as np
arr = np.linspace(5, 1, -3)
print(arr)
ATypeError: Negative step size not allowed.
BValueError: Number of samples, -3, must be non-negative.
CSyntaxError: invalid syntax in np.linspace call.
DNo error, outputs an empty array.
Attempts:
2 left
💡 Hint
Number of samples must be zero or positive integer.