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.
  • for is a keyword
  • variable holds one item at a time
  • sequence can 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

  1. Python picks the first item from the sequence
  2. Assigns it to the variable
  3. Executes loop body
  4. Moves to the next item
  5. 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


Exercise