Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions

  




 

 

Tuple Comparison Operations

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

>>> 
a=(1,2,3,4,5)

>>> 
b=(9,8,7,6,5)

>>> 
if a < b: print "a smaller"

>>> 
else: print "b smaller"

a smaller
>>> 
print 3 in a

True
>>> 
print 3 in b

False
            

Here's a longer example.

Example 13.1. redblack.py

#!/usr/bin/env python
import random
n= random.randrange(38)
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'
else:
    print n, 'black'

This script will create a random number, setting aside the zero and double zero. If the number is in the tuple of 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.


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