[Tutor] list semantics

Timo timomlists at gmail.com
Sat Apr 11 20:17:10 CEST 2015


Op 11-04-15 om 19:41 schreef Jim Mooney:
> Why does the first range convert to a list, but not the second?
>
>>>> p = list(range(1,20)), (range(40,59))
>>>> p
> ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
>   range(40, 59))
>


I'm not sure I understand correctly. This is what the top of help(range) 
says:

<
Help on class range in module builtins:

class range(object)
  |  range(stop) -> range object
  |  range(start, stop[, step]) -> range object
  |
  |  Return a virtual sequence of numbers from start to stop by step.
 >

So range() returns a range object. In your example you convert the first 
one to a list with list(), but not the second, so it prints the range 
object.

 >>> range(2)
range(0, 2)
 >>> list(range(2))
[0, 1]

Timo


More information about the Tutor mailing list