0% found this document useful (0 votes)
18 views

Python - Conditionals Relational and Logical Operators

The document provides an overview of Python conditional statements, including if, if-else, and if-elif-else structures, along with examples for checking eligibility to vote and grading. It also covers relational and logical operators, demonstrating their use in combining conditions for decision-making in programs. Additionally, practice questions and answers are included to reinforce understanding of the concepts presented.

Uploaded by

ananyakennedy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Python - Conditionals Relational and Logical Operators

The document provides an overview of Python conditional statements, including if, if-else, and if-elif-else structures, along with examples for checking eligibility to vote and grading. It also covers relational and logical operators, demonstrating their use in combining conditions for decision-making in programs. Additionally, practice questions and answers are included to reinforce understanding of the concepts presented.

Uploaded by

ananyakennedy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

HEARTFULNESS INTERNATIONAL SCHOOL, OMEGA BRANCH ​ 2024 - 25

CLASS : VIII​ ​ ​ ​ ​

TOPIC: Python - Conditional Statements , Relational and Logical Operators

Conditional Statements

Conditional statements allow the program to make decisions and execute different code blocks
based on certain conditions.

Types of Conditional Statements

1.​ if Statement​
Executes a block of code if the condition is true.​


if condition:
# Code to run if the condition is true

PROGRAM 1: Check if a person is eligible to vote​



age = 16
if age >= 18:
print("You are eligible to vote.")

2.​ if-else Statement​


Provides an alternative block of code if the condition is false.


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

PROGRAM 2: Check if a person is eligible to vote or not​



age = 16
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")

3.​ if-elif-else Statement​


Checks multiple conditions, one after the other.​

if condition1:
# Code for condition1
elif condition2:
# Code for condition2
else:
# Code if none of the conditions are true

PROGRAM 3: Grade Checker


marks = 85
if marks >= 90:
print("Grade: A")
elif marks >= 75:
print("Grade: B")
else:
print("Grade: C")
HEARTFULNESS INTERNATIONAL SCHOOL, OMEGA BRANCH ​ 2024 - 25

Relational Operators

Relational operators are used to compare two values. They return True or False based on the
comparison.

List of Relational Operators

Examples Combining Conditional Statements and Relational Operators

PROGRAM 4 : Check if a number is positive or negative​

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

PROGRAM 5: Find the largest of three numbers​

a = int(input("Enter first number:"))


b = int(input("Enter second number:"))
c = int(input("Enter third number:"))

if a > b and a > c:


print("a is the largest.")
elif b > a and b > c:
print("b is the largest.")
else: print("c is the largest.")

Key Points to Remember

1.​ Indentation is important in Python and is used to define code blocks.


2.​ Conditions must always return True or False.
3.​ Relational operators are commonly used with if, elif, and else statements.
4.​ You can combine conditions using logical operators like and, or, and not.
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.

Using Logical Operators with Conditional Statements

Logical operators are often used with if, elif, and else statements to check multiple
conditions.

1.​ Using and:​

PROGRAM 6:

age = 20
has_voter_id = True

if age >= 18 and has_voter_id:


print("You are eligible to vote.")
else:
print("You are not eligible to vote.")

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

2.​ Using or:

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.

3.​ Using not:



PROGRAM 8:
is_weekend = False

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

Combining Logical and Relational Operators

Logical operators are often combined with relational operators to create complex conditions.

PROGRAM 9: Check if a number is between two values


number = 15

if number > 10 and number < 20:


print("The number is between 10 and 20.")
else:
print("The number is not in the range.")

PROGRAM 10: Determine if a student passes a subject


marks = 55
attendance = 75

if marks >= 40 and attendance >= 70:


print("The student has passed.")
else:
print("The student has not passed.")
HEARTFULNESS INTERNATIONAL SCHOOL, OMEGA BRANCH ​ 2024 - 25

Key Points to Remember

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

1.​ Write a program to check if a number is both positive and even.

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.

Clue: A year is a leap year if:

●​It is divisible by 4, and


●​Not divisible by 100
●​unless it is also divisible by 400.

Answers to Practice Questions

1. Check if a number is both positive and even

number = int(input("Enter a number: "))

if number > 0 and number % 2 == 0:


print("The number is both positive and even.")
else:
print("The number is not both positive and even.")

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

print("You are not eligible for a discount.")

3. Determine if a year is a leap year based on multiple conditions

year = int(input("Enter a year: "))

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):


print(year+” is a leap year.")
else:
print((year+” is not a leap year.")

Try these questions on your own.

1.​ Sports Category Finder

Write a Python program to determine which sport category a person is eligible for based on their
age:

●​ If age ≤ 12, print "Eligible for Junior Category."


●​ If age is between 13 and 18, print "Eligible for Teen Category."
●​ If age > 18, print "Eligible for Senior Category."
●​ If age is below 0, print "Invalid age."

2.​ Movie Ticket Price Calculator

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

3.​ Password Strength Checker

Write a Python program to evaluate the strength of a password entered by the user.

●​ If the password length is less than 6 characters, print "Weak Password."


●​ If the password length is between 6 and 12 characters, print "Moderate Password."
●​ If the password length is greater than 12 characters and contains at least one uppercase
letter, one lowercase letter, and one digit, print "Strong Password."
●​ For any other input, print "Invalid Password."

You might also like