Loops in Python (for in, while, Nested loops) | FACE Prep

Loops in Python programming function similar to loops in C, C++, Java or other languages. Python loops are used to repeatedly execute a block of statements until a given condition returns to be False. In Python, we have 3 different kinds of looping statements, namely:

                                       loops in python

1) While loop in Python

While loop iterates through a block of statements repetitively until the given condition returns False. This means, while loop first checks the given condition and if the condition is True, then it executes the block of statements inside the while loop. Whenever the condition returns False, it breaks out of the loop and executes the next immediate line of code.

We use a while loop when we don’t know the number of times to iterate through the block of statements.

Syntax for while loop in Python:
while expression:
    statement(s)

Example: Let’s say you want to calculate the sum of all numbers less than a given number (n). This is how a while loop would help

number = 7
sum = 0
i = 0
while (i < number):
    sum = sum + i
    i = i + 1
print (sum)
Output: 21
Explanation: Here, the while loop executes until i = 6, when i = 7, the condition i < number returns False. Hence it breaks out of the while loop and executes the immediate next line of code i.e the print statement.

2) For in Loop in Python

For in loop in Python is similar to for loop in other languages. It is also used to iterate over a block of statements repeatedly until the condition returns False. For in loop is the best to use when we know the number of times to iterate. To iterate over a sequence of numbers, we can also use the range and xrange functions in Python. However, Python 3 uses only range function. Know the difference between range and xrange here. Syntax for while loop in Python: for variable in sequence: statement (s) (or) for variable in range (list of numbers): statement (s) Example:
even_numbers = [2, 4, 6, 8, 10]
for i in even_numbers:
    print(even_numbers)
Ouput:
[2, 4, 6, 8, 10]
[2, 4, 6, 8, 10]
[2, 4, 6, 8, 10]
[2, 4, 6, 8, 10]
[2, 4, 6, 8, 10]

Example: Now, let’s say you want to print all the even numbers from 2 to 10. In this case, range function can be used this was

for i in range (2, 12, 2):
    print (i)
Output: 
2
4
6
8
10

Explanation: Here range function is used to iterate through the sequence 2 to 12 with a difference of 2 between the numbers. The syntax of range function used here is a range (lower limit, upper limit, the difference between numbers). In this case, the lower limit is inclusive and upper limit if exclusive of the sequence.

3) Nested Loops in Python

Loop inside a loop is simply a nested loop. In Python, you can use any loop inside any other loop. For instance, a for in loop inside a while loop, a while inside for in and so on. 

Syntax for for loop inside for loop (nested for loop):
for variable in sequence:
    for variable in sequence:
        statement (s)
        statements(s)
Syntax for while loop inside while loop (nested while loop):
while condition:
    while condition:
        statement (s)
        statements(s)

Example of nested loops in Python: Let’s say you want to print the below pattern.

1
1 2
1 2 3
1 2 3 4
#program to print a pattern

for i in range (1, 5):
    for j in range(i):
        print (j + 1, end = ' ')
    print ()
Output:
1
1 2
1 2 3
1 2 3 4

Which Loop to Choose, for vs while in Python?

The statements inside a while loop get executed only when the while condition is True. So this means, there may be a chance that the statements might not get executed even once. Also, most times, you might know the number of times you need to iterate through the statements. So considering these, it is recommended to go for a for loop if you know the number of elements to be iterated over. However, if you have a condition to be evaluated rather than a list of element to loop over, then you can go for  a while loop.

Control Statements in Python

Control statements are very useful while looping in Python. They are used to control the execution of loops. Below are the 3 control statements in Python.

NameDescription
Break statementWhenever a loop encounters a break statement, it terminates the loop and the control shifts to the immediately next statement outside the loop.
Continue statementWhenever a loop encounters a continue statement, the control shifts to the beginning of the loop.
Pass statementThe pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.

Loops in Python FAQs

There are 2 different kinds of loops in Python. Namely, for in and while. Nested loops are a combination of these loops and hence they may or may not be treated as a separate type of looping statement in Python.

Loops in Python are used to iterate over a set of statements repeatedly until they find the condition to be False.