Home > Articles > Open Source > Python

  • Print
  • + Share This
This chapter is from the book

Boolean Expressions and Truth Values

The and, or, and not keywords can form Boolean expressions. The behavior of these operators is as follows:

Operator

Description

x or y

If x is false, return y; otherwise, return x.

x and y

If x is false, return x; otherwise, return y.

not x

If x is false, return 1; otherwise, return 0.


When you use an expression to determine a true or false value, True, any nonzero number, nonempty string, list, tuple, or dictionary is taken to be true. False, zero, None, and empty lists, tuples, and dictionaries evaluate as false. Boolean expressions are evaluated from left to right and consume the right operand only if it’s needed to determine the final value. For example, a and b evaluates b only if a is true.

  • + Share This
  • 🔖 Save To Your Account