Conditional
Statements in
Python
BY
Y DAYANAND
KUMAR
What is a Conditional
Statement?
Conditional statements help a program make decisions.
They execute different blocks of code based on whether a condition is
True or False.
Types of Conditional
Statements in Python
`if` statement
`if-else` statement
`if-elif-else` statement
Syntax of `if` statement
if condition:
# code block
Executes the code block only if the condition is
true.
Example of `if`
statement
num = 10
if num > 0:
print("Positive number")
Output : Positive number
Syntax of `if-else`
statement
if condition:
# code block 1
else:
# code block 2
Executes one of two blocks depending on the condition
Example of `if-else`
statement
num = -5
if num > 0:
print("Positive number")
else:
print("Negative number")
Output: Negative Number
Syntax of `if-elif-else`
statement
if condition1:
# code block 1
elif condition2:
# code block 2
else:
# code block 3
This is used when checking more than two conditions.
Flowchart Activity-
Check Number Type
Draw a flowchart to check whether a number is positive, zero or
negative.
Example of `if-elif-else`
statement
num = 0
if num > 0:
print("Positive")
elif num == 0:
print("Zero")
else:
print("Negative")
Output : Zero
Individual
Activity-Pass/Fail
Checker
Write a program to check if a student has passed or not.
marks = 45
if marks >= 33:
print("Pass")
else:
print("Fail")
Output : Pass
Debugging task
What’s wrong in this code?
x = 10
if x = 10:
print("x is 10")
Answer:
== should be used instead of = for checking the equality
Quiz
1. Which keyword is used to check multiple conditions?
Ans : if-elif-else
2. What does == mean in Python?
Ans: It checks for equality
Contd…
3. What will this print?
x = 15
if x % 3 == 0 and x % 5 == 0:
print("Divisible by both")
Ans : Divisible by both
Summary
Conditional statements control the flow of a program.
Use `if` for single condition.
Use `if-else` for two alternatives.
Use `if-elif-else` for multiple conditions.
They help us make decisions in programs, just like in real life.
Practice Questions
Write a Python program for each of the following:
1. Check whether a number is even or odd.
2. Check whether a number is divisible by 3 and 5
3. Grade calculator:
•Marks ≥ 90 → A
•Marks ≥ 75 → B
•Marks ≥ 50 → C
•Else → Fail
THANK YOU