0

Possible Duplicate:
Ternary conditional operator in Python

I've programmed in Java for quite sometime, I'm learning Python in school and I remember in Java for a boolean expression you could do

boolean ? (if boolean is true this happens) : (if boolean is false this happens)

Is their a way to do the above Java code in Python? And what is the statement above properly called?

Community
  • 1
  • 1
Jonathan Beaudoin
  • 2,108
  • 4
  • 25
  • 58
  • 3
    Related: [Ternary conditional operator in Python](http://stackoverflow.com/questions/394809/ternary-conditional-operator-in-python). – Kevin Oct 01 '12 at 19:02
  • 1
    In the beggining Python did not include this, because it was thought such expressions would hinder readability. Being one of the few languages in wide use that does not inherit the C syntax directly (like Java does), when they finally added this to Python, the idea was something that would be more naturally readable than the "C" way. (The way to do it is in Martiin's answer bellow) – jsbueno Oct 01 '12 at 19:06

3 Answers3

10

Yes, use a conditional expression:

somevalue if oneexpression else othervalue

Examples:

>>> 'foo' if True else 'bar'
'foo'
>>> 'foo' if False else 'bar'
'bar'

Before Python 2.5 where this was introduced, people used a combination of and and or expressions to achieve similar results:

expression and truevalue or falsevalue

but if the truevalue part of the expression itself evaluated to something that has the boolean value False (so a 0 or None or any sequence with length 0, etc.) then falsevalue would be picked anyway.

Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
3

Python:

x if condition else y

Example:

val = val() if callable(val) else val
greeting = ("Hi " + name) if name != "" else "Howdy pardner"

This is often called "the ternary operator" because it has three operands. However, the term "ternary operator" applies to any operation with three operands. It just happens that most programming languages don't have any other ternary operators, so saying "the" is unambiguous. However, I'd call it the if/else operator or a conditional expression.

In Python, due to the way the and and or operators work, you can also use them in some cases for things you'd usually use the ternary operator for in C-derived languages:

# provide a default value if user doesn't enter one
name = raw_input("What is your name? ") or "Jude"
print "Hey", name, "don't make it bad."

# call x only if x is callable
callable(x) and x()
kindall
  • 158,047
  • 31
  • 244
  • 289
2

Yes, you can use this (more pythonic):

>>> "foo"'if condition else "bar"

Or, this (more common, but not recommended):

>>> condition and "foo" or "bar"
timakina
  • 71
  • 3
  • *If* the latter is more common, it's just because the former was introduces a few version ago and a lot of existing code was written before that. It shouldn't be used at all by now. –  Oct 01 '12 at 19:06
  • The latter has huge problems if the right-hand expression of the `and` expression could evaluate to falshy (empty string, empty sequence, `None`, etc.) – Martijn Pieters Oct 01 '12 at 19:06