A for loop in Python is a control flow statement that is used to iterate over a sequence (like a list, tuple, dictionary, set, or string). It's commonly used when you have a block of code that you want to repeat a fixed number of times. Here's a simple lesson on how to use a for loop in Python.

Basic Syntax

The basic syntax of a for loop in Python is:

for variable in sequence:
    # Do something

Here, 'variable' is the name of the variable that takes the value of the item inside the sequence on each iteration. The 'sequence' is what you are iterating over.

The range() Function

If you want to repeat an action a certain number of times, you can use range() with a for loop.

for i in range(5):
    print(i)

Looping Through a String

You can also loop through each character in a string:

for char in "Hello":
    print(char)

This loop will print each character of the string "Hello" on a separate line.

*Later in this course, we will learn how to iterate over different sequence types, such as Lists .