How to Print the Contents of a `range()`?


#1

In the exercise this code:

 to_one_hundred = range(101)
 backwards_by_tens = to_one_hundred[::-10]
 print backwards_by_tens

outputs this list:
[100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0]

But when I replicate it in Python 3 (with parenthesis added to the print statement) it outputs
range(100, -1, -10)

Can anyone tell me how to update this exercise’ syntax for Python 3?


FAQ: Learn Python - Advanced Topics in Python - Stride Length
#2

Hi @codewhiz37795,

You’ll need to iterate through the range with a loop:

forwards = range(51)
backwards_by_fives = forwards[::-5]

for i in backwards_by_fives:
  print(i)

#3

in python3, range returns an iterable (is faster and more memory efficient), of course if you want a list you can cast to list (using list() ) or unpack (slightly more complicated).

Looping is also an option, but depends what you need for it.