Custom Search

Monday, April 18, 2011

python boolean tricks

isSelected = True
result = ('not-selected', 'selected')[isSelected]
print "-------result------", result
#it indexes the tuple with isSelected.

isSelected = False
result = ('not-selected', 'selected')[isSelected]
print "-------result------", result


isSelected = 1
result = ('not-selected', 'selected')[isSelected]
print "-------result------", result
#it indexes the tuple with isSelected.

isSelected = 0
result = ('not-selected', 'selected')[isSelected]
print "-------result------", result

Output
=====
-------result------ selected
-------result------ not-selected
-------result------ selected
-------result------ not-selected

No comments:

Post a Comment