Sitemap

If Else Statements in Python

3 min readJun 27, 2026

--

The image is taken from YoungWonks representing an ifelse statement in python

Hi! My name is Faizan Ahmed and I am currently pursuing Software Engineering. I am currently in my 4th semester and I started learning python, I have learned C++, C and some other languages. Python is by far the easiest language to learn. So, I learned about if else statements few days ago and I thought of writing an article about it and sharing with you guys what I have learned.

If else statements are used to choose an option from a number of options based on some specific conditions.

We use a number of conditions and if those conditions become true, we execute the statements which are under that condition. If else statements let us choose only ONE option from a number of options.

If else can be most easily learned by looking at an example of a simple calculator program. In the calculator program given below, we input two numbers and one operator from the user. We then check conditions using if else statements to run the correct arithmetic operation based on the operator.

num1 = float(input(“Enter a number: “))
num2 = float(input(“Enter another number: “))
op= input(“Enter an operator:”)if op == “+”:
print(num1 + num2)
elif op == “-”:
print(num1 - num2)
elif op ==“*”:
print(num1 * num2)
elif op ==“/”:
if num2==0:
print(“Cannot divide by zero”)
else:
print(num1/num2)
else:
print(“Invalid operator”)

In the program written above, we first check that if the operator which have been saved in the variable op is a “+“ operator using an if statement, If it is a “+“ operator then we execute the addition statement written under the op==”+” condition.

Get Faizan Ahmed’s stories in your inbox

Join Medium for free to get updates from this writer.

In python, we don’t use brackets like C++ or C to show the statements which belong to an if clause, we use indentation and colon after if and elif.

If the operator is not a “+“, we go to the elif part, which in C++ or C is written as else if, it has been just shorten to elif in python. There are three elif parts, in which we check whether the operation is -, * or /.

The last elif part, the “/“ one also has one if else statement, which is to check that whether the num2 which the user has entered is zero or not. As, dividing by zero is not allowed in mathematics.

The last part is the else part, when all the conditions return false, then the else part is executed, which in this case is printing invalid operator.

One thing to remember is that if statement can be executed alone, but without if statement, elif and else can’t exist and the compiler will through an error.

So, that’s if else statements in python. If you’re a programmer, you know that if else statements are used everywhere in programming and are a critical part of every program. Everyone should learn if else statements.

So, that’s all from my side, will meet you guys in my next article. Till then remember one thing, LIFE GOES ON UNTIL IT GOES OFF, and byyyeeeeeeeeeeee!

--

--

Faizan Ahmed
Faizan Ahmed

Written by Faizan Ahmed

Hi! my name is Faizan and I write about nearly everything but mostly fun stuff or nerd stuff