Introduction:
In our previous lesson, we learned how to use if
statements with ==
and !=
to compare values and make decisions based on whether things are equal or not. Now, let’s take things a step further and introduce comparison operators like <
(less than), >
(greater than), <=
(less than or equal to), and >=
(greater than or equal to) to compare numerical values.
Real-World Example:
Imagine you’re checking if you’re tall enough to ride a rollercoaster. The park might have a rule like this:
- If your height is greater than or equal to 140 cm, you can ride.
- Else, you can’t.
This is a perfect example of using comparison operators in an if
statement. Python can compare numbers and decide what happens next!
What is an if
Statement with Numerical Comparisons?
When you use operators like <
, >
, <=
, or >=
, you’re asking Python to compare two numbers. If the comparison is true, your program will run one block of code. If it’s false, it will run something else (in the else
clause).
Here’s what that looks like in Python:
if age >= 18: print("You are an adult.") else: print("You are not an adult.")
This code checks if a person’s age is greater than or equal to 18. If true, the program prints that the person is an adult. If false, it prints the alternative message.
Predict:
Let’s warm up by predicting the outcome of this code. What do you think will be printed?
height = 160 if height < 140: print("You are too short to ride.") else: print("You are tall enough to ride!")
Hint: The <
operator checks if the value of height
is less than 140. Do you think 160 is less than 140?
Run:
Let’s try out an example in the Python IDE that checks if a number is positive, negative, or zero using nested if
statements. This way, we can make multiple decisions by putting one if
statement inside another.
Explanation:
- The first
if
checks if the number is greater than 0 (positive). - If it’s not, the program checks another
if
statement to see if the number is equal to 0. - If neither of these conditions are true, it assumes the number is negative in the
else
clause.
This approach allows you to structure your conditions step by step.
In this example:
- The outer
if
checks whether the number is not zero. - If it’s not zero, it enters the nested
if
statement:- If the number is greater than 0, it prints that the number is positive.
- Else, it prints that the number is negative.
- If the outer
if
is false (i.e., the number is zero), theelse
clause handles that case.
By using nested if
statements, you can see how we can test multiple conditions without needing to use elif
yet. In the next lesson, we’ll simplify this further by using the elif
statement!
Investigate:
Now, let’s explore how comparison operators work when we add more conditions. The following code checks if a student has passed or failed based on their test score. Run it in the IDE, and then change the score
variable to see how the output changes.
Instructions:
- Change the value of
score
and see how the program behaves. - What happens if the score is 50? What if it’s below 50?
This example shows how comparison operators can be used to check if a value falls within a range.
Modify:
Here’s a piece of code that has a small mistake. It’s supposed to check if someone is eligible for a discount based on their age. Can you correct it?
Hint: There is nothing wrong with the logic, but can you explain why we used and
? It combines two conditions: the person’s age must be greater than 13 and less than or equal to 18. Play around with the values and see how the logic behaves!