"do" as a keyword

Tor Erik Sønvisen torerik81 at gmail.com
Tue Dec 11 11:39:14 EST 2007


> I also started to ponder about the "'do
> something' if 'true' else 'do this'", and pondered if perhaps
> this statement could do with the including of the keyword do.

Python has support for this in versions >= 2.5:

>>> a = range(0, 5)
>>> b = range(5, 8)
>>> min(a) if sum(a) < sum(b) else min(b)
0

In prior versions, you can use the and/or trick:

>>> (sum(a) < sum(b) and [min(a)] or [min(b)])[0]
0

-Tor Erik



More information about the Python-list mailing list