Exploring Loops
Using a Loop
A loop is a way to repeat the same action.
Instead of writing:
1 2 3 4 5 6 7 8 9 |
|
We can use a loop:
1 2 3 4 |
|
Let's examine the pieces of the for statement:
- Starts with the reserved word for
- Uses a variable that iterates
- Next is the reserved word in
- Finally, there is the data that is iterated over. In this example, we have a set (or range) of numbers. Note: a range is defined (by default) as starting at zero, so range(4) means that we are counting 0, 1, 2, 3.
See a loop in action: https://trinket.io/python/11386c7fa2
More on Loops
- We can change the starting value of a range, for example: range(2, 6)
- What numbers are in this sequence?
- We can change the way a loop counts, for example: range(1, 100, 2)
- What numbers are in this sequence?
- We can nest loops:
1 2 3
for x in range(2, 6): for y in range(1, 100, 2): z = x * y
- Remember that Python variables can be words as well as numbers, so what happens if we do this?
1 2 3
animal = 'squirrel' for x in animal: print(x)
Experiments
- Can you make the turtle draw a larger square? Hint: change the distance to be 80. How big can you make the square before the turtle goes off the screen?
- A polygon's internal angle can be calculated as: angle = 360 / Number of sides. A square is a type of polygon. It has four sides, so the internal angle = 360 / 4 = 90 degrees!
- Can you make a hexagon? This is a figure with six sides. Hint: Use the equation above to calculate the internal angle.
- Can you make an octagon? An Octagon has eight sides.
- Can you make a stop sign? You will need to use a jet.color('red'). a jet.beginfill() and a jet.endfill(). You can add the text of the word "stop" by using jet.moveto(x,y) and jet.write('STOP', font=("Arial", 30, "normal")). You can also use the jet.hideturtle() so that the outline of the turtle is not displayed at the end.
Drawing the Stop Sign: https://trinket.io/python/eb27ed7fef