8. Omitting Indices


#1

Hi, I can't understand why my python 3 print out the follow in the console:

range(1, 11, 2)

instead of

[1, 3, 5, 7, 9]

my_list = range(1, 11) 
print (my_list[::2])


#2

In Python 3 range returns an iterator

So in Python 3.x, the range() function got its own type. In basic terms, if you want to use range() in a for loop, then you're good to go. However you can't use it purely as a list object. For example you cannot slice a range type.
http://pythoncentral.io/pythons-range-function-explained/

To treat it as a list we have to re-cast it as one.

>>> list(range(5))
[0, 1, 2, 3, 4]
>>>

#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.