Python Loops


Table of contents

Python Loops

As a programmer, you will encounter many situations when you need to execute a single or block of statements repeatedly. You can use loops to do just that instead of writing those statements repeatedly. Python supports two types of loops, ‘for’ and ‘while’.

for Loop

The ‘for’ loop in python works a little bit different than other programming languages like C, C++ or Java. It is used to iterate through sequences or iterable objects such as lists, dictionaries, tuples and so on. The keyword ‘in’ is used with the ‘for’ loop.

For example:

li = [1, 2, 3, 4] 
for i in li:
    print(i)

Output:

1
2
3
4

In the above example, each element of the list is assigned to the variable ‘i’ in each iteration and printed using the print() function. The ‘for’ loop terminates automatically after it reaches the last item in the sequence.

while Loop

The ‘while’ loop can be used to iterate the block of statements until the condition or test expression is True. The test expression in the ‘while’ statement is checked first and if it returns True, the block inside the ‘while’ loop is executed. After all the statements inside the ‘while’ loop are executed, the control comes back to the condition in the ‘while’ loop again. If the condition still returns True, then the statements are executed again. If the test expression is False, the ‘while’ loop is terminated. Python doesn’t support the ‘do while’ loop.

For example:

a = 0
while a < 5:
    print(a)
    a = a + 1

Output:

0
1
2
3
4

In the above example, the test expression ‘a < 5’ returns True until a is less than 5. The statement ‘a = a + 1’ will increment the value of a by 1 in each iteration. Thus, the ‘while’ loop terminates when the value of a becomes 5.

Nested Loops

Just like ‘if…elif…else‘, you can also nest a loop inside another loop. These types of loops are called nested loops. Along with nesting same type of loop inside another same type of loop, you can also nest a ‘for’ loop inside a ‘while’ loop and vice versa. You can nest loops up to any level.

For example:

a = [[1, 2], [3, 4]]
for i in a:
    for j in i:
        print(j)

Output:

1 
2
3
4

In the above example, the outer loop goes over two lists ([1, 2] and [3, 4]. The inner loop goes through each item in those two lists. In the first iteration of the outer loop, the inside loop goes through both of the items in the first list and then control goes back to the outer loop. In the second iteration, the inside loop again goes over each item in the second list and then the nested loop is terminated.

Loop Control Statements

Control statements in Python are used to change the flow of normal loops.

  • break: The break statement is used to terminate the current loop and take the flow of the program to the next statement outside the loop.
  • continue: The continue statement is used to skip over the execution of statements after it for the current iteration and take the control back to the start of the loop.
  • pass: The pass statement is used as a placeholder to implement stubs in Python.