Conditional Statements in Python

Rina Mondal
3 min readMar 22, 2024

--

Conditional statements are essential in programming, allowing developers to control the flow of execution based on certain conditions.

An if-else statement allows you to execute different blocks of code based on whether a condition is true or false.

Here’s the basic syntax:

if condition:
# code block to execute if the condition is True

else:
# code block to execute if the condition is False

In this article, we will discuss about many usages of an if-else statement:

#1. To Check whether a given input is greater than 5 or not

x = 10
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")

#Output: x is greater than 5
#2. You can also chain multiple conditions using `elif` (short for "else if"):
y = 7
if y > 10:
print("y is greater than 10")
elif y == 10:
print("y is equal to 10")
else:
print("y is less than 10")

#Output: y is less than 10
# 3. Checking if a Number is Even or Odd
num = 10
if num % 2 == 0:
print("The number is even")
else:
print("The number is odd")

#Output: The number is even
# 4. Determining the Maximum of Three Numbers
a = 5
b = 10
c = 7
if a >= b and a >= c:
print("The maximum number is:", a)
elif b >= a and b >= c:
print("The maximum number is:", b)
else:
print("The maximum number is:", c)

# Output:The maximum number is: 10
# 5. Checking Leap Year
year = 2024
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(year, "is a leap year")
else:
print(year, "is not a leap year")

#Output: 2024 is a leap year

These examples demonstrate the versatility of if-else statements in Python, showing how they can be used to handle various scenarios and make decisions based on different conditions.

Nested if-else:

Nested if-else statements can be used to handle multiple levels of conditions. This is useful when you need to check for more specific conditions based on the outcome of previous conditions.

#Nested if-else statement
x = 10
y = 5
if x > 5:
if y > 2:
print("Both x and y are greater than their respective thresholds")
else:
print("x is greater than 5, but y is not greater than 2")
else:
print("x is not greater than 5")

Ternary Operator:

Python also supports a ternary operator, which provides a concise way to write conditional expressions.

# Ternary Operator: 
x = 10
result = "greater than 5" if x > 5 else "less than or equal to 5"
print("x is", result)

Assert Statement:

The “assert” statement is used for debugging purposes to ensure that a condition is true. If the condition evaluates to false, an “AssertionError” exception is raised.

 x = 10
assert x > 5, "x must be greater than 5"

Understanding how to use if-else statements effectively is crucial for writing clear and concise Python code. They enable your programs to respond dynamically to different situations, making them more versatile and powerful.

Tutorial on if-else condition for detailed explanation:

Interview Session- Programmings on if-else Conditional Statement:

--

--

Rina Mondal

I have an 8 years of experience and I always enjoyed writing articles. If you appreciate my hard work, please follow me, then only I can continue my passion.