Tuple Comparison Operations
The standard comparisons (
) work exactly the same
tuples as they do among
compared element by element. If the corresponding elements are the same
type, ordinary comparison rules are used. If the corresponding elements
are different types, the type names are compared, since there is almost
no other rational basis for comparison.
if a < b: print "a smaller"
else: print "b smaller"
print 3 in a
print 3 in b
Here's a longer example.
Example 13.1. redblack.py
if n == 0:
print '0', 'green'
elif n == 37:
print '00', 'green'
elif n in ( 1,3,5,7,9, 12,14,16,18, 19,21,23,25,27, 30,32,34,36 ):
print n, 'red'
print n, 'black'
This script will create a random number, setting aside the zero
and double zero. If the number is in the
red spaces on the roulette layout, this is printed. If none of the other
rules are true, the number is in one of the black spaces.
Clearly the heart of this script is the extended if-statement
which contains the tuple of red positions on the roulette wheel. This
should be rewritten as a function.