7554425D-0054-440D-B95E-D2ABC13D62CD Created with sketchtool.
{body}
Your Saved Info Packages

View your saved Course or Program Packages containing pricing and detailed curriculum.

Speak with a Learning Advisor.

Have any questions? We'll call you.

Fill out the form below and a Learning Advisor will reach out at a time convenient for you.

Please pick a valid date and time between 9 AM and 8 PM eastern (Monday to Friday)

By clicking "Book a call," you accept our Terms and will also receive exclusive offers and updates about new courses, workshops and events.

Python Tutorial

Python While Loop

Ready to start your career in Data? Find out more about BrainStation's Python Course

Introduction to Python While Loops

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):

initial_value 
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.

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

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.

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.

Solution

Please see the solution below to compare against your code.

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.

Set Password

You already have an account with BrainStation, but you still need to set up a password.