Let's say a=(1,2) and b=(3,4) What should print(a+b) give?
I expect the output (4,6), but the actual output is(1,2,3,4)
Let's say a=(1,2) and b=(3,4) What should print(a+b) give?
I expect the output (4,6), but the actual output is(1,2,3,4)
a
and b
are of tuple
type. The +
operator for tuples append the tuples to one another. To actually sum element-wise tuples, you need to do the following:
[sum(x) for x in zip(a,b)]
You can find more info here: https://stackoverflow.com/a/16548756/4949074
Because it is a tuple type which is a sequence type for which the +
operator does concatenation and not addition. If you want numeric vectors, look at, e.g., NumPy.