Python equivalent to a C trick

Asun Friere afriere at
Wed Aug 11 08:47:16 CEST 2004

Erik Max Francis <max at> wrote in message news:<41189FD5.7516D933 at>...
> Thomas Krüger wrote:
> > Logic_Test and True_Result or False_Result
> > 
> > Example:
> > print (num_eggs > 1) and 'eggs" or "egg"
> Be very careful that the "True_Result" in your formulation actually is
> itself a Python true value, or this won't work:
> 	print 'egg' + (numEggs == 1 and '' or 's')
> will not work as expected.

For which reason the form (test and [true_result] or
[false_result])[0] is sometimes advocated.  This starts to look a
little too cluttered for my liking, so I tend to use it only when the
'true_result' is a variable and might be instantiated to a negative

More information about the Python-list mailing list