What is "for loops" in python | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 1

What is "for loops" in python

I was doing the python course and came across this. The first lesson made zero since though. Can someone explain? Pls.

15th Jun 2020, 2:08 AM
Gavin Peacock
Gavin Peacock - avatar
9 ответов
+ 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
15th Jun 2020, 2:44 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 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
15th Jun 2020, 2:28 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
Bit harsh _yaroslavv [online_everyday] . Gavin Peacock asked the question because he was having difficulties understanding the lesson.
15th Jun 2020, 4:46 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 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,
15th Jun 2020, 5:06 PM
Shazwan X-99
Shazwan X-99 - avatar
+ 2
Thanks Rik Wittkopp but where does the "i" come from
15th Jun 2020, 2:33 AM
Gavin Peacock
Gavin Peacock - avatar
+ 2
Thanks Rik Wittkopp I really appreciate your help. But to clarify, "I" is just a variable and I could technically use "x"
15th Jun 2020, 12:38 PM
Gavin Peacock
Gavin Peacock - avatar
+ 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",
16th Jun 2020, 5:01 PM
Abbakar_ah!!!
Abbakar_ah!!! - avatar
0
Y'all have really helped me
16th Jun 2020, 5:09 PM
Gavin Peacock
Gavin Peacock - avatar