Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you don't want to <code>from __future__ import print_function</code> you can do the following:</p> <pre><code>a = 100 b = True print a if b else "", # Note the comma! print "see no new line" </code></pre> <p>Which prints:</p> <pre><code>100 see no new line </code></pre> <p>If you're not aversed to <code>from __future__ import print_function</code> or are using python 3 or later:</p> <pre><code>from __future__ import print_function a = False b = 100 print(b if a else "", end = "") </code></pre> <p>Adding the else is the only change you need to make to make your code syntactically correct, you need the else for the conditional expression (the "in line if else blocks") </p> <p>The reason I didn't use <code>None</code> or <code>0</code> like others in the thread have used, is because using <code>None/0</code> would cause the program to <code>print None</code> or <code>print 0</code> in the cases where <code>b</code> is <code>False</code>.</p> <p>If you want to read about this topic I've included a <a href="http://docs.python.org/release/2.5/whatsnew/pep-308.html" rel="noreferrer">link</a> to the release notes for the patch that this feature was added to Python.</p> <p>The 'pattern' above is very similar to the pattern shown in PEP 308:</p> <blockquote> <p>This syntax may seem strange and backwards; why does the condition go in the middle of the expression, and not in the front as in C's c ? x : y? The decision was checked by applying the new syntax to the modules in the standard library and seeing how the resulting code read. In many cases where a conditional expression is used, one value seems to be the 'common case' and one value is an 'exceptional case', used only on rarer occasions when the condition isn't met. The conditional syntax makes this pattern a bit more obvious:</p> <p>contents = ((doc + '\n') if doc else '')</p> </blockquote> <p>So I think overall this is a reasonable way of approching it but you can't argue with the simplicity of:</p> <pre><code>if logging: print data </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload