Python if, else and elif Statement

In this tutorial, we shall focus on Python if, else and elif statement.

Decisions are one of the most important feature of any computer program. It basically helps to systematically and logically execute the program based upon the input of the user (sometimes) or other factors which lets the user receive the desired output depending upon the input.

Decision-making can be provided by making use of branching structures and loops. These Branching structures helps in conditional programming. In this tutorial you will learn about branching structures.

 

Python if Statement

An if statement or an if block as it is commonly said, is useful to make decisions based on conditions in a program sometimes based on user input data.

An if block needs the keyword ‘if’ with a following condition and a set of statements to be executed in case the condition turns out to be True. A condition is mostly an expression which can be evaluated either to true or false.

 

Syntax

 

Python if statement
Image Source

 

Example

 

Output

A is Greater than 8

 

Here, a variable ‘a’ is initialized to 10. Then, an if block is used with the condition that checks whether a is greater than 8 or not. If it evaluates to be true, then the set of statements following that condition will be executed and then the program will come out of that particular block and execute the next set of statements.

Note: We generally use indentations to make the program look more readable that follows a much better logical approach. Here, it is important to use indentations as it helps to construct the if block in a logical manner.

 

Python if else Statement

The if-else condition is useful when you have multiple conditions to be evaluated in a program. Suppose you want to check for a particular condition and if that evaluates to false, you can then opt for another condition checking to evaluate it. This helps to produce a better output based on verification.

In this type of structure, the program will execute at least one of the blocks; either if block or else block.

Note: You must use proper indentations in if-else structure and if you dont do so, you will get an error as the Python Interpreter wont be able to understand the difference between the if block and else block.

 

Syntax

 

Python if else statement
Image Source

 

Example

 

Output

A is Greater than 8

 

Here, first the variable a is initialized to 10. In the if block, the condition is checked whether it is less than 8 and it is evaluated to be false. The Python Interpreter now has to go to the else block so as to execute the else condition statements which is mandatory. So, this is better than a single if block as this provides a better informed output to the user.

 

Python if elif else Statement

Suppose, you need to check for multiple conditions, you cant do it with a single if Block or an if-else Block. The if-elif-else block comes in use when you want to comapare a variable or any object for that matter to multiple other values. You can add as many elif conditions you want. elif stands for else-if. Here, else block is optional.

Note: You must have a single condition that must evaluate to true otherwise it will go to the else Block. Whenever a condition evaluates to be true, the interpreter will execute that particular block and will then exit out of the if-elif-else structure and wont even check for any other succeeding conditions.

 

Syntax

 

Python if elif else statement
Image Source

 

Example

 

Output

A is 4

 

Here, we have used comparison operator == to compare values of the variable a in the condition. Suppose the first condition evaluated to be true, then the control will execute that particular block and will go directly out of the if-elif-else Structure.

 

If you found any mistake in above Python if, else and elif statement tutorial then mention it by commenting below.

Leave a Comment

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