What Are Python While Loops?

What Are Python While Loops?

Anatomy of a While loop

A while loop will continuously execute code depending on the value of a condition. It begins with a keyword while, followed by a comparison to be evaluated, then a colon. On the next line is the code block to be executed. Indented to the right. Similar to an if statement, the code in the body will only be executed if the comparison is evaluated to be true. What sets a while loop apart, however, is that this code block will keep executing as long as the evaluation statement is true. Once the statement is no longer true, the loop exits, and the next line of the code will be executed.


username = get-username()

while not valid_username(username):

        print("Invalid username")

        username = get_username()

The break Statement

With the break statement we will stop the loop albeit the while condition is true:

Exit the loop  once I  is 3:
i = 1
while i < 6:
if i == 3:
i += 1

The continue Statement

With the continue statement we will stop the present iteration, and continue with the next:

Continue to  subsequent  iteration if i is 3:
i = 0
while i < 6:
i += 1
if i == 3:

The else Statement

With the else statement we will run a block of code once when the condition is not true:

Print a message once the condition is false:
i = 1
while i < 6:
i += 1
print("i  is not any  longer  but  6")

While Loop inside a function

def attempts(n):


    while x<=n:




Common Pitfalls with Variable Initialization

We’ll want to watch out for a common mistake: forgetting to initialize variables. If we try to use a variable without first initializing it, we’ll run into a NameError. This is the Python interpreter catching the mistake and telling us that we’re using an undefined variable. The fix is pretty simple: initialize the variable by assigning the variable a value before we use it.

Another common mistake to watch out for that can be a little trickier to spot is forgetting to initialize variables with the correct value. If we use a variable earlier in our code and then reuse it later in a loop without first setting the value to something we want, our code may wind up doing something we didn’t expect. Don’t forget to initialize our variables before using them.

Why initializing Variables Matters

while my_variable<10:




my_variable = 5

while my_variable<10:



Infinite Loops and Code Blocks

Another easy mistake that can happen when using loops is introducing an infinite loop. An infinite loop means the code block in the loop will continue to execute and never stop. This can happen when the condition being evaluated in a while loop doesn’t change. Pay close attention to the variables and what possible values they can take. Think about unexpected values, like zero. We may see an error message that reads “Evaluation took more than 5 seconds to complete”.  This means that the code encountered an infinite loop, and it timed out after 5 seconds. We should take a closer look at the code and variables to spot where the infinite loop is.

More about Python

Using Python on our own

The best way to learn any programming language is to practice it on our own as much as we can. If we have Python installed on our computer, we can execute the interpreter by running the Python3 command (or just python on Windows), and we can close it by typing exit() or Ctrl-D.

If we don’t already have Python installed on our machine, that’s alright.  We can still practice by using one of the many online Python interpreters or code pads available online. There’s not much difference between an interpreter and a code pad. An interpreter is more interactive than codepad, but they both let us execute code and see the results.

Below find links to some of the most popular online interpreters and code pads. Give them a go-to find our favorite.

  • https://www.python.org/shell/
  • https://www.onlinegdb.com/online_python_interpreter
  • https://repl.it/languages/python3
  • https://tutorialspoint.com/execute_python3_online_php
  • https://rextester.com/I/python3_online_compiler
  • https://trinket.io/python3

Additional Python resources

Some great ways to help us find additional info:

  • Read the official Python documentation.
  • Search for answers or ask a question on Stack Overflow.
  • Subscribe to the Python tutor mailing list, where we can ask questions and collaborate with other Python learners.
  • Subscribe to the Python-announce mailing list to read about the latest updates in the language.
Mansoor Ahmed is Chemical Engineer, web developer, a writer currently living in Pakistan. My interests range from technology to web development. I am also interested in programming, writing, and reading.
Posts created 422

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top