Python For Loop

Let’s look at for loop

  1. Notice

    A for loop takes successive values from an iterable. The body runs once for each value it receives.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
items = 'tea'
for item in items:
    print(item)
Live state

Top-level scope

items→Object 1
item→Object 4
Object 1 · str'tea'Object 4 · str'a'
This step

A for loop takes successive values from an iterable. The body runs once for each value it receives.

One item at a time

A for loop asks the iterable for the next item. It stops when there are no items left.

Image brief

Alt text
A for loop over tea visits t, e, and a in order; the loop body runs once for each character and ends after the final character.
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python For Loop". Teaching focus: A for loop asks the iterable for the next item. It stops when there are no items left. Depict exactly the relationships and labels in this reference diagram: items = "tea" NEXT ITEM --> "t" --> LOOP BODY NEXT ITEM --> "e" --> LOOP BODY NEXT ITEM --> "a" --> LOOP BODY NO ITEMS LEFT --> CONTINUE Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A for loop over tea visits t, e, and a in order; the loop body runs once for each character and ends after the final character.

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
items = 'tea'
for item in items:
    print(item)
Live state

Top-level scope

items→Object 1
item→Object 4
Object 1 · str'tea'Object 4 · str'a'
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
t
e
a

What do you expect this program to print?

What does this example display?

items = 'tea'
for item in items:
    print(item)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

t e a

Follow the printed values in order. The output is: t e a

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

A for loop takes successive values from an iterable. The body runs once for each value it receives.

A for loop takes successive values from an iterable. The body runs once for each value it receives.

For Loop

A for loop in Python allows you to iterate over a sequence—like a list, string, tuple, or range — and execute a block of code for each item in that sequence. Unlike traditional C-style for-loops, Python takes a more readable and elegant approach.

Basic Syntax

for variable in sequence:
    # block of code

Here, variable takes the value of each item in sequence, one by one, and executes the indented block.

Example 1: Looping Over a List

Each item in the list is printed on a new line. The loop automatically stops after the last element.

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

Example 2: Using Range with For Loop

range(5) generates numbers from 0 to 4. If you need to include 5, use range(6). Reference: range() bulitin function.

for i in range(5):
    print("Step", i)
Step 0
Step 1
Step 2
Step 3
Step 4

Example 3: Looping Through a String

Strings are iterable in Python—each character is treated as an element in the sequence.

for char in "Python":
    print(char)
P
y
t
h
o
n

Example 4: Using range(start, stop, step)

This version of range lets you control the start, stop, and increment values. It’s great for custom numeric patterns.

for i in range(2, 10, 2):
    print(i)
2
4
6
8

Example 5: Loop with else Clause

The else clause runs after the loop completes normally. It’s skipped if the loop is exited via break.

for i in range(3):
    print("Counting", i)
else:
    print("Loop finished!")
Counting 0
Counting 1
Counting 2
Loop finished!

Example 6: Nested For Loop

Nested loops let you work with multidimensional data or matrix-style problems.

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

Example 7: Breaking a Loop

The loop stops when num equals 5. Use break carefully—it ends the loop prematurely.

for num in range(10):
    if num == 5:
        break
    print(num)
0
1
2
3
4

Example 8: Skipping Iterations with continue

continue skips the current iteration and jumps to the next one.

for num in range(5):
    if num == 2:
        continue
    print(num)
0
1
3
4

Example: Using enumerate()

The enumerate() function is used to loop through the list names while keeping track of both the index and the value at the same time. In each iteration, index holds the position (0, 1, 2) and name holds the corresponding value from the list. This allows printing both index and name together.

names = ["Alice", "Bob", "Charlie"]
for index, name in enumerate(names):
    print(index, name)
0 Alice
1 Bob
2 Charlie

Tips and Gotchas

  • Always check indentation. Python uses indentation to define loop blocks.
  • Modifying the sequence you're iterating over can lead to unpredictable behavior.
  • Use range(len(list)) only if you need index values. Otherwise, loop directly over items.
  • Prefer enumerate() if you need both index and item.

What to remember

Connect what you noticed in the example with the idea below.

Remember

A for loop takes successive values from an iterable. The body runs once for each value it receives.

Watch out for this idea

A for loop must always count integers.

This is the misconception to avoid.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Python While Loop →