Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
221,938 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?