Python For-Loop

Python for loop is one of the most important tools in Python, helping you automate repetitive tasks and iterate over sequences like lists, strings, and ranges. If you're handling a collection of data or need to repeat an action multiple times, the for loop is your go-to solution.

Here are some common ways to use the for loop:

  • Iterating over a list: Loop through each item in a list to process or print it.
  • Iterating over a string: Loop through each character in a string.
  • Iterating over a range of numbers: Repeat an action a specific number of times using the range() function.
  • Nested loops: Use loops within loops to handle more complex data structures.


What You'll Learn

You will learn how to use the for loop in Python to iterate over lists, strings, ranges, and perform repetitive tasks. We’ll cover the basic syntax and give you real examples.


Understanding the for Loop

The basic syntax for the for loop is:

python
for item in iterable:
    # Code to execute for each item
  • item: This is the variable that takes the value of each element in the iterable.
  • iterable: The collection you want to loop through (like a list, string, or range).

The for loop will iterate over the iterable and execute the block of code for each element.


Example 1: Iterating Over a List

Before we jump into the example, let's quickly go over two things:

What is a List?

A list in Python is a way to store multiple items in one variable. Lists can hold any type of data, such as numbers, strings, or even other lists!
You can create a list by placing items inside square brackets [ ], with each item separated by a comma. For example:

python
my_list = [1, 2, 3, 4, 5]

This creates a list called my_list that holds five numbers.

We’ll use this list in the next example to demonstrate the for loop.

Now, let’s see Iterating Over a List using For-Loop. Here's an example:

python
numbers = [1, 2, 3, 4, 5]
for num in numbers:
    print(num)

How It Works:

  • The for loop goes through each element in the numbers list, one by one.
  • Each element is assigned to the variable num for each iteration of the loop.
  • The print(num) statement prints each value of num as the loop iterates through the list.

Output

1
2
3
4
5

Example 2: Nested Loops with Two Lists

Now, let’s iterate over each combination of numbers from two lists:

python
numbers1 = [1, 2, 3]
numbers2 = [1, 2, 3]

for num1 in numbers1:  # Outer loop iterates over the first list
    for num2 in numbers2:  # Inner loop iterates over the second list
        print(num1, num2)

How It Works:

  • The for loop iterates over each element of the numbers1 list, one by one.
  • The inner for loop goes through all elements of the numbers2 list for each item in numbers1.
  • The print function outputs each pair of numbers in the format (num1, num2).

Output

(1, 1)
(1, 2)
(1, 3)
(2, 1)
(2, 2)
(2, 3)
(3, 1)
(3, 2)
(3, 3)

Example 3: Iterating Over a String

Now, let’s iterate over each character in a string:

python
word = "Python"
for letter in word:
    print(letter)

How It Works:

  • The for loop goes through each character in the string "Python" and assigns each character to letter.
  • The print function prints each character on a new line.

Output

P
y
t
h
o
n

Exercises

Try out these exercises to practice your understanding of the for loop:

1. Write a for loop to print all numbers in the list: [10, 20, 30, 40, 50].
python
# Exercise 1: Print all numbers in the list
numbers = [10, 20, 30, 40, 50]
for num in numbers:
    print(num)

2. Write a for loop to print each character in the string: "Hello World".
python
# Exercise 2: Print each character in the string
word = "Hello World"
for letter in word:
    print(letter)

*Tip: After completing an exercise, feel free to experiment with the for loop to try other variations!


Frequently Asked Questions

What is a for loop in Python?

A for loop in Python is used to iterate over a sequence like a list, string, or range, executing a block of code for each item.


What can I iterate over using a for loop?

You can iterate over any iterable in Python — including lists, strings, tuples, sets, dictionaries, and range().


How is a for loop different from a while loop?

A for loop runs a set number of times or over a defined sequence, while a while loop runs as long as a condition is True.


Can I use a for loop with a range of numbers?

Absolutely! Use the range() function to loop through a sequence of numbers using a for loop.


How do I exit a for loop early?

Use the break statement inside your for loop to stop it immediately, even before finishing the iteration.



What's Next?

Now that you’ve mastered the basics of the for loop, you’re ready to take your iteration skills to the next level! In the next section, we’ll dive into the break statement. You’ll learn how to control the flow of your loops, stop the loop early, and handle more complex scenarios in your code.