python tutorial

Python While Loop

Learn more about Python, one of the world’s most versatile and popular programming languages.

Python While Loops are a very clever way to run or execute a block of code multiple times with different values. This allows you to write the logic in that block of code only once and provide a different value to be used each time you run the block of code.

There are many ways to write loops in Python. One of them is by using a while loop. The steps to create and use a while loop are:

  1. Create a variable that holds the initial value.
  2. Start a while loop with a condition that involves the variable created in step 1.
  3. Write the logic inside the while loop that should be executed if the condition is True in step 2. The condition is always checked first and only if it’s True, the logic inside of the while loop will be executed otherwise the code that follows the while loop will be executed.
  4. Change the value of the variable created in step 1 inside the while loop.
  5. Repeat.

The generic syntax for a while loop is as follows (pay attention to indentation):

Code Example
while initial_value meets the condition:
    run this block of code if the condition for the while loop is True
    change intial_value
Run this code if condition for the while loop if False

Let’s review an example to better understand this. Imagine you want to write a program that prints numbers 1 to 10 but without having to repeat your logic of printing 10 different times. You can achieve this as follows by using a while loop. Please type the code as follows and run your cells in the notebook.

Code Example
initial_value = 1
while initial_value <= 10:
    print('initial value is: ', initial_value)
    initial_value = initial_value + 1
# initial value is:	1
# initial value is:	2
# initial value is:	3
# initial value is:	4
# initial value is:	5
# initial value is:	6
# initial value is:	7
# initial value is:	8
# initial value is:	9
# initial value is:	10

We have just created our first while loop that prints values 1 to 10 without writing our logic of printing 10 different times. It is very important to change the value of the variable that is used in the condition of the while loop. This should always be done inside of the while loop.

We increment the value of initial_value by 1 and assign it back into the same variable intial_value. This will update the value in initial_value to be 1 more than previously. When the value in initial_value reaches the value of 11, the while loop condition will fail as it is not less than or equal to 10.

Exercise: Python Loops

Python Loops Exercise Overview

Let’s put the skills of using while loops into practice by doing a small exercise.

Create a new notebook and name it python-loops-exercise. Please follow the steps below to implement the exercise. By the end, you will know to loop through a block of code multiple times until the condition remains True.

Python Loops Exercise Instructions

  • As part of this exercise, your job is to spend money from a piggy bank until there is none left, however, you can only spend $10 at a time. You will have $100 to start with. Each time you spend money, you will print the remaining amount of money left in the piggy bank.
  • Create a variable called piggy_bank_balance and assign it a value of 100 to start with.
  • Write a while loop as follows to spend money and print the remaining balance:
    • Start your while loop and put a condition for the loop to check if the piggy_bank_balance is greater than zero.
    • If the condition remains True, inside the while loop, deduct 10 from the piggy_bank_balance and assign the remaining amount back to the same variable piggy_bank_balance.
    • Print piggy_bank_balance with a string Remaining Balance.
  • After the while loop ends, print You have spent all your saved money.

Python Loops Exercise Solution

Please see the solution below to compare against your code.

Code Example
piggy_bank_balance = 100
while piggy_bank_balance > 0:
    piggy_bank_balance = piggy_bank _balance - 10
    print('Remaining Balance: ', piggy_bank_balance)
print('You have spent all your saved money')
# Remaining Balance:	90
# Remaining Balance:	80
# Remaining Balance:	70
# Remaining Balance:	60
# Remaining Balance:	50
# Remaining Balance:	40
# Remaining Balance:	30
# Remaining Balance:	20
# Remaining Balance:	10
# Remaining Balance:	0
# You have spent all your saved money

Great work creating Python programs! As you can see, these fundamental concepts are used to handle data and implement basic functionality. As your skills continue to grow, these foundational concepts will help you write more advanced logic.

Now that you have learned the basic syntax of control flow statements and loops, let’s shift our focus to yet another important programming language concept called functions.

Learn Python Today

Get hands-on experience writing code with interactive tutorials in our free online learning platform.

  • Free and fun
  • Designed for beginners
  • No downloads or setup required