numpy.arange() in Python: Introduction, Syntax & Examples

The numpy.arange() function in Python’s NumPy library is used to generate arrays of evenly spaced values within a specified range. It’s similar to Python’s built-in range() function but produces a NumPy array as output.

In this article, we will understand Python numpy.arange() function, its syntax, and learn how to use it with the help of five unique examples. Let’s get started.

Syntax of numpy.arange() Function

Below is the syntax for using the numpy.arange() function in Python.

Syntax:

numpy.arange(start, stop, step, dtype=None)

Parameters:

  • start (optional): The start of the range (inclusive). If not provided, it defaults to 0.
  • stop: The end of the range (exclusive). This value is required.
  • step (optional): The step size between values in the range. If not provided, it defaults to 1.
  • dtype (optional): The data type of the output array. It’s typically inferred based on the input arguments, but you can specify a different data type if needed.

Return:

This returns an array of evenly spaced values.

Examples of numpy.arange() Function

Let us now look at some examples to demonstrate the use of numpy.arange() function in Python. In each example, we have passed different parameter combinations to understand how this function works in different scenarios.

Example 1: By specifying only stop

Here we will only provide a stop in the numpy.arange() function which is the end of the range and is exclusive.

import numpy as np
np.arange(10)

In the above code, we first imported the NumPy library as np then provided parameter stop as 10 in the np.arange() function means we only provided the end of the range or the endpoint which is exclusive.

Output:

Example: By specifying the stop in numpy.arange() function

In the above output, we got an array that starts from 0 and ends with 9.

Example 2: By specifying start & stop

Here we will provide start and stop in the numpy.arange() function which is the start and end of the range respectively.

np.arange(1,10)

In the above code, we provided parameters start as 1 which is inclusive and stop as 10 which is exclusive in the np.arange() function.

Output:

Example: By specifying the start and stop in numpy.arange() function

In the above output, we got an array that starts from 1 and ends with 9 and the step size value as 1 which is the default value for generating the array.

Example 3: By specifying start, stop, & step

Here in the numpy.arange() function we will provide a start and stop with the step which is the step size between values in the range and by default its value is 1.

np.arange(1,10,2)

In the above code, we provided parameters start as 1, stop as 10, and step as 2 means the step size between the values is 2.

Output:

Example: By specifying the start, stop, and step in numpy.arange() function

In the above output, we got an array with the values from 1 to 10 and the difference between the values is 2 as the step size was 2.

Example 4: By specifying the stop & dtype

Here in the numpy.arange() function we will provide stop and dtype which means the data type of the output array.

np.arange(20,dtype="complex")

In the above code, we have provided parameter stop as 20 and dtype as complex means we have set the data type of values in the output array as complex.

Output:

Example: By specifying the stop and dtype in numpy.arange() function

In the above output, we got an array with values from 0 to 20 and the datatype of those values is complex.

Example 5: By specifying the start, stop, step, & dtype

Here we will provide all the parameters start, stop, step, and dtype together in the numpy.arange() function.

np.arange(1,10,2,dtype="float")

In the above code, we have provided the parameters start as 2, stop as 10, step as 2, and dtype as the float.

Output:

Example: By specifying the start, stop, step, and dtype in numpy.arange() function

In the above output, we got an array with values from 1 to 9 and the difference between the values is 2. Also, the datatype of those values is float.

Summary

The numpy.arange() is a fundamental function for generating sequences of numbers, which makes it invaluable in a broad range of scientific and numerical tasks. In this tutorial, we have discussed numpy.arange() function provided by Python’s NumPy library and also explored five examples to generate an array of evenly spaced values. After reading this tutorial, we hope you can easily generate an array of evenly-spaced values in Python.

Reference

https://stackoverflow.com/questions/54111744/numpy-arange-function-returns-inconsistant-array

Priyanshu Singh
Priyanshu Singh
Articles: 44