Python: For Loops

This is a worksheet to teach you for loops in Python/Sage:

Here is the basic “for loop”. This is a way to do something many times over. In this example, we wish to repeat the command print("hi") 10 times.

 
The variable i is incremented from 0 to 9 when use range(10). The funny offset (counting from 0) is just something funny to keep in mind. Before you hit enter on the following code, guess exactly what it will do:

If you want to start counting somewhere else, you can use range(a,b) to count from a to b-1. Now, you can do lots of fun things in your for loop:
 

 
Here’s a box for you to experiment with: