Bird
Raised Fist0
NumPydata~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the np.linspace(start, stop, num) function do in NumPy?
easy
A. Generates integers from start to stop with step size num.
B. Generates random numbers between start and stop.
C. Generates num evenly spaced numbers between start and stop inclusive.
D. Generates a sequence of numbers starting at start with length num.

Solution

  1. Step 1: Understand the function purpose

    np.linspace creates numbers evenly spaced between two points.
  2. Step 2: Analyze parameters

    The parameters start, stop, and num define the range and count of numbers.
  3. Final Answer:

    Generates num evenly spaced numbers between start and stop inclusive. -> Option C
  4. Quick Check:

    np.linspace = evenly spaced numbers [OK]
Hint: Remember: linspace means linear spacing between start and stop [OK]
Common Mistakes:
  • Confusing linspace with random number generation
  • Thinking it generates integers only
  • Assuming it excludes the stop value by default
2. Which of the following is the correct syntax to create 5 evenly spaced numbers from 0 to 10 using np.linspace?
easy
A. np.linspace(5, 0, 10)
B. np.linspace(0, 10, 5)
C. np.linspace(start=0, stop=10)
D. np.linspace(0, 10, step=5)

Solution

  1. Step 1: Check parameter order and count

    np.linspace requires start, stop, and num (number of points).
  2. Step 2: Validate each option

    np.linspace(0, 10, 5) correctly uses start=0, stop=10, and num=5. np.linspace(0, 10, step=5) wrongly uses step which is invalid. np.linspace(start=0, stop=10) misses num. np.linspace(5, 0, 10) reverses start and stop and uses wrong count.
  3. Final Answer:

    np.linspace(0, 10, 5) -> Option B
  4. Quick Check:

    Correct syntax includes start, stop, and number [OK]
Hint: Use (start, stop, num) order; no step parameter [OK]
Common Mistakes:
  • Using step instead of num
  • Omitting the number of points
  • Reversing start and stop unintentionally
3. What is the output of the following code?
import numpy as np
arr = np.linspace(2, 6, 3)
print(arr)
medium
A. [2. 6. 10.]
B. [2. 3. 4.]
C. [2. 3.5 5.]
D. [2. 4. 6.]

Solution

  1. Step 1: Understand parameters

    The code creates 3 numbers evenly spaced from 2 to 6 inclusive.
  2. Step 2: Calculate spacing and values

    Spacing = (6 - 2) / (3 - 1) = 2. Values: 2, 4, 6.
  3. Final Answer:

    [2. 4. 6.] -> Option D
  4. Quick Check:

    3 points from 2 to 6 spaced by 2 [OK]
Hint: Divide range by (num-1) to find step size [OK]
Common Mistakes:
  • Assuming step size is 1
  • Confusing number of points with step size
  • Forgetting that stop is included by default
4. Which of the following statements is correct about this code snippet:
import numpy as np
arr = np.linspace(1, 5)
print(arr)
medium
A. Missing the number of points to generate.
B. Start value cannot be less than stop value.
C. np.linspace requires integer inputs only.
D. The print statement is incorrect.

Solution

  1. Step 1: Check function parameters

    np.linspace requires start and stop, with num optional (defaults to 50).
  2. Step 2: Examine the code

    The code omits num, so it uses the default of 50 points.
  3. Step 3: Evaluate options

    A: False, start can be less than stop. B: Incorrect, the number of points is not missing, it defaults to 50. C: False, floats are allowed. D: False, print is correct.
  4. Final Answer:

    np.linspace uses a default number of points (50) if num is omitted, so no error occurs. -> Option A
  5. Quick Check:

    Default num=50 if omitted in linspace [OK]
Hint: Remember num defaults to 50 if not given [OK]
Common Mistakes:
  • Thinking num is mandatory
  • Assuming start must be greater than stop
  • Believing only integers allowed
5. You want to create a smooth curve for plotting a sine wave between 0 and 2π. Which np.linspace call is best to generate 100 points including 0 and 2π?
hard
A. np.linspace(0, 2*np.pi, 100, endpoint=True)
B. np.linspace(0, 2*np.pi, 100, endpoint=False)
C. np.linspace(0, 2*np.pi, 99, endpoint=True)
D. np.linspace(0, 2*np.pi, 101, endpoint=False)

Solution

  1. Step 1: Understand plotting needs

    For smooth plotting, include both start (0) and stop (2π) points with enough samples.
  2. Step 2: Analyze options

    np.linspace(0, 2*np.pi, 100, endpoint=True) generates 100 points including 2π (endpoint=True). np.linspace(0, 2*np.pi, 100, endpoint=False) excludes 2π, which may cause plot to miss endpoint. Options A, B, and D have wrong number of points or exclude endpoint.
  3. Final Answer:

    np.linspace(0, 2*np.pi, 100, endpoint=True) -> Option A
  4. Quick Check:

    Include endpoint for full range in plotting [OK]
Hint: Use endpoint=True to include last point in plots [OK]
Common Mistakes:
  • Excluding endpoint causing incomplete plots
  • Using too few points for smooth curves
  • Confusing number of points with step size
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard