0% found this document useful (0 votes)
4 views3 pages

Python Handout 3

The document provides an overview of relational operators and conditional statements in Python, detailing their usage and examples. It explains common relational operators like equal to, not equal to, greater than, and less than, along with the if, if-else, and elif statements for executing code based on conditions. Additionally, it includes programming challenges for practicing these concepts.

Uploaded by

salmakhatun0225
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Python Handout 3

The document provides an overview of relational operators and conditional statements in Python, detailing their usage and examples. It explains common relational operators like equal to, not equal to, greater than, and less than, along with the if, if-else, and elif statements for executing code based on conditions. Additionally, it includes programming challenges for practicing these concepts.

Uploaded by

salmakhatun0225
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Handout

Topics : Relational Operators and if statement

Relational Operators:

• Relational operators are used to compare values in Python.


• Common relational operators include equal to (==), not equal to (!=), greater than (>),
less than (<), greater than or equal to (>=), and less than or equal to (<=).
• They return either True or False based on the comparison result

Operator Description Example

== Equal to x == y

!= Not equal to x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y

Examples:

# Equal to
result = (5 == 5) # result is True

# Not equal to
result = (5 != 3) # result is True

# Greater than
result = (10 > 5) # result is True

# Less than
result = (3 < 5) # result is True

# Greater than or equal to


result = (7 >= 7) # result is True

# Less than or equal to


result = (4 <= 2) # result is False

Conditional Statements:
• Conditional statements allow you to execute different blocks of code based on certain
conditions.
• The if statement is used to execute a block of code if a condition is True.
• The if-else statement is used to execute one block of code if the condition is True and
another block if it's False.
• The elif statement is used to check multiple conditions sequentially.

Examples on if statement

Program 1: Check if a Number is Positive

number = 5

if number > 0:

print("The number is positive.")

Explanation: This program checks if the variable number is greater than zero. Since 5 is greater
than 0, it prints "The number is positive."

Program 2: Determine if a Person is a Teenager

age = 13
if age >= 13 and age <= 19:
print("You are a teenager!")

Explanation: This program checks if the variable age falls within the range that defines teenagers
(13 to 19 years old). The if statement uses logical operators to ensure the age is between these
values. Since 13 is within this range, it prints "You are a teenager!"

Program 3: Check for Passing Grade


grade = 75
if grade >= 70:
print("You passed the exam.")

Explanation: This program evaluates whether the grade is 70 or higher, which is often the
threshold for passing. Since the grade is 75, the program prints "You passed the exam."

Program 4: Determine Eligibility for a Competition


age = 18 if age >= 18:

print("You are eligible to participate in the competition.")

Explanation: This program checks if a person is 18 years old or older to determine if they are
eligible to participate in an adult competition. Since the age is 18, it prints "You are eligible to
participate in the competition."

Program 5: Verify Login Credentials

username = "admin"

password = "12345"

if username == "admin" and password == "12345":

print("Access granted!")

Explanation: This program checks if both the username and password match a predefined set of
credentials. Since both the username and password match correctly, the program prints "Access
granted!"

Homework

Use only separate if statements.

Challenge 1: Number Properties - Write a program that takes a number as an input and prints:

o "Positive" if number > 0

o "Negative" if number < 0

o "Zero" if number == 0

Challenge 2: Divisibility Check - Write a program that takes a number as an input and prints:

o "Divisible by 2" if number % 2 == 0

o "Divisible by 3" if number % 3 == 0

o "Divisible by both 2 and 3" if number is divisible by both

Challenge 3: Salary Bonus - Write a program that takes a salary as an input and prints:

o "Bonus 10%" if salary ≥ 50000

o "Bonus 5%" if salary ≥ 30000

o "No Bonus" if salary < 30000

You might also like