Tip Details Add a Related Tip

Rate as: Positive Negative

Ternary operator in Pyton

One of the most annoying things about Python is the lack of a ternary operator. You know, that whole ? : that allows you to set conditional values in a single line:

cond ? valueIfConditionIsTrue : valueIfConditionIsFalse

If Python is your first language, you'll never even notice it's gone. But if you've used other languages, most of them have a ternary operator. And they have it for a reason, it fills a very nice niche for setting conditionals values with the minimal amount of code.

Thankfully as of version 2.5 Python now has something that is not quite the ternary operator, but it fills the same need and one could argue -- and I'm sure several have -- that it's actually a little better the way the implemented since it's more of a single-line if-else statement which is a little easier to understand if you are not use to the ternary operator. It goes something like this:

valueIfConditionIsTrue if condition else valueIfConditionIsFalse

or in actual usage:

'thisStatementIsTrue' if 1 == 1 else 'thisStatementIsFalse'

which will return the value 'thisStatementIsTrue'.

So if you've upgraded to version 2.5 of Python, congratulations you now have something that can be used as a ternary operator. If you have not, you are still going to be writing some extra code to assign condition values.
Rating: 100% positive, 1 Vote
Categories: Python programming shortcut
Added: on Jul 11, 2008 at 9:36 pm
Added By: an anonymous user

Comments on this Tip

Add a Comment
There are no comments for this tip
Your Comment:
(how to format)
Rate This Tip:

Verify Humanity:
Sorry, we know it's annoying, but please enter the characters shown in the image to the left so that we know you're an actual person and not an evil spammer. Thanks.
       

Related Tips

Viewing plist files in Leopard

100% positive, 0 comments
– Tip added by an anonymous user on Mar 06, 2008 at 3:21 pm

Learn Python in 10 minutes

Categories: programming Python web
100% positive, 0 comments
– Tip added by Marcos84 on Jul 20, 2007 at 7:42 am

Figuring out shortcuts in Eclipse

no ratings, 0 comments
– Tip added by an anonymous user on Aug 13, 2008 at 3:17 pm

Creating shortcuts in Microsoft Visual Studio

no ratings, 0 comments
– Tip added by juice on Aug 28, 2007 at 7:29 am

Searching pages in Firefox 3

Categories: Firefox search shortcut
100% positive, 0 comments
– Tip added by an anonymous user 4 days, 9 hours ago

Better copy-and-paste

100% positive, 0 comments
– Tip added by an anonymous user 2 days, 12 hours ago

Control iTunes from the Mac Dock

Categories: iTunes Mac Dock shortcut
100% positive, 0 comments
– Tip added by an anonymous user 4 days, 11 hours ago

Logging in on the Mac

100% positive, 0 comments
– Tip added by an anonymous user on Aug 28, 2008 at 8:33 am

Launch apps and websites quicker

100% positive, 0 comments
– Tip added by an anonymous user on Aug 22, 2008 at 3:03 pm

Entering domains on the iPhone

100% positive, 0 comments
– Tip added by an anonymous user on Aug 22, 2008 at 10:00 am

Right-clicking on the Mac trackpad

Categories: Mac notebook mouse shortcut
100% positive, 0 comments
– Tip added by an anonymous user on Aug 20, 2008 at 11:28 am

Matching any character in a javascript regular expression

100% positive, 0 comments
– Tip added by an anonymous user on Aug 18, 2008 at 9:06 am

Shortcut to mute sound in Ubuntu

100% positive, 0 comments
– Tip added by an anonymous user on Aug 18, 2008 at 6:14 am

Auto zoom content on the iPhone

Categories: iPhone safari shortcut
100% positive, 0 comments
– Tip added by an anonymous user on Aug 14, 2008 at 7:59 pm

temporarily turn off auto-indent

Categories: JEdit keyboard shortcut
100% positive, 0 comments
– Tip added by an anonymous user on Aug 12, 2008 at 3:33 pm

Typing periods on the iPhone

100% positive, 0 comments
– Tip added by an anonymous user on Aug 08, 2008 at 9:32 am

Reopen closed tabs in Google Chrome

no ratings, 0 comments
– Tip added by an anonymous user 1 day, 14 hours ago

Searching images on live search

no ratings, 0 comments
– Tip added by an anonymous user 2 days, 14 hours ago

Quick scroll to the top on the iPhone

Categories: iPhone shortcut
no ratings, 0 comments
– Tip added by an anonymous user on Aug 27, 2008 at 7:41 am

attaching events to nodes in Javascript

no ratings, 0 comments
– Tip added by an anonymous user on Aug 18, 2008 at 11:23 am