

So, the above code will give us the following output. Otherwise, we will print the value of x and repeat. Write a program to find the product of the digits of a number accepted from the user.

Write a program to find the sum of the digits of a number accepted from the user. If it is 7 then we will skip the printing part. Write a program to check whether a number is prime or not using while loop. Inside the body of the while loop we are first increasing the value of x by 1. In the above code we are starting by initialising x = 0. If the number is 7 then we are skipping it using the continue statement. In the following Python program we are printing integer number from 1 to 10. We use the continue keyword to skip the current iteration and move to the next. When the value of x = 7 then the break statement will be executed and we will jump out of the while loop body.įollowing is the output of the above code. In the following Python program we are going to print integer numbers from 1 to 10 but we will jump out of the while loop when we encounter number 7. We use the break keyword to come out of the while loop body even if the condition of the loop is satisfied. In the following Python code we are printing only the odd numbers between 1 to 20 both inclusive. When the value of x becomes 11 the condition fails so we come out of the while loop. Then we are incrementing the value of x by 1.Īfter incrementing the value of x we are again checking the loop condition and repeating the body. Inside the body of the while loop we are printing the value of x. So, as long as value of x is less than or equal to 10 we are going to execute the body of the loop. We start by initialising the variable x = 1.įor the condition we are checking is x <= 10. The above code will print the following output. In the following Python program we are printing integer number 1 to 10 using while loop. Inside the body of the while loop we keep updating the condition of the loop so that we can come out of it. Where, condition is some condition and if it is satisfied then the body of the while loop is executed otherwise, it is ignored. We use the while keyword to create a while loop in Python.

So, to solve this task we take help of a loop that executes the print statement for us for 1000 or N number times. You can write the print statement 1000 times which we both know is very tiring and not the best way to solve the problem. Imagine you have to print "Hello World" text 1000 times. We use loop to complete repeating task with ease. Check out my post on 21 Python for Loop Examples.In this tutorial we will learn about while loop in Python.Ī loop is a block of code that gets executed over and over again as long as the given condition is satisfied. Finding the factorial of a given number using while loop n = int(input("Enter the number: ")) Print("Sum of even numbers till n:",sum) 18. Finding the sum of even numbers using while loop i = 0 Reversing a number using while loop in Python n = int(input("Enter a number: ")) Finding the multiples of a number using while loop n = int(input("Enter an integer: ")) Printing the square of numbers using while loop n = 1 Print("Average of given Numbers:",average) 14. Finding the average of 5 numbers using while loop p = 5 Example of using while loops in Python n = 1 Check out these examples to get a clear idea of how while loops work in Python. I also explained, the for loop is used when you know the.
Python while loop how to#
In this post, I have added some simple examples of using while loops in Python for various needs. In the for loop chapter, we learned how to use the for loop with examples. The syntax of a while loop is as follows: while condition: The loop will stop its execution once the condition becomes not satisfied. The while loop checks a condition and executes the task as long as that condition is satisfied.

In Python programming, we use while loops to do a task a certain number of times repeatedly.
