← Back to Chapters

Python For Loops

? Python For Loops

⚡ Quick Overview

In Python, a for loop is used to iterate over a sequence such as a list, tuple, string, or a range of numbers. For each item in the sequence, the loop body is executed once with the loop variable set to that item.

For loops are essential for tasks like processing collections of data, repeating actions a fixed number of times, and working with multi-dimensional structures using nested loops.

? Key Concepts

  • Iterable: Any object you can loop over (lists, tuples, strings, ranges, etc.).
  • Loop variable: The variable that takes each value from the iterable in turn.
  • range(): A built-in function that generates a sequence of numbers.
  • Nested loops: A loop inside another loop, useful for grids, tables, and multi-dimensional data.
  • for-else: An optional else block that runs when the loop finishes normally (no break).

? Syntax and Theory

General syntax of a Python for loop:

? View Basic For Loop Syntax
for variable in iterable:
    # loop body
    # code that uses "variable"

On each iteration, variable receives the next item from iterable, and the loop body is executed. When there are no items left, the loop stops and execution continues after the loop.

? Code Examples

? Basic For Loop over a List

A for loop iterating over a list of fruits:

? View Code Example
# define a list of fruits
fruits = ["apple", "banana", "cherry"]
# loop through each fruit and print it
for fruit in fruits:
    print(fruit)

? Using range() with For Loop

Looping through a sequence of numbers using range():

? View Code Example
for i in range(5):
    print(i)  # prints 0, 1, 2, 3, 4

? Nested For Loops

A for loop inside another for loop:

? View Code Example
# outer loop runs 3 times
for i in range(3):
    # inner loop runs 2 times for each i
    for j in range(2):
        print(f"i={i}, j={j}")

? For Loop with Else

The else block runs only if the loop completes all iterations without hitting a break statement.

? View Code Example
# loop from 0 to 2
for i in range(3):
    print(i)
# runs because the loop finished without a break
else:
    print("Loop finished without break")

? Live Output and Explanation

? What These Loops Print

  • Fruits loop:
    Output:
    apple
    banana
    cherry
  • range(5) loop:
    Output:
    0
    1
    2
    3
    4
  • Nested loops: prints all pairs (i, j) where i is 0–2 and j is 0–1, e.g. i=0, j=0, i=0, j=1, …, i=2, j=1.
  • for-else loop: prints 0, 1, 2, and then "Loop finished without break" because the loop completes normally.

✅ Tips & Best Practices

  • Use for loops when iterating over sequences (lists, strings, ranges, etc.).
  • Use range(start, stop, step) to generate numeric sequences.
  • Nested loops are useful for working with multi-dimensional data structures like matrices or tables.
  • The else block can be used to run code if the loop completes without a break.
  • Avoid modifying a list while iterating over it; iterate over a copy instead if needed.
  • Remember the colon : at the end of the for line.
  • Be careful with range() boundaries to avoid off-by-one errors (remember it stops before the stop value).

? Practice Tasks

  • Write a for loop to print numbers from 1 to 10.
  • Use a for loop to iterate over a list of names and print each name in uppercase.
  • Create a nested loop to print a multiplication table for numbers 1–3.
  • Use a for-else loop to check if a number exists in a list. If not found, print a message like "Number not found".