Conditional Execution using If, Ifelse and Elif in Python

Many times, in programming, we need our programs to execute a particular part of code depending upon certain conditions.  This is called Conditional Execution. This function is mostly performed using if, if-else, and elif in python.

This article Conditional Execution using if, if-else and elif in Python is a part of our Python learnings as from the below list:

We are looking to add more and more leaning articles soon!

Example of If, If-else and Elif in Python

For example, suppose a company wishes to give a bonus to an employee who has worked for more than 5 years and only salary to someone has worked for less than 5 years. If we are writing a code for this we would need a condition to be checked (term of an employee) and perform a different action if that condition satisfies and a different action if it doesn’t.

In Python, we can perform such conditional execution using conditional statements such as if, if-else and elif.

Conditional Statements in Python are:

  • if statement
  • if-else statement
  • elif statement

if statement in Python

The if statement in Python is used to check a particular condition. If the condition is satisfied, a specific block of code runs. If the condition is not satisfied, that specific block of code doesn’t run and we do not receive output.

#If statement example

x = 3
if x > 2:
    print('x is greater than 2')
x is greater than 2  #Output

Here, we can see that we specify the condition after the if keyword and end the statement with a colon character (:) and the following lines of code are indented.

x = 1
if x > 2:
    print('x is greater than 2')

Here, in this case, we do not receive any output since the if condition is not satisfied.

We can overcome this problem by using another control statement.

if-else statement in Python

Since we do not receive an output in the if statement if the condition is not satisfied, we use else keyword which runs another block of code when the condition is not met and gives us the required output. Therefore, here we receive an output when the condition is met and also when the condition is not met.

#If else statement example
#To check for whether a number is positive or negative
x = 2
if x > 0:
    print("It is  a positive number")
else:
    print("It is a negative number")
It is a positive number  #Output
x = -2
if x > 0:
    print("It is  a positive number")
else:
    print("It is a negative number")

It is a negative number # Output

Here, we can see that we get an output even when the if condition is not met.

However, there is an issue with the else part since it gives us an output when the condition is not met. The output that we receive sometimes is not valid.

x = 0
if x > 0:
    print("It is  a positive number")
else:
    print("It is a negative number")

It is a negative number # Output

Here, we can see that our if condition is not satisfied but the number entered is 0 and not negative, so we require to add more condition to make it more apt for the examples like these. Therefore, else in Python gives us an output when the condition is not satisfied. To deal with such situations we can use elif statements.

Learn: The best python courses available online

elif statement in Python

The elif statement helps us to write multiple if conditions and the specific block of code is executed where the condition is met.  We can have any number of elif statements in our program however, elif statements are optional.

Suppose, we want to modify the above example and write one more condition to identify zero, we can do it as follows:

Elif statement example

x = 0
if x > 0:
    print("It is  a positive number")
elif x == 0:
    print("It is a  zero")
else:
    print("It is a negative number")

It is a zero # Output

This block of code separately identifies zero and hence we can get proper output for any number.

Feel free to ask any query on the Python forums.

This article is written by Jinal Shah. Please share your views and feedback.

About the Author

Jinal Shah

Graduate in Applied Statistics & Analytics (NMIMS) and Aspiring Actuary

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.