Python for loops are one of the most popular type of loops used to execute a block of statements repeatedly. When you are working with data the for loops are a powerful method used for iterating over a sequence.
Python for loops can be of great use when we know exactly the number of iterations we need. In this blog, we will learn more about the workflow of Python for loops with some examples.
Python For Loops Syntax
The Python for loops is similar to the other loops where we put the condition after the for keyword.
for variable in iterable:
#code block to execute |
variable: A variable is a value that each element takes in the sequence on each iteration.
iterable: A sequence like a list, tuple, string, or range which the loop will iterate over.
Let us check a simple example to print all the cars name present in a list named as “cars”
cars = [“Honda”, “Amaze”, “City”, “Mahindra”, “Kia”]
for x in cars print (x) |
Examples of Python For Loops
Let us move forward with some simple examples of Python loops below.
1. Use break statement to break out of for loop after iterator value becomes equal to 5.
for i in range(10):
if i == 5: print(“Breaking the loop at”, i) break print(i) |
2. Write a program to find the sum of elements in a list using the for loop.
numbers = [1, 2, 3, 4, 5]
total = 0 for number in numbers: total += number print(“Sum of numbers:”, total) |
3. Write a program to count specific elements in a list. In this example, we will count the number of red data in the list.
colors = [‘red’, ‘blue’, ‘red’, ‘green’, ‘red’]
count_red = 0 for color in colors: if color == ‘red’: count_red += 1 print(“Number of ‘red’ in the list:”, count_red) |
4. Write a program to print the factorial of a number using the for loop.
n = 5
factorial = 1 for i in range(1, n + 1): factorial *= i print(f”Factorial of {n} is {factorial}”) |
Flowchart of Python For Loops
Let us understand the flow of Python for loops with this flowchart given below.
- Step 1: The program begins with the for loop starting with the first item in the iterable.
- Step 2: The loop checks whether or not there are more items to iterate through in the iterable.
- Step 3: The loop executes the current value if there are more items in the body.
- Step 4: After the loop executes, the loop moves to the next item in the iterable i,e. List, tuple, dictionary, etc.
- Step 5: We repeat steps 2 to 4 for each item in the iterable unless a break or continue statement is used.
- Step 6: The program moves to the next section of the code once each item in the loop is iterated.
Read More: Python Scripting: Automate your Workflow using Python Scripting
Using range( ) Method In For Loop
The range( ) function is used with for loops to generate a sequence of numbers for a specific number of times.
for i in range(5):
print(i) |
The for loop here will run for five times where it will start with 0 and ends up to 4. The for loop iterates over this sequence and prints each number in the output. In the range() method you can also specify the start, stop and step for range().
for i in range(2, 10, 2):
print(i) |
Here, the numbers starting from 2, up to but not including 10 with each step of 2.
Using Python For Loops With Break & Continue
Let us use break and continue statements with for loops to exit the loop or continue the loop.
The Break
The break statement can be used with Python for loops to exit the loop after a certain condition is met.
for i in range(10):
if i == 5: print(“Breaking the loop at”, i) break print(i) |
The Continue
The continue statement allows you to skip the rest of the code inside the loop of the current iteration and move to the next iteration.
for i in range(10):
if i % 2 == 0: continue # Skip even numbers print(i) |
Working of Python For Loops
The Python for loop can be used to iterate over a sequence and execute a block of code repeatedly for each item in the sequence. It is one of the most commonly used control flow structures in Python programming. The working of Python for loops consists of three major concepts.
for variable in iterable:
#body of the loop |
- Iterable: This consists of the iterable i,e. List, strings, tuple, dictionary, set, and more where the loop will iterate over. The object must support iteration for loops to work.
- Variable: The loop variable takes the value of the current item in the iterable after each iteration.
- Loop body: The block of code that will be executed for each item in the iterable.
Infinite Condition for Python For Loops
An infinite loop condition in Python for loops is a condition in which loop never stop as there are no stopping conditions. In Python, for loops are rarely infinite but in Python you need additional methods to allow execution of the loops for infinite duration.
1. Using iter( ) with a sentinel value
We can run Python for loops for infinite duration by using the iter() function which creates an iterator and is used with a sentinel value.
# Infinite loop using iter() with a sentinel
for _ in iter(int, 1): print(“This will also run forever!”) |
2. Using for loop with a large Range
We can use the Python loop to create an infinite loop using the range() function without an upper bound.
# Infinite loop using range
for i in range(0, 1000000000): # Very large range print(“This will keep going!”) |
Read More: Top 5 Python Internships To Apply in April 2025
How to Stop an Infinite Python For Loop?
You can stop an infinite running loop using two methods given below.
- Press “Ctrl + C” to generate an interrupt in the terminal where Python code is running to stop the loop.
- You can also use a break statement to stop the Python for loops.
You can also stop a Python loop manually by clicking on the stop button given in the code editor you are using on your device.
Nested For Loops In Python Language
In Python, for loop inside another for loop makes a nested for loop structure. This structure helps you to iterate over multi-dimensional data such as matrices, grids, lists, and more.
Let us get a simple syntax of nested Python for loops below.
for outer_variable in outer_sequence:
for inner_variable in inner_sequence: # Code to execute for each inner iteration |
The outer loop in the above syntax run onces for each item in the outer sequence and the inner loop runs once for each item in the inner sequence for each iteration of the outer loop.
Let us understand the nested loop with an example where we want to print the multiplication table for numbers 1 to 5 using a nested for loop.
# Outer loop iterates through numbers 1 to 5
for i in range(1, 6): # Inner loop iterates through numbers 1 to 5 for j in range(1, 6): print(f”{i} * {j} = {i * j}”, end=”\t”) # \t adds a tab space for formatting print() |
Learn Python Programming with PW Skills
Become a Python programmer with PW Skills Decode DSA With Python Course. Here you can build your programming skills with Python programming along with the knowledge of data structures and algorithm using Python.
Get interactive classes, recorded videos, practice exercises and hands on training projects within this self paced course. Prepare for your job ready career in Python programming with our dedicated mentors and doubt support throughout the course duration. Get course certification after completing all assessments and quizzes in the course.
Python For Loops FAQs
Q1. What are Python for loops?
Ans: A for loop in Python is a control flow statement which is used to repeatedly execute a given block of statements until the condition is satisfied in the loop.
Q2. Can Python for loop be infinite?
Ans: Yes, Python for loops can be made infinite by using the iter() with sentinel value or by giving a large value to the upper limit of the range() method.
Q3. Can we use break and continue statements with Python for Loops?
Ans: Yes, we can use both break and continue statement with Python to execute the loop by moving over a value or break out from the loop.
Q4. What are 3 loops in Python?
Ans: In Python, a while loop, for loops and nested loops can be used to repeat a set of statements over and over again until a given condition is met.