Boolean Operators in Python (and, or, not)
Python provides Boolean operators, and, or, and not. These are used, for example, when describing the relationship between multiple conditions in an if statement.
See the following article for bitwise operations on each bit of an integer. Use & and | instead of and and or.
For more details on if statements, see the following article.
and (Logical AND)
and returns the logical AND of two values.
print(True and True)
# True
print(True and False)
# False
print(False and True)
# False
print(False and False)
# False
In practice, it is often used not with True and False but with conditional expressions, such as those using comparison operators (<, >, etc.). The same applies to or and not.
a = 10
print(0 < a)
# True
print(a < 100)
# True
print(0 < a and a < 100)
# True
Multiple comparisons can be chained as follows.
print(0 < a < 100)
# True
or (Logical OR)
or returns the logical OR of two values.
print(True or True)
# True
print(True or False)
# True
print(False or True)
# True
print(False or False)
# False
not (Negation)
not returns the negation of a value. True and False are inverted.
print(not True)
# False
print(not False)
# True
Precedence of and, or, not
The precedence of Boolean operators is not > and > or (not is the highest precedence).
In the sample code below, the first expression is equivalent to the second one. It does not matter if there are extra parentheses (), so it may be clearer to explicitly write them.
print(True or True and False)
# True
print(True or (True and False))
# True
If you want to calculate or before and, enclose or with parentheses.
print((True or True) and False)
# False
Comparison operators such as <, > have higher precedence than Boolean operators. This means parentheses () are generally not required to group comparison operations.
print(0 < a and a < 100)
# True
However, to clarify operation order or when mixing different types of operators, using parentheses can be helpful.
See the official documentation below for a summary of operator precedence in Python.
Boolean operators with non-bool objects
Boolean operators and, or, not can evaluate non-bool objects, such as numbers, strings, and lists, as truth values.
The following objects are considered false, as in the official documentation above.
- Constants defined to be false:
NoneandFalse - Zero of any numeric type:
0,0.0,0j,Decimal(0),Fraction(0, 1) - Empty sequences and collections:
'',(),[],{},set(),range(0)
All other objects are considered true.
bool() can be used to explicitly convert objects to bool type, but note that even strings '0' or 'False' are converted to True.
print(bool(10))
# True
print(bool(0))
# False
print(bool(''))
# False
print(bool('0'))
# True
print(bool('False'))
# True
print(bool([]))
# False
print(bool([False]))
# True
To treat strings '0' or 'False' as false, use distutils.util.strtobool(). See the following article for reference.
and and or do not always return bool values
In Python, and and or do not always return bool values (True or False); they return either the left or right value according to their truthiness. On the other hand, not always returns a bool value, inverting the truthiness of its operand.
x = 10 # Evaluated as true
y = 0 # Evaluated as false
print(x and y)
# 0
print(x or y)
# 10
print(not x)
# False
The definitions of the return values for and and or are as follows.
The expression
x and yfirst evaluatesx; ifxis false, its value is returned; otherwise,yis evaluated and the resulting value is returned.The expression
x or yfirst evaluatesx; ifxis true, its value is returned; otherwise,yis evaluated and the resulting value is returned. 6. Expressions - Boolean operations — Python 3.12.1 documentation
The return values of and and or can be summarized in the following table:
| x | y | x and y | x or y |
|---|---|---|---|
| true | false | y | x |
| false | true | x | y |
| true | true | y | x |
| false | false | x | y |
When used in conditionals such as if statements, the result is evaluated based on its truthiness, so there is no particular need to worry. However, be cautious when using the return value in subsequent operations.
x = 10 # Evaluated as true
y = 100 # Evaluated as true
print(x and y)
# 100
print(y and x)
# 10
print(x or y)
# 10
print(y or x)
# 100
x = 0 # Evaluated as false
y = 0.0 # Evaluated as false
print(x and y)
# 0
print(y and x)
# 0.0
print(x or y)
# 0.0
print(y or x)
# 0
print(bool(x and y))
# False
To treat the result as True or False, you can use bool() as shown in the last example.
Short-circuit evaluation
If x is false in x and y or if x is true in x or y, the return value is x regardless of the value of y. In such cases, y is not evaluated.
Note that if you call a function or method on the right side of and or or, it may not be executed if the left side determines the outcome.
def test():
print('function is called')
return True
print(True and test())
# function is called
# True
print(False and test())
# False
print(True or test())
# True
print(False or test())
# function is called
# True