How to Generate Evenly Spaced Elements in Python
Posted on: February 26, 2021 by Ariessa Norramli
In this article, you will learn how to generate evenly spaced elements in Python. You will be creating 2 arrays of numbers in the range of 2 to 10 with different intervals.
Generate Evenly Spaced Elements in Python
In order to generate evenly spaced elements, you can use the numpy.arange()
method.
# Import numpy module
import numpy as np
# Generate numbers in the range of 2 to 10, with an interval of 1
print(np.arange(2, 10), "\n")
# [2 3 4 5 6 7 8 9]
# Generate numbers in the range of 2 to 10, with an interval of 2
print(np.arange(2, 10, 2), "\n")
# [2 4 6 8]
Note: The numpy.arange()
method functions by creating an array of elements with evenly spaced intervals. By default, this method will create an array of elements with an interval of 1.
Share on social media
//