• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / Numpy arange() function with examples

Numpy arange() function with examples

December 25, 2022 by cmdlinetips

In this post, we will learn how to use arange(), one of the most useful functions in Numpy. With Numpy arange function we can create evenly spaced arrays.

For example, If you need to create an array of numbers within a given range, you can use the arange function.

To get started let us load numpy and check the Numpy version we are using.

import numpy as np
np.__version__
1.24.0

Numpy arange() Syntax

Basic syntax of using Numpy’s arange() function with default values is

np.arange(start=0, end, step=1)

Here, the argument start is optional and it can be integer or real. It defines the start of the interval including the value. By defulat, start is set to 0.

The next argument is stop and it defines the end of the interval. It can be integer or real. The interval we get does not include the end value (except due to some round off effects under certain condition).

The step argument is optional and it defines the spacing between the values in the interval. And the default value of step is 1. In addition to these arguments, we can also specify dtype for the type of output array. It is optional and arange() function infers the datatype from the inputs.

How to use Numpy arange() to generate evenly spaced numbers

For example, to create an array of numbers from 0 to 9, you can use the following code:

np.arange(10)

We get evenly spaced numbers from 0 to 9.

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

As we saw in the Numpy arange syntax, the code below is the same as the one in the above example to get evenly spaced numbers from 0 to 10 (not including 10) with an increment of 1.

np.arange(start=0, end=10, step=1)

To get a sequence of even numbers, we can change the step argument as shown below.

even_array = np.arange(0, 10, 2)

And we get even numbers.

even_array
array([0, 2, 4, 6, 8])

Similarly, with start=1 and step=2, we get the list of odd numbers from 1 to 9.

odd_array = np.arange(1, 10, 2)

odd_array
array([1, 3, 5, 7, 9])

Numpy arange example with float step size

In the previous examples our step sizes were integers. Here is an example where the step size is 0.1 and we generate array from 0 to 1 with arange()
.

# Generate an array of 10 evenly spaced values from 0 to 1, with float64 data type
np.arange(0, 1, 0.1, dtype=np.float64)

[0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]

Numpy arange example with specific data type

In the example below, we set the datatype to be int8 and generate an array of 5 evenly spaced numbers from 1 to 10 using Numpy’s araange()

# Generate an array of 5 evenly spaced values from 1 to 10, with int8 data type
np.arange(1, 11, 2, dtype=np.int8)

[1 3 5 7 9]

A caveat to note with Numpy arange

Numpy’s arange() function is very similar to Python range() function. One big difference between arange() and range() function is that Python’s range() function can take arbitrary size integer.

However, Numpy’s arange() produces numpy.int32 or numpy.int64 numbers. And this can result in incorrect results for large integer values. Numpy’s help function for arange() nicely illustrates this problem with the following example

power = 40
modulo = 10000
x1 = [(n ** power) % modulo for n in range(8)]
x2 = [(n ** power) % modulo for n in np.arange(8)]
print(x1)
[0, 1, 7776, 8801, 6176, 625, 6576, 4001]  # correct
print(x2)
[0, 1, 7776, 7185, 0, 5969, 4816, 3361]  # incorrect

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailNumpy reshape function with examples Default Thumbnail12 Basic Commands with NumPy Array Default ThumbnailNumpy dot() function with examples Default ThumbnailNumpy isclose() function with examples

Filed Under: Numpy Tips, Python, Python Tips Tagged With: Numpy arange function

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version