For Loops with Ranges and Lists
Explore how to use for loops with ranges and lists to automate repetitive tasks in Python. Understand iterating through sequences, and apply these skills to build a weekly step tracker that totals and averages daily steps. This lesson helps you master essential looping techniques and develop practical coding projects.
The project
You’ve already created a step tracker that records daily steps. At the moment, though, it only works when you enter the numbers manually. Printing a single line, such as Steps today: 3,000, can be useful, but it doesn’t show your progress over an entire week. What you need instead is a system that stores seven days of step counts in a list and then loops through them to calculate both the total and the average.
Your project: build a weekly step tracker that keeps 7 days of steps in a list, uses a loop to add them up, and calculates the average steps per day.
The programming concept
A while loop repeats while a condition is true. But sometimes you already know exactly how many times you want something to be repeated. For example: Say hello for 5 times. Do you think a for loop or a while loop is more natural for that? The for loop in Python can work in two modes:
for loop can iterate on a range, e.g.,
for in range(5)directs Python to loop for 5 timesfor loop can work on a ...