Skip to content

Python For Loop with Else Statement Vijetha Spark By {Examples}

  • by

For/while else statement is the unique feature of Python. In simple words, you can use the else block after the for loop/while loop in Python. Usually, you can see in many languages like, ‘c’, ‘c++’, and ‘java’ else statement will use along with the if statement. These languages don’t support this feature. Using this unique feature of Python you can allow the else block after the body of the for loop.

Our loop has the else block, if you want to execute it only the loop terminated after reaching all the iterations in for loop. Whereas while loop, it will execute only the loop terminated after reaching a certain condition.

Note: You can use the break statement to exit the loop. In such cases, the else part will not be executed.

In this article, I will explain the concept of an else statement within a for loop with examples.

1. Quick examples of For Loop with Else Statement

Following are the quick examples of For Loop with an else statement.

Following are the examples of for loop with else statement

# Example 1: Iterate given lists using for loop with else statement
# Initialize the list
courses = [“c”, “c++”, “python”, “java”, “pandas”]
print(“List:”, courses)
for i in courses:
print(i)
else:
print(“All technologies are printed”)

# Example 2: Using break with for loop and else statement
for i in courses:
if i ==”java”:
print(“Java is found”)
break
else:
print(“Java is not found”

# Example 3: Iterate given lists using while loop with else
print(“List:”, courses)
i = 0
while i < len(courses):
print(courses[i])
i += 1
else:
print(“All technologies are printed”)

# Example 4: Using break with while loop and else statement
print(“List:”, courses)
i = 0
while i < len(courses):
if courses[i] == “java”:
print(“Java is found”)
break
i+=1
else:
print(“Java is not found”)

# Example 5: To handle the ZeroDivision Error using else with for statement
x =10
my_list = [10, 40, 0, 60]
for i in my_list:
try:
x/i
except ZeroDivisionError:
print(“zero division error”)
break
else:
print(“Zero division error not found”)

2. Else Statement with For Loop

In Python, you can use the else statement block below the for loop block. Using this feature you can execute the else statement when the loop exists after completing all its iterations. You can take a list of strings and iterate it using for loop. For completing all iterations of the loop the else part will execute and print its statement below the for statements.

# Iterate given lists using for loop with else statement
# Initialize the list
courses = [“c”, “c++”, “python”, “java”, “pandas”]
print(“List:”, courses)
for i in courses:
print(i)
else:
print(“All technologies are printed”)

3. For Loop with Else Statement in Python using Break

You can use a break statement along with for loop and an else statement to terminate the loop without executing the else part. You can use a break statement along with an else statement and for a loop to iterate over the list of strings(courses). For every iteration, it will check if the i == java, if it is True then the loop will reach the break statement and terminate the loop without executing the else block. If it is False the loop won’t reach the break statement and execute the else block.

# Iterate given lists using for loop with if else statement
courses = [“c”, “c++”, “python”, “java”, “pandas”]
for i in courses:
if i ==”java”:
print(“Java is found”)
break
else:
print(“Java is not found”)
)

Yields below output.

As you can see, from the above else block is working with for loop not with if statement.

4. Else Statement with While Loop In Python

In Python, you can use an else statement with a while loop. Let’s use the else block with a while loop to iterate a list and execute the else statement when the loop terminates after reaching a certain condition. If the while loop is terminated by the break statement then the else block will not execute.

# Iterate given lists using while loop with else
courses = [“c”, “c++”, “python”, “java”, “pandas”]
print(“List:”, courses)
i = 0
while i < len(courses):
print(courses[i])
i += 1
else:
print(“All technologies are printed”)

Yields below output.

# Output
List: [‘c’, ‘c++’, ‘python’, ‘java’, ‘pandas’]
c
c++
python
java
pandas
All technologies are printed

5. While Loop with Else using Python Break

You can use a break statement along with a while loop and an else statement to terminate the loop without executing the else part. Let’s use the break statement to exit the while loop without executing the else part after reaching a certain condition. For example,

# Using break with while loop and else statement
courses = [“c”, “c++”, “python”, “java”, “pandas”]
print(“List:”, courses)
i = 0
while i < len(courses):
if courses[i] == “java”:
print(“Java is found”)
break
i+=1
else:
print(“Java is not found”)

# Output:
# Java is found
)

6. Use Else with For Loop To Handle the Zero Division Error

You can use the else statement with for loop/while loop to handle the ZeroDivisionError. If a number is divided by 0 it will fall a ZeroDivisionError in Python. You can use the try-except statement within a for loop to except the ZeroDivisionError.

If ZeroDivisionError occurs in the try statement, it will print the related error statement. If no exception the else clause will execute and print its statement.

# To handle the ZeroDivision Error using else with for statement
x =10
my_list = [10, 40, 0, 60]
for i in my_list:
try:
x/i
except ZeroDivisionError:
print(“zero division error”)
break
else:
print(“Zero division error not found”)

# Output:
# zero division error
)

6. Conclusion

In this article, I have explained the concept of an else statement with a for loop/while loop. Also explained how we can implement the else statement with and without the break statement and explained using the else statement with loop how to handle the zero division Error.

References

https://docs.python.org/3/tutorial/controlflow.html
 For/while else statement is the unique feature of Python. In simple words, you can use the else block after the for loop/while loop in Python. Usually, you can see in many languages like, ‘c’, ‘c++’, and ‘java’ else statement will use along with the if statement. These languages don’t support this feature. Using this unique  Read More Python, Python Tutorial, Pending review by admin, Python For Loop Examples 

Leave a Reply

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