Creating a Range of Numbers as an Array in Numpy
using the arange() and linspace() function

The Numpy arange() function and linspace() function are both used to create a range of numbers as an array. Their operation is however slightly different. In this tutorial, you will learn how to create NumPy arrays within a specified range using both functions. By the end of the end, you will know the following:
- Numpy arange() function in Python
- Using the arange() function with all 3 arguments.
- Using the arange function() with 2 arguments.
- How to Count Backwards with the step argument.
- Using the linspace function
Let’s get started.
Numpy arange() function in Python
The arange() works like the range function in python. It is used to create a list of integers within the range of numbers.
Syntax
numpy.arange([start, ]stop, [step, ], dtype=None) -> numpy.ndarray
Parameters
- start: This is the number the array values begin.
- stop: This number indicates the end of the array. Note, however, that this number is not included in the array.
- step: This is the spacing between consecutive indices. By default, it is set to 1 and so integers are within the start and stop value is returned. If set to 2, a number is skipped.
Note that the step cannot be set to zero. Else, a ZeroDivisionError will be returned.
Let’s see an example of how the function works.
Using the arange() function with all 3 arguments.
To use the arange() function with all 3 functions is simple and straightforward. If we wish to return even numbers from 0 to 15, the arange() function can be used as shown below.
#import the necessary libraries import numpy as np print(np.arange(0, 15, 2))
Output:
[ 0 2 4 6 8 10 12 14]
For clarity, you may decide to use the keyword arguments as shown in the example below.
#import the necessary libraries import numpy as np print(np.arange(start=0, stop=15, step=2))
Output:
[ 0 2 4 6 8 10 12 14]
It returns the same result as the first example.
Using the arange function() with 2 arguments.
If you are defining just 2 arguments, without keywords, the arguments are the start and end arguments. In this case, the step argument is set to one. Which implies that the integers will be printed consecutively. The example below prints the numbers between 0 and 15.
#import the necessary libraries import numpy as np print(np.arange(0, 15))
Output:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
You may also decide to use keyword arguments as shown below.
#import the necessary libraries import numpy as np print(np.arange(start=0, stop=15))
Output:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
When using keyword arguments, however, the start and end arguments must be defined. Leaving out either of those will return an error.
How to Count Backwards with the step argument.
In the examples above, the count has always been done from start to finish, progressively. If you wish to count backward, the step argument is set to a negative integer. If we set it to -1, it prints all the numbers within the range but with a decreasing step.
One thing to note here is that the start value must be greater than the end value for it to run. So say we wish to return a list from 15 to 1 backwards, the start value is set to 15 while the end value is set to 1. The step is set to -1. See the code below.
#import the necessary libraries import numpy as np print(np.arange(15, 0, -1))
Output:
[15 14 13 12 11 10 9 8 7 6 5 4 3 2]
Using the linspace function
The linspace function is used to create an array of evenly spaced elements. When the linspace function is called, it receives 3 required arguments. It however has 4 arguments to be defined as shown in its syntax below.
Syntax:
numpy.linspace(start, stop, num, endpoint)
Parameters:
- start: This is the first value in the array
- end: This is the last value in the array
- num: This is the number of elements that will be created. By default it is set to 50.
- endpoint: This takes a boolean, True or False. It determines whether or not the end value itself would be included. By default, it is set to True which means that the end value is included in the generated array.
If say the start index was set to 1, the end index was set to 3, and the num was set to 60, sixty numbers between 1 and 3 that are evenly spaced will be created. See the example below.
#import the necessary libraries import numpy as np print(np.linspace(1, 3, 60))
Output:
[1. 1.03389831 1.06779661 1.10169492 1.13559322 1.16949153
1.20338983 1.23728814 1.27118644 1.30508475 1.33898305 1.37288136
1.40677966 1.44067797 1.47457627 1.50847458 1.54237288 1.57627119
1.61016949 1.6440678 1.6779661 1.71186441 1.74576271 1.77966102
1.81355932 1.84745763 1.88135593 1.91525424 1.94915254 1.98305085
2.01694915 2.05084746 2.08474576 2.11864407 2.15254237 2.18644068
2.22033898 2.25423729 2.28813559 2.3220339 2.3559322 2.38983051
2.42372881 2.45762712 2.49152542 2.52542373 2.55932203 2.59322034
2.62711864 2.66101695 2.69491525 2.72881356 2.76271186 2.79661017
2.83050847 2.86440678 2.89830508 2.93220339 2.96610169 3. ]
Notice that the start and end element are 1 and 3 respectively. All other numbers are the sequence of numbers required to count 1 to 3 in 60 placed. This can be useful when you wish to plot a trigonometric graph.
In summary,
You have learned how to create a range of numbers that are evenly spaced. While the arange function is limited to integers, the linspace function can create floats between the defined range. If you do have any questions, please leave them in the comment section and I’d do my best to answer them.