This content originally appeared on CodeSource.io and was authored 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.
The post How to Generate Evenly Spaced Elements in Python appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
Ariessa Norramli | Sciencx (2021-02-26T10:54:58+00:00) How to Generate Evenly Spaced Elements in Python. Retrieved from https://www.scien.cx/2021/02/26/how-to-generate-evenly-spaced-elements-in-python/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.