I tried to add only using the pop method without modules
a = [-1, 2, -1, 3, 3, -5] b = a.pop(2) c = a.pop(3) d = b + c print(d) I tried to add only using the pop method without modules
a = [-1, 2, -1, 3, 3, -5] b = a.pop(2) c = a.pop(3) d = b + c print(d) As far as I understood the task:
a = [-1, 2, -1, 3, 3, -5] b = 0 for digit in a: b += abs(digit) print(b) If you do not fool with the cycle then:
a = [-1, 2, -1, 3, 3, -5] b = sum(abs(i) for i in a) print(b) Or so:
a = [-1, 2, -1, 3, 3, -5] b = sum(map(abs, a)) print(b) b = sum([abs(i) for i in a]) -> b = sum(abs(i) for i in a) - gil9red a = [-1, 2, -1, 3, 3, -5] acc = 0 for i in a: acc += abs(i) print(acc) or
from functools import reduce from operator import add a = [-1, 2, -1, 3, 3, -5] b = reduce(add, map(abs, a)) print(b) Option through pop :
a = [-1, 2, -1, 3, 3, -5] b = 0 while a: b += abs(a.pop()) print(b) # 15 Source: https://ru.stackoverflow.com/questions/893526/
All Articles
sum(a)does not fit? - insolorsum(abs(x) for x in a)- andreymal