# How to Create a Sequence of Linearly Increasing Values with NumPy Arrange?

NumPy is a Python library widely used for numerical computations and scientific data analysis. One of the most commonly used functions of NumPy is ‘numpy.arange()’, which creates a sequence of linearly increasing values with a given start, stop, and step size. In this tutorial, we'll examine how to use ‘numpy.arange()’ to produce a sequence of linearly increasing values. We will illustrate three examples of linearly arranged values with different steps.

In this tutorial, we will learn to create a sequence of linearly increasing values with a NumPy arrange. We will be using NumPy, which is a famous python library.

## Syntax

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


In this syntax, we can see that start is an optional argument that specifies the start of the sequence (default is 0), stop is the end of the sequence, and step is the spacing between values (default is 1). The dtype parameter specifies the data type of the output array.

## Example

Here’s a simple example that creates a linear arranged sequence of values.

import numpy as np
print(np.arange(0, 10, 1))
print(np.arange(0, 20, 1))
print(np.arange(0, 30, 1))


In this example, we first imported numpy, a python library and then used the ‘numpy.arange()’ function to create a sequence of linearly increasing values from 0 to 29, a step size of 1.

## Output

[0 1 2 3 4 5 6 7 8 9]
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23  24 25 26 27 28 29]


## Example

Here’s an example that creates a linear sequence of values from 1.5 to 9.5 incremented by 0.5.

import numpy as np
print(np.arange(1.5, 2, 0.5))
print(np.arange(1.5, 3, 0.5))
print(np.arange(1.5, 4, 0.5))
print(np.arange(1.5, 5, 0.5))
print(np.arange(1.5, 6, 0.5))
print(np.arange(1.5, 7, 0.5))
print(np.arange(1.5, 8, 0.5))
print(np.arange(1.5, 9, 0.5))
print(np.arange(1.5, 10, 0.5))


## Output

[1.5]
[1.5 2. 2.5]
[1.5 2. 2.5 3. 3.5]
[1.5 2. 2.5 3. 3.5 4. 4.5]
[1.5 2. 2.5 3. 3.5 4. 4.5 5. 5.5]
[1.5 2. 2.5 3. 3.5 4. 4.5 5. 5.5 6. 6.5]
[1.5 2. 2.5 3. 3.5 4. 4.5 5. 5.5 6. 6.5 7. 7.5]
[1.5 2. 2.5 3. 3.5 4. 4.5 5. 5.5 6. 6.5 7. 7.5 8. 8.5]
[1.5 2. 2.5 3. 3.5 4. 4.5 5. 5.5 6. 6.5 7. 7.5 8. 8.5 9. 9.5]


## Example

In this example, we have created a linear sequence with values from 1 to 17 with a step size of 4.

import numpy as np
print(np.arange(1, 11, 4))
print(np.arange(1, 12, 4))
print(np.arange(1, 13, 4))
print(np.arange(1, 14, 4))
print(np.arange(1, 15, 4))
print(np.arange(1, 16, 4))
print(np.arange(1, 17, 4))
print(np.arange(1, 18, 4))
print(np.arange(1, 19, 4))


## Output

[1 5 9]
[1 5 9]
[1 5 9]
[ 1 5 9 13]
[ 1 5 9 13]
[ 1 5 9 13]
[ 1 5 9 13]
[ 1 5 9 13 17]
[ 1 5 9 13 17]


We learned that ‘numpy.arange()’ is a powerful function that can be used to create a sequence of linearly increasing or decreasing values with a given start, stop, and step size. It is a very useful tool for numerical computations and scientific data analysis, and it is widely used in scientific and engineering communities. By understanding the syntax and usage of ‘numpy.arange()’, developers can create complex sequences of values for computational needs.

Updated on: 10-Aug-2023

138 Views