Lesson 1 of 0
In Progress
Lesson #6: Boolean
Python uses boolean logic to evaluate conditions and get True
or False
. The boolean values True and False are returned when an expression is compared or evaluated.
💡 Note: True and False both start with capital letters.
print(50 > 51) # False
print(34 == 20) # False
print(10 < 15) # True
print(34 == 20) # False
print(10 < 15) # True
💡 Note: Comparison between two variables is done using the double equals operator “==”.
Print a message based on whether the condition is True
 or False
:
num_1 = 1
num_2 = 2
if num_1 > num_2:
print(“num_1 is greater than num_2”)
else:
print(“num_1 is not greater than num_2”)
num_2 = 2
if num_1 > num_2:
print(“num_1 is greater than num_2”)
else:
print(“num_1 is not greater than num_2”)
The following table describes these Boolean comparators:
Boolean Comparator | Example | Meaning |
---|---|---|
== | num_1 == num_2 | num_1 equal to num_2 |
!= | num_1 == num_2 | num_1 not equal to num_2 |
>= | num_1 >= num_2 | num_1 greater than or equal to num_2 |
<= | num_1 <= num_2 | num_1 less than or equal to num_2 |
> | num_1 > num_2 | num_1 greater than to num_2 |
< | num_1 < num_2 | num_1 less than to num_2 |
Conditional expressions are not limited to comparing numbers. You can also use them to compare values such as strings:
print(c > c) # False
print(a == a) # True
print(a < b) # True
print(a == a) # True
print(a < b) # True
In Python, strings are ordered lexicographically, which means they’re ordered as they would appear in a dictionary.