Python addition is done with +
, the addition operator.
>>> 8 + 3
11
>>> 3 + 2 + 1
6
Adding floats:
>>> 8.3 + 2.3
10.600000000000001
Note that floating point operations can produce small rounding errors, 8.3 + 2.3 is not necessarily 10.6.
When adding numbers, if any of the numbers in the operation are floats, Python will return a float.
Adding negatives:
>>> -8 + -11
-19
Just adding a +
in front of a constant/variable will just give back the number itself, the value’s identity.
>>> +3
3
You can also use the addition operator +
to concatenate certain data types, like strings, lists and tuples:
>>> 'hey' + ' ' + 'there'
'hey there'
>>> [3, 4] + [5, 8]
[3, 4, 5, 8]
>>> (3, 4,) + (2,)
(3, 4, 2)