0

I ran into this expression in a question here about knapsack problems:

def f(v, i, S):
  if i >= len(v): return 1 if S == 0 else 0
  count = f(v, i + 1, S)
  count += f(v, i + 1, S - v[i])
  return count

When I try to write out line two if i >= len(v): return 1 if S == 0 else 0 in a more general form I get an error:

In [3]: if test1 : print x if test2 else print y 
  File "<ipython-input-3-9d4131fa0c48>", line 1
    if test1 : print x if test2 else print y
                     ^
SyntaxError: Missing parentheses in call to 'print'

Here is a generalized form:

In [16]: if True : print("first") if True else print("second") 
first

In [17]: if True : print("first") if False else print("second")
second

In [18]: if False : print("first") if True else print("second")
[nothing]

In [19]: if False : print("first") if False else print("second")
[nothing]

What do you call this?

I'm surprised you can just take out the second positive case for if...then...else and turn it into if...else.


UPDATE: Sorry bout the python3 noob mistake, I just wasn't paying attention. As noted, the answers don't make sense without the mistake, so I've striked out the erroneous code.

Community
  • 1
  • 1
xtian
  • 2,765
  • 7
  • 38
  • 65
  • 3
    It's called a conditional expression or ternary expression, see http://stackoverflow.com/questions/394809/does-python-have-a-ternary-conditional-operator and https://docs.python.org/2/reference/expressions.html#conditional-expressions . – RemcoGerlich Dec 12 '16 at 15:57
  • Note your immediate problem is that `print()` is a function in Python 3, and calling it requires parentheses. – Frédéric Hamidi Dec 12 '16 at 15:58
  • you need to fix the parentheses for print when running your script with Python 3. – Jean-François Fabre Dec 12 '16 at 15:58
  • I rolled back the question to your original post because your edits render the accepted and upvoted answer (You are missing `()` in your print statement) meaningless. – SiHa Dec 12 '16 at 16:27
  • @SiHa, Yes. I've made the necessary continuity correction. Correct answer is for the example. I know what a conditional expression is. What I wanted was a name for the single line as in: "so what do i call it besides if-if-else?" LOL. – xtian Dec 12 '16 at 19:43

4 Answers4

4

You have found the ternary operator, which is known as a Conditional Expression in Python. The expression x if condition else y means that if the condition (which can be a complex statement or function) evaluates to True, the expression returns x, and if the condition evaluates to False, the expression returns y.

It works like the following if-statement:

if test1:
    if test2:
        print(x)
    else:
        print(y)

Your error stems from not wrapping the print function arguments in a parentheses. This is a change made to Python 3, whereas in Python 2, your syntax would have been fine. Rewrite it to look like:

if test1: print(x if test2 else y)

and your error will go away.

Community
  • 1
  • 1
Noah Bogart
  • 1,703
  • 1
  • 16
  • 24
1

I feel it is important to point out that what you describe (if-if-else) is not a conditional expression per se, but it does include one:

1 if S == 0 else 0 is a conditional expression

if i >= len(v): return 1 if S == 0 else 0 is a compound statement which comprises a simple if statement with a conditional expression.

So, if the first if evaluates to True, then the conditional expression will be evaluated and the appropriate element (in this case 1 or 0) returned to the preceding statement (return, here).

SiHa
  • 7,830
  • 13
  • 34
  • 43
  • I think you captured what I was after. As far as I'm concerned, it's not a duplicate, but a misunderstanding (on my part) that this is better understood as compound (conditional) statement. – xtian Dec 23 '16 at 03:12
0

It is an if-expression: a if condition else b means: if condition is true, then the expression has value a, otherwise b

Your problem is unrelated, Seems you are using python 3, you got the example from python 2. In python 3 print is a function, so just add parentheses

blue_note
  • 27,712
  • 9
  • 72
  • 90
-2

if you are using python of version >3, then print should have parenthesis i.e ()

Gautam Mokal
  • 171
  • 1
  • 4