Python - Conditionals Relational and Logical Operators
Python - Conditionals Relational and Logical Operators
CLASS : VIII
Conditional Statements
Conditional statements allow the program to make decisions and execute different code blocks
based on certain conditions.
1. if Statement
Executes a block of code if the condition is true.
if condition:
# Code to run if the condition is true
if condition:
# Code to run if the condition is true
else:
# Code to run if the condition is false
HEARTFULNESS INTERNATIONAL SCHOOL, OMEGA BRANCH 2024 - 25
if condition1:
# Code for condition1
elif condition2:
# Code for condition2
else:
# Code if none of the conditions are true
Relational Operators
Relational operators are used to compare two values. They return True or False based on the
comparison.
number = -7
if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
else:
print("The number is zero.")
HEARTFULNESS INTERNATIONAL SCHOOL, OMEGA BRANCH 2024 - 25
Logical Operators
Logical operators in Python are used to combine multiple conditions. These operators return
True or False based on the logic applied to the conditions.
Logical operators are often used with if, elif, and else statements to check multiple
conditions.
PROGRAM 6:
age = 20
has_voter_id = True
Output:
If age = 20 and has_voter_id = True, the output will be:
You are eligible to vote.
HEARTFULNESS INTERNATIONAL SCHOOL, OMEGA BRANCH 2024 - 25
PROGRAM 7:
is_raining = False
has_umbrella = True
if is_raining or has_umbrella:
print("You can go outside.")
else:
print("Stay indoors.")
Output:
If is_raining = False and has_umbrella = True, the output will be:
You can go outside.
if not is_weekend:
print("It's a weekday. Time for work!")
else:
print("Enjoy the weekend!")
Output:
If is_weekend = False, the output will be:
It's a weekday. Time for work!
HEARTFULNESS INTERNATIONAL SCHOOL, OMEGA BRANCH 2024 - 25
Logical operators are often combined with relational operators to create complex conditions.
1. and: All conditions must be true for the result to be True.
2. or: At least one condition must be true for the result to be True.
3. not: Negates (reverses) the result of the condition.
4. Logical operators can be combined with relational operators to evaluate complex
expressions.
HEARTFULNESS INTERNATIONAL SCHOOL, OMEGA BRANCH 2024 - 25
Practice Questions
Clue: A positive number must be greater than zero, The number must be divisible by two
(or the number/2 must give a zero)
2. Check if a person is eligible for a discount if they are either a student or a senior citizen.
Clue: Ask the user to input who they are (either a "student", "senior citizen", or "other")
3. Write a program to determine if a year is a leap year based on multiple conditions.
2. Check if a person is eligible for a discount if they are either a student or a senior
citizen
is_student = input("Are you a student? (yes/no): ").lower() == "yes"
is_senior = input("Are you a senior citizen? (yes/no): ").lower() ==
"yes"
if is_student or is_senior:
print("You are eligible for a discount.")
else:
HEARTFULNESS INTERNATIONAL SCHOOL, OMEGA BRANCH 2024 - 25
Write a Python program to determine which sport category a person is eligible for based on their
age:
Write a Python program to determine the ticket price for a movie based on the user's age and
the showtime:
● If the user is under 12 or over 60, and the showtime is before 5 PM, the price is $5.
● If the user is under 12 or over 60, and the showtime is after 5 PM, the price is $7.
● If the user is between 12 and 60, and the showtime is before 5 PM, the price is $10.
● Otherwise, the price is $12.
HEARTFULNESS INTERNATIONAL SCHOOL, OMEGA BRANCH 2024 - 25
Write a Python program to evaluate the strength of a password entered by the user.