Coding is (of course) about building things to help others. However, creating programs and software has more to do with automating repetitive or complex tasks than anything else. Python’s while loop lets you repeat suites of code to automate many actions at once.
In this post, we show you how to use Python’s while loop. First, let’s talk about what the while loop does and where it’s best used.
Introducing Python’s while Command
Before we get into creating a while loop, let’s set the scene. In a nutshell, the while command runs defined blocks of code (a “statement”) until a condition (the “expression”) is met.
This condition is evaluated in “Boolean context.” In other words, a while loop runs code while the expression equals true and stops when it’s false.
We show you just how minimal it is as a command further down, but for now, understand that you can do plenty with only a few lines.
Why You’d Want to Create a while Loop with Python
Far from being a niche command, while loops are found everywhere in code. Consider the following scenarios:
- You’re making something and need to keep the program running until the user wants to quit.
- You build websites using Django or Flask and often pull data from a MySQL or MariaDB database.
- A script you’ve written needs to parse an entire spreadsheet of data and carry out specific actions.
As such, while loops are flexible and powerful – and they will have many more applications than this.
However, “while” is more of a basic command compared to another looping command, such as “for.” In short, while is used to run code over a longer term than for. The latter works with number ranges, so there is always going to be a natural end to the loop.
How to Create a while Loop with Python
To create a while loop, you’ll need to define the command, then offer a measurable condition. In fact, you could do this in two lines:
while True: print("True!")
However, this will cause an “infinite loop” – i.e. one that never ends. You’re welcome to try it, but make sure you can quit your terminal session!
To offer you an example of a game loop, you may choose to do the following:
running = '-1' while running != 1: run_game()
Let’s break this down. First we define a variable with a specific value. Usually you’d pick something that has no chance of cropping up in your program.
Next, we begin a while loop and use the variable in the expression. Here we’re saying “start this loop if ‘running’ isn’t equal to ‘1.’ ” Of course, “running” isn’t, so the loop calls the
run_game() function. Within
run_game(), you’d include the variable and change the value if you wanted to break out of it.
However, this is only completes half of the job. To get out of a while loop, you can nest “if” statements inside the while loop and use
break commands. Take this example:
list = ['a', 'b', 'c'] while True: if not list: break print(list.pop(-1))
Here, the while loop begins as
True and pops items from the list to print them. Once the list is empty, the expression becomes
False, and the while loop breaks.
Automating your code is one way to increase its efficiency. What’s more, you can build complex actions and give the end user powerful functionality. Python’s while loop is a wild horse to tame, but once you do so, it will become a staple of your workflow.
Are you considering using Python’s while loop, and if so, for what application? Let us know in the comments section below!
Our latest tutorials delivered straight to your inbox