Loops in Python

Python : Loops

Loops are used to execute a block of code repeatedly. There are two types of loops in Python: for loops and while loops.

  1. For Loops in Python

A for loop is used to iterate over a sequence (such as a list, tuple, or string) and execute a block of code for each item in the sequence.

The syntax for a for loop in Python is:

for item in sequence:
    # code block to execute for each item
  1. While Loops in Python

A while loop is used to execute a block of code repeatedly as long as a certain condition is true.

The syntax for a while loop in Python is:

while condition:
    # code block to execute while condition is true

Previous Article

Next Article