In general Computer, Programs are executed sequentially. It will execute the first line then the second line than the third line and so on.

Programming Languages nowadays also provide various control statements that allow us to control the execution of a Program.

There may be sometimes you want to execute a particular amount of code multiple times ( Fixed Number of times ) or maybe Dynamic number of times.

and to solve the problem of executing the limited number of lines of code multiple numbers of times Python Programming Language provides multiple Loops to solve the problem of executing multiple lines multiple times.

There are majorly 3 kinds of Loops in Python:-

  • While Loops
  • For Loops
  • Nested Loops

1. While Loops

In Python, while loop is used to execute a block of statements repeatedly until a given condition is satisfied. And when the condition becomes false the loop is terminated and the program continues to execute code after the while loop.

Format of While Loop:-

while expression:

For Example:-

Python program to illustrate while loop
count = 0
while (count < 3):
count = count + 1
print("Hello Geek")

2. For Loops

In any Programming Languages For Loops are used for sequential traversal. In Python, we use For each loop just like we have in another programming language, unlike C or C++.

Syntax :-

for iterator_var in sequence:

For Example :-

n = 4
for i in range(0, n):

3. Nested Loops

Nested Loops are termed to those Loops which are used inside other loops in other word loop inside a loop. We can put any loop inside any loop for example we can put For Loop inside while Loop.


for iterator_var in sequence:
    for iterator_var in sequence:

**In any Programming Language we have loop control statements also to control the execution of the loop as we want to **

We have 3 types of Loop Control Statements

  • break
  • continue
  • pass

1. Break

It is a simple keyword or token which brings the control out of the loop and executes the rest of the program code.


# Code to display the use case of break statements
# this code will break out of the for loop as soon it finds 'e' or 'r'

for letter in 'coderaman07':
    if letter == 'e' or letter == 'r':

print(f'Current Letter :{letter}')


Current Letter : e

2. Continue

It is a keyword or token which just skips the execution of the code which was meant to be executed for all.


Prints all letters except 'e' and 's'
for letter in 'coderaman07':
    if letter == 'e' or letter == 'r':
    print(f'Current Letter :{letter}')
    var = 10


Current Letter :c
Current Letter :o
Current Letter :d
Current Letter :a
Current Letter :m
Current Letter :a
Current Letter :n
Current Letter :0
Current Letter :7

3. Pass

It is an empty Control statement that is often used to describe that the code here has to be written here in near future.


An empty loop

for letter in 'coderaman07':
print(f'Last Letter :{letter}')


Last Letter : 7

So wrapping this Lesson up we had a brief insight of For loop, While loops, and also Nested Loops with 3 control statements.

Hope to see you in the next Blog Post for more Python Stuff and a codebase to code something best out of these blog posts.

If you feel to ask questions on any topics just Tag me on Twitter or Instagram or Facebook with my username coderaman07. I will be happy to help you out.

For Development Related Stuff Hit on to my Website for Open Source Software's RushX

Also, join my email list for weekly updates coderaman07

Also, you can visit my Portfolio website for any Full-time Job as I am going to Graduate this coming April. You can go to my Portfolio website by clicking here

This post is also available on DEV.