While Loop in python

While Loop in Python | While Loop Example | Print First 10 Natural Numbers | Repeat Statements

Introduction to While Loop in Python

In Python, a loop is a programming structure that executes a set of statements repeatedly. While loop is one of the loop structures in Python. The while loop is used to execute a block of code repeatedly until a certain condition is met. In this type of loop, the condition is checked at the beginning of the loop, and if the condition is True, then the loop continues to execute. The loop stops executing when the condition becomes False.

Syntax of the while loop in Python

while condition:
# statements

The statements in the block are executed repeatedly as long as the condition is True. The condition is re-evaluated at the beginning of each iteration. If the condition is False at the beginning, then the statements inside the loop will not be executed at all.

Examples of While Loop in Python

Example 1: Program to print first 10 natural numbers

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

OUTPUT:

1
2
3
4
5
6
7
8
9
10

In this example, we use a while loop to print the first 10 natural numbers. The loop runs until the value of n becomes greater than 10. Inside the loop, we print the value of n, and then we increment the value of n by 1.

Example 2: Calculate the Sum of Numbers from 1 to 10

Program to calculate the sum of numbers from 1 to 10

n = 1
sum = 0
while n <= 10:
sum += n
n += 1
print(“The sum of numbers from 1 to 10 is:”, sum)

OUTPUT:

The sum of numbers from 1 to 10 is: 55

In this example, we use a while loop to calculate the sum of numbers from 1 to 10. We initialize the value of n to 1 and the value of sum to 0. Inside the loop, we add the value of n to sum and then increment the value of n by 1. The loop runs until the value of n becomes greater than 10. Finally, we print the value of sum.

Example 3: Print the Multiplication Table of a Number

Program to print the multiplication table of a number

num = int(input(“Enter a number: “))
n = 1
while n <= 10:
print(num, “x”, n, “=”, num*n)
n += 1

OUTPUT:

Enter a number: 7
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

In this example, we use a while loop to print the multiplication table of a number. We ask the user to enter a number using the input() function. Inside the loop, we print the multiplication table of the number by multiplying the number with the loop variable n. The loop runs until the value of n becomes greater than 10.

Conclusion

In conclusion, the while loop is a fundamental control structure in Python that enables us to execute a set of statements repeatedly until a specified condition is met. It is a versatile construct that is used in various programming applications, such as iteration, searching, and data processing. By understanding the syntax and usage of the while loop in Python, developers can design efficient and effective programs that solve complex problems.

Similar Resources

Printing oscillating series in Python