b a d p o p c o r n

Python Conditional Expressions

Written by Ben on September 19, 2006 |

Python had lacked a C equivalent of the “condition ? true_value : false_value” expression. So we used a work around until now… conditional expressions finally made it in to Python’s 2.5 release.

In Python2.4:

x = ((condition) and true_value) or false_value

In Python2.5:

x = true_value if condition else false_value

Yeah, the statement looks better in Python2.5.

Posted in Python


2 Comments »

  1. Uh … is this related to SoAP? I can’t tell. It’s late, and I’m catching a plane for NZ in a few hours. I just thought I’d check in and see what you’ve been up to. Saw you left Europe early and wondered whether it was something we said …. And what you’re getting up to back in the good ‘ol US of A — besides general geekiness. Adjusting alright? Drop me a line! And wish me luck for NZ : / I need sleep.

    Cheers–

    Brie

    Comment by Brie — October 2, 2006 @ 4:09 pm

  2. I just stumbled upon this googling for conditionals in python. The a and b or c is smart, but it wont work for false b obviously… I hope your code didnt have bugs due to this :-)

    Comment by saurabh — April 15, 2008 @ 2:03 pm

RSS feed for comments on this post. | TrackBack URI

Leave a comment

XHTML ( You can use these tags): <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .