Python Loops

Let’s look at loops

  1. Notice

    A loop repeats a block. A for loop visits items from an iterable; each visit binds the loop name to the next item.

  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
total = 0
for number in range(3):
    total = total + number
print(total)
Live state

Top-level scope

total→Object 4
number→Object 3
Object 4 · int3Object 3 · int2
This step

A loop repeats a block. A for loop visits items from an iterable; each visit binds the loop name to the next item.

Picture loops

A loop repeats a block. A for loop visits items from an iterable; each visit binds the loop name to the next item.

Image brief

Alt text
A diagram for Python Loops. Instructions: total = 0 for number in range(3): total = total + number print(total). Displayed result: 3. The final example bindings are total --> object 4 (int); number --> object 3 (int). object 4: 3; object 3: 2
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python Loops". Teaching focus: A loop repeats a block. A for loop visits items from an iterable; each visit binds the loop name to the next item. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS total = 0 for number in range(3): total = total + number print(total) | v DISPLAYED RESULT 3 FINAL BINDINGS total --> object 4 (int) number --> object 3 (int) object 4: 3 object 3: 2 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 diagram for Python Loops. Instructions: total = 0 for number in range(3): total = total + number print(total). Displayed result: 3. The final example bindings are total --> object 4 (int); number --> object 3 (int). object 4: 3; object 3: 2

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
total = 0
for number in range(3):
    total = total + number
print(total)
Live state

Top-level scope

total→Object 4
number→Object 3
Object 4 · int3Object 3 · int2
This step

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

Terminal
Output
3

What do you expect this program to print?

What does this example display?

total = 0
for number in range(3):
    total = total + number
print(total)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

3

Follow the printed values in order. The output is: 3

Which explanation matches what you learned?

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

Answer and explanation

A loop repeats a block. A for loop visits items from an iterable; each visit binds the loop name to the next item.

A loop repeats a block. A for loop visits items from an iterable; each visit binds the loop name to the next item.

Loops

Loops help us repeat actions in Python. Imagine telling the computer: "Do this again and again." That’s what loops are for!

Why Use Loops?

Let’s say you want to print "Hi!" five times. Without a loop, you’d write:

print("Hi!")
print("Hi!")
print("Hi!")
print("Hi!")
print("Hi!")
Hi!
Hi!
Hi!
Hi!
Hi!

That works… but what if you wanted to print it 100 times? That’s where loops save time.

Types of Loops in Python

  • for loop – repeats a certain number of times
  • while loop – repeats while a condition is true

1. for Loop

A for loop is useful when you know in advance how many times you want to repeat a block of code. It’s commonly used to iterate over sequences like lists, strings, or ranges of numbers.

Syntax

for variable in sequence:
    # code to run for each item in the sequence
  • for: The loop keyword that starts the iteration.
  • variable: A temporary variable that takes the value of each item in the sequence, one at a time.
  • in: A keyword that links the loop variable with the sequence to iterate over.
  • sequence: Any iterable object like range(), a list, or a string.
  • Indented block: The code under the loop that will execute once per iteration.

Example

for i in range(5):
    print("Hi!")
Hi!
Hi!
Hi!
Hi!
Hi!

Explanation:

  • range(5) generates a sequence of numbers from 0 to 4 (5 numbers in total).
  • The variable i takes on each of those values — first 0, then 1, and so on up to 4.
  • For each value of i, the line print("Hi!") is executed once.
  • As a result, "Hi!" is printed 5 times.

2. while Loop

A while loop in Python is used to repeatedly execute a block of code as long as a specified condition remains True.

Syntax

while condition:
    # code to repeat
  • while: The keyword that starts the loop.
  • condition: An expression that is evaluated before each iteration. The loop runs as long as this condition is True.
  • :: Indicates the start of the loop block.
  • Indented block: The code inside the loop that gets executed repeatedly.

Example

count = 1
while count <= 3:
    print("Welcome!")
    count = count + 1
Welcome!
Welcome!
Welcome!

Explanation:

  • count is initialized to 1.
  • The condition count <= 3 is checked before each iteration.
  • If the condition is True, the loop body runs — printing "Welcome!" and then increasing count by 1.
  • This continues for count = 1, 2, and 3.
  • When count becomes 4, the condition count <= 3 becomes False, so the loop stops.

Looping Through a List

A for loop in Python can be used to iterate over elements in a list — executing a block of code once for each item.

Example

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

Explanation:

  • A list named fruits is defined with three string elements: "apple", "banana", and "cherry".
  • The for loop goes through each element in the list one by one.
  • On each iteration, the variable fruit holds the current list item.
  • The statement prints the current value of fruit.
  • As a result, each fruit name is printed on a new line.

Using break in a Loop

break stops the loop early.

for i in range(5):
    if i == 3:
        break
    print(i)
0
1
2

Explanation:

  • When i becomes 3, the break line runs and stops the loop.
  • So 3 and 4 are never printed.

Using continue in a Loop

continue skips the rest of the loop for that time only.

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

Explanation:

  • When i is 2, it skips print(i).
  • Everything else runs normally.

What to remember

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

Remember

A loop repeats a block. A for loop visits items from an iterable; each visit binds the loop name to the next item.

Watch out for this idea

range(n) includes n as its last value.

This is the misconception to avoid.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Python For Loop →