Boolean Logic
You have seen in the previous sections how it was possible to define conditions using the comparison operators (>, <). In this section we will find out how it is possible to create more complex conditions using Boolean logic using Boolean operators (AND, OR and NOT).
These operators allow you to combine multiple conditions that make use of comparison operators to get more complex.
For example, if I want a condition that returns True for all values between 0 and 5, I can use the and operator.
>>> x = 3
>>> x >= 0 and x <= 5
True
In fact, inserting and between two conditions I want both at the same time to be verified to obtain True as the value returned.
If you want a condition that returns True, for all values that are less than zero or greater than 5, you can use the or operator.
>>> x = 7
>>> x < 0 or x > 5
True
In fact, by inserting or between two conditions you want at least one of the two to be verified to get True as return value.
Finally, if you want a condition that returns False when a condition is verified and True when it is not, I will use the not operator.
>>> x = 7
>>> not x > 5
False