In this tutorial, we will learn about elif statements in Python. We will cover the basics of conditional execution using if-elif-else statements.
An elif statement is a conditional statement that allows multiple conditions to be tested sequentially. It provides a way to execute different code blocks based on different conditions.
The syntax for the elif statement in Python is:
if condition1:
# Code block to execute if condition1 is true
elif condition2:
# Code block to execute if condition2 is true
else:
# Code block to execute if none of the conditions are true
The elif statement evaluates the specified conditions in order. The first condition that is true will have its code block executed; if none of the conditions are true, the code block inside the else statement is executed.
num
.num
.num
is positive, negative, or zero.num
is positive, negative, or zero.num = -5
if num > 0:
print(f"{num} is positive.")
elif num < 0:
print(f"{num} is negative.")
else:
print(f"{num} is zero.")
-5 is negative.
marks
.marks
.marks
.marks = 85
if marks >= 90:
print("Grade: A")
elif marks >= 80:
print("Grade: B")
elif marks >= 70:
print("Grade: C")
elif marks >= 60:
print("Grade: D")
else:
print("Grade: F")
Grade: B
temperature
.temperature
.temperature
.temperature = 75.5
if temperature > 100:
print("It's extremely hot.")
elif temperature > 85:
print("It's hot.")
elif temperature > 60:
print("It's warm.")
elif temperature > 32:
print("It's cold.")
else:
print("It's freezing.")
It's warm.