While Loops in Python: A Standard Tutorial

Mastering the Basics to become a Pro

Raphael Madu
4 min readDec 31, 2022
Photo by AltumCode on Unsplash

In a previous post, we talked loosely about the idea of for and while loops, when to use them, and essentially grazed the surface of what we can accomplish with the individual types of loops.

In today’s tutorial, I will be talking about while loops, what they are, when to use while loops and finally, I will be solving an algorithm problem that requires us to use a while loop. Without further ado, let’s get right into it

What are while loops?

Just to refresh our memory, WHILE loops repeatedly execute a statement as long as a given expression is true. A statement may be a single line of code or an entire block of code. The condition may be an expression. The loop iterates while the condition is true.

When do you want to implement a WHILE loop?

While loops are used when we want to repeat a certain block of code an unknown number of times.

Now that we have a basic understanding of what a while loop does and when to use it, let’s hop over to the problem set.

Sign Up for our Newsletter to never miss a time I post an article. You’ll also receive the Ultimate Python Machine Learning Cheat Sheet, The only Machine learning Cheat Sheet you’ll ever receive!!

Problem Set: Factorial of any number n is represented by n! and is equal to 1*2*3*….*(n-1)*n. Example:

4! = 1*2*3*4 = 24

3! = 3*2*1 = 6

2! = 2*1 = 2

Also:

1! = 1

0! = 1.

Write a program to calculate the factorial of a number.

Solution:

We are going to assign our user input to a variable called “number”. The input() function by default stores the user’s input as a string, so we have to convert the input to an integer and store it in a variable:

We set our “factorial” variable to 1 and I will explain that later in the code block. Now we have to check our user input by the defined conditions in the question. Reading over the question, we notice 0! still equals one, so we can write an if / else conditional to always print out 1 if the user inputs 0 as the value:

We will write the WHILE loop within the else statement. The reason we use a WHILE loop here is that we do not know what number the user would input so we want to repeat the particular code block an unknown number of times. For this problem, we are repeating a certain code as long as the user input is less than equal to 1 because 1! is 1 * 1 is still 1. We set our “factorial” variable to the factorial multiplied by the number the user inputted and subtract 1 from the number and keep on repeating this WHILE the number is greater than or equal to 1:

Outside the WHILE loop, we want our script/program to print out the final “factorial” output, only when the number is less than 1.

Result: Our final program should look something like the image below. Remember to always comment on your code.

The goal of this blog is to teach and explain Python algorithm questions to both new and experienced Python software developers.

Although this example was fairly straightforward and user-friendly for beginners, it’s crucial for someone who is new to programming to consistently practice the fundamentals. What will set you apart from the majority of other programmers is the complete mastery of the fundamentals.

--

--

Raphael Madu

Former web app developer turned machine learning engineer. Subscribe to my Newsletter to receive the Ultimate Python Machine Learning Cheat Sheet