else Statement

The ’else’ statement in Python serves as a contingent mechanism, typically employed within control flow structures to execute a block of code should all preceding conditions fail to meet their respective criteria. This makes it a crucial component when writing decision-making code in Python.

Syntax of else statement

The syntax of the ’else’ statement in Python is straightforward:

1if <expression>:
2    # execute these statements if expression is true.
4    # execute these statements if expression is false.

The if statement checks for a condition. If that condition is True, the block of code under if executes. If that condition is False, the block of code under else executes.

Demonstrating the use of else statement

Let’s demonstrate the use of an else statement in Python.

1age = 16
2if age >= 18:
3    print("You are eligible to vote.")
5    print("You are not eligible to vote.")

In this example, since the age is less than 18, the output will be “You are not eligible to vote.”


Remember, the else block is optional. The decision to utilize it will subject to your program’s requirements.

else Statement in Loop Structures

The ’else’ statement in Python can also be used in loop structures. Here’s how:

1for i in range(3):
2    print(i)
4    print("Loop has ended.")

In this example, after the for loop has exhausted the iterable, the else block executes and prints “Loop has ended.”


The else block only executes if the loop completes naturally. If the loop is prematurely terminated by a break statement, the else block won’t be executed.

Grasping the use of the ’else’ statement alongside ‘if’ and loop structures can greatly enhance your control flow handling capabilities in Python by providing a concise and clean method of executing code based on varying conditions.