Rectangle 27 48

The print statement has long appeared on lists of dubious language features that are to be removed in Python 3000, such as Guido's "Python Regrets" presentation [1]. As such, the objective of this PEP is not new, though it might become much disputed among Python developers.

The following arguments for a print() function are distilled from a python-3000 message by Guido himself [2]:

  • print is the only application-level functionality that has a statement dedicated to it. Within Python's world, syntax is generally used as a last resort, when something can't be done without help from the compiler. Print doesn't qualify for such an exception.
  • At some point in application development one quite often feels the need to replace print output by something more sophisticated, like logging calls or calls into some other I/O library. With a print() function, this is a straightforward string replacement, today it is a mess adding all those parentheses and possibly converting >>stream style syntax.
  • Having special syntax for print puts up a much larger barrier for evolution, e.g. a hypothetical new printf() function is not too far fetched when it will coexist with a print() function.
  • If print() is a function, it would be much easier to replace it within one module (just def print(*args):...) or even throughout a program (e.g. by putting a different function in __builtin__.print). As it is, one can do this by writing a class with a write() method and assigning that to sys.stdout -- that's not bad, but definitely a much larger conceptual leap, and it works at a different level than print.

Nice, but doesn't address why an expression (function-call) can be easier to deal with than a statement in terms of grammar constructs, most notably a simple lambda.

What is the advantage of the new print function in Python 3.x over the...

python function printing python-3.x python-2.x
Rectangle 27 0

The reason is in the statement "return q, r"

You cannot return multiple values from a function. What python is doing is creating a tuple with q and r, and returning that, as return q, r is interpreted as return (q, r) Then print takes that and outputs a tuple. So it is really equivelent to print (q, r)

However, print q, r is executed differently, as a multi-argument print and will print all arguments with spaces in between.

It did, thank you; how would I change the return q,r to do what I am intending it do?

parentheses - How to remove parenthesis from output python - Stack Ove...

python parentheses
Rectangle 27 0

  • The return statement leaves the current function call with the expression list.

So returning multiple values from a function like you do here returns them in a tuple by definition.

Try explicitly unpacking the tuple returned by d():

In [1]: import math

In [2]: x=10.01

In [3]: y=9.01

In [4]: def d():
   ...:         b = 2.00 *  x / math.sqrt(7)
   ...:         q=round(y-b,2)
   ...:         r=round(y+b,2)
   ...:         return q,r
   ...: 

In [5]: a, b = d()

In [6]: print a, b
1.44 16.58

parentheses - How to remove parenthesis from output python - Stack Ove...

python parentheses
Rectangle 27 0

You appear to be using Python 2.

should give you the results you're looking for. Or, using the newer str.format() method:

In Python 3, your statement print("a",a) will work as expected. Check your build system in Sublime to make sure you're calling python3 instead of python. Run this code to see what version is actually being used:

To create a Python 3 build system, open a new file with JSON syntax and the following contents:

Save the file as Packages/User/Python3.sublime-build where Packages is the folder opened when you select Sublime Text -> Preferences -> Browse Packages.... You can now select Tools -> Build System -> Python3 and, assuming python3 is in your PATH, you should build with the correct version.

If the build fails with an error that it can't find python3, open Terminal and type

to see where it's installed. Copy the entire path and put it in the build system. For example, if which python3 returns /usr/local/bin/python3, then the "cmd" statement in your .sublime-build file should be:

Not to contradict your point. The print("a",a) statement is correct but in py2 the O/p is a tuple. It has to work in Py3

python - Parentheses and quotation marks in output - Stack Overflow

python printing output parentheses quotation-marks
Rectangle 27 0

Are you sure you are executing it on Python 3 interpreter? In Python 2 print is an statment so it takes no parentheses

is the same as

or in Python 3:

python - Parentheses and quotation marks in output - Stack Overflow

python printing output parentheses quotation-marks
Rectangle 27 0

print is (correctly) printing the tuple that is returned from d. To print the values individually, try this:

print '{} {}'.format(*d())
dret = d()
print dret[0],dret[1]

print a,b doesn't interpret a,b as a tuple, but rather as a sequence of parameters to the print statement. Note that the print statement changes syntax in Python3 for added confusion.

To make the a,b case parallel to the q,r case, it would look more like this:

c = a,b
print c

In that case the print statement would receive a tuple, and not two individual values.

In the second example the code should be: print dret[0], dret[1].

parentheses - How to remove parenthesis from output python - Stack Ove...

python parentheses