not are the three basic types of boolean operators that are present in math, programming, and database logic.
In other programming languages, you might have seen the concept of
and represented with
or, represented with
not represented by
!. The Python language is instead focused on readability. So we’ll use the english
and instead of trying to remember fancy symbols. Python still uses the
! expressions, but they’re used for bitwise operations.
You can use them to compare one (or more expressions) and determine if they evaluate to
Thankfully, you don’t have to be a computer scientist to understand them if you use this handy table.
||if a is False, then b, else a|
||if a is False, then a, else b|
||if a is false, then
a and b, if a is false, a is returned. Otherwise b is returned.
a and b are both
boolean values, the expression evaluates to
True if both a and b are
>>> a = True # a is True >>> b = True >>> a and b # True is returned. (value of b) True >>> a = False # a is False >>> b = True >>> a and b # False is returned. (value of a) False >>> a = False # a is False >>> b = False >>> a and b # False is returned. (value of a) False
Notice what happens when do the same thing to values that have a “truthiness” to them.
>>> bool(0) # Verify that zero is "falsey" False >>> bool(1) # Verify that one is "truthy" True >>> 0 and 1 # 0 is False. 0 is returned. 0
a or b, if a is false, b is returned. If a is true, a is returned.
a or b evaluates to
True if either (or both) of the expressions are true.
>>> a = True # a is true >>> b = True >>> a or b # True is returned (value of a) True >>> a = False # a is false >>> b = True >>> a or b # True is returned (value of b) True >>> 0 or 1 # 0 is false. Return 1. 1
not a reverses the
boolean value of
a. If it was true, it will return
False. If it was false, it will return
>>> a = True >>> not a # not returns the opposite. True -> False False >>> a = False >>> not a # not returns the opposite. False -> True True
And again, with numbers. Remember, zero is considered
False, any other number is considered
>>> bool(1) True >>> not 1 False >>> bool(0) False >>> not 0 True
When combining multiple boolean operators, you can add optional parenthesis for readability.
>>> a = True >>> b = True >>> c = False >>> a and (b or c) True
You can combine multiple operators to test complex assumptions. For example, to return
True only if both values are
False, we can use the
not negation operation on the result of an
>>> a = False >>> b = False >>> a or b # False because both are False. False >>> not (a or b) # True - checking if both are False. True
Remember, we learned that some values in Python are falsey like the number zero, and some are truthy like any number expect for zero.
It’s a little counter intuitive, but when we compare values other than
booleans, our code behaves a little differently.
||if x is false, then y, else x|
||if x is false, then x, else y|
Let’s see it in action. First, lets test our assumptions again.
>>> bool(0) # Truthiness of 0 is False False >>> bool(1) # Truthiness of 1 is True True >>> bool(None) # Truthiness of None type is False False >>> 1 or 0 # Returns 1, the True value 1 >>> 1 and 0 # Returns 0, the False value 0 >>> 0 or None # Neither are True. Returns nothing (None)