 There are three main structures in programming, sequence, repetition, and conditionals. Everything we've done so far is sequence, one command after another. Now let's look at the second structure, repetition. Consider this program that draws a square. Let's run it, and sure enough that's what it does. This program goes forward 100 left 90 degrees four times. What if we had to draw a pentagon with five sides? That would require us to type forward and left five times. An octagon with eight sides would require us to type or copy and paste forward and left eight times, and it would be easy to lose count. What to do? But first, let's take a quick side trip and discuss calculating the angles for a regular polygon. For the square, we turned left 90 degrees because squares are all right angles. What if you went an equilateral triangle? You might think, oh, the angle's in a triangle all add up to 180 degrees. So you'll write code like this. Let's go forward 100, and then left 60 degrees, and let's copy and paste it so we have a total of three times, run the program, and it doesn't work. Why not? Pause the video and give it some thought before I reveal the answer. The problem is that the interior angles of a triangle all add up to 180 degrees, but that's not the perspective we want. The way we want to look at the problem is that we need to get the turtle back to its starting position in three turns for the three sides, and that's one full revolution of 360 degrees, so each turn has to be 120 degrees, 360 divided by 3. So let's change these 60s to 120s. And notice that's one other disadvantage of sequence. If I had five sides or eight sides, I'd have five or eight corrections to do. Let's run the program, and now the angle is calculated correctly. That's much more like it. That means if we want to draw a pentagon, five-sided figure, we have to go 360 degrees in five turns, which is 72 degrees each, 360 divided by 72. Here's the beginning of the program for the pentagon, and instead of writing a forward and left and copying and pasting it to get five total, this time I'm going to write a for loop. I'm going to say the word for, and then a loop variable, the variable that controls the loop, and in this case I'll call it counter, because I'm counting how many times I've drawn aside, the word in, and then a list of values that you want the counter or loop variable to take on. So one, two, three, four, five, and then a colon. After the four is set up, you have the body of the loop, the things you want done that many repeated times. And in this case I want to go forward 100 pixels and then left 72 degrees. After the loop is finished, I'm going to set the window background color to light blue. The body of the loop, on lines 11 and 12, is indented under the for statement, and it must be indented properly. All the statements in the loop body must line up exactly at their left-hand edge, or Python will complain. Let me put in a mistake on purpose, and you'll see that Python complains that I have an unexpected indent. Indenting properly is easy with Thani, because Thani helps you do it correctly. What's actually happening when we run this program? The loop variable counter refers to the first value in the list, one, and then it does the body of the loop, which draws one side of the pentagon. The loop variable then moves on to refer to the next value in the list, and does the body of the loop again. The loop variable counter proceeds to refer to the next value in the list, three. Does the body of the loop again to draw the third side of the pentagon? Counter moves on to the next value into the list, four. Does the loop body again? Then counter loop variable moves on to refer to the next value in the list, five. And does the loop body once more? That completes the pentagon. There aren't any more items in the list, so the for loop is finished, and the program proceeds to the next statement after the for. Again, it's important to note that setting the window background color to light blue is not indented, so it's not part of the loop body, it's outside the loop. Let's run the program to show that it does what we really expected it to do, and sure enough it works great. This program doesn't use the loop variable in the body of the loop. Let's change the program so that each side of the pentagon is drawn in a different color. First, I'm going to change the list of values to a set of color names. Instead of 1, 2, 3, 4, 5, I'm going to set the strings green, red, blue, orange, and purple. There are still five items in the list, so the loop will be executed five times. I'll still get a pentagon. But in this case, the loop variable isn't really designed to count. The loop variable is telling us which color to draw the side in. So let's change its name from counter to something more appropriate, like side color. Then inside the loop, before I draw the side, I'll set the color of the pen to be the side color. Let's run the program, and I get a multi-colored pentagon. Let's do one more program that involves a for loop, but not a graphic one. I'm going to move the shell up a bit, and I'm going to clear it and create a new program. This program is going to display the cube of the numbers 10 through 15. As usual, I put my name and the date. First, I'm going to have a line that labels my output. I'm going to print the word number and cube. And now I'm going to set up my for loop for counter in 10, 11, 12, 13, 14, and 15. The body of the loop will print the number and its cubed nicely lined up with my heading. I'm going to need formatted output for that, so I'm going to have my format string. This says print as an integer with six places, a space, and then another integer with six spaces. Format that and fill in the blanks with the counter and the counter to the third power, the cube of the counter. And then I'll print the end when I'm out of the loop. So counter will first refer to 10, and it will print 10 and a thousand. Then counter will refer to 11, and it will print 11 and whatever 11 cubed is, then 12 and 12 cubed, 13 and 13 cubed, all the way up to 15 and 15 cubed. When I try to run this program, I have to save it with a name, let's call it cubetable.py, and there I have my numbers and the cubes from 10 through 15 inclusive. There's an unanswered question here. What if we wanted the program to ask the user how many sides they want for their polygon? So they can say, oh, I want eight sides, or I want seven sides, or I want 12 sides. What if we wanted to ask them to start an ending number of the cube table? They want the cubes of all the numbers from 23 through 37. In that case, we can't write a list of values for our for loop, because we don't know in advance how many values we're going to need. We'll solve that problem in the next video.