Python While Loops

There's another type of loop you can use called a while loop. These types of loops keep going round and round until a condition is met, counter is greater than 10, for example. Let's see how they work.

Add the following variable to your code from the previous lesson:

loopCounter = 0

Now add this:

while loopCounter < 10:


loopCounter = loopCounter + 1

The while loop is easier to use than the for loop. On the first line, you type the word while then a space. Next you need a condition that can evaluate to True or False. In our code, we're just asking Python to check if the variable called loopCounter is less than 10. If it is, the loop keeps going round and round. If it's not less than 10 then Python bails out of the loop:

while loopCounter < 10:

Notice that you need a way to get out of the loop. If, for example, we don't advance the loopCounter variable then Python would be stuck in an infinite loop and the program would crash. We add 1 to the value of loopCounter each time round:

loopCounter = loopCounter + 1

Run your code and you should see the numbers 0 to 9 printed to the output window.

You can check the length of a list to see how many items it has. The condition for your while loop can then test if loopCounter is less than the length of the list. While it is less than the length of the list, you can loop round. To see what this means, make sure you still have your superheroes list set up:

superheroes = ["Jessica Jones", "Matt Murdock", "Kamala Khan", "Luke Cage"]

Now change the first line of your while loop to this:

while loopCounter < len(superheroes):

To get the length of a list, you just use the len function. In between the round brackets of len, type the name of a list you're trying to check. We're saying, while the length of the loopCounter variable is less than the length of the superheroes list then keep looping.

Change your print line to this:

print( superheroes[loopCounter] )

Notice how we're using the loopCounter variable with the list:


Square brackets are used on lists to access the index number of list items. Instead of saying superheroes[0], superheroes[1], etc, we're using loopCounter. The loopCounter variable changes each time round the loop so we can cycle through each list item.

Run your code and you'll see each superhero printed out.

This is a powerful technique as you don't have to use range to find out how big the list is. Just check the length of the list against a loop counter variable.


Python Break Statement

You can break out of your loops early, if you want. The command you need here is the keyword break. Here's some code to try:

loopCounter = 0

while loopCounter < len(superheroes):

if superheroes[loopCounter] == "Kamala Khan":




loopCounter = loopCounter + 1

Inside of the loop, we have an if statement

if superheroes[loopCounter] == "Kamala Khan":

If it's true that the current list item is Kamala Khan then we break out of the loop. If it's not true then our print statement is executed.

Run your code and you'll see that only Jessica Jones and Matt Murdock get printed.

In the next lesson, you learn what a tuple is in Python.

Python Tuples >