Your intuition seems to be that if endpoint=False is specified, then the last element of the returned array should be 1 less than the stop value.

Suppose we implement things that way. What does the following linspace call return?

numpy.linspace(0, 0.5, 5, endpoint=False)

Is it going to end at -0.5, counting down? That wouldn't make much sense.


numpy.linspace always divides the interval from start to stop into equally-sized chunks, and it always returns an array of length num. The difference between endpoint=True and endpoint=False is that with endpoint=False, it makes one extra chunk to compensate for leaving out the right endpoint. The step from 16 to 18 is the same size as the step from 18 to the omitted 20.

Answer from user2357112 on Stack Overflow
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.linspace.html
numpy.linspace — NumPy v2.5 Manual
Size of spacing between samples. ... Similar to linspace, but uses a step size (instead of the number of samples).
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-linspace
NumPy linspace() Method | Create Evenly Spaced Array - GeeksforGeeks
February 14, 2026 - The calculated step size is 2.5, which is the difference between adjacent elements. numpy.linspace() can also be used to generate values for multi-dimensional arrays.
🌐
NumPy
numpy.org › devdocs › › reference › generated › numpy.linspace.html
numpy.linspace — NumPy v2.6.dev0 Manual
Size of spacing between samples. ... Similar to linspace, but uses a step size (instead of the number of samples).
🌐
Codecademy
codecademy.com › docs › python:numpy › built-in functions › .linspace()
Python:NumPy | Built-in Functions | .linspace() | Codecademy
May 26, 2025 - Returns a numpy ndarray of evenly spaced samples. If retstep is True, it also returns the step size between samples. This example shows how to create a simple array of evenly spaced numbers using the .linspace() function:
🌐
DataCamp
datacamp.com › doc › numpy › linspace
NumPy linspace()
import numpy as np array = np.linspace(0, 10, num=5, endpoint=False) Powered by · Here, the endpoint=False parameter is used, resulting in 5 values from 0 to just below 10: [0. , 2. , 4. , 6. , 8.]. 3. Returning Step Size · import numpy as np array, step = np.linspace(0, 10, num=5, retstep=True) Powered by ·
🌐
Codegive
codegive.com › blog › numpy_linspace_step_size.php
Numpy linspace step size
The "step size" in linspace refers to the constant difference between any two consecutive elements in the generated array. Unlike np.arange where you specify the step, np.linspace calculates the step size based on your start, stop, and num (and endpoint) values.
🌐
DataCamp
datacamp.com › home › tutorials › python › how to use the numpy linspace() function
How to Use the NumPy linspace() Function | DataCamp
April 5, 2024 - If you’re in a hurry, here’s the quickest explanation of the linspace() function. NumPy's linspace() function generates an array of evenly spaced numbers over a defined interval. Here, for example, we create an array that starts at 0 and ends at 100, throughout an interval of 5 numbers.
Top answer
1 of 2
3

Your intuition seems to be that if endpoint=False is specified, then the last element of the returned array should be 1 less than the stop value.

Suppose we implement things that way. What does the following linspace call return?

numpy.linspace(0, 0.5, 5, endpoint=False)

Is it going to end at -0.5, counting down? That wouldn't make much sense.


numpy.linspace always divides the interval from start to stop into equally-sized chunks, and it always returns an array of length num. The difference between endpoint=True and endpoint=False is that with endpoint=False, it makes one extra chunk to compensate for leaving out the right endpoint. The step from 16 to 18 is the same size as the step from 18 to the omitted 20.

2 of 2
0

It would be helpful to look at the source here. This will allow you to walk through what the function is doing, which will give you a better understanding of the output of linspace.


Two parameters are used to calculate the step size: div and delta. However, the difference between endpoint=True, and endpoint=False, is that div is equal to num-1 if endpoint=True, and num if endpoint=False.

div = (num - 1) if endpoint else num

Here are the other relevent pieces of the source (very slimmed down):

delta = stop - start

y = _nx.arange(0, num, dtype=dt)

# ...

if num > 1:
    step = delta / div

# ...

y = y * step

# ...

y += start

If we walk through each of these the outputs make a lot more sense.

endpoint=True:

div = num - 1                     # 4
y = _nx.arange(0, num, dtype=dt)  # [0, 1, 2, 3, 4]
delta = 20 - 10                   # 10
step = delta / div                # 2.5
y = y * step                      # [0.0, 2.5, 5.0, 7.5, 10.0]
y += start                        # [10.0, 12.5, 15.0, 17.5, 20.0]

endpoint=False:

div = num                         # 5
y = _nx.arange(0, num, dtype=dt)  # [0, 1, 2, 3, 4]
delta = 20 - 10                   # 10
step = delta / div                # 2
y = y * step                      # [0.0, 2.0, 4.0, 6.0, 8.0]
y += start                        # [10.0, 12.0, 14.0, 16., 18.0]
Find elsewhere
🌐
Zubairkhalid
zubairkhalid.com › blog › data-analysis › numpy-linspace-syntax-examples
NumPy linspace: Syntax and Examples | Dr. Zubair Khalid
October 3, 2026 - When the natural input is a step size, such as sampling every 0.5 units, numpy.arange expresses that intent more directly [2]. And because linspace computes the step from num, changing num changes every value in the array, which can silently ...
🌐
GeeksforGeeks
geeksforgeeks.org › numpy-linspace
NumPy linspace() Method | Create Evenly Spaced Array - GeeksforGeeks
January 24, 2025 - import numpy as np # Generate 10 numbers between 0 and 1 array_without_endpoint = np.linspace(0, 1, num=10, endpoint=False) print(array_without_endpoint) ... import numpy as np # Generate 5 numbers between 0 and 10 and return the step size array, step = np.linspace(0, 10, num=5, retstep=True) print("Step Size:", step)
🌐
Programiz
programiz.com › python-programming › numpy › methods › linspace
NumPy linspace() | Programiz
August 30, 2023 - Array1: [2. 2.25 2.5 2.75 3. ] Array2: [2. 2.2 2.4 2.6 2.8] Array3: [2. 2.25 2.5 2.75 3. ] Step Size: 0.25 · import numpy as np · # create an array of 5 elements between [1, 2] and [3, 4] array1 = np.linspace([1, 2], [3, 4], num=5) ...
🌐
AskPython
askpython.com › python-modules › numpy › numpy-linspace-python
NumPy linspace(): Create Arrays Fast - AskPython
January 24, 2026 - import numpy as np # Using step size for calculations values, spacing = np.linspace(1, 100, 20, retstep=True) next_value = values[-1] + spacing print(f"Next value in sequence: {next_value}") # Output: Next value in sequence: 105.21052631578948
🌐
datagy
datagy.io › home › numpy › numpy linspace: creating evenly spaced arrays with np.linspace
NumPy linspace: Creating Evenly Spaced Arrays with np.linspace • datagy
December 30, 2022 - The NumPy linspace function is useful for creating ranges of evenly-spaced numbers, without needing to define a step size.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.linspace.html
numpy.linspace — NumPy v2.1 Manual
Size of spacing between samples. ... Similar to linspace, but uses a step size (instead of the number of samples).
🌐
NumPy
numpy.org › doc › stable › user › how-to-partition.html
How to create arrays with regularly-spaced values — NumPy v2.5 Manual
numpy.linspace can include the endpoint and determines step size from the num argument, which specifies the number of elements in the returned array.
🌐
Real Python
realpython.com › np-linspace-numpy
np.linspace(): Create Evenly or Non-Evenly Spaced Arrays – Real Python
July 3, 2026 - The step size as a float, if retstep is set to True · You can use this section as a reference when you start experimenting with np.linspace() and the different ways you can customize its output.
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.linspace.html
numpy.linspace — NumPy v2.0 Manual
Size of spacing between samples. ... Similar to linspace, but uses a step size (instead of the number of samples).
🌐
Python Guides
pythonguides.com › python-numpy-linspace
NumPy linspace: Formula, Endpoint, vs arange
May 7, 2025 - The step is (stop - start) / (num - 1) by default. With endpoint=False it becomes (stop - start) / num. linspace takes the number of values and includes the stop. arange takes a step size and excludes the stop.