4.notes Conditional Statements
4.notes Conditional Statements
CLASS: 8
Learning Objective:
Students will use Python conditional statements to solve problems and
make decisions based on given conditions.
Introduction:
Conditional statements in Python allow you to execute different blocks of code
depending on whether a certain condition is true or false.
In Python, if, if..else, and if..elif..else… are conditional statements used to control
the flow of execution based on certain conditions
1. if Statement
The if statement is used to execute a block of code only if a condition is
true.
Syntax Example
if condition: age = 18
statement 1 if age >= 18:
statement 2 print("You are eligible to vote.")
2. if..else Statement
The if-else statement allows you to execute one block of code if the
condition is true, and another block of code if the condition is false.
Syntax Example
if condition: age = 16
statement 1 if age >= 18:
statement 2 print("You are eligible to vote.")
else: else:
statement 3 print("You are not eligible to
statement 4 vote.")
3. if..elif..else Statement
The if-elif-else statement allows you to check multiple conditions and
execute a different block of code for each condition
Syntax Example
if condition: marks = 75
statement 1 if marks >= 90:
statement 2 print("Grade: A")
elif: elif marks >= 80:
statement 3 print("Grade: B")
statement 4 elif marks >= 70:
else: print("Grade: C")
statement 5 else:
statement 6 print("Grade: D")
Lab Activity:
Write a Python program that accepts marks as input from the user and displays
the corresponding grade along with feedback, based on the grading table below:
If the entered mark is less than 0 or greater than 100, display a message asking
the user to enter a valid mark.
NIM Activity:
Write a Python program that asks the user to enter a color of the UAE flag and
print the meaning of the entered color. If the input is not part of the UAE flag,
then print “That is not a color of the UAE flag”.