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.

5
  • 3
    It's called a conditional expression or ternary expression, see stackoverflow.com/questions/394809/… and docs.python.org/2/reference/… . Commented Dec 12, 2016 at 15:57
  • Note your immediate problem is that print() is a function in Python 3, and calling it requires parentheses. Commented Dec 12, 2016 at 15:58
  • you need to fix the parentheses for print when running your script with Python 3. Commented Dec 12, 2016 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. Commented Dec 12, 2016 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. Commented Dec 12, 2016 at 19:43

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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).

1 Comment

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.
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

Comments

-2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.