Here are some tools to use to do loops in the Python language.
The While Loop
while statement will continually cycle through the block of code until the condition is no longer true.
A forever loop:
To use a while loop to cycle through something 5 times, you could use this code.
Using Continue and Break
continue statement is used when you want to go back to the beginning of the
break statement is used when you want to leave the
while loop all together.
The For Loop
Another way to do a loop is using a
Here is an example:
This will loop through 10 times because of the
range(10) method and print out the value 45.
range() method allows 3 arguments to be passed to it. Here are 3 ways to use it:
For Each Item in a List
If you want to do something with the values of a list, you can cycle through it like this.
If you want to iterate through a list and make edits to the list you can do this:
This changes all your F grades to As!
Looping Through all Keys
If using a dictionary, you can loop through all keys this way: