Booleans, Logic, and Loops
Get an understanding of Booleans, if statement logic, for loops, and while loops.
We'll cover the following...
We'll cover the following...
Booleans
The last data type is bool, short for Boolean. A bool can only have one of two values: True or False. By themselves, bools are not that useful. Their usefulness comes from doing logical comparison operations like equals and does not equal. Below is a list of the logical comparisons in Python:
| Operation | Python syntax |
|---|---|
| Equals | == |
| Not equals | != |
| Less than | < |
| Less than or equal to | <= |
| Greater than | > |
| Greater than or equal to | >= |
| At least one condition is true | or |
| Both conditions are true | and |
Here are some examples of using logical comparisons:
Python 3.8
print("1 == 2 -->", 1 == 2)print("100000 > 1 -->", 100000 > 1)print("5 > 4 and 9 > 8 -->", 5 > 4 and 9 > 8)print("3 > 2 or 1 > -1 -->", 3 > 2 or 1 > -1)print("(3 > 2) or (1 > -1) -->", (3 > 2) or (1 > -1))