0

I am new to this site and very new at studying Python. I am following a youtube teaching by Dr. Charles Severance of Univ. of Michigan. There are times (few) when he does something as an example, but I do not get the same results when I do it. Like this below, my range doesn't print each individual item. It only prints "range" and the start & end...so like...range(0, 4) not [0, 1, 2, 3].

he does, print(range(4))

and gets... [0, 1, 2, 3]

I get... range(0, 4)

...or he does...

friends = ['Joseph', 'Glenn', 'Sally']
print(range(len(friends)))

...and he gets... [0, 1, 2]

I get... range(0, 3)

I mean I am typing exactly the same thing. What is wrong? Is it my install of Python? Is there something that I have to install, import, or enable to get mine to work the same?

It also happens when he uses quit(). His quits when it reaches it, mine does not quit? no traceback or any indication that anything is wrong. It is like Python just ignores it.

Any help would be greatly appreciated.

Thank you in advance.

busybear
  • 10,194
  • 1
  • 25
  • 42
Brian
  • 1

4 Answers4

3

In Python-3.x, range(0,4) produces a range object instead of a list, which is an update from Python-2.x.

If you want to get the same output as the tutorial you are following, try list(range(0,4))

Derek O
  • 16,770
  • 4
  • 24
  • 43
1

This is because he uses Python 2. In Python 2, many things work differently than Python 3, which is what you probably use. In order to get the first result, useprint([i for i in range(4)]) For the second result, use print([i for i in range(len(friends))])

JaysFTW
  • 101
  • 6
1

I guess the youtube video is using Python 2, and you are using Python 3.

Python 3 turn range to a list

In Python 3, you get a range type instead of a list, so you have to do:

>>> print(list(range(4)))
[0, 1, 2, 3]

Though, quit() must still work even in Python 3. :)

>>> quit()
~ $
kamion
  • 461
  • 2
  • 9
0

I think the video your are seeing is created by python 2.X and now you are using 3.X

try this,

print(list(range(0,4)))
print(list(range(len(friends))))
Vignesh
  • 1,553
  • 1
  • 10
  • 25