Contents

## NumPy – Initialize Array with Range of Numbers

To initialise an array with a range of numbers in NumPy, you can use numpy.arange() function.

The syntax to call numpy.arange() function is

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

where

`start`

[mandatory] is the starting of the range.`stop`

[mandatory] is the ending of the range. stop is not inclusive in the returned array.`step`

[optional] is the difference between adjacent numbers in the range.

The function returns a numpy array with elements starting from `start`

, in steps of step value `step`

, until `stop`

.

## Examples

### 1. Create 1D numpy array with numbers from 10 to 19

In the following program, we create one dimensional numpy array with a range of numbers from 10 upto 20(20 not included).

**Python Program**

```
import numpy as np
arr = np.arange(10, 20)
print(arr)
```

Run **Output**

`[10 11 12 13 14 15 16 17 18 19]`

### 2. Create numpy array with numbers from 10 to 50 in steps of 5

In the following program, we create one dimensional numpy array with a range of numbers from 10 to 50 in steps of 5.

**Python Program**

```
import numpy as np
arr = np.arange(10, 50, 5)
print(arr)
```

Run **Output**

`[10 15 20 25 30 35 40 45]`

### 3. Create 2D numpy array with a range of numbers

In the following program, we create a two dimensional array with a range of numbers starting from `1`

, with an array shape of `(4, 5)`

.

Since we need `4*5=20`

elements to create an array of shape `(4, 5)`

, and start of the range is `1`

, ending of the range must be `(4*5)+1`

.

**Python Program**

```
import numpy as np
shape = (4, 5)
arr = np.arange(1, (4*5)+1).reshape(shape)
print(arr)
```

Run **Output**

```
[[ 1 2 3 4 5]
[ 6 7 8 9 10]
[11 12 13 14 15]
[16 17 18 19 20]]
```

## Summary

In this tutorial of Python Examples, we learned how to create or initialise a numpy array with a range of numbers using **numpy.arange()** function.