If, Elif, and Else Statements in Python

Introduction

In real life, often we make a decision to perform a certain task. Further actions are carried out depending on the correct decision being made. But the computer can't think with its own intellect. We train them through programming. Here comes the importance of If Else. Let's understand it through an example.

For example, A film intended for persons above 18 years of age. If we create a program that can tell us whether or not a person can watch a movie based on a given age, the logic would be as follows.

if age > 18, he/she can watch the movie.

if age < 18, he/she can't watch the movie.

This logic can build simply by using the if-else condition. So, in this tutorial, you will learn everything about if else statements in python.

If Statement

Let's begin with the condition we discussed above. Here, we will create a simple program that will tell us if a person can watch a film depending on his/her age.


age = int(input("Enter your age: "))

if age > 18:
print("You can watch this movie.")
if age < 18:
print("You can't watch this movie.")

Output

Enter your age: 17
You can't watch this movie.

Else Statement

If you notice carefully, we have used two if statements in the above program to make a decision. You know, the same problem can be solved a little differently. In this case, we'll add an else statement in place of the second if statement. See the program below.


age = int(input("Enter your age: "))

if age > 18:
print("You can watch this movie.")
else:
print("You can't watch this movie.")

Output

Enter your age: 25
You can watch this movie.

Elif Statement

Suppose, there are several conditions and we have to go through each one to make the correct decision; In this case, if the first condition fails, we can implement the rest of the conditions using the elif statement (short for else if).

Here, we will create a program for a grading system that will tell the corresponding grade of a number.


print("Grade System")

marks = int(input("Enter your marks: "))
if marks > 90:
print("Outstanding!")
elif marks > 80:
print("A+")
elif marks > 60:
print("A")
elif marks > 45:
print("B+")
elif marks > 30:
print("B")
else:
print("Fail")

Output

Grade System
Enter your marks: 91
Outstanding!

Conditional If Else

We can add further conditions to the if-else statements using 'and', 'or' keywords. See the python program below. We added both 'name' and 'age' to the if-else conditions to tell us whether a person can watch or download a movie.


print("Get permission to Watch Fifty Shades of Gray")

name, age = input("Enter your Name and Age: ").split(',')

if name == 'Smith' and int(age) > 18:
print("You can watch the movie")
elif name == 'Smith' or name == 'Alex' and int(age) >= 17 :
print("You can download this movie")
else:
print("You can't watch or download this movie")

Output

Get permission to Watch Fifty Shades of Gray
Enter your Name and Age: Alex, 17
You can download this movie

In Keyword in Python

Here, the code will find if a particular character is present or not in a string.


name = 'reverse shell'
if 'e' in name:
print("e is Present ")
else:
print("Not present")

Output

e is Present

Check if a String is Empty or Not

This python program will check if a string is empty or not using the If Else statement.


name = 'Python'
if name:
print("All is OK!")
else:
print("There is no name to print")

Output

All is OK!

If Else in One line

You can also declare if else statements in one line.


age = 17

print("It's OK!") if age > 18 else print("Not OK!!")

Output

Not OK!!

Subhankar Rakshit

Meet Subhankar Rakshit, a Computer Science postgraduate (M.Sc.) and the creator of PySeek. Subhankar is a programmer, specializes in Python language. With a several years of experience under his belt, he has developed a deep understanding of software development. He enjoys writing blogs on various topics related to Computer Science, Python Programming, and Software Development.

Post a Comment (0)
Previous Post Next Post