Pythondex

Tutorial

For Loops in Python

A for loop in Python allows you to execute a block of code multiple times. It is commonly used to iterate over a sequence of numbers.

Syntax

Example

Explanation

  • range(1, 6) generates numbers 1, 2, 3, 4, 5
  • The variable i takes each value in the range one by one
  • The indented code block runs for each value of i