next up previous contents index
Next: top Up: Conditionals and recursion Previous: top   Contents   Index

startsection section 1 0mm-3.5ex plus -1ex minus -.2ex0.7ex plus.2exBoolean expressions

A boolean expression is an expression that is either true or false. In Python, an expression that is true has the value 1, and an expression that is false has the value 0.

The operator == compares two values and produces a boolean expression:


>>> 5 == 5
1
>>> 5 == 6
0

In the first statement, the two operands are equal, so the expression evaluates to 1 (true); in the second statement, 5 is not equal to 6, so we get 0 (false).

The == operator is one of the comparison operators; the others are:


      x != y               # x is not equal to y
      x > y                # x is greater than y
      x < y                # x is less than y
      x >= y               # x is greater than or equal to y
      x <= y               # x is less than or equal to y

Although these operations are probably familiar to you, the Python symbols are different from the mathematical symbols. A common error is to use a single equal sign (=) instead of a double equal sign (==). Remember that = is an assignment operator and == is a comparison operator. Also, there is no such thing as =< or =>.


next up previous contents index
Next: top Up: Conditionals and recursion Previous: top   Contents   Index
root 2004-05-05