8

As a beginner in python, I was trying to test the function range() in the IDLE terminal. I wrote in the terminal the below posted code and I expected to see result like this:

range(10)==>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

But unfortunately, i do not get the expected result

Python Code I Tried:

range(10)
print(range(10))

The Result From The shell:

range(10)
7
  • You haven't assigned the range(10) to a variable in the first line. The second line print(range(10)) should work. Commented May 7, 2013 at 11:36
  • range(11)? range(1, 11)? Commented May 7, 2013 at 11:36
  • 1
    Are you using python 3.x? Commented May 7, 2013 at 11:37
  • 1
    If you are using python3 range is now what xrange was in python2. To obtain a list of numbers use list(range(...)). Commented May 7, 2013 at 11:38
  • @Ashwini: i'm using python 3.3.1 Commented May 7, 2013 at 12:16

2 Answers 2

14

In python 3, range() returns a generator, that's why it shows you the object rather than the values:

>>> print(range(10))
range(0, 10)

If you were expecting a list, you will need to convert it to one before printing it:

>>> print(list(range(10)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Generators only create one value at a time in order to save memory. You can read up on them here, which includes an example suited to your test case.

Sign up to request clarification or add additional context in comments.

1 Comment

Eh, same conclusion i came to.
1


Cross version solution

C:\Documents and Settings\U009071\Desktop>python
Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> print(list(range(10)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
>>> for i in range(10):
...     print(i)
...
0
1
2
3
4
5
6
7
8
9
>>>

Python2:

>>> print(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a = range(10)
>>> print(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Don't know your Python version, but mine works fine. Try specifying range(0,10) to be sure.

5 Comments

range(0, 10) is equivalent to range(10) for any version of Python (well at least from v2.0 on and if you are using pre-2001 Python...)
I'm away, i'm also aware that he described a issue that shouldn't occur.. so any option that can be thrown in there to solve the mystery error is valid to note :)
@glglgl good point, list is just a type defenition per se so it should work just as fine in Python2 as in Python3.
@Torxed - no, list is a little more than a type definition, it is actually a constructor that takes an enumerable, and that behavior is consistent between Python 2 and 3.
@PaulMcGuire and everything is a object, i'm well aware. I shouldn't begin to argue specifics with anyone of you because i tend to "ease up" on the language unless i got a few hours do dig in to a subject or i get paied to do so, my free time is limited and i just mentioned that the the baseline is that list() returns a list definition and not some other variable, and since a list is iterable i made the assumption that a non-monkey would figure the other stuff out, i just pointed the lad in a general direction. Also, answer marked as solved, i stoped caring about specifics :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.