np.linspace(start, stop, num) returns a requested number of evenly spaced samples. By default, it includes both start and stop; set endpoint=False to omit stop while keeping the same sample count. Use linspace when the number of values matters, and np.arange when a fixed step is the natural requirement.
What values does np.linspace return?
In NumPy, num is the number of samples to produce. It defaults to 50 and must be nonnegative. For example:
As an Amazon Associate I earn from qualifying purchases.
import numpy as np
np.linspace(2.0, 3.0, num=5)
# array([2. , 2.25, 2.5 , 2.75, 3. ])
There are five values, including both bounds, with a spacing of 0.25. The sample count—not a step size—is the input that controls how many values are returned. NumPy documents linspace as returning evenly spaced numbers over a specified interval: NumPy linspace reference.
What is the linspace formula?
For scalar bounds and num > 1, let i be a sample index from 0 through num - 1.
#1 Best Overall
- With the default
endpoint=True, the value at indexiisstart + i * (stop - start) / (num - 1). Dividing the interval intonum - 1gaps makes the last sample equal tostop. - With
endpoint=False, the value at indexiisstart + i * (stop - start) / num. The interval is divided intonumgaps, and the returned samples omitstop.
For zero or one requested sample, do not apply those denominator formulas: the count and endpoint setting determine the result directly, and the formula above is specifically for more than one sample.
Does np.linspace include the endpoint?
Yes, by default: endpoint=True. With five samples from 2 to 3, the values are [2.0, 2.2, 2.4, 2.6, 2.8] when endpoint=False. The final value is below 3 because NumPy preserves the five-sample count while excluding the upper bound. This half-open behavior is useful, for example, when building a periodic grid where including both ends would duplicate a boundary point.
Set the option explicitly when endpoint placement matters:
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →np.linspace(2.0, 3.0, num=5, endpoint=False)
# array([2. , 2.2, 2.4, 2.6, 2.8])
When should you use linspace instead of arange?
The key difference is what you specify: linspace takes a sample count, while arange takes a step size. NumPy describes arange as similar to linspace but using a step size instead of a number of samples: NumPy arange reference.
| Decision | np.linspace |
np.arange |
|---|---|---|
| Main input | Number of samples (num) |
Increment (step) |
| Usual interval behavior | Includes stop by default; excludes it with endpoint=False |
Normally half-open: includes start and excludes stop |
| Best fit | A fixed number of points or deliberate endpoint placement | A sequence defined by a fixed increment, especially an integer step |
| Floating-point concern | Count is explicit, though individual values can still be floating-point approximations | Output length may be numerically unstable, and the last value can exceed stop |
For a grid that must contain exactly 101 values from 0 to 1, use np.linspace(0, 1, 101). For integer values at increments of 2 below 10, use np.arange(0, 10, 2). These examples express the difference in intent: choose a count for the first requirement and a step for the second.
Floating-point steps such as 0.1 are a case where the choice matters. The arange reference warns that output length can be unstable and the last value can exceed stop because of floating-point rounding and internal casting behavior. NumPy’s array-creation guide also recommends linspace for a fixed-size grid: NumPy array creation guide. Using linspace makes the count predictable, but it does not make floating-point representations exact.
Rank #4
What do retstep, axis, and dtype change?
Get the spacing with retstep=True
Pass retstep=True when downstream code also needs NumPy’s spacing. The return is a pair: the sample array and the step value.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
samples, step = np.linspace(2.0, 3.0, num=5, retstep=True)
# samples: array([2. , 2.25, 2.5 , 2.75, 3. ])
# step: 0.25
Place the sample dimension with axis
If start or stop is array-like, axis selects where the sample dimension is inserted. Its default is axis 0. This matters when the samples are part of a multidimensional result and its layout must match surrounding arrays.
Best Value
Request integer output deliberately
By default, linspace does not infer an integer dtype, even when the bounds or some results are whole numbers. If you explicitly request an integer dtype, current NumPy documentation says values round toward negative infinity. That behavior changed in NumPy 1.20.0; it can differ from truncation toward zero for negative, non-integral values. If truncation-like conversion is intended, generate the default result and then call .astype(int) instead. See the NumPy linspace reference for the documented dtype behavior and version note.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




