Reputation: 11
I want to write a long list of numbers in python, but I don't want to enter the numbers one by one because that's boring. So, how can I do that? For the ones who are familiar with MATLAB, I want to write something witch looks 1:100, but I don't know how to write it in python! By the way, I can append numbers for a one-element-list in a loop but I am looking for something mostly like a built in operator.
Upvotes: 0
Views: 2044
Reputation: 309861
Do you want something like the builtin range
function?
>>> range(1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1,10,2)
[1, 3, 5, 7, 9]
in python 3.x, range
doesn't return a list anymore, it returns a range object
. Range objects behave like lists in a lot of ways, but they aren't actually lists. If you really need a list on python 3.x, you can use list(range(...))
Numpy has a similar function arange
which will work with floats as well:
>>> np.arange(1,2,.1)
array([ 1. , 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9])
Upvotes: 4