Why concatenation is happening although there is no strings in a list, please explain | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why concatenation is happening although there is no strings in a list, please explain

https://code.sololearn.com/cwraZfjv1SxX/?ref=app

18th May 2020, 11:51 AM
Teja Swaroop
Teja Swaroop - avatar
5 Answers
+ 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]
18th May 2020, 8:46 PM
Lothar
Lothar - avatar
+ 1
Can I do arthematic addition with list Without numpy library??
18th May 2020, 12:07 PM
Teja Swaroop
Teja Swaroop - avatar
+ 1
Can I do [4,5]+[8,9]=[12,14]
18th May 2020, 12:10 PM
Teja Swaroop
Teja Swaroop - avatar
+ 1
If you want to actually add the coresponding values, youd have to make a class and overload the + operator though
18th May 2020, 12:12 PM
Slick
Slick - avatar
+ 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]
18th May 2020, 12:25 PM
Slick
Slick - avatar