Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Equivalent of MATLAB's colon operator

In MATLAB I can create monotonically spaced vectors as in the examples below using the :, colon, operator. How can I do this in Python in a similarly concise manner?

>> x=1:10
x =
     1     2     3     4     5     6     7     8     9    10

or

>> x=0:2:10
x =
     0     2     4     6     8    10
like image 669
Glenn Jocher Avatar asked Mar 07 '23 17:03

Glenn Jocher


2 Answers

@karakfa is right in that this is the way to create a simple list.

Matlab's vectors and matrices offer vectorised computation, though, and if that's what you need, you should probably use numpy.array:

>>> import numpy
>>> numpy.arange(1, 11)
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
like image 157
Martin Sand Christensen Avatar answered Mar 17 '23 04:03

Martin Sand Christensen


there is range

range([start], stop[, step])

[] shows optional arguments. Default ranges starts with zero

like image 20
karakfa Avatar answered Mar 17 '23 04:03

karakfa