← Back to NumPy Course | Chapter 2: Creating Arrays | Lesson 4 of 7

np.linspace()

np.linspace() gives you an exact number of evenly spaced points between two values, including the end.

In this page:

  1. np.linspace()
Syntax
python
np.linspace(start, stop, num)

np.linspace()

np.linspace(start, stop, num) returns num evenly spaced samples and includes stop by default. It is the go-to tool for plotting axes and sampling functions. Set endpoint=False to exclude the last point.

Note: Use retstep=True to also get the spacing between points.

Example: np.linspace()

python
import numpy as np

print(np.linspace(0, 1, 5))
print(np.linspace(0, 10, 4))
print(np.linspace(0, 1, 4, endpoint=False))

# Output:
# [0.   0.25 0.5  0.75 1.  ]
# [ 0.          3.33333333  6.66666667 10.        ]
# [0.   0.25 0.5  0.75]
Related Topics
Common Mistakes
  1. Confusing the third argument with a step size
  2. Forgetting the endpoint is included
  3. Using arange with floats when linspace is safer
Chapter Summary
  • linspace takes a point count not a step
  • Includes the endpoint by default
  • Great for plotting ranges
  • endpoint=False excludes the stop
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.