AppDividend
Latest Code Tutorials

NumPy Arange Tutorial With Example | Python NumPy Functions

0 54

Get real time updates directly on you device, subscribe now.

NumPy Arange Tutorial With Example | Python NumPy Functions is today’s topic. The numpy.arange function returns a ndarray object containing evenly spaced values within the given range.  It returns an evenly spaced values within a given interval. For integer arguments, the method is equivalent to a Python built-in range function but returns the ndarray rather than a list.

NumPy Arange Tutorial

The syntax of numpy.arange function is following.

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

The parameters are following.

start: number, optional. Start of an interval. The interval includes this value. The default start value is 0.

stop: number. End of the interval. The interval does not include stop value, except in some cases where a step is not an integer and floating point round-off affects the length of out.

step: number, optional.

dtype: The type of an output array. If the dtype is not given, infer the data type from the other input arguments.

Let’s see the Python NumPy arange function example in Jupyter Notebook.

Write the following code inside the first cell.

import numpy as np

Run that cell using Ctrl + Enter and then write the following code in the next cell.

npdata = np.arange(3)
npdata

See the output below.

 

NumPy Arange Tutorial

It has created numpy array from 0 to 2 elements with a length of 3.

Let’s see another example. Write the following Python code in the cell.

npdata = np.arange(40)
npdata.shape = (5, 8)
npdata

In the above code, we have defined an array with the items of 40, and then we have numpy array’s shape attribute to shape that array into 5 rows and 8 columns. See the output.

 

Python NumPy Functions

Start and stop parameters set in Python NumPy

Let’s define the start and stop parameters in numpy arange function see the output.

npdata = np.arange(1,21,3)
npdata

In the above code, we have passed the first parameter as a starting point then go to 21 and with step 3. See the output below.

 

Start and stop parameters set in Python NumPy

Finally, NumPy Arange Tutorial With Example | Python NumPy Functions is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.