您在這裡

Boolean expressions

1 九月, 2015 - 16:57

A boolean expression is an expression that is either true or false. The following examples use the operator ==, which compares two operands and produces True if they are equal and False otherwise:

>>> 5 == 5True>>> 5 == 6FalseTrue  and  False  are special values that belong to the type bool; they are not strings:>>> type(True)<type 'bool'>>>> type(False)<type 'bool'>

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

x != yx > yx < yx >= yx <= y
# x is not equal to y# x is greater than y# x is less than y# x is greater than or equal to 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 relational operator. There is no such thing as =< or =>.