๐ฏ Learning Objectives
By the end of this lesson, learners will be able to:
-
Use comparison operators to evaluate conditions
-
Combine multiple conditions using logical operators (
and
,or
,not
) -
Write expressions that return Boolean (
True
/False
) values -
Understand the role of conditions in control flow (used in next module)
๐ Lesson Content
๐น What Are Comparison Operators?
Comparison operators compare two values and return either True
or False
.
Operator | Description | Example (a = 5 , b = 10 ) |
Result |
---|---|---|---|
== |
Equal to | a == b |
False |
!= |
Not equal to | a != b |
True |
> |
Greater than | a > b |
False |
< |
Less than | a < b |
True |
>= |
Greater than or equal to | a >= 5 |
True |
<= |
Less than or equal to | b <= 10 |
True |
๐ธ Examples:
These comparisons return Boolean values (True
or False
), which are crucial for making decisions in programs.
๐น What Are Logical Operators?
Logical operators are used to combine multiple conditions.
Operator | Description | Example | Result |
---|---|---|---|
and |
True if both conditions true | a > 0 and b > 0 |
True |
or |
True if any condition true | a > 0 or b < 0 |
True |
not |
Reverses result (True โ False) | not (a == b) |
True |
๐ธ Examples:
โ Logical operators are often used in if statements and loop conditions.
๐ก Mini Exercise:
Write a Python program that:
-
Takes a number input from the user.
-
Checks if the number is greater than 10 and less than 100.
-
Displays
True
orFalse
accordingly.
๐ง Concept Recap Table
Expression | Evaluates To |
---|---|
10 > 5 |
True |
5 == 5 and 3 < 1 |
False |
not (4 != 4) |
True |
7 <= 7 or 8 > 10 |
True |
๐ Assignment
Create a program that:
-
Takes age as input
-
Checks if the user is between 18 and 60
-
Prints if they are eligible to vote and work