Loops in Python
The main purpose of using loops in any programming language is to repeat a certain set of instructions. This happens because the output must be having some sort of repetition or pattern. Hence to execute this pattern, we break the normal flow of program sequence and add a loop to it.
Loops are very helpful in reducing the lines of code in a program. Besides that, they enable us to reuse code as many times we want.
There are 2 types of loops in Python:
- For loop
- While loop
This is the most common loop used in python. It is used to traverse through lists, tuples, sets, etc. This loop is generally used when the number of iterations is known beforehand.
Following is the syntax of for loop:
avengers = ["Tony", "Stephen", "Bruce", "Steve", "Sam", "Natasha", "Peter"] for i in avengers: print(i) # when you want to explicitly specify the range for i in range(0, 10): print(i)
We have taken a sample list here to understand the for loop.
Here i is called an iterative variable. This variable will read through the provided list. The colon marks indicate the end of the conditional line and the beginning of the instructions. There is one tab space to indicate the beginning of a new block in the loop. This is in accordance with the basic structure of Python.
If we explicitly specify the range, then we used the predefined range() function in for loop.
Formats of for loop
We can use for loop in different formats. There are two common formats we generally implement and they are nesting of for loops and conditional for loops.
In nesting for loops, we add for loops inside a for a loop. This continues until the condition is satisfied.
A sample syntax to illustrate nested for loop:
i, j = 0, 0 for i in range(0, 10): print() for j in range(0, i+1): print("*", end="")
Here we use two for loops nested inside each other. Note that the indentation also varies accordingly. Run and see what output the program gives.
Note: The end keyword is used to instruct that we need to execute commands in a new line once the inner loop finishes.
In conditional for loop, we treat for a loop as an if-else block. The format is the same as that of an if-else structure.
A sample syntax to illustrate conditional for loop:
avengers = ["Tony", "Stephen", "Bruce", "Steve", "Sam", "Natasha", "Peter"] for i in avengers: if i == "Bucky": print("Avenger found!") else: print("Avenger doesn't exist")
Note that, the else here belongs to the for loop, not the if statement.
This loop is used when the number of iterations is not known. However, we must know the condition when the loop will end. That condition itself must be representable in a code format.
Syntax of While loop:
i = 1 while i < 10: print(i) i += 1
Additional Keywords: Break & Continue
Few keywords like break and continue are often used in a loop. It is because they serve a special purpose in deciding unique control flows in a loop.
The break is a keyword which brings the control out of a loop. Following example will make it clear.
for i in range (0,100): if i == 30: break else: print(i)
Here the looping range is up to 100. But we have executed a break statement which will stop when the value is 30. Hence it won’t print values more than 29.
continue is a keyword which helps in skipping certain operation in a loop. When continue is executed, then it passes all the further instructions and iterates the loop again by incrementing the loop variable. Consider the following example:
for i in range(1, 15): if i == 5 or i == 6 or i == 7: continue else: print(i)
Here the loop is designed to print sequentially from 1 to 14. However, the continue condition tells to skip the instruction when the values are 5 or 6 or 7. Hence when we execute this block, we don’t get these 3 values in our output.