Reputation: 73
I have a list of 90 values and I would like to find the value of the sum of the first 30 of these values. Is there a way I can do this easily?
Upvotes: 0
Views: 645
Reputation: 11
Simplest way to do is slice the list and use sum function
ans = sum(input[:30])
If you want to add numbers between a particular range let's say you want to add numbers between 20-30(inclusive) then you can do that as
ans = sum(input[20:31])
Upvotes: 0
Reputation: 482
You can use the following command:
l = [1,2,3,4,5.....90]
s = sum(l[:30])
Upvotes: 2