Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Looping Statements in Python

Last Updated on April 20, 2023 by Prepbytes

By default, programs in any programming language are executed in a sequential order where each statement is executed one after the other. However, there may be situations where a block of code needs to be repeated multiple times. To address this, programming languages provide various types of loops that allow a set of instructions to be repeated until a specific condition is met. Here, we will discuss the different types of looping statements available in Python, the syntax of different looping statements in Python along with examples for a better understanding. We will also discuss the control Statements that are used to control the flow of Looping Statements in Python.

Looping Statements in Python

Looping is a powerful technique that simplifies complex problems and allows programmers to repeat a set of instructions a finite number of times, thus avoiding repetitive code. Python offers three types of looping Statements in Python:

  • for loop,
  • while loop
  • nested loop

Let’s learn about each of the above-mentioned Looping Statements in Python in detail.

For Loop

The for loop is one of the looping statements in Python that is used to iterate over a sequence of elements. This sequence can be a list, tuple, string, or any other object that can be iterated.

Syntax of for Loop in Python

The for loop syntax in Python is as follows.

for variable in sequence:
    # Code block to be executed

In this syntax,

  • The variable is a temporary variable that holds the value of each element in the sequence during each iteration of the loop.
  • The sequence is the data structure that is being traversed.
  • The code block that follows the for statement is executed repeatedly for each element in the sequence.

Example of for Loop in Python

The given code shows the working of the for Loop in Python.

list = ['PrepBytes', 'CollegeDekho', 'Ed-Tech']
for index in list:
    print(index)

Output:

PrepBytes
CollegeDekho
Ed-Tech

Explanation:
In the above code, the for loop iterates over each element in the ‘list’ list and prints it on a new line.

While Loop

The while loop is another looping statements in Python that are used to repeat a block of code until a certain condition is met.

Syntax of while Loop in Python

The syntax of the while loop in Python is given below.

while condition:
    # Code block to be executed

In this syntax,

  • condition is a boolean expression that is evaluated at the start of each loop iteration.
  • The code block that follows the while statement is executed repeatedly until the condition evaluates to False.

Example of while Loop in Python

Here is the example code that uses the while Loop in Python.

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

Output:

0
1
2
3
4

Explanation:
Here, the while loop repeats the code block until the count variable is less than 5. During each iteration, the count variable is incremented by 1, and the current value of the count is printed on a new line as we can see in the output.

Nested Loops

In Python, a nested loop is a loop within another loop. It is used when we want to iterate over a sequence of elements that contains multiple levels of nesting.

Syntax of Nested Loop in Python

The syntax of the nested loop in Python is as follows.

for variable in sequence:
    for inner_variable in inner_sequence:
        # Code block to be executed

In this syntax,

  • The variable is a temporary variable that holds the value of each element in the sequence during each iteration of the outer loop.
  • The inner_variable is a temporary variable that holds the value of each element in the inner_sequence during each iteration of the inner loop.
  • The code block that follows the inner for statement is executed repeatedly for each element in the inner sequence, and for each element in the outer sequence.

Example of Nested Loop in Python

The code given below uses the Nested Loop.

matrix = [[1, 2, 3] , [4, 5, 6] , [7, 8, 9]]
for row in matrix:
    for element in row:
        print(element)

Output:

1
2
3
4
5
6
7
8
9

Explanation:
In this example, the nested loop iterates over each element in the ‘matrix’ list and prints it on a new line.

Loop Control Statements

Loop control statements are used to alter the normal flow of the looping Statements in Python. There are three types of loop control statements in Python, which are, break, continue, and pass.

Break Statement

The break statement is used to terminate the loop prematurely in Python. It is used when we want to exit the loop before it has completed all of its iterations.

Syntax of break Statement

The syntax of the break statement in Python is as follows:

for variable in sequence:
    if condition:
        break

Here,

  • The variable is a temporary variable that holds the value of each element in the sequence during each iteration of the loop.
  • The condition is a boolean statement that is evaluated at the start of each loop iteration. If the condition evaluates to True, the break statement is executed, and the loop is terminated.

Example of break Statement

Let us understand the break Statement in Python with the following example.

fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
    if fruit == 'banana':
        break
    print(fruit)

Output:

apple

Explanation:
In this above code, the for loop iterates over each element in the “fruits” list and prints it on a new line. However, when the “fruit” variable equals “banana”, the break statement is executed, and the loop is terminated.

Continue Statement

In Python, the continue statement is used to skip the current iteration of the loop. It is used when we want to skip a certain element in the sequence and continue with the next iteration of the loop.

Syntax of continue Statement

The syntax of the continue statement in Python is given below.

for variable in sequence:
    if condition:
        continue
    # Code block to be executed

Here,

  • The variable is a temporary variable that holds the value of each element in the sequence during each iteration of the loop.
  • The condition is a boolean statement that is evaluated at the start of each loop iteration.

Example of continue Statement in Python
Here is the example code using the continue Statement in Python.

fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
    if fruit == 'banana':
        continue
    print(fruit)

Output

apple
cherry

Explanation:
In this example, the for loop iterates over each element in the “fruits” list and prints it on a new line. However, when the “fruit” variable is equal to “banana”, the continue statement is executed, and the current iteration of the loop is skipped.

Pass Statement

The pass statement is used as a placeholder in Python. It is used when we want to write empty code blocks and want to come back and fill them in later.

Syntax of pass Statement

The syntax of the pass statement in Python is given below.

for variable in sequence:
    pass

In this syntax, the variable is a temporary variable that holds the value of each element in the sequence during each iteration of the loop.

The pass statement is used to create an empty code block, which is filled later.

Example of pass Statement in Python

The pass Statement in Python is used as shown in the code given below.

fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
    # Code block to be filled later
    pass

Explanation:
In this example, the for loop iterates over each element in the “fruits” list, and an empty code block is created using the pass statement.

Conclusion
In conclusion, looping statements in Python are a crucial component as They enable the programmer to repeat a set of instructions until a specific condition is met, which helps to simplify complex problems and avoid repetitive code. There are three types of looping Statements in Python: for loop, while loop, and nested loop, each with its unique features and applications. We have discussed all these looping statements in Python

Frequently Asked Questions(FAQs)

Some Frequently Asked Questions related to “Looping Statements in Python” are given below.

Ques 1. What is a loop in Python?
Ans. A loop in Python is a control structure that allows a set of instructions to be executed repeatedly until a specific condition is met.

Ques 2. What is the difference between a for and while looping Statements in Python?
Ans. A for loop in Python is used when the number of iterations is known in advance, while a while loop is used when the number of iterations is not known.

Ques 3. What is an infinite loop in Python?
Ans. An infinite loop in Python is a loop that never terminates.

Ques 4. How can you create an infinite loop in Python?
Ans. You can create an infinite loop in Python by using a while loop with the condition that is always true.

Leave a Reply

Your email address will not be published. Required fields are marked *