Python if-else

Python if-else

The else statement is associated with if statement. An else statement executes if the conditional expression in if the statement becomes false or a Zero value.

If the conditional expression is false, then control transfers to the else block. An else statement is optional, and there could be only one else statement.

Syntax

The syntax of the if…else statement is following.

The colon (:) is important because it splits the condition from the statements to be executed after the evaluation of the condition. This is especially important for statements where there is only a single statement and the bracket ( ) is not used.

Flow Diagram

In the above flowchart, first it checks the condition if the condition is true, then conditional code (if statement) will be executed. If the condition is false, then else block (else statement) will be executed.

The if…else statement examples are the following:

  1. Write a program to check number is even or odd

Output:

  1. Write a program to check the voter’s age for valid for voting or not

Output:

  1. Write a program to display student marks 

Output: