Understanding for Loops

What is a For Loop?

A for loop is used to repeat a block of code a specific number of times. It is especially useful when working with collections like lists, strings, or ranges of numbers.


How Does a For Loop Work?

A for loop:


Basic Syntax

for variable in sequence:

    # Code to repeat


Example: Print numbers from 1 to 5

for number in range(1, 6):

    print(number)


Output:

1  

2  

3  

4  

5  



Flowchart of a For Loop