
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
Numpy arange() Function
The Numpy arange() function is used to create a numpy array with evenly spaced elements as per the interval. The function takes the start, stop, and step parameters to define the sequence. It returns an array of elements that are generated within the specified interval.
The primary use of arange() function is to create sequences of numbers for iteration, data plotting, or as input for matrix operations. It's optimized for performance and integrates seamlessly with other NumPy operations. It is similar to the built-in range() function but it returns a numpy array instead of a list.
Syntax
Following is the syntax of the Numpy arange() function −
numpy.arange([start, ]stop, [step, ]dtype=None, like=None)
Parameters
Following are the parameters of the Numpy arange() function −
- start(optional): It is the start of interval range. By default start = 0
- stop: It is the end of interval range
- step(optional): It is the spacing(difference) between each two consecutive values in the array. By default step = 1.
- dtype(optional): This is the type of output array
- like (optional): It allows the creation of an array which is like the input object but uses an existing array-like object (like another NumPy array).
Return Values
This function returns a numpy array of evenly spaced values.
Example
Following is a basic example to create a evenly spaced numpy array using Numpy arange() function −
import numpy as np my_Array = np.arange(10) print("Numpy Array",my_Array) #type of array print(type(my_Array))
Output
Following is the output of the above code −
Numpy Array [0 1 2 3 4 5 6 7 8 9] <class 'numpy.ndarray'>
Example : Specified Start, Stop, Step
Using numpy.arange() function, we can generate a numpy array from start value to the end value with a spacing between two consecutive numbers by specifing step value.
In the following example, we have generated a NumPy array of multiples of 2 less than 10 using the numpy.arange() function, with the start value set to 2, the stop value set to 10, and the step value set to 2 −
import numpy as np my_Array = np.arange(2,10,2) print("Numpy Array -",my_Array)
Output
Following is the output of the above code −
Numpy Array - [2 4 6 8]
Example : Using 'step=0' in 'arange()'
The step parameter provides spacing between the two values. By default step values is 1. When we assign step value to zero it will raise ZeroDivisionError.
In the following example, we have assigned step parameter to zero in numpy.arange() function −
import numpy as np my_Array=np.arange(10,20,0) print(my_Array)
Output
Following is the output of the above code −
Traceback (most recent call last): File "/home/cg/root/32073/main.py", line 2, in <module> my_Array=np.arange(10,20,0) ZeroDivisionError: division by zero
Example : N-dimensional Array Using 'arange()'
The numpy.arange() function, when combined with the reshape() method, allows for the creation of multi-dimensional arrays.
Here, we have generated 2-D numpy array of shape (3,4) using numpy.arange(). Where the start = 1, stop = 10 and step = 2 −
import numpy as np my_Array = np.arange(0,8,2).reshape(2,2) print("2D Numpy Array \n",my_Array)
Output
Following is the output of the above code −
2D Numpy Array [[0 2] [4 6]]
Example : Creating a Float Data-type
We can create a NumPy array of floating-point numbers using numpy.arange() by specifying the dtype as float.
In the following example, we have generated a float point numpy array using numpy.arange() −
import numpy as np # create an array with elements from 0 to 1 with stepsize 0.2 array1 = np.arange(0, 10,3,dtype='float') print("Float Numpy Array-",array1)
Output
Following is the output of the above code −
Float Numpy Array- [0. 3. 6. 9.]