Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

Numpy.arange() Method

  • 29 Jul, 2025
  • Com 0
Numpy.arange() Method in Python (np.arange())

Numpy.arange() method creates an array of evenly spaced values within a specified interval. It is highly efficient for creating arrays with a fixed step size, commonly used in scientific computing and data analysis.

numpy.arange()

Let’s generate integers from 0 to 3. Here, 4 is exclusive.

import numpy as np

print(np.arange(4))

# Output: [0 1 2 3]

Syntax

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

Parameters

Argument Description
start (optional) It represents the starting value of the sequence (inclusive). 

The default is 0.

stop (required) It is the end value of the sequence (exclusive).
step (optional)

It represents the spacing between values. Default is 1.

dtype (optional)

It is the desired data type of the output array (e.g., int, float).

If the dtype is not given, infer the data type from the other input arguments.

Specifying start and stop

We can specify the starting point and ending point to generate a sequence.

np.arange() with start and stop

import numpy as np

arr = np.arange(18, 22)

print(arr)

# Output: [18 19 20 21]

Specifying step size

Let’s generate a sequence from 17 to 22 with a step of 2.

Specifying step size

 

import numpy as np

arr = np.arange(17, 22, 2)

print(arr)

# Output: [17 19 21]

Passing ‘float’ arguments

To create a sequence of floating-point numbers, we need to pass the floating value to the np.arange() method.

Passing 'float' arguments

 

import numpy as np

arr = np.arange(2, 11.1, 3)

print(arr)

# Output: [ 2.  5.  8. 11.]

Creating a multi-dimensional array

You cannot create a 2D or multi-dimensional array using the arange() method alone, but you can reshape it to 2D using the .reshape() method.

Creating a multi-dimensional array

 

import numpy as np

print(np.arange(4).reshape(2, 2))

# Output:
# [[0 1]
#  [2 3]]

Negative Step

You can pass the step as a negative number, so it generates numbers from 6 to 1 (decreasing order).

import numpy as np

arr = np.arange(6, 1, -1)

print(arr)

# Output: [6 5 4 3 2]

Zero step

If you pass the step argument as 0, it returns the ZeroDivisionError: division by zero exception.

import numpy as np

try:
    arr = np.arange(1, 5, 0)
except ValueError as e:
    print(e)

# Output: ZeroDivisionError: division by zero

Large step size

What if the step size is larger than stop-start? Well, in that case, the array may contain only the start value or be empty.

import numpy as np

arr = np.arange(1, 3, 5)

print(arr)

# Output: [1]

That’s it.

Post Views: 24
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Numpy.cbrt(): Finding a Cube Root of an Array
How to Create an Empty File in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend