Coding Ref

What does numpy.arrange() do?

What does numpy.arrange() do?

numpy.arange() is a function in the NumPy library that returns an array containing evenly spaced values within a given range.

It is similar to the range() function in Python, but it returns an array instead of a list, which allows it to be used with other NumPy functions.

Syntax

Here is the syntax for numpy.arange():

numpy.arange(start, stop, step, dtype)
  • start: The starting value for the sequence. This parameter is optional and the default value is 0.
  • stop: The end value for the sequence. This parameter is required and the returned array will include all values from start up to, but not including, stop.
  • step: The distance between consecutive values in the sequence. This parameter is optional and the default value is 1.
  • dtype: The data type of the returned array. This parameter is optional and the default value is int64.

Examplesss

Here are some examples:

main.py
import numpy as np

# Create an array with values from 0 to 9
a = np.arange(0, 10)
print(a)  # prints [0 1 2 3 4 5 6 7 8 9]

# Create an array with values from 0 to 9, with a step size of 2
b = np.arange(0, 10, 2)
print(b)  # prints [0 2 4 6 8]

# Create an array with values from 10 to 1, with a step size of -1
c = np.arange(10, 0, -1)
print(c)  # prints [10 9 8 7 6 5 4 3 2 1]

In the first example, we create an array with values from 0 to 9, with a default step size of 1.

In the second example, we create an array with values from 0 to 9, with a step size of 2.

In the third example, we create an array with values from 10 to 1, with a step size of -1.

You'll also like

Related tutorials curated for you

    How to use numpy.ndarray.flatten() in Python

    How to use numpy.zeros() in Python

    How to use numpy.ravel() in Python

    How to use numpy.round() in Python

    How to use numpy.clip() in Python

    How to use numpy.random.uniform() in Python

    NumPy numpy.transpose()

    How to use numpy.percentile() in Python

    How to use numpy.linspace() in Python

    What does numpy.arrange() do?

    How to use numpy.exp() in Python

    ModuleNotFoundError: No module named 'numpy' in Python