Skip to content

Beginner's Tutorial on For Loops

Introduction

Welcome to our beginner's tutorial on for loops in Python. For loops are very important in programming. They let you run a block of code many times, based on a sequence. This tutorial will give you a strong understanding of for loops. It will cover their structure, common uses, and key ideas you need to know.

Prerequisites

Before you start learning about for loops, it's important to know the basics of:

Variables and data types

Functions and basic syntax

Print statements

What is a For Loop?

A for loop controls the flow of a program. It lets you move through a sequence, such as a list, tuple, string, or range. For each item in the sequence, the for loop runs a block of code.

Syntax

Here's the basic syntax for a for loop in Python:

python
for item in sequence:  
    # Block of code to execute for each item  
    print(item)

Example 1: Iterating Over a List

php
fruits = ['apple', 'banana', 'cherry']  
  
for fruit in fruits:  
    print(fruit)

Output:

apple
banana
cherry

In this example, the for loop iterates over each item in the fruits list, and the print statement prints each fruit.

Example 2: Iterating Over a Range

The range() function in Python generates a sequence of numbers. You can use it with for loops to execute a block of code a specific number of times.

python
for i in range(5):  
    print(i)

Output:

0
1
2
3
4

In this example, the range(5) generates numbers from 0 to 4, and the for loop prints each number.

Example 3: Iterating Over a String

You can also use for loops to iterate over strings.

python
greeting = "Hello, World!"  
  
for char in greeting:  
    print(char)

Output:

H
e
l
l
o
,

W
o
r
l
d
!

In this example, the for loop iterates over each character in the greeting string, and the print statement prints each character.

Nested For Loops

You can nest for loops inside each other to create more complex iterations.

python
for i in range(3):  
    for j in range(2):  
        print(f"i = {i}, j = {j}")

Output:

i = 0, j = 0
i = 0, j = 1
i = 1, j = 0
i = 1, j = 1
i = 2, j = 0
i = 2, j = 1

In this example, the outer loop runs 3 times, and for each iteration of the outer loop, the inner loop runs 2 times.

Breaking and Continuing the Loop

You can use the break and continue statements to control the flow of the loop.

break: Terminates the loop and exits.
continue: Skips the current iteration and moves to the next.

Using break

python
for i in range(10):  
    if i == 5:  
        break  
    print(i)

Output: 0 1 2 3 4

Using continue

python
for i in range(10):  
    if i % 2 == 0:  
        continue  
    print(i)

Output: 1 3 5 7 9

Enumerate Function

The enumerate() function is useful when you need both the index and the value while iterating over a sequence.

fruits = ['apple', 'banana', 'cherry']

python
for index, fruit in enumerate(fruits):  
    print(f"Index: {index}, Fruit: {fruit}")

Output:
Index: 0, Fruit: apple
Index: 1, Fruit: banana
Index: 2, Fruit: cherry

Conclusion

Congratulations! You have finished the beginner's tutorial on for loops in Python. For loops are a strong tool for going through sequences and doing tasks repeatedly. Keep practicing with various examples and look into more complex uses as you keep learning Python.