Given that a list
[10,20,30,40,50,60]
If I want to sum up every second number. For instance 20+40+60=120
.
So what should I write in python? Is it while loop or for loop?
Given that a list
[10,20,30,40,50,60]
If I want to sum up every second number. For instance 20+40+60=120
.
So what should I write in python? Is it while loop or for loop?
Try this.
nth = 2
values = [10,20,30,40,50,60]
total = sum(values[1::nth])
print(total)