Python Forum
[Basic] Ternary/Conditional Expressions
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Basic] Ternary/Conditional Expressions
#1
If you have ever required a simple if/else statement in your code then conditional expressions might be of interest to you.

The standard if/else statement might look like this:
if eggs:
    mystring = "Spam and eggs."
else:
    mystring = "Spam and spam."
I know that when I have to do such things I often lament that I needed four lines (or two if you put the assignment statement on the same line as the colon, which is also discouraged) to do so.

In these cases one can use what is called a ternary or conditional expression (py2.5 and later).
With this statement the previous code becomes:
mystring = "Spam and eggs." if eggs else "Spam and spam."
This allows us to one-line our simple if/else statement.

Now I personally love this syntax, but it should obviously be used with some discretion.  It is very easy to make an extremely long incomprehensible statement using this syntax.  Some Python programmers think it should be avoided, but I tend to disagree.  That said, I often default to it when there is a better alternative and have to catch myself.  Essentially, be certain that it is actually making things easier to read when you use it; not more convoluted.

Now if you are comfortable with the syntax of such expressions, it is natural to ask if you can use it with the augmented assignment operators.  The answer here is yes, but again, it is quite easy to make something that isn't particularly readable when doing so.

Here is an example:
a += 1 if b % 2 else 2
Now what exactly is this code saying?
It is telling us to increment the value of a by 1 if b is odd.  If b is even, increment the value of a by 2.  Is this the clearest way to write this statement? I don't know.  If you are unsure if it is clear, don't use it.  If you do decide to use the ternary expression with augmented assignment operators just be aware that the final value after the else will be evaluated using the same augmented assignment operator. 

So our previous statement is equivalent to:
if b % 2:
    a += 1
else:
    a += 2
Notice also that our operator is only typed at the beginning of the ternary expression.  If you write:
a += 1 if b % 2 else a += 2
while you may think that this is clearer, it won't work.  When we use the ternary operator there are still only two sides to the statement; The name to be assigned to, and the expression (which in this case is conditional) being assigned.  As this is the case, for clarity's sake I often find it is sometimes clearer to write the statement as:
a += (1 if b % 2 else 2)
These parenthesis, while not necessary, I find make the intention of the statement much clearer.

For more information on the subject here is the original PEP in which it was added: PEP 308
And for more on the ternary operator/expression in general see the wiki page: Ternary Operators ?:

Cheers,
-Mek
#2
This message left intentionally blank.


Possibly Related Threads…
Thread Author Replies Views Last Post
  Comprehension Expressions micseydel 0 4,394 Oct-10-2016, 03:28 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020