NumPy library provides various functions to create arrays with evenly spaced numbers within range.
In previous tutorials, we learned about Key Features of NumPy Arrays in Python. In this tutorial, we will learn to create NumPy arrays using numpy.arrange()
function.
numpy.arange()
to Create Array of Evenly Spaced Numbers within a Range
numpy.arange()
is used to create arrays of evenly spaced numbers within a range. It is similar to Python’s built-in range()
function, but numpy.arange()
returns a NumPy array, which allows for efficient mathematical operations.
Syntax => numpy.arange([start,] stop[, step,], dtype=None, *, like=None)
start
(optional) – This is the starting value of the range (inclusive). Default value is 0. If the start value is not specified then it is taken as 0.stop
(required) – This is the stopping value of the range (exclusive). The end value of the sequence is required.step
(optional) – This is the step size between values. Default value is 1.step
is added to the previous value to get the next value. Step can be of float, positive and negative value.dtype
(optional) – This is the data type of the array elements. If not specified it will get from input values.- Note –
arange()
is only supported for booleans when the result has at most length 2. - If start is greater than stop, then it will return an empty array.
numpy.arange()
Example Program – Create NumPy Array using import numpy as np
# Create an array from 0 to 11
arange_array_1 = np.arange(12) # start = 0 (default), stop = 12 (exclusive), step = 1 (default)
print("An array created from range 0 to 11", arange_array_1)
# array created using start value 4 and stop value 13
arange_array_2 = np.arange(4, 14) # start = 4, stop = 14 (exclusive), step = 1 (default)
print ("An array created from range 4 to 13", arange_array_2)
# Array created using specified step size
arange_array_3 = np.arange(0, 30, 3) # start = 0, stop = 30 (exclusive), step = 3
print("An array created from range 0 to 27", arange_array_3)
# Output
# An array created from range 0 to 11 [ 0 1 2 3 4 5 6 7 8 9 10 11]
# An array created from range 4 to 13 [ 4 5 6 7 8 9 10 11 12 13]
# An array created from range 0 to 27 [ 0 3 6 9 12 15 18 21 24 27]
np.arange(12)
=> Here, start = 0 (default), stop = 12 (exclusive), step = 1 (default). This creates an array[ 0 1 2 3 4 5 6 7 8 9 10 11]
.np.arange(4, 14)
=> Here, start = 4, stop = 14 (exclusive), step = 1 (default). This creates an array[ 4 5 6 7 8 9 10 11 12 13]
.np.arange(0, 30, 3)
=> Here, start = 0, stop = 30 (exclusive), step = 3. This creates an array[ 0 3 6 9 12 15 18 21 24 27]
.
numpy.arange()
with Float Data-type
import numpy as np
# Array create using float datatype
arange_array_4 = np.arange(0, 10, 1, dtype=float) # start = 0, stop = 10 (exclusive), step = 1, dtype = float
print("An Array created with float value from 0 to 9", arange_array_4)
# Output
# An Array created with float value from 0 to 9 [0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
In this program, we are using np.arange(0, 10, 1, dtype=float)
to create an array. With dtype=float
, all elements of this array will be float numbers. Output => [0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
numpy.arange()
with Boolean Data-type
import numpy as np
# Array create using boolean datatype
arange_array_5 = np.arange(0, 3, 2, dtype=bool) # start = 0, stop = 3 (exclusive), step = 2, dtype = bool
print("An Array created with bool value from 0 to 2", arange_array_5)
# Array create using boolean datatype
arange_array_7 = np.arange(0, 5, 2, dtype=bool) # start = 0, stop = 5 (exclusive), step = 2, dtype = bool
print("An Array created with bool value from 0 to 5", arange_array_7)
# Output
# An Array created with bool value from 0 to 2 [False True]
# TypeError: arange() is only supported for booleans when the result has at most length 2.
- In this program, we are using
np.arange(0, 3, 2, dtype=bool)
to create an array. Withdtype=bool
, elements of this array will be of boolean values. In this range is only for 2 elements. Output array will be =>[False True]
np.arange(0, 5, 2, dtype=bool)
=> In this range we have more than 2 elements, but boolean data-type only works with 2 elements array. It will raiseTypeError
: arange() is only supported for booleans when the result has at most length 2.
numpy.arange()
with Complex Data-type
import numpy as np
# Array create using complex datatype
arange_array_8 = np.arange(0, 10, 2, dtype=complex) # start = 0, stop = 10 (exclusive), step = 2, dtype = complex
print("An Array created with complex value from 0 to 9", arange_array_8)
# Output
# An Array created with complex value from 0 to 9 [0.+0.j 2.+0.j 4.+0.j 6.+0.j 8.+0.j]
In this program, we are using np.arange(0, 10, 2, dtype=complex)
to create an array. With dtype=complex
, all elements of this array will be complex numbers. Output => [0.+0.j 2.+0.j 4.+0.j 6.+0.j 8.+0.j]
numpy.arange()
with Float Step Size
import numpy as np
# Array create using float step Size
arange_array_9 = np.arange(0, 6, 0.5) # start = 0, stop = 6 (exclusive), step = 0.5
print("An Array created with float value from 0 to 5", arange_array_9)
# Output
# An Array created with float value from 0 to 5 [0. 0.5 1. 1.5 2. 2.5 3. 3.5 4. 4.5 5. 5.5]
In this program, we are using np.arange(0, 6, 0.5)
to create an array. With step=0.5
(float value), each next element will increment by 0.5. Output => [0. 0.5 1. 1.5 2. 2.5 3. 3.5 4. 4.5 5. 5.5]
numpy.arange()
with Negative Step Size
import numpy as np
# Array create using negative step size
arange_array_10 = np.arange(6, 0, -0.5) # start = 6, stop = 0 (exclusive), step = -0.5
print("An Array created with float value from 6 to 0", arange_array_10)
# Output
# An Array created with float value from 6 to 0 [6. 5.5 5. 4.5 4. 3.5 3. 2.5 2. 1.5 1. 0.5]
In this program, we are using np.arange(6, 0, -0.5)
to create an array. With step= -0.5
(negative), each next element will decrease by 0.5. Output => [6. 5.5 5. 4.5 4. 3.5 3. 2.5 2. 1.5 1. 0.5]
. This is helpful to create arrays in reverse order.
numpy.arange()
with Negative Start, Stop and Step
import numpy as np
# Array create using np.arange() with Negative Start, Stop and Step
arange_array_11 = np.arange(-2, -7, -0.5) # start = -2, stop = -7 (exclusive), step = -0.5
print("An Array created with float value from -2 to -7", arange_array_11)
# Output
# An Array created with float value from -2 to -7 [-2. -2.5 -3. -3.5 -4. -4.5 -5. -5.5 -6. -6.5]
In this program, we are using np.arange(-2, -7, -0.5)
to create an array. With negative start, stop and step values, Output => [-2. -2.5 -3. -3.5 -4. -4.5 -5. -5.5 -6. -6.5]
Conclusion
In conclusion, the numpy.arange()
function is a powerful tool for generating NumPy arrays with a wide range of customizable parameters. This tutorial has shown how we can use this function for the creation of a rather simple array of integer values to more complicated handling related to float, boolean, and even complex data types. We have also seen how to specify a float step size for finer control over the spacing of arrays, and how to use negative step sizes to create arrays that count in reverse order. We have also shown how to define arrays with negative start, stop, and step values, which offers all possible ways of creating arrays that you might need.
Code snippets and programs related to Create NumPy Array using numpy.arange()
, can be accessed from GitHub Repository. This GitHub repository all contains programs related to other topics in NumPy tutorial.
Related Topics
- Introduction to NumPy: Core Features and Importance in different FieldsIntroduction to NumPy NumPy stands for Numerical Python. It is an open-source Python library, which is predominantly mostly used for numerical computations in Python. It provides mathematical functions applicable to work with multi-dimensional arrays and matrices. It is a powerful tool to handle large data structures and allows performing complex arithmetic calculations. It has built-in computational features…
- How to Install NumPy on Windows, Linux and Mac: step-by-step guideWe can install NumPy on all major operating systems such as Windows, Linux and macOS. NumPy installation process is very simple and can be done using pip, which is the Python package installer. Here are the steps to install NumPy on Windows, Linux and Mac operating systems. Install NumPy on Windows, Linux and Mac Prerequisites…
- NumPy Array in Python: First Step into Numerical PythonNumPy array in Python are basic entities for numerical computations. They originate from the NumPy library, an essential package in Python for scientific computing. A NumPy array also termed as ndarray, is highly efficient and an object of multi-dimensional array (N-dimensional array), which provides the base functions of numerical operations. Being NumPy Arrays an essential…
- numpy.array(): Create NumPy Arrays using Lists, Tuples and Arrays (with Example Programs)NumPy (Numerical Python) is one of the most powerful libraries for numerical computations, data manipulation, and analysis in Python. At the core of NumPy is the ndarray, a multi-dimensional or N-dimensional array that allows to store and manipulate large datasets efficiently. NumPy provides several ways to create arrays, from simple ones to more complex multi-dimensional…
- Create Arrays with Predefined Values using np.zeros(), np.ones(), np.full() and np.empty()Create Arrays with Predefined Values NumPy library provides various functions to create arrays with predefined values. While creating an array, these NumPy functions helps to initialize the arrays with initial values. In last tutorial, we learned about Key Features of NumPy Arrays in Python. In this article, we will learn about 4 functions to create…