From the course: Introduction to Python: Learn How to Program Today with Python by Pearson

Unlock this course with a free trial

Join today to access over 25,400 courses taught by industry experts.

Use a "while" loop

Use a "while" loop

It's finally time to start looking at looping, which is a classic programming concept and controlling the flow, so you don't have to repeat yourself over and over again. The first loop we're going to talk about is while loops. This is one of the ones that's a bit easier to grab your head around when you're a beginner developer. Basically, you're going to just keep looping until a certain condition is met or until you're told to stop. The way it works in Python is, let's just go straight into working in the example file, so I don't have to deal with multi-line code in the REPL. So you can use the while keyword. And there's going to be a condition here. For now, we can just use while true. We'll loop. I can print something and print hi. Now, if I run this, it is going to just print hi forever. And it just keeps printing to the console until I stop it. And that is a totally valid thing to do. You might have a program that you want it to keep running until the user manually stops it. And…

Contents