Python for Loop

Last Updated : 14 Apr 2026

Python for loop is used to go through elements of a sequence one by one. In this chapter, you will learn about the Python for loop, how it works, and how to use it to iterate over different types of data.

What is Python for Loop?

The for loop is used to iterate over iterable objects like lists, tuples, strings, and dictionaries. It runs a block of code for each element in the sequence until all elements are processed.

Python for Loop

Syntax of for Loop

The for loop follows this syntax:

Here,

  • var_name: A variable that takes the value of each element in the sequence one by one.
  • sequence: A collection of items such as a list, tuple, string, or dictionary that we want to iterate over.

Flowchart (Working) of for Loop

The image below shows the working of a for loop.

Python for Loop
  • Step 1: The for loop starts and takes the first item from the sequence.
  • Step 2: It executes the given block of code for that item and then moves to the next item.
  • Step 3: This process continues until all items in the sequence are processed.
  • Step 4: Once the last item is reached, the loop stops.

Examples of for Loop

In the following examples, we will learn how to use the for loop in different ways to perform various tasks.

Example 1: Printing Fibonacci Series

We will now take a look at an example to print the factorial of a number. For this Python program, we will use the 'for' loop to iterate through each number from 1 to that number and add them to return the factorial of the number.

Compile and Run

Output:

Enter a Number: 5
5! = 120

Explanation:

In this example, we have used the 'for' loop to iterate through the range from 2 to that number and multiply the value from the current iteration with the initialized factorial value. As a result, we calculated the factorial of the number.

Example 2: Printing Elements from a List or Tuple

Lists and Tuples are the data structures used in Python to store multiple items in a single variable. We can use the 'for' loop to iterate through each element of these sequential data structures.

Let us take a look at the following example:

Compile and Run

Output:

Tata
Honda
Mahindra	
Suzuki
BMW

Explanation:

In this example, we are given a list. We used the 'for' loop to iterate through element of the list and printed them.

Nested for Loop

A nested for loop means a for loop inside another for loop. It is commonly used when working with multi-dimensional data, creating patterns, or performing tasks that need multiple levels of iteration.

Syntax

The nested for loop has the following syntax:

Example: Printing the Elements of the Matrix

In the following example, we are using a nested for loop to iterate through each element of a matrix and print its values.

Compile and Run

Output:

Given Matrix:
13 4 27 
22 16 8 
5 11 19

Explanation:

In the above example, we are given a 3x3 matrix. We used the nested for loop to iterate through the rows and columns in the given matrix and print the elements.

Example: Pyramid Pattern Printing

In the following example, we are using nested for loops to print a pyramid pattern of stars based on the number of rows entered by the user.

Compile and Run

Output:

Enter number of rows: 5
         * 
      * * * 
    * * * * * 
  * * * * * * * 
* * * * * * * * *

Explanation:

In this example, we are have created a star pyramid using the nested for loop.

Using break With for Loop

The break statement is used to stop the loop immediately when a condition is met.

Example

In the following example, we are stopping the loop when the value becomes "Mahindra".

Compile and Run

Output:

Tata
Honda

Explanation:

In the above example, we used the break statement in the 'for' loop to stop the iterations when the current iteration value is "Mahindra".

Using continue With for Loop

The continue statement is used to skip the current iteration and move to the next iteration.

Example

In the following example, we are skipping the value "Mahindra" and continuing the loop.

Compile and Run

Output:

Tata
Honda
BMW

Explanation:

In this example, we used the continue statement to skip the current iteration of the 'for' loop.

Using pass With for Loop

The pass statement is used as a placeholder when we do not want to write any code for a condition.

Example

In the following example, we are using pass as a placeholder for numbers divisible by 3.

Compile and Run

Output:

1
2
4
5
7
8
10

Explanation:

In this pass statement in for loop example, the pass statement is a placeholder indicating that a piece of code can be added in the if-block in the future.

Using else With for Loop

The else block runs after the loop completes all its iterations.

Example

In the following example, we are printing numbers using a loop and then executing the else block after completion.

Compile and Run

Output:

1
2
3
4
5
6
7
8
9
Loop Finished

Explanation:

In this example, the else statement is execute after the completion of the 'for' loop.