A Boolean expression is an expression that evaluates to either True
or False
. For example,
result1 = True
result2 = False
print(result1) # True
print(result2) # False
Here, result1 represents True
boolean value and result2 represents False
boolean value.
Python Comparison Operators
Python has a set of comparison operators that allow us to compare two values. If the comparison is right, we get True
and if the comparison is wrong, we get False
.
Let's see an example,
number = 5
print(number < 10) # True
number = 15
print(number < 10) # False
Here, the <
comparison operator is used to compare whether number is less than 10 or not.
List of Python Comparison Operators
Operator | Meaning |
---|---|
Less than | |
> | Greater than |
== | Is equal to |
!= | Not equal to |
>= | Greater than or equal to |
Less than or equal to |
Examples: Comparison Operators
# comparison operators in action
number = 15
print(number > 10) # True
number = 10
print(number > 10) # False
number = 10
# equal to
print(number == 10) # True
number = 10.0
# comparing float and integer
print(number == 10) # True
number = '10'
# comparing string and integer
print(number == 10) # False
number = '10'
# not equal to
print(number != 10) # True
number = 10
# less than or equal to
print(number <= 10) # True
number = 10
# greater than or equal to
print(number >= 10) # True
Note: Comparison operators are used in decision-making and loops.
Python Logical Operators
Python also has three logical operators that operate on the boolean values. Here's a list of the logical operators:
Logical Operator | Meaning |
---|---|
and |
True if both operands are True |
or |
True if either of the operands is True |
not |
True if the operand is False |
and Operator in Python
If both of the expressions are True
, then the result is True
.
age = 22
gpa = 3.8
result = age >= 18 and gpa > 3.6
print(result) # True
However, if either of these expressions is False
, the result is False
.
age = 22
gpa = 3.8
print(age >= 18 and gpa > 3.9) # False
or Operator in Python
If either of the expressions is True
, then the result is True
. If both expressions are False
, only then the result is False
.
age = 22
gpa = 3.8
print(age >= 18 or gpa > 3.9) # True
print(age <= 18 or gpa > 3.9) # False
not Operator in Python
The not
operator gives the complement of a given value:
- If the value is
True
, it returnsFalse
. - If the value is
False
, it returnsTrue
.
Let's see an example,
result = True
print(result) # True
result = True
print(not result) # False
Example: Python Booleans, Comparison and Logical Operators
language = 'Python'
print(language == 'python') # False
age = 18
print(age >= 18) # True
print(age > 18) # False
print(age >= 18 and language == 'Java') # False
Output
False True False False
Truthy and Falsy Values in Python
The values that evaluates to True
are Truthy values and the values that evaluates to False
are Falsy values.
In Python these values are considered falsy.
None
False
- 0, 0.0
- empty strings, lists, dictionaries etc.
All other values are considered truthy.
Example: Truthy and Falsy
Suppose we need to find the smallest number between three numbers.
n1 = 60
n2 = 80
n3 = 50
if n1 < n2 and n3:
print(n1)
elif n2 < n1 and n3:
print(n2)
else:
print(n3)
Output
60
Here, we are getting 60 as the smallest number, which is wrong.
Can you find out where the error is?
Take a look at this code:
if n1 < n2 and n3:
print(n1)
Here, n1 < n2
evaluates to True
, and n3
also evaluates to True
. So the statement inside the if
block is executed.
Instead here's what we should have written:
if n1 < n2 and n1 < n3:
print(n1)
elif n2 < n1 and n1 < n3:
print(n2)
Now we will get the actual smallest number i.e 50.
Recommended Reading: Python match...case