9 Answers
+ 4
It doesn't have to be i, it can be what you designate.
Example:
for sums in range(4):
print(sums +2)
I prefer i as it reminds me that it is an iterator
+ 3
Example:
for i in range(4):
print(i +2)
So this means:- for the iteration in the range of (0 to 3), print the iterated value +2 each time.
0+2 =>2
1+2 =>3
2+2 =>4
3+2 =>5
+ 3
Bit harsh _yaroslavv [online_everyday] .
Gavin Peacock asked the question because he was having difficulties understanding the lesson.
+ 3
for loops use a var that keeps increment everytime it repeat.
Let say;
for x in range(2):
print(x, end = ',')
#output: 0,1,2,
Not necessarily use 'i', you can use any var. Also works with list;
list1 = [a, b, c]
for letter in list1:
print(letter, end = ',')
#letter is refer to elements in list1 and keep increment till the list end.
#output: a,b,c,
+ 2
Thanks Rik Wittkopp but where does the "i" come from
+ 2
Thanks Rik Wittkopp I really appreciate your help. But to clarify, "I" is just a variable and I could technically use "x"
+ 1
Loops are use to repeat things x number of time, here x can be any value 2, 4, 5 up to whatever,
Imagine we want to repeat "hello, world 20 time", we can do this
for i in range(20):
print("hello, world!)
This will write "hello world 20 time",
0
Y'all have really helped me