Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.linspace

Generates a chosen number of evenly spaced NumPy samples between two boundaries. The upper boundary is included by default and can be excluded.

How it works

linspace constructs an array containing a requested number of evenly spaced samples between start and stop. By default the interval is closed, but endpoint=False excludes stop; retstep=True changes the result to a pair containing the samples and spacing. Array-valued boundaries are supported, with axis controlling the placement of the generated dimension.

Syntax

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0, *, device=None)

Parameters

NameTypeDescription
startarray_likeThe starting value of the sequence.
stoparray_likeThe end value of the sequence, unless endpoint is set to False.
numint, optionalNumber of samples to generate. Defaults to 50 and must be non-negative.
endpointbool, optionalIf True, stop is the last sample; otherwise it is not included.
retstepbool, optionalIf True, return samples together with the spacing.
dtypedtype, optionalThe type of the output array; otherwise inferred from start and stop.
axisint, optionalThe axis in the result along which the samples are stored.
devicestr, optionalThe device on which to place the array.

Returns

An ndarray of evenly spaced samples; when retstep=True, a pair containing the samples and the spacing.

Key points

  • Use linspace when a calculation needs regularly spaced points across a numeric interval.
  • The sample count is controlled directly by num, rather than by a requested step size.
  • Set endpoint=False when the upper boundary must not appear in the generated values.
  • If num is omitted, linspace creates 50 samples.
  • retstep=True exposes the calculated spacing alongside the generated array.
  • Use dtype to request a particular output array type.

Common mistakes

  • num cannot be negative; passing a negative sample count raises an error.
  • With endpoint=False, stop is excluded and the spacing differs from the closed-interval case.
  • The output type is inferred unless dtype is explicitly supplied; it is not inferred as an integer merely because the boundaries are integers.

Example: Generate evenly spaced samples

import numpy as np

samples, step = np.linspace(2.0, 3.0, num=5, retstep=True)
print(samples)
print(step)

Creates five evenly spaced values including both endpoints and also returns their spacing.

When to use it

Plotting grids

Create x-coordinates for plotting a curve over a fixed domain.

Numerical sampling

Evaluate a model at uniformly spaced parameter values.

Simulation domains

Build regularly spaced inputs for simulations or numerical integration.

Interview questions

QWhat does numpy.linspace generate, and what does endpoint control?

It returns num evenly spaced samples over the interval from start to stop. With the default endpoint=True, both boundaries are included; endpoint=False makes the interval half-open by excluding stop.

Follow-up: How does retstep=True affect the result? It returns a two-item result containing the sample array and the spacing between samples.

QWhat are the roles and constraints of start, stop, and num?

num specifies how many samples to create and defaults to 50. It must be non-negative, while start and stop define the interval boundaries.

Follow-up: What happens when num is zero? The function produces an empty sample array; the requested count still satisfies the non-negative requirement.

QHow do axis and dtype affect multidimensional or typed output?

For array-like start and stop, linspace generates samples along a new axis. The axis argument selects where that generated dimension is inserted, and dtype can specify the output type.

Follow-up: Why might axis matter in multidimensional calculations? It determines how the generated sequences align with existing array dimensions for broadcasting and downstream operations.

  • numpy.arange
  • numpy.geomspace
  • numpy.logspace

Source: numpy.linspace — NumPy v2.5 Manual. Checked 2026-10-15.