Python Loops

What is a loop?

When any program is implemented, it runs consecutively. The statement which shows up first in the series is implemented first then the following statement etc. till the end statement of the program. Several times there is a prerequisite to run the similar structure of code in a program several times, then there emerges a requirement for a control structure referred to as loops.

The loop creates a statement or collection of statements in a structure of the program to implement several times if the situation is true and exits the loop while the situation develops into false.

Types of Loop in Python

Following are the three type of loops which are as follows:

Python Loops
while loop

While loop reruns a statement or set of statements while a providing condition is true. It checks the situation each time it implements the loop body, and it exits the loop while the situation develops into false.

The syntax of a while loop are as follows.

while expression:


count = 0
while (count < 3) :
print('The current count is:', count)
count = count + 1
print("while loop Ended! ")

The current count is : 0

The current count is : 1

The current count is 0 : 2

for loop

It implements a series of statements several times and abbreviates the program that handles the loop variable.

The syntax of a for loop is as follows:

for iterating_var in sequence:

statements (s)

veggies = ['potatoes', 'tomatoes', 'onion']
for index in range(len(veggies)) :
print ('The Current Veggie :', vegetables[inedex])
print("For loop ended! ")

The Current Veggie : potato

‘The Current Veggie : tomato

‘The Current Veggie : onion

Nested loop

It is a loop within a loop. In Python, we may utilize a while loop in another while or for loop or for loop in another while or for loop.

Syntax for Nested for loop

for iterating_var in sequence:

for iterating_var in sequence:



Enroll Yourself: Python Training In Delhi

Copyright 1999- Ducat Creative, All rights reserved.