While Loop in Python (With Examples)

python-while-loop-with-examples

The while loop is a fundamental control flow statement in Python. While loop in python allows a program to repeat a set of instructions as long as a certain condition is true.

As you know in the previous tutorial we learn all about Conditional statements. Now we will go to learn about while loop in python. So, let’s start without wasting time.

So, the syntax of the while statement is as follows:

while condition:
    statements

In this syntax, “condition” is an expression evaluated before each iteration of the loop, and “statements” are the instructions executed if the condition is true.

The loop will continue to execute as long as the condition remains true, and will exit when the condition becomes false.

To get more understanding of while statements, we are going to discuss some examples:

How to print numbers from 1 to 10 by using the while loop:

Here we are going to print counting from 1 to 10 by using a while loop.

Code:

i = 1
while i <= 10:
    print(i)
    i = i + 1

Output:

1
2
3
4
5
6
7
8
9
10

This program initializes the variable “i” to 1, and then uses a while loop to print the value of “i” on each iteration, as long as “i” is less than or equal to 10.

The variable “i” is incremented by 1 on each iteration, so the loop will exit after printing the numbers from 1 to 10.

How to calculate the sum of numbers from 1 to 100 by using the while loop:

Here we are going to print the sum of numbers from 1 to 100 by using a while loop.

Code:

total = 0
i = 1
while i <= 100:
    total = total + i
    i = i + 1
print ("Total numbers between 1 to 100 is", total)

Output:

Total numbers between 1 to 100 is 5050

This program initializes the variable “total” to 0, then uses a while loop to calculate the sum of the numbers from 1 to 100.

The variable “i” is used to keep track of the current number being added to the total.

On each iteration, the present value of “i” is added to the “total”, and “i” is incremented by 1. After the loop exits, the total sum is printed.

How to find Factorial by using a while loop in Python:

Here is Python code to find the factorial of a number using a while loop.

Code:

n = 5
result = 1
i = 1
while i <= n:
    result = result * i
    i = i + 1
print("The factorial of", n, "is", result)

Output:

The factorial of 5 is 120

In this code, we initialize the number n to find the factorial (since the factorial of 0 and 1 is 1), and i is used to tracking how many times to multiply the number.

We then use a while loop to iterate over the numbers from 1 to n. For each number i, we multiply the result by i and increment i by 1.

Finally, we print out the result using the print function. This code will output: The factorial of 5 is 120.

So, here is all about the while loop in python. In this tutorial, we know how to use a while loop, and we also cover some examples of the while loop.

So, in the next tutorial, we are going to learn about For Statements. So, make sure to subscribe to us to learn python in an easy way.

Was this helpful?
YesNo

Related Articles:

Recent Articles:

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x