Bird
Raised Fist0
Pythonprogramming~10 mins

For loop execution model in Python - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - For loop execution model
Start
Get next item from iterable
Is item available?
NoExit loop
Yes
Execute loop body with item
Repeat: Get next item
The for loop gets each item from a list (or other iterable), runs the loop body with that item, and repeats until no items remain.
Execution Sample
Python
for num in [1, 2, 3]:
    print(num)
This code prints each number in the list one by one.
Execution Table
Iterationnum valueCondition (item available?)ActionOutput
11YesPrint num=11
22YesPrint num=22
33YesPrint num=33
4NoneNoExit loop
💡 No more items in the list, loop ends
Variable Tracker
VariableStartAfter 1After 2After 3Final
numNone1233
Key Moments - 3 Insights
Why does the loop stop after printing 3?
Because after the third item, there are no more items in the list. The condition 'item available?' becomes No, so the loop exits (see execution_table row 4).
Is the variable 'num' still set after the loop ends?
Yes, after the loop finishes, 'num' retains the value of the last item, 3 (see variable_tracker final value is 3).
What happens if the list is empty?
The loop body never runs because the condition 'item available?' is No immediately, so it exits without printing anything.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'num' during iteration 2?
A1
B2
C3
DNone
💡 Hint
Check the 'num value' column in execution_table row for iteration 2
At which iteration does the loop condition become false?
AIteration 4
BIteration 1
CIteration 3
DNever
💡 Hint
Look at the 'Condition (item available?)' column in execution_table
If the list was empty, what would the execution table show for iteration 1?
Anum=1, Condition Yes, Print num=1
Bnum=0, Condition Yes, Print num=0
Cnum=None, Condition No, Exit loop
DNo iteration rows at all
💡 Hint
Think about what happens when no items are available at the start (see key_moments)
Concept Snapshot
For loop syntax:
for variable in iterable:
    body

Behavior:
- Gets each item from iterable
- Runs body with item
- Stops when no items left

Key rule: Loop variable changes each iteration to next item.
Full Transcript
A for loop in Python takes each item from a list or other iterable one by one. It assigns the item to a variable, then runs the code inside the loop using that variable. This repeats until there are no more items. For example, looping over [1, 2, 3] prints each number. The loop stops after the last item because the condition to get a new item fails. The loop variable retains the value of the last item after the loop ends. If the list is empty, the loop body never runs.

Practice

(1/5)
1. What does a for loop do in Python?
easy
A. Repeats a block of code for each item in a sequence
B. Runs code only once
C. Stops the program immediately
D. Creates a new variable

Solution

  1. Step 1: Understand the purpose of a for loop

    A for loop runs code repeatedly for each item in a list, string, or range. 'Repeats a block of code for each item in a sequence' matches this; others do not.
  2. Final Answer:

    Repeats a block of code for each item in a sequence -> Option A
  3. Quick Check:

    For loop = repeat for each item [OK]
Hint: For loops repeat actions for every item in a list [OK]
Common Mistakes:
  • Thinking for loops run only once
  • Confusing for loops with if statements
  • Believing for loops stop the program
2. Which of the following is the correct syntax for a for loop in Python?
easy
A. for (i=0; i<5; i++) print(i)
B. for i to range(5) print(i)
C. for i in range(5): print(i)
D. foreach i in range(5): print(i)

Solution

  1. Step 1: Recall Python for loop syntax

    Python uses 'for variable in sequence:' followed by indented code. 'for i in range(5): print(i)' matches; others use wrong keywords or styles.
  2. Final Answer:

    for i in range(5): print(i) -> Option C
  3. Quick Check:

    Python for loop = for variable in sequence: [OK]
Hint: Use 'for variable in sequence:' syntax in Python [OK]
Common Mistakes:
  • Using 'to' instead of 'in'
  • Using C-style for loop syntax
  • Using 'foreach' which is not Python
3. What is the output of this code?
for i in range(3):
    print(i * 2)
medium
A. 2 4 6
B. 0 1 2
C. 1 2 3
D. 0 2 4

Solution

  1. Step 1: Trace the loop range and output

    range(3) gives i=0,1,2. Prints i*2: 0, 2, 4.
  2. Final Answer:

    0 2 4 -> Option D
  3. Quick Check:

    Multiply each i by 2 = 0 2 4 [OK]
Hint: Multiply loop variable by 2 for each iteration [OK]
Common Mistakes:
  • Printing i instead of i*2
  • Starting count from 1 instead of 0
  • Confusing range(3) with range(1,3)
4. Find the error in this code:
for i in range(5)
    print(i)
medium
A. Indentation error on print line
B. Missing colon ':' after range(5)
C. range(5) is invalid syntax
D. Variable i is not defined

Solution

  1. Step 1: Check for syntax errors in for loop

    Python requires ':' after the for statement. The code misses ':' after range(5).
  2. Final Answer:

    Missing colon ':' after range(5) -> Option B
  3. Quick Check:

    For loop needs ':' after header [OK]
Hint: Always add ':' after for loop header [OK]
Common Mistakes:
  • Forgetting colon ':' after for statement
  • Incorrect indentation
  • Assuming range(5) is invalid
5. You want to create a list of squares for numbers 0 to 4 using a for loop. Which code correctly does this?
hard
A. squares = [] for i in range(5): squares.append(i*i) print(squares)
B. squares = [i*i for i in range(5)] print(squares)
C. squares = [] for i in range(5): squares = i*i print(squares)
D. for i in range(5): squares.append(i*i) print(squares)

Solution

  1. Step 1: Identify correct code for list of squares using for loop

    Initialize squares = [], then for i in range(5): append i*i, then print. squares = [] for i in range(5): squares.append(i*i) print(squares) works; others miss init, use comp, or overwrite.
  2. Final Answer:

    squares = []\nfor i in range(5):\n squares.append(i*i)\nprint(squares) -> Option A
  3. Quick Check:

    Initialize list, append squares in loop [OK]
Hint: Initialize list before loop, append inside loop [OK]
Common Mistakes:
  • Not initializing list before appending
  • Overwriting list variable inside loop
  • Using list comprehension but question asks for for loop