26

Possible Duplicate:
Python Ternary Operator

In some languages including Java, C/C++, C#, etc. you can assign a value based on the result of an inline boolean expression.

For example,

return (i < x) ? i : x

This will return i if i < x, otherwise it will return x. I like this because it is much more compact in many cases than the longer syntax which follows.

if (i < x)
  return i
else
  return x

Is it possible to use this syntax in python and if so, how?

Community
  • 1
  • 1
user1162865
  • 263
  • 1
  • 3
  • 6

6 Answers6

24

You can use (x if cond else y), e.g.

>>> x = 0
>>> y = 1
>>> print("a" if x < y else "b")
a

That will work will lambda function too.

jsbueno
  • 99,910
  • 10
  • 151
  • 209
chl
  • 27,771
  • 5
  • 51
  • 71
6

Yes, it looks like this:

return i if i < x else x

It's called the conditional operator in python.

recursive
  • 83,943
  • 34
  • 151
  • 241
5

a if b else c syntax was introduced in Python 2.5. Most people have already upgraded to the recent version but in legacy code you may find another approach:

some_var = a<b and a or c

If you ever will be using this syntax remember that a must not evaluate to False.

real4x
  • 1,433
  • 12
  • 11
3

As the other answers state, Python's version for this is:

i if i < x else x

(of course for this particular example, one would prefer writing min(i, x) , as it is easier on the eyes)

However, this expression syntax was just made available on Python 2.5 (I think it was around 2004). before that, the following idiom was used - but care should be taken, as it is error prone:

i < x and i or x - because the logical operator "and" actually evaluates to the last true value on its chain - therefore, if the expression was true, i < x and i evaluates to i - and the or operator evaluates to first true value on the chain. On this case, if i < x would be false, so would i< x and i and the first true value would be x.

It is easy to see how error prone this construct was, since if the boolean value of i would be false ( for example if i==0), than it would return x, even if i < x where true.

I myself, back in those days, used to write this construct instead:

(x, i)[i < x]

The expression "i < x" ha a numerical value of "1" (true) or "0" (false) and I used this proeprty to have it work as an index to the previous tuple. The problem with this approach was that it would always evaluate both expressions on the tuple, even though it would use ony one of then (if the expressions where function calls this could get expensive, or even conflicting)

jsbueno
  • 99,910
  • 10
  • 151
  • 209
2

Try this in Python:

return i if i < x else x

It's exactly the equivalent of the following expression in Java, C, C++ C#

return i < x ? i : x;

Read more about Python's conditional expressions.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
2

Ternary operator in python.

a if b else c

>>> a=1
>>> b=2
>>> a if a<b else b
1
>>> a if a>b else b
2
RanRag
  • 48,359
  • 38
  • 114
  • 167