What are Conditional Statements?
Conditional statements are used to make decisions in a Python program.
They execute different blocks of code
depending on whether a condition is
True or False.
if,
elif and
else
to make decisions.
1. if Statement
The if statement executes
code when a condition is True.
age = 20
if age >= 18:
print("You are an adult")
You are an adult
2. if-else Statement
The else block executes when
the if condition is False.
age = 16
if age >= 18:
print("Eligible")
else:
print("Not Eligible")
Not Eligible
3. elif Statement
The elif statement is used
when we need to check multiple conditions.
marks = 75
if marks >= 90:
print("Grade A+")
elif marks >= 75:
print("Grade A")
elif marks >= 60:
print("Grade B")
else:
print("Grade C")
Grade A
4. Nested if Statement
A nested if means using one
if statement inside another
if statement.
age = 20
has_id = True
if age >= 18:
if has_id:
print("Entry allowed")
Entry allowed
5. Practical Example
Conditional statements can be used to check whether a number is positive, negative or zero.
number = 10
if number > 0:
print("Positive")
elif number < 0:
print("Negative")
else:
print("Zero")
Positive
Python uses indentation to define the block of code inside
if, elif
and else.
Quick Quiz
Which keyword is used when checking another condition?
Finished this lesson?