Python’s logical operators and, or, and not (logical conjunction, disjunction, negation)

Money and Business

Python provides logical operators to perform logical (Boolean) operations.(and,or,not)
It is used to describe the relationship between multiple conditions in an if statement.

This section describes the following.

  • intersection: and
  • logical add: or
  • denial: not
  • and,or,notOperator precedence

In addition, the following points are explained as cautions.

  • Logical operators for objects of type other than bool
  • and,orThese return values are not necessarily of type bool.
  • Short circuit (short circuit evaluation)

intersection: and

and returns the logical product of two values.

print(True and True)
# True

print(True and False)
# False

print(False and True)
# False

print(False and False)
# False

In fact, it is often used not for true or false, but for conditional expressions using comparison operators. For your information, the comparison operators are as follows.

  • <
  • >
a = 10
print(0 < a)
# True

print(a < 100)
# True

print(0 < a and a < 100)
# True

and can be concatenated as follows.

print(0 < a < 100)
# True

logical add: or

or returns the logical OR of the two values.

print(True or True)
# True

print(True or False)
# True

print(False or True)
# True

print(False or False)
# False

denial: not

not” returns the negation of the value; true and false are reversed.

print(not True)
# False

print(not False)
# True

and,or,notOperator precedence

The order of precedence of these logical operators is as follows: not is the highest.

  1. not
  2. and
  3. or

In the following sample code, the above expression is interpreted as if it were the one below. Since there is no problem with extra parentheses, it may be easier to clearly describe them in cases like this example.

print(True or True and False)
# True

print(True or (True and False))
# True

If you want to operate or before and, use parentheses().

print((True or True) and False)
# False

<,>These comparison operators have even higher precedence than not. Therefore, parentheses are not necessary for each comparison operation, as was the case in the example above.

print(0 < a and a < 100)
# True

See the official documentation below for a summary of operator precedence in Python.

Logical operators for objects of type other than bool

With these logical operators, not only bool types (true, false), but also numbers, strings, lists, etc. are processed as boolean values.

The following objects are considered false in Python's logical operations.

  • Constants defined to be false: None,false
  • Zero in numeric types: 0,0,0j,Decimal(0),Fraction(0, 1)
  • Empty sequence or collection: '',(),[],{},set(),range(0)

All other values are considered true.

The function bool() can be used to get the boolean value of an object. Note that the string '0' or 'False' is considered 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 handle '0' or 'false' in a string as false, use distutils.util.strtobool().

and,orThese return values are not necessarily of type bool.

Here is an example of an object other than a bool type, showing the result of each operator on a numeric value.

x = 10  # True
y = 0  # False

print(x and y)
# 0

print(x or y)
# 10

print(not x)
# False

As you can see from the example above, and and or in Python do not return true or false of type bool, but return the value on the left or right depending on whether it is true or false. The example is numeric, but the same applies to other types such as strings and lists. Incidentally, not returns true or false of type bool.

The definitions of the return values of and and or are as follows.

The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.

The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.

6.11. Boolean operations — Expressions — Python 3.10.1 Documentation

When the values of the left and right expressions are true and false separately, the return values are easy to understand. On the other hand, if both are true or both are false, the return value will be different depending on the order.

If you use it as a conditional expression in an if statement, etc., the result is judged as a boolean value and processed, so you don't need to worry about it, but if you use the return value for further processing, you need to be careful.

x = 10  # True
y = 100  # True

print(x and y)
# 100

print(y and x)
# 10

print(x or y)
# 10

print(y or x)
# 100
x = 0  # False
y = 0.0  # 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

If you want to treat it as true or false, you can do as in the last example.
bool(x and y)

The return values of and and or are summarized in the table below.

xyx and yx or y
truefalseyx
falsetruexy
truetrueyx
falsefalsexy

Short circuit (short circuit evaluation)

As you can see from the table above, if x is false in x and y, or if x is true in x or y, the return value will be x regardless of the value of y.

In such a case, y is not evaluated.

and,orNote that if you call a function or method on the right side of these processes to do some processing, the process may not be executed depending on the result on the left side.

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