What does if-if-else one-line do in Python?
<p>I ran into this expression in a question here about <a href="https://stackoverflow.com/a/3421173/343215">knapsack problems</a>:</p>

<pre><code>def f(v, i, S):
  if i &gt;= 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
</code></pre>

<p><strike>When I try to write out line two <code>if i &gt;= len(v): return 1 if S == 0 else 0</code> in a more general form I get an error:</p>

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

<p></strike>
Here is a generalized form:</p>

<pre><code>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]
</code></pre>

<p>What do you call this?</p>

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

<hr>

<p><strong>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.</strong></p>