Open In App

numpy.arange() in Python

Last Updated : 24 Jan, 2025
Comments
Improve
Suggest changes
28 Likes
Like
Report

numpy.arange() function creates an array of evenly spaced values within a given interval. It is similar to Python's built-in range() function but returns a NumPy array instead of a list.

Let's understand with a simple example:


Output
[5 6 7 8 9]

Syntax of numpy.arange():

numpy.arange([start, ]stop, [step, ]dtype=None, *, like=None)

Parameters of numpy():

  • start (optional): The starting value of the sequence. Default is 0.
  • stop (required): The endpoint of the sequence, exclusive.
  • step (optional): The spacing between consecutive values. Default is 1.
  • dtype (optional): The desired data type of the output array.

Return Type:

  • Array of evenly spaced values.

Specify Start and Stop

Generate a sequence of integers starting from 5 to 14.


Output
Basic Sequence: [0 1 2 3 4 5 6 7 8 9]

Floating-Point Step Size

Generate a sequence of floating-point numbers.


Output
Floating-Point Sequence: [0.  0.2 0.4 0.6 0.8]

Combining with Conditional Filtering

Generate a sequence and filter specific values.


Output
Filtered Sequence: [12 15 18]

Explore