Home > Net >  Sum of series in python
Sum of series in python

Time:09-27

How do write a program in python to print sum of the following series:1 1/4 1/7 1/10 1/13 1/16 1/19 1/22 1/25. I tried it using the sum of series formula by taking 1st term as 1, last term as 25 and number of terms as 9. I use the if loop and it is not running.

CodePudding user response:

Maybe something like this?

number_of_terms = 9
total = sum(1/(1 i*3) for i in range(number_of_terms))

Or, if you want to provide start, end and step, use:

start = 1
end = 25
step = 3
total = sum(1/i for i in range(start, end 1, step))

CodePudding user response:

If you have a list of values, you can use the sum() function:

list = [1, 2, 3, 4, 5]
total = sum(list)
print(total) # 15
  • Related