Introduction

A for loop is used to execute a block of code repeatedly by iterating through a sequence, such as a list, tuple, string, or range of numbers.

Instead of writing the same code multiple times, we can use a for loop to make our programs shorter and easier to understand.

What Is a For Loop in Python?

A for loop is a control flow statement that allows us to iterate over a sequence of elements.

It executes the code inside the loop once for each item in the sequence.

For example, if we want to print numbers from 1 to 5, we can use a for loop instead of writing five separate print statements.

Basic Syntax of For Loop

The basic syntax of a for loop in Python is:

for variable in sequence: statements 

Explanation:

  • for: Keyword used to create a for loop.
  • variable: Stores the current item during each iteration.
  • in: Used to iterate through a sequence.
  • sequence: Collection of elements or a range of values.
  • statements: Block of code executed during each iteration.

Example of For Loop Using Range()

The range() function is commonly used with for loops to generate a sequence of numbers.

Example:

for i in range(1, 6): print(i) 

Output:

1 2 3 4 5 

Explanation:

  • range(1, 6) generates numbers from 1 to 5.
  • i takes each number one by one.
  • print(i) displays the current number.

The ending value 6 is not included in the range.

Different Ways to Use Range() in For Loop

Python's range() can be used in three ways.

1. Range with One Argument

When we provide only one argument, the sequence starts from 0.

Example:

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

Output:

0 1 2 3 4 

2. Range with Two Arguments

When we provide two arguments, the first value is the starting point and the second value is the ending point.

Example:

for i in range(1, 6): print(i) 

Output:

1 2 3 4 5 

3. Range with Three Arguments

When we provide three arguments, the third argument specifies the step value.

Example:

for i in range(2, 11, 2): print(i) 

Output:

2 4 6 8 10 

Here, the loop increases the value by 2 in each iteration.

For Loop with a String

We can use a for loop to access each character in a string.

Example:

name = "Python" for i in name: print(i) 

Output:

P y t h o n 

Here, the for loop accesses each character from the string one by one.

For Loop with a List

We can also use a for loop to iterate through the elements of a list.

Example:

fruits = ["Apple", "Banana", "Mango"] for fruit in fruits: print(fruit) 

Output:

Apple Banana Mango 

Here, the loop accesses each element from the list and prints it.

Nested For Loop in Python

A nested for loop means writing one for loop inside another for loop.

The inner loop executes completely for every iteration of the outer loop.

Example:

for i in range(1, 4): for j in range(1, 3): print(i, j) 

Output:

1 1 1 2 2 1 2 2 3 1 3 2 

Here, the outer loop runs three times, and the inner loop runs two times for each outer loop iteration.

Loop Control Statements in For Loop

Python provides three important statements to control the execution of a for loop.

1. Break Statement

The break statement is used to stop the loop immediately.

Example:

for i in range(1, 6): if i == 4: break print(i) 

Output:

1 2 3 

Here, when i becomes 4, the break statement stops the loop.

2. Continue Statement

The continue statement skips the current iteration and moves to the next iteration.

Example:

for i in range(1, 6): if i == 3: continue print(i) 

Output:

1 2 4 5 

Here, when i becomes 3, that iteration is skipped, and the loop continues.

3. Pass Statement

The pass statement does nothing. It is used as a placeholder when we want to write a loop without adding any statements yet.

Example:

for i in range(1, 6): if i == 3: pass print(i) 

Output:

1 2 3 4 5 

Here, the pass statement does not affect the execution of the loop.

Why Is For Loop Important in Python?

For loops are important because:

  1. They reduce code repetition.
  2. They help iterate through sequences easily.
  3. They save time and effort.
  4. They make programs shorter and easier to understand.
  5. They are useful for processing large amounts of data.