From the course: Learning Python

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Loops

Loops

- [Instructor] Running a block of code over and over again, via a construct known as a loop is also a very common scenario in programming and Python provides a couple of ways of doing that, which we're going to take a look at now. So in the editor, let's go ahead and open up loops underscore start, and you can see here, I've got a function called main and a variable named X, which I'm initializing to zero. So let's start by taking a look at the while loop. So I'm going to write the while statement and then inside parentheses, I'm going to write X is less than five, and then I'll have a colon which defines the scope block of my while statement. I'm going to print X and then I'm going to have X become equal to X plus one, all right? So once again, you can see that the code in the while loop is indented to indicate that it belongs to the scope of the loop. A while loop executes a block of code while a particular…

Contents