DEV Community

Cover image for For loop in Python with Examples
Keshav Jindal
Keshav Jindal

Posted on

For loop in Python with Examples

Introduction
For loops are very helpful in Python as they resist the repetition of the same codes again and again until the specified condition is satisfied. There are basically 3 types of loop in Python :
1.For loop
2.While loop
3.Nested loop
For loop
With the for loop, we can execute a set of statements in the given range.
Syntax
In Python, the syntax of the for loop is - for i in range(0,10).
Remember (i) denotes a variable. for is the keyword in the loop.
Image description
While loop
While loop is used to execute a certain block of code one after another until the give condition is specified.
syntax
Image description
Nested loops
The nested loop is the loop inside the loop. This means that we want to execute the inner loop code multiple times.
syntax
Image description
Break
break keyword stops the loop when condition is satisfied.
Image description

Continue
with the help of the continue keyword, we can stop the current iteration of the loop, and continue with the next. The else keyword in for loop specifies a block of code to be executed after the loop is ended.

Top comments (0)