How to use the arange function in Python?
In Python, the arange() function is used to generate a sequence of evenly spaced values, with parameters for the start, stop, and step size. You can use the arange() function in the following way:
- import the numpy module:
import numpy as np
- Generate an arithmetic sequence using the arange() function.
arr = np.arange(start, stop, step)
start represents the initial value (default is 0), stop represents the ending value (not included in the sequence), and step represents the increment (default is 1).
For example, generate a sequence of integers from 0 to 9.
import numpy as np
arr = np.arange(0, 10)
print(arr)
Output:
[0 1 2 3 4 5 6 7 8 9]
Generate a sequence of even numbers from 1 to 10.
import numpy as np
arr = np.arange(2, 11, 2)
print(arr)
Output:
[ 2 4 6 8 10]
It is important to note that since the arange() function does not include the stopping value, it is necessary to choose the start and stop values accordingly when generating a sequence.