For Loops in Python

Learn Python for loops with our clear guide! Explore syntax, examples, and tips to iterate over lists, strings, and more. Improve your coding skills today.

For loops are statements used to repeat a specific action multiple times. It is used to execute a block of code for each element of an iterable (List, Tuple, Set, String, Dictionary).

General Structure of Python For Loop

The general structure involves three main components:

  1. The loop variable

  2. The iterable, and

  3. The loop body.

The loop variable is assigned to each element of the iterable or sequence as the program processes each item. The iterable is essentially any object that contains multiple elements that can be accessed individually. The loop body, defined by an indented block of code, contains the specific actions, operations, or instructions that should be performed during each iteration. This setup allows for organized, step-by-step processing of sequences and makes it easier to automate repeated tasks in a clear and simple way.

General Syntax of Python For Loop

The general syntax consists of the for keyword, followed by a temporary iterator variable that takes on the value of each element in the sequence, the in keyword, and the iterable itself. This is followed by a colon (:) and an indented block of code that defines the actions to be performed for each item, providing a clear and efficient way to process collections of data or automate repetitive tasks.

Python

The code above demonstrates the basic syntax of the for loop in python, see some examples to demonstrate the for loop below.

Example Problems

Problem 1:

Write a for loop in Python that prints the numbers from 1 to 5, each on a new line.

Solution:

Python

Problem 2:
Write a for loop that prints each character in the string "Python".

Solution:

Python

Problem 3:
Write a for loop that prints the square of each number in the list [2, 4, 6, 8].

Solution:

Python

Problem 4:
Write a for loop that calculates the sum of all numbers from 1 to 100.

Solution:

Python

Problem 5:
Write a for loop that prints only the even numbers from 1 to 20.

Solution:

Python

The pass Keyword

In the context of a for loop, The pass keyword acts as a placeholder when no action is required for certain iterations, allowing the loop to continue running without interruption.

Examples use of the pass keyword

Problem 6:

Write a Python program that loops through numbers from 0 to 4, but does nothing inside the loop when the number is even. Print the number when it's odd.

Solution:

Python

Output:

The break Keyword

The break keyword is used to exit a loop prematurely. Normally, a loop runs until all items in the iterable have been fully processed, but there are times when you may need to exit the loop before reaching the last item.

Examples use of the pass keyword

Problem 7:
Given a list of numbers, write a for loop that searches for the number -1. Once -1 is found, the loop should stop immediately using the break keyword. If found, print "Found -1, stopping the loop." Otherwise, print each number.

Solution:

Python

Output:

The continue Keyword

The continue keyword is used to skip the body of the loop and move directly to the next item in the iterable. It is typically used when you want to skip a specific element or skip the current iteration if a certain condition is met. The purposes of the pass and continue keywords are often misunderstood, but a clear distinction is presented in the table below.

Problem 8:
Write a for loop that prints all the numbers from 1 to 10, except for the number 5. Use the continue keyword to skip over 5 without stopping the loop.

Solution

Python

Output:

Differences Between The Pass And Continue Keywords

Feature

continue

pass

Purpose

Skips the rest of the code in the current iteration and jumps to the next iteration of the loop.

Acts as a placeholder that does nothing; the loop continues normally.

Effect

Alters the flow of the loop by skipping part of the loop body.

Does not alter the flow; used to maintain syntax when no code is required.

Use Case

When you want to skip certain iterations based on a condition.

When you want to leave a block empty (e.g., to be implemented later or intentionally left blank).


Nested loops in python

Nested loops happen when you put one loop inside another loop. This means the inner loop runs completely for each time the outer loop runs once. You might want to use nested loops when you need to repeat actions in multiple steps or levels—like checking every cell in a grid, going through rows and columns in a table, or combining items from two lists. Nested loops help you handle these kinds of tasks easily by going through all possible pairs or groups.

Example Use of Nested Loops

Problem 9:

Write a Python program that uses nested for loops to print all pairs of numbers, where the outer loop ranges from 1 to 3 and the inner loop ranges from 1 to 2.

Solution:

Python

Output:

Ads