Tuesday , November 24 2026
Python Loops - For Loop, While Loop and Nested Loops

Python Loops: for and while Loops with Examples

Python loops repeat a block of code. Python has two loop statements: the for loop, which iterates over the items of a sequence such as a list, tuple, string or range(), and the while loop, which keeps running as long as a condition is true.

Key takeaways

  • Use a for loop when you know what you are iterating over; use a while loop when you repeat until a condition changes.
  • range(start, stop, step) generates the numbers a for loop counts through; stop is not included.
  • break exits the loop immediately, while continue skips to the next iteration.
  • A loop’s else block runs only when the loop finishes without hitting break.
4 Looping Statement

Python Loops

Python has two primitive loop commands:

  • for loops
  • while loops

For Loop

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

In [1]:
L=[10,20,"Machine Learning",40,50]
for i in L:
    print("Printing",i)
Printing 10
Printing 20
Printing Machine Learning
Printing 40
Printing 50

The range() Function

To loop through a set of code a specified number of times, we can use the range() function

In [2]:
L=list(range(10))
for i in L:
    print(i,end=' ')   
0 1 2 3 4 5 6 7 8 9 
In [3]:
for i in range(10):
    print(i,end=' ')
0 1 2 3 4 5 6 7 8 9 
In [4]:
for i in range(1,10):
    print(i,end=' ')
1 2 3 4 5 6 7 8 9 
In [5]:
for i in range(1,10,1):
    print(i,end=' ')
1 2 3 4 5 6 7 8 9 
In [6]:
for i in range(1,10,2):
    print(i,end=' ')
1 3 5 7 9 
In [7]:
for i in range(5):
    print("MachineLearning.org.in")
MachineLearning.org.in
MachineLearning.org.in
MachineLearning.org.in
MachineLearning.org.in
MachineLearning.org.in

For Loop Control Statement

In [8]:
for i in range(1,10):
    if i%3==0:
        print(i,end=' ')
3 6 9 

Looping Through a String

Even strings are iterable objects, they contain a sequence of characters:

In [9]:
for x in "python":
    print(x)
p
y
t
h
o
n

For Loop with break Statement

With the break statement we can stop the loop before it has looped through all the items:

In [10]:
for i in range(1,10):
    if i==5:
        break
    print(i,end=' ')
1 2 3 4 

For Loop with Continue Statement

With the continue statement we can stop the current iteration of the loop, and continue with the next:

In [11]:
for i in range(1,10):
    if i==5:
        continue
    print(i,end=' ')
1 2 3 4 6 7 8 9 

Else in For Loop

The else keyword in a for loop specifies a block of code to be executed when the loop is finished:

In [12]:
for x in range(6):
    print(x)
else:
    print("Finally finished!")
0
1
2
3
4
5
Finally finished!

Nested For Loop

A nested loop is a loop inside a loop.
The “inner loop” will be executed one time for each iteration of the “outer loop”:

In [13]:
adj = ["Red", "Big", "Tasty"]
fruits = ["Apple", "Watermelon", "Mango"]

for x in adj:
    for y in fruits:
        print(x, y)
Red Apple
Red Watermelon
Red Mango
Big Apple
Big Watermelon
Big Mango
Tasty Apple
Tasty Watermelon
Tasty Mango

The while Loop

With the while loop we can execute a set of statements as long as a condition is true.

In [14]:
i=1
while(i<=5):
    print("MachineLearning.org.in")
    i=i+1
MachineLearning.org.in
MachineLearning.org.in
MachineLearning.org.in
MachineLearning.org.in
MachineLearning.org.in
In [15]:
i = 1
while i <= 10:
    print(i,end=' ')
    i += 1
1 2 3 4 5 6 7 8 9 10 

The break Statement

With the break statement we can stop the loop even if the while condition is true:

In [16]:
i = 1
while i <= 10:
    if i==4:
        break
    print(i)
    i += 1
1
2
3

The continue Statement

With the continue statement we can stop the current iteration, and continue with the next:

In [17]:
i = 0
while i <= 10:
    i += 1
    if i==4:
        continue
    print(i) 
1
2
3
5
6
7
8
9
10
11

The else Statement

With the else statement we can run a block of code once when the condition no longer is true:

In [18]:
i = 1
while i < 6:
    print(i)
    i += 1
else:
    print("i is no longer less than 6")
1
2
3
4
5
i is no longer less than 6

Summary

This tutorial covered both Python loop types with runnable examples: iterating over lists and strings with for, counting with range(), controlling the flow with break, continue and else, nesting loops, and repeating code with while. Practise each snippet and change the values to see how the output changes.

Frequently asked questions

What is the difference between for and while loops in Python?

A for loop iterates over a sequence and stops when the items run out. A while loop repeats as long as its condition stays true, so you must update the condition inside the loop to avoid an infinite loop.

How do I stop a loop in Python?

Use the break statement to exit the loop immediately. Use continue if you only want to skip the rest of the current iteration.

Does Python have a do-while loop?

No. You can get the same behaviour with while True: and a break at the end of the loop body once your condition is met.

Related tutorials

Learn it step by step

Build a strong foundation with the free Python Foundation with Data Structures & Algorithms course and earn the Python Programming Professional Certification.

About Machine Learning

Check Also

Python MySQL Insert Into Table

Python MySQL Insert Into Table

4 Python MySQL Insert Into Table Python MySQL Insert Into Table¶To fill a table in …