Why Wont My While Loop in a for Loop Go Through Again

Command Flow Statements: Python While Loop

Equally discussed in the previous module, we know that Python, similar other summit programming languages, consists of some control menstruation statements. One of the command flow statements that we have already studied in the previous module is the Python if else statement. Another ane of the command flow statements is loops. Loops are used when we want to repeat a block of code a number of times. In this module, we volition learn about Python while loop.

Post-obit is the list of all topics that we will encompass in this module.

  • What Is While Loop in Python?
  • Infinite While Loop in Python
  • Practice While loop in Python
  • While truthful in Python
  • Else with While Loop in Python
  • Python While Loop Interruptions
  • Number Pattern Program in Python using While Loop
  • Factorial Program in Python using While Loop

Then, without any further delay, let'due south go started.

What Is a While Loop in Python?

While loop statements in Python are used to repeatedly execute a sure statement as long as the condition provided in the while loop statement stays truthful. While loops let the program command to iterate over a block of code.

Syntax of While Loop in Python:

while test_expression: body of while

The following flowchart explains the working of the while loop in Python.

python while loop

The program first evaluates the while loop condition. If it'southward true, and so the programme enters the loop and executes the torso of the while loop. It continues to execute the trunk of the while loop as long as the condition is true. When it is false, the programme comes out of the loop and stops repeating the trunk of the while loop.

Let'south see the following case to empathise it better.

a = 1   while( a<10): impress(" loop entered", a, "times") a = a+1 print("loop ends here")   Output: loop entered i times loop entered 2 times loop entered 3 times loop entered 4 times loop entered 5 times loop entered vi times loop entered 7 times loop entered viii times loop entered 9 times loop ends here

Interested in learning Python? Enroll in our Python Course in London now!

Infinite While Loop in Python

Space while loop refers to a while loop where the while condition never becomes false. When a condition never becomes faux, the programme enters the loop and keeps repeating that aforementioned block of code over and over once more, and the loop never ends.

The following case shows an infinite loop:

a = 1   while a==1: b = input("what's your name?") print("Howdy", b, ", Welcome to Intellipaat!")

If nosotros run the above code cake, it will execute an infinite loop that will inquire for our names once again and once more. The loop won't break until we press 'Ctrl+C'.

Output:

what's your name? Akanksha Rana #user input Howdy Akanksha Rana , Welcome to Intellipaat!   what's your proper noun? Amrit #user input Hi Amrit , Welcome to Intellipaat!   what's your name? Shubham #user input How-do-you-do Shubham , Welcome to Intellipaat!   what'south your name? Traceback (most recent call last): #Stopped the loop past entering CTRL+C File "", line 2, in KeyboardInterrupt

Exercise While loop in Python

Python doesn't take a do-while loop. But we can create a program to implement do-while. Information technology is used to check weather after executing the statement. Information technology is like a while loop but it is executed at least once.

          i = i          while True:                    print(i)                                i = i + 1                      if(i > 5):                      intermission                      The output volition be          ane          2          3          4          5        

Now, take a wait at our Python grooming for upgrading your career to new heights. As well, bank check out our free Python Interview Questions.

While true 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 information technology breaks. Nosotros and so write the intermission statements within the lawmaking block.

The while true in python is simple to implement. Instead of declaring whatever Python variable, applying weather, and then incrementing them, write true within the conditional brackets.

          weeklySalary = 0          dayOfWeek = i          week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Fri", "Saturday", "Sun"]          while(True):                      if(week[dayOfWeek] == "Sun"):                      impress("Holiday!!")                      suspension                      weeklySalary += 2000                      dayOfWeek += 1          impress(str(weeklySalary))          The output will be          Vacation!!          10000        

Else with the While Loop in Python

In Python, we can also utilise the else statement with loops. When the else argument is used with the while loop, it is executed only if the condition becomes imitation.

a = 1  while a<v: print("status is true") a=a+i  else: print("status is simulated at present")

The example illustrates how the else statement works with the while loop.

Output:

condition is true status is true status is true status is true condition is false now

In the to a higher place example, 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 1 and every time the program entered the loop the value of a is increased past 1, the condition becomes false after the program enters the loop for the fourth time when the value of a is increased from 4 to five. When the program checks the condition for the fifth time, information technology executes it equally false and goes to the else block and executes the body of else, displaying, 'condition is imitation at present.'

Watch this video on 'Python Tutorial':

Python While Loop Python While Loop

Boot-starting time your career in Python with the perfect Python Course in New York now!

Python While Loop Interruptions

Python offers the post-obit 2 keywords which we can apply to prematurely cease a loop iteration.

Interruption statements in While loop

  1. Suspension: The break keyword terminates the loop and transfers the command to the finish of the loop.
Example: a = 1 while a <v: a += 1 if a == 3: intermission print(a) Output: 2

Continue statements in While loop

  1. Continue: The continue keyword terminates the ongoing iteration and transfers the control to the top of the loop and the loop condition is evaluated again. If the condition is truthful, then the next iteration takes identify.

Case:

a = i while a <5: a += 1 if a == 3: continue print(a)  Output: 2 4 5

Go for the almost professional Python Class Online in Toronto for a stellar career now!

Number Pattern Program in Python using While Loop

          n = int(input("Enter the number of rows: "))          i = 1          while i <= due north:                      j = 1                      while j <= i:                      print("*", finish = " ")                      j += 1                      print()                      i += 1          The output will be          Enter the number of rows: 4          *          **          ***          ****        

Factorial Plan in Python using While Loop

          num = int(input("Enter a number: "))          fac = ane          i = one          while i <= num:                      fac = fac * i                      i = i + one          impress("Factorial of ", num, " is ", fac)          The output will exist          Enter a number: iv          Factorial of  4  is  24        

With this, we come to the end of this module on Python Tutorial. You can too get through this Python Data Science tutorial to know why Python is the about preferred linguistic communication for Data Science. Also, check out our costless Python Interview Questions.

Get 100% Hike!

Master Most in Demand Skills Now !

mccannandits.blogspot.com

Source: https://intellipaat.com/blog/tutorial/python-tutorial/python-while-loops/

0 Response to "Why Wont My While Loop in a for Loop Go Through Again"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel