Linux/Unix > Python Essential Reference, Second Edition > 4. Operators and Expressions > Boolean Expressions and Truth Values | See All Titles

< BACK | Make Note | Bookmark | CONTINUE >

155117184014003188065099048180054212144238241179195140058238110137104030195072077016073191

### 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, any nonzero number or nonempty string, list, tuple, or dictionary is taken to be true. 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.

| Last updated on 3/28/2002 Python Essential Reference, Second Edition, © 2002 New Riders Publishing

< BACK | Make Note | Bookmark | CONTINUE >

© 2002, O'Reilly & Associates, Inc.