range()
Title
Question
in the tutorial there is a question
what will be the output of range(1,5)? The answer for this is mentioned in it is [1,2,3,4]
but in the interpreter if we give this the output will be range(1,5). Please explain what happens actually when range() function invokes.
How this works in for loop
Python-3.4.3 Getting-started-with-for 04-05 min 50-60 sec
Answers:
In new python versions range function is a generator, in older python version it used to be a list. That is why the outputs are different. However, generators work perfectly fine when used in for loops.
Please try to see Generators in Python official docs if you wish to understand what they are.
for i in range(1,5):
print(I)
we will get the out put
1
2
3
4
if your code is
for i in range(5):
print(I)
then the output will be
0
1
2
3
4
5
Login to add comment