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.
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.

The for loop follows this syntax:
Here,
The image below shows the working of a for loop.

In the following examples, we will learn how to use the for loop in different ways to perform various tasks.
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.
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.
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:
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.
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.
The nested for loop has the following syntax:
In the following example, we are using a nested for loop to iterate through each element of a matrix and print its values.
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.
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.
Output:
Enter number of rows: 5
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
Explanation:
In this example, we are have created a star pyramid using the nested for loop.
The break statement is used to stop the loop immediately when a condition is met.
In the following example, we are stopping the loop when the value becomes "Mahindra".
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".
The continue statement is used to skip the current iteration and move to the next iteration.
In the following example, we are skipping the value "Mahindra" and continuing the loop.
Output:
Tata Honda BMW
Explanation:
In this example, we used the continue statement to skip the current iteration of the 'for' loop.
The pass statement is used as a placeholder when we do not want to write any code for a condition.
In the following example, we are using pass as a placeholder for numbers divisible by 3.
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.
The else block runs after the loop completes all its iterations.
In the following example, we are printing numbers using a loop and then executing the else block after completion.
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.
We request you to subscribe our newsletter for upcoming updates.