top of page

Python Control Flow: Repeating Tasks Using for Loop

The for loop is one of the most powerful features in Python — it allows us to repeat tasks and automate work efficiently.


Observe:


1 is a natural number

The square of 1 is 1  

The cube of 1 is 1


2 is a natural number

The square of 2 is 4  

The cube of 2 is 8


for Loop Python
Repetition

In the above examples, we are repeating the same set of statements for different natural numbers. Imagine if we had to write this for many more numbers—it would become tedious and repetitive.


Can we automate this using Python?

Yes! Python provides the for loop to handle such repetition easily.


Quick Links



#1: The for Loop or for Statement in Python


The for keyword is used to repeat things in a given sequence. The sequence can be a list, string, or tuple—any iterable.


Example:

things = [1, 2, 3] 
for thing in things: 
	print(thing)

The thing is called the loop (or control) variable, and things is a sequence. The two important keywords — for and in — are used to repeat the sequence items one by one until a condition is met, which is the end of the sequence in this example. The task print(thing) to be done is always indented.


Let us try this in notebook!

numbers = [1, 2, 3]
for number in numbers: 
	print(number)

You will get:

1

2

3


How it works?

numbers is a list with three elements — 1, 2, and 3. The for keyword tells Python to start looping (iterating) through a sequence. The line for number in numbers: checks the first element of the numbers list, fetches it, and assigns 1 to the variable number. Since this is valid, Python goes to the next indented line and prints number (which is 1).


The loop then continues, assigns the second element (2) to number, and prints it. Finally, it takes 3, prints it, and since the list ends there is nothing more to iterate. The loop terminates, and control moves to the next line of code outside the for loop (if any).


Use singular for loop variables (e.g., number) and plural for the sequence (e.g., numbers) for better readability.
The in keyword is used to loop through the sequence or list.



#2: The range() Function in Python


Python has a built-in function called range() that returns a sequence of numbers.


Example:

for number in range(3):
	print(number)

This will print:

0

1

2


Note: range(3) goes up to but not including 3. This is often called the off-by-one rule.


You can also use any loop variable name (like _, i, or n):


for i in range(3):
	print("I am learning Python.")

Or

for _ in range(3): 
	print("I am learning Python.")

fruits = ["Apple", "Banana"] 
for fruit in fruits: 
	print(f"{fruit} is a sweet fruit.") 
	print(f"I love eating {fruit.lower()}s.")

range() with More Control

You can specify:

  • start

  • stop (not included)

  • step (how much to increment by)


range(start, stop, step)

for number in range(1, 10, 2): 
	print(number)

This prints numbers from 1 to 9, skipping every second number.


Useful Built-in Functions for Lists


numbers = [-1, 10, 3.5] 
# Adds all 
print(sum(numbers)) 

# Finds the largest 
print(max(numbers)) 

# Finds the smallest
print(min(numbers)) 

#3: List Comprehensions


Breaking and Skipping Loops

You can control the loop using:

  • break — to exit the loop early.

  • continue — to skip the current iteration and move to the next.


List comprehensions are short Python expressions written inside square brackets that create a new list from an existing one.


new_list = [item for item in list]

# Example (uppercase version):

new_list = [item.upper() for item in list]


We just discussed and practiced a lot about the for loop statement for repeating tasks or code in Python. There are other control loop statements as well, like while, which we will discuss in the next post. Thank you for learning Python!


Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating

Most Read

AI Nine O Subscription

Subscribe to AI Nine O for 90-second learning.

Your question...

Venus will reply here...

VenusMoon Logo

Promoting equitable education for all through the fruitful use of AI.

© 2024-25 by VenusMoon Education | Udyam Registration Number: UDYAM-MP-10-0030480

bottom of page