List a range of numbers and add one by one

0

I do not know how to start, I would like to know how to add a number to another in a range for example:

2 = 2
3 = 5
4 = 11

And so on I can not go on, if anyone can help me thank you right away.

    
asked by anonymous 01.04.2018 / 15:23

3 answers

1

You can use the function sum the code would be this:

print(sum([1,2,3]))

separated by variables:

lista = [1,2,3]

soma = sum(lista)

print(soma) 
    
02.04.2018 / 03:58
0

If you understand your question, you want to add all elements of a range

use FOR next to range,

n_elementos = 5
soma = 0

for i in range(1,n_elementos+1):
    soma += i

print(soma)

o 'n_elements + 1' is due to the range (x, y) go from x to y-1

    
01.04.2018 / 16:53
0

For this we can use two functions in python.

The range function that creates an iterative range of values from the given parameters and the sum function that sums an iterable and returns the result

follows an example:

sum(range(1,5))

follows the links of the function documentation

link

link

    
03.04.2018 / 14:06