jkisolo.com

Exploring the Versatile Uses of 'Else' in Python Programming

Written on

Chapter 1: Understanding the Basics of 'Else'

In addition to the traditional if-else statement, the 'else' keyword in Python can be used in three other scenarios: within for loops, while loops, and try-except blocks.

1) The Classic If-Else Statement

fruit = 'orange'

if fruit == 'apple':

print('apple juice')

else:

print('not apple')

In this example, the code within the if block executes if the condition evaluates to True, while the else block runs if the condition is False.

2) Utilizing For-Else

for i in range(5):

# perform actions

else:

print('this runs if break DOES NOT occur in the above for loop')

The else block associated with a for loop executes only if the break statement is not triggered within that loop. For instance:

for i in range(5):

print(i)

if i == 2:

break

else:

print('else block runs')

In this case, the loop generates numbers from 0 to 4, but since the loop breaks when i equals 2, the else block does not execute.

Conversely, consider the following:

for i in range(5):

print(i)

if i == 100:

break

else:

print('else block runs')

Here, since the break condition never occurs, the else block executes.

3) The While-Else Structure

while condition:

# perform actions

else:

print('else block runs if break DOES NOT occur in while loop')

This behaves similarly to the for-else statement. If a break occurs, the else block does not run; if not, it does.

4) Try-Else Mechanism

try:

# perform actions

except Exception as e:

print(e)

else:

print('else block runs if except block DOES NOT execute')

In this structure, if the code in the try block raises an exception, the except block runs, and the else block does not. For example:

try:

x = 1 / 0

except Exception as e:

print('except block runs ->', e)

else:

print('else block runs')

Here, the ZeroDivisionError in the try block triggers the except block, preventing the else block from executing.

Conversely:

try:

x = 1 / 1

except Exception as e:

print('except block runs ->', e)

else:

print('else block runs')

In this scenario, since no exception occurs, the else block executes as expected.

Conclusion

I trust this explanation has been enlightening regarding the various uses of the 'else' statement in Python.

If you’d like to support me as a creator, consider these small gestures: clap 50 times for this article, leave a comment sharing your thoughts, or highlight your favorite part.

Thank you! Your support means a lot to me!

The first video titled "How to Use If Else Statements in Python" offers a comprehensive tutorial on employing if-else statements effectively in Python programming.

The second video, "If Else Statements in Python | Python for Beginners," provides insights tailored for beginners, helping them grasp the concept of if-else statements.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Protecting Your Inner Peace from Toxic Influences

Discover strategies to safeguard your joy from toxic people and enhance your mental well-being.

Renewable Energy and Climate Challenges: A Deep Dive

Explore the latest on renewable energy initiatives, climate-induced flooding threats, and insights on the new EPA nominee.

The Diminishing Threat of Russia's Nuclear Arsenal in Ukraine

An exploration of the potential ineffectiveness of Russia's nuclear weapons amid the Ukraine conflict.