0

How does this work? why is there an i? for i in range(5): print("hello!")

14th Sep 2016, 11:29 AM
Giovanni
4 Respuestas
+ 3
The range-function returns an object of a sequence of intergers. It starts with 0 (inclusive) by default. The stop integer is the argument of the range-function, but be careful: here it is exclusive. In your example range(5) produces an list-like object [0,1,2,3,4]. i is just a variable and iterates through the range object.
14th Sep 2016, 2:11 PM
Amarie
+ 3
i ... is a variable When you write >>> for i in range(10): ....... print("Hello") Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello It means that the variable called "i" will assume the value 0 then 1 then 2 .... Until 9 (10 is excluded, because it starts from 0 and ends to 9 and if you count the number from 0 to 9, they are ten number ... You have to accept this rule. So: the loop will repeat what is after the first line for 10 times. You can use also a different variable instead of i ... x, c or also num, money ... Ecc. >>> for numbers in range(5): ....... print(number) 0 1 2 3 4 This time the variable called "number" not only is used to count the times that the print() function will be repeated but it will be printed also so that you will see how the variable changes until it reaches the number 4, because 5 is excluded being the last number
14th Sep 2016, 4:50 PM
Giovanni Gatto
Giovanni Gatto - avatar
0
ok thanks!
14th Sep 2016, 9:56 PM
Giovanni
- 4
NEEEEEEEEEEEWSSS
14th Sep 2016, 11:42 AM
Keeeeeeemstarrrrrrr
Keeeeeeemstarrrrrrr - avatar