0
Why concatenation is happening although there is no strings in a list, please explain
8 Réponses
+ 4
Yes , you can do math operations: +, -, *, /, //, **, %:
lst1 = [4,5]
lst2 = [8,9]
print([i + j for i, j in list(zip(lst1, lst2))])
# output is [12, 14]
+ 1
Can I do arthematic addition with list Without numpy library??
+ 1
Can I do [4,5]+[8,9]=[12,14]
+ 1
If you want to actually add the coresponding values, youd have to make a class and overload the + operator though
+ 1
class NumList:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return f'[{self.x}, {self.y}]'
def __add__(self, other):
nl = NumList(self.x + other.x, self.y + other.y)
return nl
a = NumList(2,4)
b = NumList(3,5)
print(a + b)
[5, 9]