Use of loops with “while”

I made a Python program with a basic “while” loop.

For using loops in phyton, you have to apply the while function, for using it you have to place a condition and if that condition is True, this loop will end and the program is going to do the next action after the while,while loops, like the for loop, are used for repeating sections of code – but unlike a for loop, the while loop will not run n times, but until a defined condition is met. As the for loop in Python is so powerful, while is rarely used, except in cases where a user’s input is required its important to know that the condition of the while has to be the opposite you want for the loop end. for example if we want to end the loop when the variable is equal to 0 we use:while(variable!=0):, but this wont include the 0 on the program, so we use while(variable>0):. It is important to know that the code that is going to be in the loop has to be indented, this means with 4 spaces or a shift distance from the left than the while, like in the example below.

CC BY 4.0 Use of loops with “while” by Jorge Padilla is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.