136

C and many other languages have a conditional (AKA ternary) operator. This allows you to make very terse choices between two values based on the truth of a condition, which makes expressions, including assignments, very concise.

I miss this because I find that my code has lots of conditional assignments that take four lines in Python:

if condition:
    var = something
else:
    var = something_else

Whereas in C it'd be:

var = condition ? something : something_else;

Once or twice in a file is fine, but if you have lots of conditional assignments, the number of lines explode, and worst of all the eye is drawn to them.

I like the terseness of the conditional operator, because it keeps things I deem un-strategic from distracting me when skimming the code.

So, in Python, is there a trick you can use to get the assignment onto a single line to approximate the advantages of the conditional operator as I outlined them?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Will
  • 71,757
  • 38
  • 162
  • 237

2 Answers2

230

Python has such an operator:

variable = something if condition else something_else

Alternatively, although not recommended (see karadoc's comment):

variable = (condition and something) or something_else
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
carl
  • 48,340
  • 17
  • 72
  • 81
  • 9
    That "variable = condition and something or something_else" doesn't even work. For example, "True and False or True" returns True, whereas "True ? False : True" would return False. – karadoc May 17 '12 at 10:27
  • Something or Something_else works fine right? For instance if something was null you'd set it to something_else? – Breedly May 14 '13 at 13:01
  • @karadoc right point but bad example&no explanation for the second example: **for beginners:** if "something" is considered empty/falsy => it is evaluated to `False` => so you will **NEVER get** values like `0` (int zero), `""`(empty string), `[]` empty array, `False`, and other "empty" values => ***because they evaluate to `False` => `True and False` is `False`*** :) Some languages even consider `"0"` (zero as string) as empty value :) – jave.web Mar 01 '20 at 21:18
  • If `something` is an expression, is it lazily evaluated? – trebor Aug 04 '21 at 19:54
  • `count+=1 if condition else count -= 1` is it possible – Azhar Uddin Sheikh Feb 01 '22 at 18:31
18

In older Python code, you may see the trick:

condition and something or something_else

However, this has been superseded by the vastly superior ... if ... else ... construct:

something if condition else something_else
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Greg Hewgill
  • 890,778
  • 177
  • 1,125
  • 1,260
  • 1
    why don't "return if not whatever" work, though? – Will Jun 22 '10 at 08:21
  • 4
    @Will: Because "return if not whatever" is not syntactically correct Python? – Greg Hewgill Jun 22 '10 at 08:36
  • @Will Probably because you don't provide an `else` part? – glglgl Jul 29 '14 at 19:07
  • You can `return 1 if not cond else 2` since that returns the expression `1 if not cond else 2`. But you can't just use `return if not cond` (even with `else`) since `if...else` is an *expression*: it evaluates to a value. You want to use `if` as *control flow* which is different. But `if not cond: return` is ok. – Andrew Jaffe Jul 30 '14 at 09:01