k=5
for k in range (5,10):
print(2*k**2)
50
72
98
128
162
I need to add these results.
k=5
for k in range (5,10):
print(2*k**2)
50
72
98
128
162
I need to add these results.
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 .
You can do this:
soma = 0
for k in range (5,10):
soma += 2 * k ** 2
print(2 * k ** 2)
print(soma)