{Notes to self, programming, technology, linux, windows, git} U {Papers, reviews, games, coffee, tabletennis, ramblings} = {things worth saving}

Monday, October 13, 2008

Python trick - a and b or c

and / or do not return boolean values as in java, etc. Instead they return one of the values under comparison. With this we can do tricks like a? b: c (java, c, etc)

a and b - returns b, the last true element, if both are true, otherwise a.
a or b - returns a, the first true element. if none are true, returns b.

if using a and b or c, a must always be true if we wish to make a choice between b or c.

No comments:

Followers