For loop in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

For loop in python

The for loop is too confusing it assigns checks and increase without stating any code. Can someone explain.

19th Aug 2019, 5:53 PM
Madiha Farooqui
2 Answers
+ 3
It just counts through each item in an iterable (lists, tuples, range objects, strings): for item in [4, 8, 2]: "run" - item = 4 - "run" - item = 8 - "run" - item = 2 - "run" - break Other programming languages also have a similar for loop (foreach loop), that is used to count through arrays similarly.
19th Aug 2019, 6:40 PM
Seb TheS
Seb TheS - avatar
+ 3
You can also use the range function. Range function takes three arguments. Third argument is optional. First argument is the starting value of a loop. Second argument is the ending value of a loop. Third argument is the step count. for i in range(0, 6): print i Output: 0 1 2 3 4 5 Remember that the first argument is inclusive but the second argument is exclusive. That's why it started from 0 and ended at 5. By default, third argument has a value 1. for i in range(0, 7, 2): print i Output: 0 2 4 6 Now we passed third argument with value of 2. So after each iteration, 2 will be added to the value of i.
19th Aug 2019, 9:04 PM
blACk sh4d0w
blACk sh4d0w - avatar