DEV Community

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

Posted on

3 2

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.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay