+ 1

What is difference between ::: for i in range (). Or for i in ()

Python

29th Sep 2020, 3:03 PM
Mandrine
Mandrine - avatar
3 Antworten
+ 5
range(3) does not create numbers [0,1,2,3], but [0,1,2], as the last number is not included! Mandrine , To learn how range() is working you can have a look to the python docs by using a browser or you can use the console to type in: help(range) you will get syntax and some more information about this function and class.
29th Sep 2020, 4:29 PM
Lothar
Lothar - avatar
+ 3
The for loop iterates over a sequence or an iterable object. In this case, range() is a function that returns a sequence of numbers. For example range(3) is similar to as [0,1,2] (as a range object), so for i in range(3) will iterate over [0,1,2]. This is similar to just writing for i in [0,1,2]. This isn't limited to numbers. For example, to iterate over some words, you can write for i in ("red", "blue", "green").
29th Sep 2020, 3:16 PM
Zerokles
Zerokles - avatar
+ 1
Lothar Forgot about that. My bad. I'll edit it out.
29th Sep 2020, 4:56 PM
Zerokles
Zerokles - avatar