Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
in Blog Post by 15 41 68
edited by

In this post, we're gonna learn how to write the different types of loops in Python for beginners.

loops in python

What're Loops?

  • Loops mean repetition, looping back over the same block of code again and again.
  • A loop statement allows us to execute a statement or group of statements multiple times.
  • Loops can execute a block of code as long as a specified condition is reached.

Loops in Python

Python has two primitive loop commands:

  • While loop: Loops through a block of code as long as a specified condition is True.
  • For loop: When you know exactly how many times you want to loop through a block of code.

While Loop in Python

  •  A while loop in Python repeatedly executes a target statement as long as a given condition is true.

The syntax of While loop in Python

while expression:

Looping In Python

For loop in Python

  • A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

The Syntax of For loop in Python

for iterating_var in sequence:

For loop in Python\

You can also loop through the letters in the word:

for x in "apple":

for loop with range function

syntax of range function:

range(start, stop [, step])

use: it usually used in for statement to define the number of loops iteration or

for i in range(1, 4):
    print ('This is the', i, 'iteration.')


This is the 1 iteration.
This is the 2 iteration. 
This is the 3 iteration.

the default step value is 1

Example of defining the step by 2

this example will print the odd numbers of iteration

for i in range(1, 9, 2):
    print ('This is the', i,' iteration.')


This is the 1 iteration.
This is the 3 iteration.
This is the 5 iteration.
This is the 7 iteration.


In this post, we have discussed the different types of loop in Python, and when we can use it.

See Also

If you don’t ask, the answer is always NO!