if elif else and nested if in Python

In this class, we discuss if elif else and nested if in python.

For Complete YouTube Video: Click Here

if elif else in Python

The discussion about conditional statements is made here. Please watch before we dive into if elif else.

Let’s take an example and understand the concept.

Example:

Take details of the student and display the grade.

if elif else nested if in python1
Sample Program

From the above program, it will check the first if condition.

The first condition is True. execute the block of code present in that if condition.

No need to check the remaining elif conditions and else.

if the first if condition not True. check the next elif condition.

This is how it continues checking if elif conditions one by one.

If all the if elif conditions are false then go to the else part and execute the block of code present in the else block.

This is how execution is done for conditional statements.

Nested if

Example:

Take details of an employee if he is a manager and he had above five years of experience give him 15% bonus. Otherwise, give 10% of bonus. if he is not manager give him 5% of bonus.

if elif else and nested if in python 2
Sample Program

From the above program, we have if condition True. ie if he is a manager.

Inside the if condition one more if the condition is written.

This we call it nested if conditions.

If he is a manager then check inside if he had 5 years of experience or not. based on that update the bonus.