Conditional sum
The loop in Listing 1 adds each even value of i to total.
for loop (Question 1).
What is total after the loop?
- 0
- 3
- 6
- 10
TipHint
range(start, stop, step)supplies integers fromstarttoward, but excluding,stop.stepdefaults to1, andrange(stop)starts at0.list(range(2, 7, 2))is[2, 4, 6];list(range(5, 1, -2))is[5, 3].- A range is empty when the step cannot reach the stop; a zero step raises
ValueError.
listcollects the supplied values in a new list.