Numpy Arange Function

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will learn about the numpy.arange() function of the NumPy library which is used for array creation.

The NumPy arange() function is one of the array creation routines that is usually based on numerical ranges. This method basically creates an instance of ndarray with evenly spaced values and returns the reference to it.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-86393{{"`Numpy Arange Function`"}} python/with_statement -.-> lab-86393{{"`Numpy Arange Function`"}} python/tuples -.-> lab-86393{{"`Numpy Arange Function`"}} python/importing_modules -.-> lab-86393{{"`Numpy Arange Function`"}} python/numerical_computing -.-> lab-86393{{"`Numpy Arange Function`"}} python/build_in_functions -.-> lab-86393{{"`Numpy Arange Function`"}} end

Understanding the Syntax of numpy.arange()

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

The above syntax is the required syntax to use the numpy.arange() function. The first three parameters are used to determine the range of the values, while the fourth parameter is used to specify the type of the elements.

Exploring the Parameters of numpy.arange()

Parameter 1: start

This is an optional parameter used for indicating the start of the interval. The default value of this parameter is 0. This value is included in the interval.

Parameter 2: stop

This parameter is a number (integer or decimal) that is used to represent the value at which the interval ends excluding this value.

Parameter 3: step

This is an optional parameter indicating the step size of the interval and it is a number by which the interval values change.

Parameter 4: dtype

This option is used to indicate the data type of the NumPy array items. The default value of this parameter is None.

Creating a basic numpy.arange() array

In this step, we will create an array using all the range arguments, then print the array using the print() function.

import numpy as np

## Create a basic NumPy array using all the range arguments
a = np.arange(start=2, stop=12, step=2)

## Print the output
print("The Output is :", a)

The output should be [2, 4, 6, 8, 10].

Creating a numpy.arange() array using only two range arguments

In this step, we will create an array using only two range arguments, then print the array using the print() function.

import numpy as np

## Create a NumPy array using only two range arguments
a = np.arange(start=2, stop=12)

## Print the output
print("The Output is :", a)

Since only two arguments are provided, the step parameter takes the default value of 1. The output should be [2, 3, 4, 5, 6, 7, 8, 9, 10, 11].

Creating a numpy.arange() array with a single range argument

In this step, we will create an array using a single range argument, then print the array using the print() function.

import numpy as np

## Create a NumPy array using a single range argument
a = np.arange(12)

## Print the output
print("The Output is :", a)

Since only one argument is provided, it is taken as stop, and the default values of start and step are taken as 0 and 1 respectively. The output should be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].

Creating a numpy.arange() array with negative arguments

In this step, we will create an array with negative arguments and have a positive value for the step argument.

import numpy as np

## Create a NumPy array with negative start and stop values
a = np.arange(-10, -1)

## Print the output
print("The output is:", a)

The output should be [-10, -9, -8, -7, -6, -5, -4, -3, -2].

Summary

In this lab, we covered the numpy.arange() function which is the primary array creation routine in the NumPy library. We learned about its syntax, parameters, and the value returned by this function. We also created basic arrays using different range arguments and learned how to print their output.

Other Python Tutorials you may like