My While Loop Wont Run Again
Control Flow Statements: Python While Loop
As discussed in the previous module, we know that Python, like other top programming languages, consists of some command flow statements. One of the control menstruum statements that we have already studied in the previous module is the Python if else statement. Another one of the control menses statements is loops. Loops are used when we want to repeat a block of lawmaking a number of times. In this module, we will learn nigh Python while loop.
Post-obit is the list of all topics that nosotros will cover in this module.
- What Is While Loop in Python?
- Infinite While Loop in Python
- Exercise While loop in Python
- While truthful in Python
- Else with While Loop in Python
- Python While Loop Interruptions
- Number Blueprint Program in Python using While Loop
- Factorial Program in Python using While Loop
And then, without whatever further delay, let'south become started.
What Is a While Loop in Python?
While loop statements in Python are used to repeatedly execute a certain statement every bit long as the condition provided in the while loop statement stays true. While loops permit the programme control to iterate over a cake of code.
Syntax of While Loop in Python:
while test_expression: trunk of while
The following flowchart explains the working of the while loop in Python.
The program first evaluates the while loop condition. If it's true, then the program enters the loop and executes the body of the while loop. It continues to execute the torso of the while loop as long as the condition is true. When it is false, the program comes out of the loop and stops repeating the body of the while loop.
Let'due south encounter the post-obit case to sympathise it better.
a = 1 while( a<ten): print(" loop entered", a, "times") a = a+i print("loop ends here") Output: loop entered 1 times loop entered 2 times loop entered 3 times loop entered 4 times loop entered 5 times loop entered half-dozen times loop entered 7 times loop entered 8 times loop entered 9 times loop ends here Interested in learning Python? Enroll in our Python Course in London now!
Space While Loop in Python
Infinite while loop refers to a while loop where the while condition never becomes fake. When a status never becomes fake, the programme enters the loop and keeps repeating that aforementioned block of code over and over again, and the loop never ends.
The post-obit case shows an infinite loop:
a = 1 while a==ane: b = input("what's your name?") print("Hi", b, ", Welcome to Intellipaat!") If we run the in a higher place lawmaking block, information technology will execute an infinite loop that will ask for our names once more and again. The loop won't break until nosotros press 'Ctrl+C'.
Output:
what's your name? Akanksha Rana #user input Howdy Akanksha Rana , Welcome to Intellipaat! what's your proper name? Amrit #user input Hi Amrit , Welcome to Intellipaat! what'due south your name? Shubham #user input Hi Shubham , Welcome to Intellipaat! what'due south your name? Traceback (most recent call last): #Stopped the loop by entering CTRL+C File "", line 2, in KeyboardInterrupt
Practise While loop in Python
Python doesn't have a do-while loop. But nosotros tin can create a programme to implement practise-while. Information technology is used to check weather after executing the argument. Information technology is similar a while loop simply it is executed at least in one case.
i = 1 while True: print(i) i = i + 1 if(i > 5): break The output will exist 1 2 iii 4 5
Now, take a wait at our Python grooming for upgrading your career to new heights. Too, check out our gratis Python Interview Questions.
While truthful in Python
There is a concept of declaring a condition to be true, without evaluating any expression. This is done to indicate that the loop has to run until it breaks. We then write the pause statements inside the code cake.
The while true in python is simple to implement. Instead of declaring whatsoever Python variable, applying conditions, and so incrementing them, write truthful inside the conditional brackets.
weeklySalary = 0 dayOfWeek = i calendar week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Sabbatum", "Sunday"] while(Truthful): if(week[dayOfWeek] == "Sunday"): impress("Holiday!!") break weeklySalary += 2000 dayOfWeek += 1 print(str(weeklySalary)) The output will be Holiday!! 10000
Else with the While Loop in Python
In Python, nosotros can also use the else statement with loops. When the else statement is used with the while loop, it is executed only if the condition becomes false.
a = 1 while a<5: print("condition is truthful") a=a+ane else: print("condition is simulated now") The instance illustrates how the else argument works with the while loop.
Output:
condition is truthful condition is true condition is true status is truthful condition is false now
In the above instance, the program keeps executing the body of the while loop till the condition is true, meaning that the value of a is less than 5. Since the initial value of a is ane and every time the program entered the loop the value of a is increased past 1, the status becomes false later the plan enters the loop for the fourth fourth dimension when the value of a is increased from iv to 5. When the program checks the condition for the 5th time, it executes it as faux and goes to the else block and executes the torso of else, displaying, 'status is faux at present.'
Watch this video on 'Python Tutorial':
Python While Loop Python While Loop
Kicking-start your career in Python with the perfect Python Form in New York now!
Python While Loop Interruptions
Python offers the post-obit two keywords which nosotros tin apply to prematurely stop a loop iteration.
Interruption statements in While loop
- Break: The break keyword terminates the loop and transfers the control to the end of the loop.
Example: a = one while a <5: a += 1 if a == 3: break print(a) Output: 2
Continue statements in While loop
- Continue: The continue keyword terminates the ongoing iteration and transfers the command to the top of the loop and the loop condition is evaluated once again. If the status is truthful, then the next iteration takes place.
Example:
a = one while a <5: a += 1 if a == 3: continue impress(a) Output: 2 4 5
Go for the virtually professional Python Form Online in Toronto for a stellar career now!
Number Pattern Program in Python using While Loop
due north = int(input("Enter the number of rows: ")) i = 1 while i <= n: j = 1 while j <= i: print("*", end = " ") j += ane print() i += ane The output volition be Enter the number of rows: 4 * ** *** ****
Factorial Program in Python using While Loop
num = int(input("Enter a number: ")) fac = i i = one while i <= num: fac = fac * i i = i + ane print("Factorial of ", num, " is ", fac) The output will be Enter a number: 4 Factorial of 4 is 24
With this, we come up to the end of this module on Python Tutorial. You can too go through this Python Data Scientific discipline tutorial to know why Python is the most preferred language for Data Science. Besides, check out our costless Python Interview Questions.
Become 100% Hike!
Principal Most in Demand Skills Now !
brewsterhatte1962.blogspot.com
Source: https://intellipaat.com/blog/tutorial/python-tutorial/python-while-loops/
0 Response to "My While Loop Wont Run Again"
Post a Comment