Python is a general-purpose, high-level programming language designed to be easy to read and execute. It is open-source, which means it is free to use. In this article, we will learn one of Python programming's fundamental looping statements: the Python for loop.
What Is for Loop in Python?
Syntax: FOR COUNTER IN SEQUENCE:
Fig: Flowchart of for loop
Fig: Basic example of Python for loop
The program operates as follows: We have assigned a variable, x, which is going to be a placeholder for every item in our iterable object. In this case, the variable “x” actually represents the elements in that list. Then, we print our variable, x. This process continues until all items are printed.
Break Statement in for Loop
The Python break statement is used to exit from the loop immediately after a certain condition is met.
Fig: break statement
The program above operates as follows: The loop continues until the specified element is encountered. As soon as the ‘green’ element is encountered, the loop breaks.
Continue Statement in for Loop
The function of the continue statement is to skip the current iteration of a loop and continue with the next one.
Fig: continue statement
The Range() Function
The range() function executes a group of statements for a specified number of times.
Fig: range() function in Python for loop
The program operates as follows. When the for structure begins executing, the function
range creates a sequence of values, which range from zero to four. The first value in this sequence is assigned to the variable x, and the body of the for structure executes. For each subsequent value in the sequence, the value is assigned to variable x, and the body of the for structure executes. This process continues until all values in the sequence have been processed.
Else in for Loop
Python enables an else clause at the end of a for loop. The else part is executed if the loop terminates naturally.
Fig: else statement
In the above example, the for loop is executed first. After that, the else part is executed.
A loop inside another loop is called a nested loop. The inner loop will be performed once for each iteration of the outer loop.
Fig: Nested loop in Python for loop
Recommended programming practice:
Excessive nesting levels can make it difficult to follow the program. As a general rule, try to avoid using more than three levels of indentation.
Access Index in for Loop
To iterate over the indices in a sequence, we use the enumerate() function.
Fig: enumerate() function
The enumerate() function adds the counter to iterable, which starts from zero by default.
Looping Through Multiple Lists
We can loop through multiple lists at once. For this, we use the zip() function, which is built in the Python loop.
Fig: Looping through multiple lists
For loop exercise
Question 1: Display a “simplilearn” message after successfully executing for loop
Fig: Question 1 solution
Question 2: You are given a list of integer elements. Make a new list that will store squares of elements from the previous list.
Fig: Question 2 solution
Looking forward to make a move to programming? Take up the Python Training Course and begin your career as a professional Python programmer.
In this article, we have discussed Python for loop and its syntax different statements. We also provided a visual example that demonstrated how to use the break and continue statements in a Python for loop.
If you have any questions or comments, please post them below, and we'll have our experts get back to you as soon as possible.