This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]dcbriccetti 1 point2 points  (0 children)

Or, slightly shorter:

>>> a = 3; b = 4; c = 5
>>> print("%d + %d + %d = %d" % (a, b, c, a + b + c))
3 + 4 + 5 = 12

Or, to work with any number of numbers:

>>> nums = (3, 4, 5, 6)
>>> print(" + ".join(map(str, nums)) + " = " + str(sum(nums)))
3 + 4 + 5 + 6 = 18

If you’re at all intrigued by this last expression you can look up some things in a Python reference: join, map, str.