Syntax of For Loop
2 min read ·
The
for loop syntax defines how Python iterates over a sequence and executes a block of code for each element.foris a keywordvariableholds one item at a timesequencecan be a list, string, tuple, dictionary, or range- Indentation defines the loop body
Simple Syntax Example
- Loop runs 5 times
- Values are taken one by one from
range(5)
Syntax with List
Syntax with String
Syntax with Tuple
Syntax with Dictionary
Flow of for Loop Syntax
- Python picks the first item from the sequence
- Assigns it to the variable
- Executes loop body
- Moves to the next item
- Stops when sequence ends
Incorrect Syntax Examples
Missing Colon ❌
Missing Indentation ❌
Stop
Colon and indentation are mandatory in for loop syntax.
Real World Example
Real World Scenario
Printing marks of students