+ 1

What does range function return?

If i use range function for creating a list, then it works properly. But what Ɣould be if i would try to do it in the following way: var = range(5) ? seems like it doesnt work in such way because additionaly i have to create a list. So it would be: var = list(range(5)) correct? Then why does it work for 'for' cycle: for i in range(5): print(i) final output is: 0 1 2 3 4 It seems that in case of 'for' it automatically creates a list of values. Is it so or not? and if no, then how does it work?

18th Dec 2016, 7:27 PM
Aker Bbob
Aker Bbob - avatar
2 RĂ©ponses
+ 6
range returns a range object >>>x=range(1,4) if you cast it to a a list: >>>list(x) [1, 2, 3] or to a tuple: >>>tuple(x) (1, 2, 3) the range object which returned contains an iterator according to the third paramater (which i did not supplied so by default=1) if i would have supplied an increment value: y=range(1,10,3) >>>tuple(y) (1, 4, 7)
18th Dec 2016, 9:29 PM
Burey
Burey - avatar
0
range(begin, end, step) - returns a list
18th Dec 2016, 8:11 PM
Rishi Anand
Rishi Anand - avatar