Add the results of for x in range

1
k=5
for k in range (5,10):
    print(2*k**2)

50
72
98
128
162

I need to add these results.

    
asked by anonymous 08.05.2017 / 02:28

2 answers

3

Using list comprehension , I would read:

soma = sum(2*k**2 for k in range(5, 10))

Read: For each value of k in interval range(5, 10) calculate 2*k**2 and add all results.

You can read more about understanding here .

    
08.05.2017 / 02:49
0

You can do this:

soma = 0
for k in range (5,10):
    soma += 2 * k ** 2
    print(2 * k ** 2)

print(soma)
    
08.05.2017 / 02:31