Loops
We often want code to run again and again, until it’s time to stop. For example, we might want to read through a file until we find a certain word or until we reach the end of the file. We might also need to run part of the program for a certain number of time. For example, we might want to ask the users to enter their password three times before we give up. Loops is your tool to do that.
Loops reduce the amount of code you need to write. Instead of having to type out the same thing again and again, or run your program many times, you can use a loop to repeat your code instead.
Loops empower you to automate repetitive tasks, solve complex problems with elegance, and build powerful applications that can handle vast amounts of data.
In this weeks’s lessons, we will learn how to do that using loops.
Topics Covered
By the end of these lessons, you'll have learned:
- loops let you repeat code, either a specific number of times, or until a condition is met
while
repeats code for as long as a condition is Truefor
repeats code for a specific number of iterationsrange
lets us control the start, end, and step size of afor
loop- You can debug loops using
print
, or by stepping through the code with a debugger like pythontutor