Python Loops

Last Updated : 14 Apr 2026

Python loops are control flow structures that allow us to execute a block of code repeatedly. In this chapter, you will learn about loops, their types along with examples, and how they help in running code multiple times.

What are Python Loops?

Python loops are used to repeat a block of code multiple times. They help in performing tasks again and again without writing the same code repeatedly.

Python Loops

Python mainly provides two types of loops: for loop and while loop. It also supports nested loops, where one loop can be used inside another loop.

Python for Loop

The for loop is used to go through elements one by one in a sequence like a string, list, or tuple. It is commonly used when we know how many times we need to repeat the loop.

Python Loops

Syntax

The following is the syntax of Python for Loop:

For Loop Example

Let us take a look at a basic implementation of the for loop in Python:

Execute Now

Output:

1
2
3
4
5

Explanation:

Here, we have used a for loop to iterate through the numbers ranging from 1 to 5. In each iteration, the current number is stored and printed.

Iterating over a Sequence using for Loop

The for loop is used to go through each element of a sequence one by one, such as a string, list, or tuple. It allows us to access and work with each item in the sequence without using indexing.

Example

In the following example, we are using the for loop to iterate over different sequences like a string, list, tuple, and dictionary, and printing their elements one by one.

Execute Now

Output:

T
p
o
i
n
t
 
T
e
c
h

Welcome
to
Tpoint
Tech

Python
for
Beginners

1 : Tpoint
2 : Tech

Explanation:

In this example, we have used the for loop to traverse different types of sequences such as string, list, tuple, and dictionary.

Iterating by the Index of Sequence

We can also iterate over a sequence using the index of its elements. In this method, we first find the length of the sequence and then use range() to access each element by its index.

Example

In the following example, we are using the index to iterate over a list and print both the index and the corresponding element.

Execute Now

Output:

0 : apple
1 : banana
2 : orange
3 : mango
4 : kiwi

Explanation:

Here, we have used the len() function to determine the length of the given sequence and then used the range() function to generate the index values ranging from 0 to the length of the list minus 1. The for loop then prints both the index and the corresponding element.

Use of else Statement with for Loop

In Python, we can also use the else statement with the for loop. The for loop does not consist of any condition on the basis of which the execution will stop. Thus, the code inside the else block will only run once the for loop finishes iterations.

Syntax

The syntax of the else statement with Python for loop is shown below:

Example

In the following example, we are using a for loop to print all items of a list, and once the loop completes, the else block is executed.

Execute Now

Output:

guava
apple
orange
mango
banana
melon
Welcome to else Block.

Explanation:

In this example, we have combined the else statement with the for loop. Since the for loop does not have any conditions, it iterated over the entire list and then executed the else block.

Python while Loop

A while loop is used to run a block of code repeatedly as long as a condition is True. It is useful when we do not know in advance how many times the loop will run.

Python Loops

Syntax

It has the following syntax:

Example

In the following example, we are using a while loop to print a message multiple times until the counter reaches 5.

Execute Now

Output:

Tpoint Tech
Tpoint Tech
Tpoint Tech
Tpoint Tech
Tpoint Tech

Explanation:

In this example, we have used the while loop to print a statement five times. We have initialized a counter with an initial value of 1. We then define a condition in the while loop to iterate the loop as long as the counter is less than or equal to 5. Inside the loop statement, we have incremented the counter value by 1.

Use of else Statement with while Loop

Similar to the for loop, we can also use the else statement with a while loop. The else block runs when the loop condition becomes False and the loop finishes normally. It will not run if the loop is stopped using a break statement.

Syntax

The syntax of the else statement with Python for loop is shown below:

Example

In the following example, we are using a while loop to print all elements of a list, and once the loop completes, the else block is executed.

Execute Now

Output:

guava
apple
orange
mango
banana
melon
Welcome to else Block.

Explanation:

In this example, we have combined the else statement with the while loop. Since we have not used any break statement, the while loop runs until the given condition becomes false. After that, it will execute the print statement from the else block.

Infinite while Loop

An infinite loop is a loop that runs forever because its condition always remains True. This can happen when the loop condition never changes.

Example

In the following example, we are creating an infinite loop because the condition always remains True and the counter is never updated.

Execute Now

Output:

Tpoint Tech
Tpoint Tech
Tpoint Tech
Tpoint Tech
...

Explanation:

In this example, the variable counter is set to 0, and the condition counter == 0 always stays True. Since the value of counter never changes, the loop keeps running and printing the statement again and again.

Note: Avoid using infinite loops unless necessary, as they do not stop on their own and may require manual interruption.

Python Nested Loops

A nested loop is a loop inside another loop. In Python, we can use both for and while loops inside each other. Nested loops are useful when working with data like tables, grids, or when repeating tasks inside another loop.

Syntax

The following is the syntax of the Python nested for loop:

The syntax of the Python nested while loop is given below:

We can also use one type of loop inside another other type of loop in Python. For example, we can use a while loop inside a for loop or vice versa.

Example 1: Nested while Loops

In the following example, we are using nested while loops to print a pattern of numbers.

Execute Now

Output:

1  
1 2  
1 2 3  
1 2 3 4  
1 2 3 4 5  

Explanation:

In this example, we use a nested for loop to print "*" multiple times in each row, where the number of times it prints increases with each iteration of the outer loop (based on the value of i).

Example 2: Nested for Loops

In the following example, we are using nested for loops to print a pattern of numbers.

Output:

1  
1 2  
1 2 3  
1 2 3 4  
1 2 3 4 5  

Loop Control Statements in Python

In Python, loop control statements are used to change how a loop runs. They allow us to stop the loop, skip an iteration, or control the flow of execution inside the loop.

Python provides the following loop control statements:

Loop Control StatementDescription
ContinueIt returns the control to the beginning of the loop
BreakIt terminates the loop immediately, even if the condition or sequence is not finished.
PassIt acts as a placeholder that does nothing.

Example

Let us take a look at the following example demonstrating the use of these control statements in Python.

Execute Now

Output:

Example with continue statement:
1
2
4
5

Example with break statement:
1
2
3

Example with pass statement:
1
3

Explanation:

In this example, we have used the different loop control statements. The continue statement is used to skip the iteration when i = 3, so 3 is not printed. The break statement is used to terminate the loop when i = 4. The pass statement here did nothing and simply acted as a placeholder.


Next TopicPython For Loop