Vary the Change Amount in RangeΒΆ

There is a version of range that takes an change amount, so that you can go up by more than one each time, or even down by a negative number. Try it out!

    csp-16-6-1: Which of these changes to the program would give you just the odd values in a list? (Again: Try it!) Select all that work.
  • Start with numbers=[1,2,3,4,5,6,7,8,9,10]
  • Yes, that would work, but there's an easier way
  • Change the range to range(1,len(numbers),2)
  • Yes, just by starting at 1, then skipping 2 each time, we'd collect the odds
  • Change the range to range(0,len(numbers),1)
  • No, that would collect all the numbers in evens
  • Change the range to range(0,len(numbers),3)
  • No, that would result in 0,3,6,9 in evens

Note

Discuss topics in this section with classmates.

Show Comments
Next Section - Using a Negative Change Value with Range