Python For Loop
Python loop is used to iterate over the collection or data type like string, list, tuple.
Break Keyword
For loop will complete its iteration and stop at the end. But we can break the for loop in between and stop the iteration using a break keyword.
Continue Keyword
For loop runs in a loop from the start of the block and goes till end. But when the loop encounter continue keyword instead of going till the end of the loop. the code will continue from the continue keyword and goes up again.
For Else
Like If else we do have for else and the else part of the for loop runs once the iteration gets completed.
Key Point to Remember:
-
If for loop get break with the break keyword and didn't complete its execution then else part will not run.
-
Else part will only run when the execution of for loop is completed.
Range
range() is also an iterable object and you can use for loop to iterate over it.
Syntax : range(start,stop,step)
start and step value is optional. if you don't pass start value then python will consider 0 as start value and 1 as step value.