Python For Else

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

In Python, 'for-else' and 'while-else' constructs provide a unique way to handle loops with an additional clause executed when the loop usually concludes without a break statement. The 'for else python' structure is especially beneficial for managing loop completion scenarios.

Else in For Loop

The 'else' block in a 'for loop' in Python, often referred to as 'for else python', is executed after the for loop completes its iteration over the entire sequence, but only if a break statement didn't terminate the loop.

Example:

Output:

Using else conditional statement with for loop in python

In the context of 'for else python', we explore two scenarios: one where the 'else' block is executed and one where it isn’t.

1. Else block is executed in below Python 3.x program

Here, the 'else' block, a feature of 'for else python', is executed because the 'for' loop completes its execution without interrupting a break statement.

Output:

2. Else block is NOT executed in below Python 3.x program

In this scenario, typical of 'for else python', the 'else' block does not execute because the 'for' loop is interrupted by a break statement.

Output:

What is For Else and While Else in Python

For-else and while-else are helpful features provided by Python. You can use the else block just after the for and while loop. Otherwise, the block will be executed only if a break statement doesn't terminate the loop.

Simply put, if a loop is executed successfully without termination, then the else block will be executed.

Let’s try to understand this -

What is For Else and While Else in Python

A loop must complete all its iterations so that the else block can be executed, but the else block will be executed only once.

Syntax

For-else

In the above example, for loop will execute n times and then else block will execute.

While-else

In the above example, while loop will execute until the condition is satisfied and then else block will be executed.

Scenarios to use For Else and While Else

Now, we will see three such scenarios where for else and while else are used.

1. Search Programs

Program to find a fruit ‘mango’ in the list of fruits using for-else and while-else.

For-Else Program:

Output:

While-Else Program

Output:

2. To Check Limits

In this scenario, we will check the limit with the help of for-else and while-else.

Example

Output:

3. Help to Break Out of Nested Loops

When using nested loops in python, it's often necessary to break out of all loops when a particular condition is met, not just the innermost one.

Example:

Output:

4. Help to Handle Exceptions

Exception handling is unignorable for programming, and if we use the for-else or while-else features properly, it can be helpful as well.

In the try statement, the else clause will run when there are no exceptions during the execution of the try statement, which is a very similar concept to for-else.

Example:

Output:

Conclusion

Here are the things you need to remember-

  • Else block in for-else or while-else will only execute once when the loop completes all its iterations without termination by a break statement
  • We saw how we can use for else and while else in a searching program
  • We also took a look at how it can be used to break out of loops
  • Also, we saw its implementation in exception handling

Happy coding!