Python – Loops

Statements are typically executed in order: the first statement in a function is executed first, then the second, and so on. You may find yourself in a position where you need to run a block of code multiple times.

Different control structures are available in programming languages, allowing for more sophisticated execution routes.

A loop statement allows us to execute a statement or group of statements multiple times.

Python For Loop

The for loop in Python is used to iterate through the elements of a collection in the order in which they appear. This collection could be in the form of a sequence (list or string).

Python For Loop Syntax :

for [variable] in [sequence]:
  • First, the variable’s first value will be assigned.
  • Second, the body of the loop’s statements are all performed with the same value.
  • Third, after step two is finished, the variable is assigned the next value in the sequence, and process two is repeated.
  • Finally, it repeats until all of the sequence’s values have been allocated to the variable and processed.

Python For Loop Simple Example :


for a in range (1,6):
    print(num * a)

output of above example  :


Python Example to Find Sum of 10 Numbers :

for n in range(1,11):

output of above example  :


2.Python Nested For Loops

Example of Nested for loop :

for i in range(row):
    for j in range (i):
        print(i,end=" ") #print number
    print(" ")

By default, the print() method in Python returns a newline. If you’re a C/C++ programmer, you might be wondering how to print without a newline. The print() method in Python has a parameter named ‘end‘. This parameter’s default value is ‘n,’ which is the new line character. This argument allows you to end a print statement with any character or string.

output of above example  :

2 2
3 3 3
4 4 4 4
5 5 5 5 5

For each value of Outer loop the whole inner loop is executed.

For each value of inner loop the Body is executed each time. 

lets see one more example of patter program :

for i in range (row):
    for j in range (5,i-1,-1):
       print("*",end=" ") # print * and space
    print(" ")

output of above example  :

* * * * *
* * * *
* * *
* *

3.Python While Loop

While loops are used in Python to run a series of statements or bodies until the stated condition is true. The control will exit the loop once the condition is false.

while loops are constructed like so :

while [a condition is True]:
    [do something

Here, loop Body will execute till the expression passed is true. The Body may be a single statement or multiple statement.

Python While Loop Example :


while a>0:
    print("Value of a is",a)
print("Loop is Completed")

output of above example  :

Value of a is 10
Value of a is 8
Value of a is 6
Value of a is 4
Value of a is 2
Loop is Completed


  • Firstly, the value in the variable is initialized.
  • The condition/expression in the while is then assessed. As a result, if the condition is met, the control enters the body and executes all of the statements. If the condition/expression passed returns false, the control exists the body, and the control immediately moves on to the next instruction after the while body.
  • Thirdly, if the condition was true after all of the statements were performed, the variable is incremented or decremented. After changing the value of the variable, the second step is performed. This procedure is repeated until the expression/condition is false.
  • Finally Rest of code after body is executed.

Python While Loop Example 2 :

# Python program to illustrate
# Single statement while block
count = 0
while (count < 5): count += 1; print("Hello Python ",count)

output of above example  :

Hello Python  1
Hello Python  2
Hello Python  3
Hello Python  4
Hello Python  5

When a program executes indefinitely within a loop, it is known as an infinite loop. On the command line, press CTRL + C to end infinite loops.

This article explained how Python while loops work and how to create them. While loops continue to loop through a block of code as long as the while statement’s condition is True.

One Comment

  1. great

Leave a Comment

Your email address will not be published.