On-line Guides

How To Guides






List Comparison Operations
Prev Chapter 14. Lists Next

List Comparison Operations

The standard comparisons (<, <=, >, >=, ==, !=, in , not in ) work exactly the same among lists, tuples and strings. The lists are 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 no other rational basis for comparison.

d1= random.randrange(6)+1
d2= random.randrange(6)+1
if d1+d2 in [2, 12] + [3, 4, 9, 10, 11]:
 print "field bet wins on ", d1+d2
else:
 print "field bet loses on ", d1+d2

This will create two random numbers, simulating a roll of dice. If the number is in the list of field bets, this is printed. Note that we assemble the final list of field bets from two other lists. In a larger application program, we might separate the different lists of winners based on different payout odds.


Prev Up Next
List Operations Home List Statements

Published under the terms of the Open Publication License Design by Interspire

AltStyle によって変換されたページ (->オリジナル) /