Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Dillon Shaw
Dillon Shaw
6,400 Points

Why does range() not work in workspaces?

I was trying to make a list using range() in the python shell, but it seems to interpret the entire function call as a string. So for example typing range(4, 10) directly into the shell gives back 'range(4, 10)'. Likewise assigning this to a variable creates a string with that value instead of a list of numbers 4 to 9. On my computer's terminal this does not happen - it works as it should and makes a list of the numbers.

2 Answers

Steven Parker
Steven Parker
229,657 Points

:point_right: A range is a special kind of thing. If you want to convert it into a list, do this:

list(range(4, 10))

And this little experiment proves that is is not a string:

>>> x = range(4,10)
>>> x
range(4, 10)
>>> list(x)
[4, 5, 6, 7, 8, 9]
Dillon Shaw
Dillon Shaw
6,400 Points

Okay I see that now, but any idea why the python shell on my own computer immediately turns the result of range() into a list?