What is the linspace method in NumPy?

linspace is an in-built function in Python’s NumPy library. It is used to create an evenly spaced sequence in a specified interval.

The function signature of linspace is:

svg viewer

Function parameters

Take a look at the function parameters below.

Required parameters

  • start: The starting value of the sequence.
  • end: The end value of the sequence unless the endpoint is set to False.

Optional parameters

  • num: The number of samples needed to generate within the interval. The default value is 50.
  • endpoint: If the endpoint is set to false, then the end value is not included in the sequence.
  • retstep : If the retstep is true then (samples, step) is returned. **Step refers to the spacing between the values in the interval.
  • dtype: The type of output array. The datatype is inferred if it is ​not specified.
  • axis: The axis in the result to store the samples. (Added in version 1.16.0)

Code examples

Let’s see the code example of the np.linespace() with only the required parameters.

import numpy as np
# the default value of num is 50
print(np.linspace(1,50))

Let’s see the code example of the np.linespace() with the num and endpoint optional parameters.

import numpy as np
# Since endpoint is false, 8 will not be included in the result
print(np.linspace(start = 2, stop = 8, num = 4, endpoint = False, \
retstep = True, dtype = float))

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved