# numpy.linspace() in Python

In this tutorial, we are going to see the linspace() which is the built-in function in Python’s numpy library. linspace function and the range function are quite similar.

This linspace() also creates a sequence of evenly spaced values with defined intervals. It gives the values within the specified range.

The interval by default includes starting and ending values but the ending value can be optional.

The linspace function identifies based on how many elements you want, it is going to evenly space that array.

## How to perform numpy.linspace() in Python

• Let us see the syntax of linspace():

linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axix=0)

Here from the syntax, the start and stop parameters are mandatory and remaining parameters can be optional to the user. Because default values can also be taken.

start: It represents the starting value of the sequence.

stop: It represents the ending value of the sequence.

num: It generates a number of samples. The default value of num is 50 and it must be a non-negative number. It is of int type and can be optional.

endpoint: By default its value is True. If we take it as False then the value can be excluded from the sequence. It is of bool type and can be optional.

retstep: It itsTrue then it returns samples and step value where the step is the spacing between the samples.

dtype(data type): It represents the type of the output array. It can also be optional.

axis: The axis is the result to store the samples. It is of int type and can be optional.

• Let us consider the simple example of linspace()

We make use of linspace built-in function by importing the standard library numpy.

```import numpy
numpy.linspace(2,5,num=5)```

Output:

`array([2.  , 2.75, 3.5 , 4.25, 5.  ])`

From the output, we observe that we got 5 values from 2 to 5 which are evenly spaced. As we mentioned num=5 it returns only 5 elements.

Let us see the difference if we do not give the num.

```import numpy
numpy.linspace(2,5)```

Output:

```array([2.        , 2.06122449, 2.12244898, 2.18367347, 2.24489796,
2.30612245, 2.36734694, 2.42857143, 2.48979592, 2.55102041,
2.6122449 , 2.67346939, 2.73469388, 2.79591837, 2.85714286,
2.91836735, 2.97959184, 3.04081633, 3.10204082, 3.16326531,
3.2244898 , 3.28571429, 3.34693878, 3.40816327, 3.46938776,
3.53061224, 3.59183673, 3.65306122, 3.71428571, 3.7755102 ,
3.83673469, 3.89795918, 3.95918367, 4.02040816, 4.08163265,
4.14285714, 4.20408163, 4.26530612, 4.32653061, 4.3877551 ,
4.44897959, 4.51020408, 4.57142857, 4.63265306, 4.69387755,
4.75510204, 4.81632653, 4.87755102, 4.93877551, 5.        ])```

So now we can observe that it returns 50 elements as 50 is the default value of the num.

#### endpoint:

`numpy.linspace(2,5,num=5, endpoint=False)`

Output:

`array([2. , 2.6, 3.2, 3.8, 4.4])`

As we made the endpoint as False it excludes the last element that is 5 from the sequence.

#### retstep:

`numpy.linspace(2,5,num=5, retstep=True)`

Output:

(array([2. , 2.75, 3.5 , 4.25, 5. ]), 0.75)

As we mention the return step value as True it will return step value that is the difference between each value is 0.75 along with the sequence.

#### dtype:

`numpy.linspace(2,5,num=5,dtype=int)`

Output:

`array([2, 2, 3, 4, 5])`

As we mentioned the data type as int then it returns only the integer values.

The linspace() function can also be used to plot the graph that is evenly spaced.

So, this about the linspace(). We can create an array that contains the element in a particular interval and we can get evenly spaced num elements.