Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Loops and Conditionals

Here are resources about loops, operators, and conditional statements in Python:

00:00 Using Loops and Conditional Statements.

00:04 One of the strengths of computers is that they can repeat a task many times—often, far faster than a human can. In this section, you’ll see how to achieve this via two different methods, the for loop and the while loop. In addition, you’ll take a look at conditional statements, which are useful when interacting with users and also when working with loops.

00:24 One of the great strengths of using programming with instant visual feedback is it allows you to see what your program is doing straightaway. It can be a really important learning tool to be able to visualize the behavior of your program, as you’ll see later on in this section. Firstly, let’s take a look at the for loop, which provides for a definite number of repetitions of the instructions which we provide.

00:46 You may remember from earlier on in this course that a square was drawn in the following manner. As you can see, the same pair of instructions, .forward() then .right(), was repeated four times and led to the square on the right.

01:01 However, this is inefficient. What’s needed is a way to say, “Do this four times,” and that’s what a for loop provides. Here is an updated version using a for loop. As you can see, it has the two instructions present indented inside the for loop.

01:18 This indentation is what tells Python what belongs inside the loop. When it’s run, it leads to exactly the same end result, but with much cleaner and simpler code.

01:28 While you could imagine doing the previous version with a four-sided shape, imagine doing it for one with 30 or 100 sides. Clearly, loops like this will scale much more efficiently and leave you to concentrate on the meaning of your programs rather than repeating yourself.

01:44 This is a key part of computer science more generally.

01:49 If you want to know more about for loops, then check out the Real Python course by searching for the course name, Python “for” Loops, or following the link shown.

01:58 Sometimes, we may not know how many times a loop will need to be executed. Take the following modification of the previous program. All that’s been changed is the angle that the turtle turns on each iteration.

02:11 You may be much more mathematically aware than I am and instantly know how many iterations are needed to bring the turtle back to the starting point and complete the shape. But I don’t.

02:22 So another way to approach this is needed, and that’s to repeat the loop until the turtle returns to the home position and then stop. This is where conditional statements come into play.

02:33 We can check that something has happened and change the program’s behavior accordingly. Let’s look at a simple example. You can use conditional statements to check if a given condition is True.

02:45 If it is, then the corresponding command is executed. Try typing in the following program.

03:08 input() is used to obtain input from the user. Here it will store the user’s response under the variable u. Next, it compares the value of u with a condition that’s provided and checks, in this case, whether the value of u is "yes". If it is "yes", then the program draws a circle.

03:34 If the user types anything else, the program won’t do any more.

03:44 Note that the comparison operator is two equal signs (==). It’s used to check if the value of something is equal to something else. When you want to assign a value, you use a single equal sign (=) as the assignment operator.

03:59 To learn more about the differences between the two, check out the Operators and Expressions course in Python at You can also add an else clause to an if statement where you specify two results based on whether the condition is True or False.

04:16 Let’s look at that in this program.

04:24 Here, the program has been told to display a particular output even when the user does not say yes. You can use print() to display some predefined characters on the screen.

04:38 Note that the user doesn’t need to type no. They can type anything except yes, and then the result will be Okay, because you’re not explicitly telling the program that the user needs to type no.

04:50 Not to worry, as that can be fixed. You can add an elif clause to provide the program with several conditions and their respective actions, as you can observe here.

05:14 As you can see, this program now has more than one outcome depending on what the input is. Here’s how it works. If you type in yes, then the code processes the input and draws a circle as per your instructions.

05:27 If you type in no, then the code prints out "Okay", and the program is terminated. If you type in anything else, like hello or sandwich, then the code prints "Invalid Reply", and the program ends.

05:42 Note that this program is case-sensitive, so when you’re trying it out, be sure to put the strings in uppercase or lowercase accordingly.

05:51 To learn more about conditional statements, search for Conditional Statements in Python on the Real Python site, or follow the link in the slide. Now that you understand conditional statements, you can take a look at the while loop, which provides iteration until a condition is met and you break out of the loop.

06:10 This particular case is one which is given in the Python turtle documentation. Inside the while loop, the turtle moves forward and turns left 170 degrees.

06:21 The next line checks to see if the position of the turtle is approximately at the home location, and if it is, the loop is exited with the break command.

06:30 Otherwise, the loop actions are continued and this repeats until the turtle finds its way back home. This can take many repetitions, but you don’t have to work this out in advance.

06:41 It will just keep doing the action until it hopefully gets to the right place. In this case, this is the end result.

06:50 If you want to know more about while loops, then check out the Real Python course by searching for the course name, Python “while” Loops, on or following the link shown in the slide.

07:03 To avoid repeating yourself and aid with your experimentation, it’s possible to turn the code seen previously into a function, with the first line creating a function called draw_shape(), and it takes angle as its argument.

07:17 The rest of the function is almost identical to the previous code, just indented by another level, using Tab on the keyboard. And changing the turtle.left() command to take angle instead of the fixed value it had previously.

07:32 You can now try different angles by calling draw_shape() with the angle you want. Here’s the result for 125. This means you can do lots of experiments without having to type out the same code again and again, once more avoiding repeating yourself.

07:49 Now that you’ve seen how loops and conditionals in Python can help you create your program, let’s move on to the final project: the Python turtle race.

Become a Member to join the conversation.