< eelmine | Table of Contents | next > |
3.1 BOOLEAN AND COMPARISON OPERATORS
The Boolean data, type represented as bool
, is a built-in data type that Python offers for handling logical values. It can take two possible values: True or False. Booleans represent the truth values in the context of comparison operations, such as comparing whether one number is larger than another. They are primarily used in conditional statements and loops to control the flow of a program based on certain conditions.
COMPARISON OPERATORS
Boolean comparison operators are fundamental tools that allow you to compare values and evaluate conditions, leading to Boolean outcomes: True or False. These operators are the building blocks of decision-making in your code. Here’s an overview of all the comparison operators in Python.
Note that we can compare type int
with type float
as Python will handle the type conversion automatically. However, we cannot compare strings to numeric types. We will cover string comparison in more detail later in the course.
EQUAL TO (==)
Checks if the values on both sides of the operator are equal. If they are, the result is True; otherwise, it's False.
print(5 == 5) # Output: True print(3 == 5) # Output: False print(4.5 == 5) # Output: False
NOT EQUAL TO (!=)
Checks if the values on both sides of the operator are not equal. If they aren't, the result is True; if they are, it's False
print(10 != 10) # Output: False print(7 != 5) # Output: True print(10.5 != 10) # Output: True
GREATER THAN (>)
Check if the value on the left side of the operator is greater than the value on the right side. If it is, the result is True; otherwise, it's False.
print(5 > 3) # Output: True print(3 > 5) # Output: False print(11.25 > 25) # Output: False
LESS THAN (<)
Check if the value on the left side of the operator is less than the value on the right side. If it is, the result is True; otherwise, it's False.
print(5 < 3) # Output: False print(3 < 5) # Output: True print(15.5 < 30.56) # Output: True
GREATER THAN OR EQUAL TO (>=)
Check if the value on the left is greater than or equal to that on the right. If it is, the result is True; otherwise, it's False.
print(5 >= 5) # Output: True print(3 >= 5) # Output: False print(30.5 >= 30.1) # Output: False
LESS THAN OR EQUAL TO (<=)
Check if the value on the left is less than or equal to that on the right. If it is, the result is True; otherwise, it's False.
print(5 <= 5) # Output: True print(16 <= 10) # Output: False print(45.8 <= 50.2) # Output: True
KEY THINGS TO REMEMBER
- Boolean data type has two values:
True
andFalse
. - Equal to
==
returnsTrue
if both sides are equal and otherwiseFalse
- Not Equal to
!=
returnsTrue
if both sides are not equal and otherwiseFalse
- Greater than
>
returnsTrue
if the left side is greater than the right side, otherwiseFalse
- Less than
<
returnsTrue
if the left side is less than the right side, otherwiseFalse
- Greater than or equal to
>=
returnsTrue
if the left side is greater than or equal to the right side, otherwiseFalse
- Less than or equal to
<=
returnsTrue
if the left side is less than or equal to the right side, otherwiseFalse
SELF-CONTROL EXERCISES
< eelmine | Table of Contents | next > |