Easy pretty printing of floats?

It’s an old question but I’d add something potentially useful: I know you wrote your example in raw Python lists, but if you decide to use numpy arrays instead (which would be perfectly legit in your example, because you seem to be dealing with arrays of numbers), there is (almost exactly) this command you said you made … Read more

Why is parenthesis in print voluntary in Python 2.7?

In Python 2.x print is actually a special statement and not a function*. This is also why it can’t be used like: lambda x: print x Note that (expr) does not create a Tuple (it results in expr), but , does. This likely results in the confusion between print (x) and print (x, y) in Python 2.7 However, since print is a special syntax statement/grammar construct in Python 2.x then, without … Read more

Printing variables in Python 3.4

The syntax has changed in that print is now a function. This means that the % formatting needs to be done inside the parenthesis:1 However, since you are using Python 3.x., you should actually be using the newer str.format method: Though % formatting is not officially deprecated (yet), it is discouraged in favor of str.format and will most likely be removed from the language in a coming … Read more

Print multiple arguments in Python

There are many ways to do this. To fix your current code using %-formatting, you need to pass in a tuple: Pass it as a tuple:print(“Total score for %s is %s” % (name, score)) A tuple with a single element looks like (‘this’,). Here are some other common ways of doing it: Pass it as a dictionary:print(“Total … Read more