Table of contents
1.
Introduction
2.
What is NumPy arange()?
3.
Why Use np.arange?
3.1.
Syntax
3.2.
Python
3.3.
Parameters of np.arange
4.
Examples
4.1.
Example 1: Basic Increment
4.2.
Python
4.3.
Example 2: Specified Start, Stop, and Step
4.4.
Python
4.5.
Example 3: Floating-Point Range
4.6.
Python
5.
Pros and Cons of Using np.arange
5.1.
Pros
5.2.
Cons
6.
Do's and Don'ts of Using np.arange
6.1.
Do's
6.2.
Don'ts
7.
Key Differences Between arange and linspace
8.
Frequently Asked Questions
8.1.
What is arange() in NumPy?
8.2.
What does arange () do in Python?
8.3.
Why is NumPy called arange?
9.
Conclusion
Last Updated: Mar 30, 2025
Medium

Numpy.arange() in Python

Author Riya Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Python's NumPy library, the `arange()` function is used to create a sequence of evenly spaced values within a specified interval. It returns an array with the generated values. The function takes the start, stop, and step parameters to define the sequence. `arange()` is useful for creating numerical sequences efficiently and is commonly used for iterating over arrays, generating sequences for mathematical operations, and creating coordinate grids. In this article, we will discuss this in more detail like, its applications, and best practices, with executable code examples.

Np arange

What is NumPy arange()?

np.arange is a function in the NumPy library that generates arrays with evenly spaced values within a defined interval. It's similar to Python's built-in range function but returns a NumPy array instead of a list, which is more suitable for numerical computations.

Why Use np.arange?

The primary use case for np.arange 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.

Syntax

The general syntax of np.arange is:

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

 

Basic Usage

  • Python

Python

import numpy as np

# Create an array from 0 to 9

array1 = np.arange(10)

print(array1)
You can also try this code with Online Python Compiler
Run Code

Output:

Output

This code will output [0 1 2 3 4 5 6 7 8 9]. It creates an array starting from 0 up to (but not including) 10 with a default step size of 1.

Parameters of np.arange

np.arange accepts the following parameters:

  • start: The starting value of the sequence (inclusive). Defaults to 0 if not specified.
     
  • stop: The end value of the sequence (exclusive).
     
  • step: The difference between each value in the sequence. Defaults to 1 if not specified.
     
  • dtype: The type of the output array. If not specified, the type is inferred from other input arguments.

Examples

Example 1: Basic Increment

  • Python

Python

import numpy as np

# Using np.arange to create an array from 0 to 9

array1 = np.arange(10)

print("Array 1:", array1)
You can also try this code with Online Python Compiler
Run Code

Output 

Output

This code creates a simple array starting from 0 up to (but not including) 10 with a default step size of 1. The output is a one-dimensional array containing integers from 0 to 9.

Example 2: Specified Start, Stop, and Step

  • Python

Python

import numpy as np

# Creating an array from 10 to 50 with a step of 5

array2 = np.arange(10, 50, 5)

print("Array 2:", array2)
You can also try this code with Online Python Compiler
Run Code

Output

Output

In this example, the np.arange function creates an array starting at 10, ending before 50, and increments by 5. The output is a sequence of numbers beginning with 10 and increasing by 5 each time, stopping before reaching 50.

Example 3: Floating-Point Range

  • Python

Python

import numpy as np

# Creating an array with floating-point numbers

array3 = np.arange(0.5, 5.5, 0.5)

print("Array 3:", array3)
You can also try this code with Online Python Compiler
Run Code

Output

output

Here, np.arange is used with a starting value of 0.5, a stop value of 5.5, and a step of 0.5. The output is an array of floating-point numbers starting at 0.5 and increasing by 0.5 up to (but not including) 5.5.

Pros and Cons of Using np.arange

Pros

  • Efficiency: It is faster than native Python ranges for large datasets.
     
  • Versatility: Can handle any numeric data type.
     
  • Integration: Works well with other NumPy functions and operations.

Cons

  • Floating-Point Limitation: Due to the nature of floating-point arithmetic, sometimes it may not return the exact number of elements one might expect.
     
  • Memory Usage: For very large ranges, it can consume significant memory since it generates the entire array at once.

Do's and Don'ts of Using np.arange

Do's

  • Use np.arange when you need an array of numbers for mathematical operations.
     
  • Consider specifying the dtype if you are working with large numbers to avoid overflow issues.

Don'ts

  • Don't use np.arange with non-integer steps if you require precise endpoint control. Use np.linspace instead.
     
  • Avoid using np.arange for generating time sequences. Use pandas.date_range or similar functions that handle date and time objects better.

Key Differences Between arange and linspace

NumPy provides two functions, arange and linspace, that are commonly used for generating arrays of evenly spaced values in Python. Here are the key differences between arange and linspace:

Criteriaarangelinspace
FunctionalityGenerates values with a specified step size between start and stop, similar to Python's range.Generates values with a specified number of points evenly spaced between start and stop (inclusive).
Step Size vs. Number of PointsRequires specifying the step size between values.Requires specifying the number of evenly spaced points.
Inclusion of Stop ValueThe stop value is excluded.The stop value is included.
Data TypeGenerates integer or floating-point values based on input types.Typically generates floating-point values; data type can be specified.
Common Use CasesUseful when a specific step size is required, and the number of points is not critical.Useful when a specific number of evenly spaced points is needed, and the step size is not explicitly required.

Frequently Asked Questions

What is arange() in NumPy?

arange() in NumPy is a function that generates arrays with evenly spaced values within a given interval, similar to Python's range().

What does arange () do in Python?

arange() creates an array of values with a specified step size, akin to Python's range but extended to support floating-point values.

Why is NumPy called arange?

NumPy's arange is called so because it is an array version of the Python range() function, designed to create numerical arrays.

Conclusion

np.arange is a versatile tool in NumPy's arsenal, facilitating the creation of numerical sequences with ease and efficiency. Whether for initializing arrays or setting up axes for data visualization, np.arange proves to be an indispensable function for anyone working with Python for scientific computing. Remember to use it judiciously, keeping in mind its behavior with floating-point numbers and the potential memory footprint for large sequences. With the examples and guidelines provided, you're now equipped to utilize np.arange effectively in your projects.

Live masterclass