Loops are used in programming to repeat a specific block of code. In this article, you will learn to create a while loop in Python. Show
Video: Python while Loop
What is while loop in Python?The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true. We generally use this loop when we don't know the number of times to iterate beforehand. Syntax of while Loop in Pythonwhile test_expression: Body of while In the while loop, test expression is checked first. The body of the loop is entered
only if the In Python, the body of the while loop is determined through indentation. The body starts with indentation and the first unindented line marks the end. Python interprets any non-zero value as Flowchart of while LoopFlowchart for while loop in PythonExample: Python while Loop
When you run the program, the output will be: Enter n: 10 The sum is 55 In the above program, the test expression will be We need to increase the value of the counter variable in the body of the loop. This is very important (and mostly forgotten). Failing to do so will result in an infinite loop (never-ending loop). Finally, the result is displayed. While loop with elseSame as with for loops, while loops can also have an optional The
The while loop can be terminated with a break statement. In such cases, the Here is an example to illustrate this.
Output Inside loop Inside loop Inside loop Inside else Here, we use a counter variable to print the string Inside loop three times. On the fourth iteration, the condition in Table of Contents
Python LoopsPython has two primitive loop commands:
The while LoopWith the while loop we can execute a set of statements as long as a condition is true. ExamplePrint i as long as i is less than 6: i = 1 Try it Yourself » Note: remember to increment i, or else the loop will continue forever. The while loop requires relevant variables to be ready, in this example we need to define an indexing variable, i, which we set to 1. The break StatementWith the break statement we can stop the loop even if the while condition is true: ExampleExit the loop when i is 3: i = 1 Try it Yourself » The continue StatementWith the continue statement we can stop the current iteration, and continue with the next: ExampleContinue to the next iteration if i is 3: i = 0 Try it Yourself » The else StatementWith the else statement we can run a block of code once when the condition no longer is true: ExamplePrint a message once the condition is false: i = 1 Try it Yourself » Can you do a while loop with multiple conditions Python?Use or and and to make a while loop with multiple conditions
Use and to require that multiple conditions all be met for a while loop to execute. Use or to require that any of at least two conditions be met for a while loop to execute.
Can we put 2 conditions in while loop?Using multiple conditions
As seen on line 4 the while loop has two conditions, one using the AND operator and the other using the OR operator. Note: The AND condition must be fulfilled for the loop to run. However, if either of the conditions on the OR side of the operator returns true , the loop will run.
How do you repeat 3 times in Python?How to Repeat a String Multiple Times in Python. Problem Formulation and Solution Overview.. Method 1: Use print() and multiplication operator.. Method 2: Use a For Loop and range(). Method 3: Use the input() function.. Method 4: Use itertools.repeat(). Method 5: Use a DataFrame.. Summary.. Regex Humor.. How do you put a condition in a while loop in Python?You initiate the loop with the while keyword, then set the condition to be any conditional expression. A conditional expression is a statement that evaluates to True or False . As long as the condition is true, the loop body (the indented block that follows) will execute any statements it contains.
|