Add the results for x in range

Asked

Viewed 700 times

1

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

50
72
98
128
162

I need to add these results.

2 answers

3

Using list comprehension, would be:

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

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

You can read more about list comprehension here.

0

You can do it like this:

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

print(soma)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.