Running the example in the videos for chapter 8: Lists - A, the following code do not produce the result prof. Chuck say:
print(range(4))
the output I get is:
range(0, 4)
Can you please provide a link to the exercise you are working on?
Although, that curriculum is now considered legacy and will not be updated. I would suggest trying this more recent course material:
https://www.freecodecamp.org/learn/scientific-computing-with-python/
I think it’s caused by different version of Python. In Python 3.x, when you print an iterable directly( in this case range(4)
), it would display the range
object representation: range(0, 4)
. To print the content of the iterable, you need to add the unpacking operation *
before it, i.e. print(*range(4))
, it will give the output 0 1 2 3
. The *
operator unpacks the iterable(range(4)
) into individual elements. The print()
function then takes those individual elements as separate arguments and prints them out.
In Python 2.x, print(range(4))
will display the list of values[0, 1, 2, 3]
, print(*range(4))
will raise an error as *
operator is not supported. I’m not sure why, I only know in Python 2 print
is a statement rather than a function, don’t know the exact logic of operation.