This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 3: More on lists

Applied exercise

face Josiah Wang

Now, let’s make sure you really understand how to use lists! Time for you to get your hands dirty with a simple exercise!

Your task is a write a function named increment_list().

The function should read in a list of integers as its input argument.

The function should also take an optional keyword argument step which controls by how much the function should increment each integer in the list. The value of step should default to 1.

The function should then increment each integer in the list by the given step.

The function should return a list of incremented integers.

Sample usage #1

numbers = [1, 2, 3, 4]
incremented_numbers = increment_list(numbers)
print(incremented_numbers)  # [2, 3, 4, 5]

Sample usage #2

numbers = [2, 1, 7, 5, 2]
incremented_numbers = increment_list(numbers, step=3)
print(incremented_numbers)  # [5, 4, 10, 8, 5]

Sample usage #3

numbers = [-2, 5]
incremented_numbers = increment_list(numbers, step=-2)
print(incremented_numbers)  # [-4, 3]

Sample usage #4

numbers = []
incremented_numbers = increment_list(numbers)
print(incremented_numbers)  # []