Updated On : Jul-08,2020 Time Investment : ~10 mins

Learning Numpy - Simple Tutorial For Beginners - NumPy - Array From Numerical Ranges Part 7


This function returns an ndarray object containing evenly spaced values within a given range. The format of the function is as follows −

numpy.arange(start, stop, step, dtype)
Parameter Description
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

import numpy as np
x = np.arange(5)
print (x)
[0 1 2 3 4]

Example 2

import numpy as np
# dtype set 
x = np.arange(5, dtype = float)
print (x)
[0. 1. 2. 3. 4.]

Example 3

# start and stop parameters set 
import numpy as np
x = np.arange(10,20,2)
print (x)
[10 12 14 16 18]


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. The usage of this function is as follows −

numpy.linspace(start, stop, num, endpoint, retstep, dtype)
Parameter Description
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 True by default, hence the stop value is included in the sequence. If false, it is not included
retstep If true, returns samples and step between the consecutive numbers
dtype Data type of output ndarray

Example 1

import numpy as np
x = np.linspace(10,20,5)
print (x)
[10.  12.5 15.  17.5 20. ]

Example 2

# endpoint set to false 
import numpy as np
x = np.linspace(10,20, 5, endpoint = False)
print (x)
[10. 12. 14. 16. 18.]

Example 3

# find retstep value 
import numpy as np

x = np.linspace(1,2,5, retstep = True)
print (x)
# retstep here is 0.25
(array([1.  , 1.25, 1.5 , 1.75, 2.  ]), 0.25)


This function returns an ndarray object that contains the numbers that are evenly spaced on a log scale. Start and stop endpoints of the scale are indices of the base, usually 10.

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

Following parameters determine the output of logspace function.

Parameter Description
start The starting point of the sequence is basestart
stop The final value of sequence is basestop
num The number of values between the range. 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 array. If not given, it depends upon other input arguments

Example 1

import numpy as np
# default base is 10 
a = np.logspace(1.0, 2.0, num = 10)
print (a)
[ 10.          12.91549665  16.68100537  21.5443469   27.82559402
  35.93813664  46.41588834  59.94842503  77.42636827 100.        ]

Example 2

# set base of log space to 2 
import numpy as np
a = np.logspace(1,10,num = 10, base = 2)
print (a)
[   2.    4.    8.   16.   32.   64.  128.  256.  512. 1024.]
Dolly Solanki  Dolly Solanki

YouTube Subscribe Comfortable Learning through Video Tutorials?

If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.

Need Help Stuck Somewhere? Need Help with Coding? Have Doubts About the Topic/Code?

When going through coding examples, it's quite common to have doubts and errors.

If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.

You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.

Share Views Want to Share Your Views? Have Any Suggestions?

If you want to

  • provide some suggestions on topic
  • share your views
  • include some details in tutorial
  • suggest some new topics on which we should create tutorials/blogs
Please feel free to contact us at coderzcolumn07@gmail.com. We appreciate and value your feedbacks. You can also support us with a small contribution by clicking DONATE.

Subscribe to Our YouTube Channel

YouTube SubScribe

Newsletter Subscription