# NumPy - Array Creation using Numerical Range

In this chapter, we will see how to create an array using numerical range.

## 1. numpy.arange

This function returns an ndarray object containing evenly spaced values within a given range.

```Syntax:
numpy.arange(start, stop, step, dtype)

start - The start of an interval. If omitted, defaults to 0
stop  - The end of an interval (not including this number)
step  - Spacing between values, default is 1
dtype - Data type of resulting ndarray. If not given, data type of input is used
```

Example 1:

```# Starts from 0 to n-1
import numpy as np
num = np.arange(5)
print(num)

Output:
[0 1 2 3 4]
```

Example 2:

```#start, end (exclusive), step
import numpy as np
num2 = np.arange(10, 30, 2)
print(num2)

Output:
[10 12 14 16 18 20 22 24 26 28]
```

Example 3:

```# Output in float format
import numpy as np
num3 = np.arange(5, dtype = float)
print(num3)

Output:
[0. 1. 2. 3. 4.]
```

Example 4:

```#start, end (exclusive), step , dtype
import numpy as np
num4 = np.arange(10, 30, 2, dtype = float)
print(num4)

Output:
[10. 12. 14. 16. 18. 20. 22. 24. 26. 28.]
```

## 2. numpy.linspace

This function is similar to arange() function. In this function, instead of step size, the number of evenly spaced values between the interval is specified.

```Syntax:
numpy.linspace(start, stop, num, endpoint, retstep, dtype)

start - The starting value of the sequence
stop  - The end value of the sequence, included in the sequence if endpoint set to true
num   - The number of evenly spaced samples to be generated. Default is 50
endpoint  - If true, stop is the last value in the range
retstep - If true, returns samples and step between the consecutive numbers
dtype - Data type of output ndarray
```

Example 1:

```#start, end, number of points
import numpy as np
a = np.linspace(10, 30, 5)
print(a)

Output:
[10. 15. 20. 25. 30.]
```

Example 2:

```#start, end - It will split into 50 parts
import numpy as np
b = np.linspace(10, 30)
print(b)

Output:
[10.         10.40816327 10.81632653 11.2244898  11.63265306 12.04081633
12.44897959 12.85714286 13.26530612 13.67346939 14.08163265 14.48979592
14.89795918 15.30612245 15.71428571 16.12244898 16.53061224 16.93877551
17.34693878 17.75510204 18.16326531 18.57142857 18.97959184 19.3877551
19.79591837 20.20408163 20.6122449  21.02040816 21.42857143 21.83673469
22.24489796 22.65306122 23.06122449 23.46938776 23.87755102 24.28571429
24.69387755 25.10204082 25.51020408 25.91836735 26.32653061 26.73469388
27.14285714 27.55102041 27.95918367 28.36734694 28.7755102  29.18367347
29.59183673 30.        ]
```

Example 3:

```# start, stop, num, endpoint, retstep, dtype as Integer
import numpy as np
c = np.linspace(0, 1, 5, retstep = True)
print(c)

Output:
(array([0.  , 0.25, 0.5 , 0.75, 1.  ]), 0.25)
```

## 3. numpy.logspace

This function returns an ndarray object that contains the numbers that are evenly spaced on a log scale.

```Syntax:
numpy.logspace(start, stop, num, endpoint, base, dtype)

start - The starting value of the sequence
stop  - The end value of the sequence, included in the sequence if endpoint set to true
num   - The number of evenly spaced samples to be generated. Default is 50
endpoint  - If true, stop is the last value in the range
base - Base of log space, default is 10
dtype - Data type of output ndarray
```

Example 1:

```# Default base is 10
import numpy as np
a = np.logspace(1, 2, 5)
print(a)

Output:
[ 10.          17.7827941   31.6227766   56.23413252 100.        ]
```

Example 2:

```# Base of log space to 5
import numpy as np
a = np.logspace(1, 2, 5, base = 5)
print(a)

Output:
[ 5.          7.47674391 11.18033989 16.71850762 25.        ]
```
Total Website Visits: 40417