
Built-in Types and Operators
|
7
Operations by Category
All built-in types support the comparisons and Boolean oper-
ations listed in Table 2.
Boolean
true means any nonzero number or any nonempty
collection object (list, dictionary, etc.). The names
True and
False are pre-assigned to true and false values and behave
like integers 1 and 0. The special object
None is false.
Comparisons return
True or False and are applied recur-
sively in compound objects as needed to determine a result.
Boolean
and and or operators stop (short-circuit) as soon as a
result is known and return one of the two operand objects
(on left or right).
a
Comparison operators can be chained: x<y<zis similar to x<yandy<z, except that
y is evaluated only once in the first format.
b
Floor division (X//Y), new in Python Version 2.2, always truncates fractional remainders.
Classic division (X/Y) truncates integer divisionresults inVersion 2.2but willbe changedto true
division (always keeping remainders) in Version 3.0. In Version 2.2 and later, use from __
future__ import division
to make the / operator return a true division result (e.g., 1/2 is
0.5).
c
In Python 2.4, expressions in parentheses can also be generator expressions, which are similar to list
comprehensions but produce a series of values on demand in iteration contexts.
d
List literals in square brackets ([...]) can be a simple list of expressions, or a list comprehension;
see the ...