🌐
freeCodeCamp
freecodecamp.org › news › python-do-while-loop-example
Python Do While – Loop Example
August 31, 2021 - The while loop, on the other hand, doesn't run at least once and may in fact never run. It runs when and only when the condition is met. So, let's say we have an example where we want a line of code to run at least once. secret_word = "python" counter = 0 while True: word = input("Enter the secret word: ").lower() counter = counter + 1 if word == secret_word: break if word != secret_word and counter > 7: break
🌐
W3Schools
w3schools.com › python › python_while_loops.asp
Python While Loops
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... With the while loop we can execute a set of statements as long as a condition is true.
Discussions

Need help understanding this while loop
I’m new to while loops, and I don’t quite get what’s going on in this code from my book: current_number = 1 while current_number More on discuss.python.org
🌐 discuss.python.org
0
0
February 13, 2024
As a beginner how do I understand while loops?
It’s just a loop which continues running until the condition becomes false. More on reddit.com
🌐 r/learnpython
76
36
April 10, 2025
I need help in understanding while loop
It's nothing more than a loop that keeps running as long as the condition you give it evaluates to True. If you do while True, you get an infinite loop. If you do white x > 3, you get a loop that keeps repeating as long as the value of x is greater than 3. If you do while someFunction(), you get a loop that keeps repeating as long as someFunction() returns True. If you do while False, it never runs anything at all. This code; x = 3 while x != 0: print(x) x -= 1 print("Done!") Basically performs these steps; Set x to 3 x is not 0, so run the loop once Print x, then decrement it by 1. x is now 2 x is still not 0, so run the loop again Print x, then decrement it by 1. x is now 1 x is still not 0, so the loop runs a 3rd time Print x, then decrement it by 1. x is now 0. x is now 0, so the while loop's condition now evaluates to False and the loop will not run again. Print "Done!" More on reddit.com
🌐 r/learnpython
12
1
January 6, 2024
How do while not loops work
In general, recall the while loop syntax. while : Conceptually, coding aside, consider filling a glass of water. Is there space for more water in the glass? Okay, then pour some. Without going in detail of how this funky glass object is defined, hopefully the following makes sense as a python analog of that process. while glass.has_space(): #we're still filling the glass glass.add_water() #glass is now full of water Without writing out how this glass object works, let's say it has another method for returning a boolean (True/False) for whether or not it is currently full, instead of the previous example of whether or not it had space for more water. We can fill the glass in the same way, but we have to negate that statement using the not operator while not glass.is_full(): #we're still filling the glass glass.add_water() #glass is now full of water Going back to the initial syntax, which is still the same, the only thing we've done is changing the condition statement of the loop. Instead of checking that glass.has_space() evaluates to True, we are now checking that the expression not glass.is_full() evaluates to True. That is the same statement as evaluating that glass.is_full() evaluates to False, because the only thing the not operator does to a boolean is negating it, i.e. True becomes False and vice versa. Now looking at your code linked, the condition for looping is that not sequenceCorrect evaluates to True, which is equivalent to the statement that sequenceCorrect is False. I won't paste the code here, but we see on line 3 that sequenceCorrect starts its life being False, so upon entering the while loop we do indeed step into that block of code because at that time not sequenceCorrect is in fact True. Then the first thing we do on line 5 is reassign it to True. If the remaining lines don't change it back to False, this will stop the while loop from repeating, since in this current state not sequenceCorrect evaluates to False. So you can say that line 5 is defaulting the loop to not going to be repeated. The only way for the loop to be repeated is for lines 6-8 with the nested for loop and if statement to find some character in dna that is not in "actgn", upon which sequenceCorrect will be assigned to False and thus the statement in the while loop not sequenceCorrect would evaluate to True and thus the loop would repeat itself once more. Personally, I think this is just a pretty unclear way to achieve the goal of checking the validity of that input string. I would have defined it another way, but I can't see anything wrong with it really. If the not operation in the while loop statement is bothering you, you could equally have rewritten the code with a variable for instance named sequenceIncorrect and just flipped all assignments i.e. True's become False's and vice versa. More on reddit.com
🌐 r/learnpython
16
5
September 11, 2024
People also ask

What is a while loop Python?
A while loop has the syntax 'while condition: do_stuff' where 'do_stuff' is usually placed on the next line and indented. It executes the statements in 'do_stuff' repeatedly so long as 'condition' is true.
🌐
study.com
study.com › courses › computer science courses › computer science 113: programming in python
While Loops in Python | Definition, Syntax & Examples - Lesson ...
How do you use while in Python?
A while loop is used to repeatedly execute the indented block of code as long as the True-False condition following the word 'while' evaluates to True. It is ideal for situations where the total number of iterations necessary cannot be defined beforehand, such as obtaining user input and checking it until correct input is entered.
🌐
study.com
study.com › courses › computer science courses › computer science 113: programming in python
While Loops in Python | Definition, Syntax & Examples - Lesson ...
What is a while statement in Python?
The word 'while' in Python is a reserved word which creates a while loop using the syntax: while condition: do_stuff. If do_stuff is more than one line, it should be put on the next line and indented. The 'condition' is evaluated before each iteration of the loop, and 'do_stuff' is executed so long as 'condition' is true.
🌐
study.com
study.com › courses › computer science courses › computer science 113: programming in python
While Loops in Python | Definition, Syntax & Examples - Lesson ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-while-loop
Python While Loop - GeeksforGeeks
When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements. Python Continue Statement returns the control to the beginning of the loop. ... # Prints all letters except 'e' and 's' i = 0 a = 'geeksforgeeks' while i < len(a): if a[i] == 'e' or a[i] == 's': i += 1 continue print(a[i]) i += 1
Published   December 23, 2025
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-while.html
While Loop
The while-loop syntax has 4 parts: while, boolean test expression, colon, indented body lines:
🌐
Real Python
realpython.com › python-while-loop
Python while Loops: Repeating Tasks Conditionally – Real Python
March 3, 2025 - The next script is almost identical to the one in the previous section except for the continue statement in place of break: ... This time, when number is 2, the continue statement terminates that iteration. That’s why 2 isn’t printed. The control then returns to the loop header, where the condition is re-evaluated. The loop continues until number reaches 0, at which point it terminates as before. ... Python allows an optional else clause at the end of while loops. The syntax is shown below:
🌐
ScholarHat
scholarhat.com › home
Python While Loop - Flowchart, Syntax with Example
September 11, 2025 - Let's see the syntax of Python ... execute when condition becomes False · ctr = 0 while ctr < 5: print('This is inside the while loop loop') ctr = ctr + 1 else: print('This is ......
🌐
Python.org
discuss.python.org › python help
Need help understanding this while loop - Python Help - Discussions on Python.org
February 13, 2024 - I’m new to while loops, and I don’t quite get what’s going on in this code from my book: current_number = 1 while current_number <= 5: print(current_number) current_number += 1 After just watching a video on Yo…
Find elsewhere
🌐
Study.com
study.com › courses › computer science courses › computer science 113: programming in python
While Loops in Python | Definition, Syntax & Examples - Lesson | Study.com
July 24, 2024 - The word 'while' in Python is a reserved word which creates a while loop using the syntax: while condition: do_stuff. If do_stuff is more than one line, it should be put on the next line and indented.
🌐
Server Academy
serveracademy.com › blog › python-while-loop-tutorial
Python While Loop Tutorial - Server Academy
In such cases, you can use the break statement to terminate the loop immediately. number = 1 while number <= 10: print(number) if number == 5: break number += 1 ... Here, the loop is set to iterate until number is greater than 10, but when number ...
🌐
Berkeley
pythonnumericalmethods.studentorg.berkeley.edu › notebooks › chapter05.02-While-Loops.html
While Loops — Python Numerical Methods
A while loop or indefinite loop is a set of instructions that is repeated as long as the associated logical expression is true. The following is the abstract syntax of a while loop block. ... When Python reaches a while loop block, it first determines if the logical expression of the while ...
🌐
Tutorialspoint
tutorialspoint.com › python › python_while_loops.htm
Python - While Loops
A while loop in Python programming language repeatedly executes a target statement as long as the specified boolean expression is true. This loop starts with while keyword followed by a boolean expression and colon symbol (:).
🌐
Programiz
programiz.com › python-programming › while-loop
Python while Loop (With Examples)
while True: user_input = input("Enter password: ") # terminate the loop when user enters exit if user_input == 'exit': print(f'Status: Entry Rejected') break print(f'Status: Entry Allowed') ... Before we wrap up, let’s put your knowledge of Python while loop to the test!
🌐
DataCamp
datacamp.com › tutorial › python-while-loop
Python While Loops Tutorial | DataCamp
June 25, 2020 - In the below example, you initially start with an error equal to 50.0. Next, you will write a while loop, in the condition part, we write error > 1, so that the while loop executes again as long as the error is above 1. Inside the while loop, you divide the error by four and update the error variable. When we run it the first time, we receive 12.5. Python will then go back to the condition of the while loop with the new error equal to 12.5.
🌐
Mimo
mimo.org › glossary › python › while-loop
Master Python While Loops: A Comprehensive Guide
The Python while loop is a control flow statement that runs a block of code for as long as a specified condition is true. The while loop will execute the code in the body of the loop until the specified condition becomes false. The syntax of a while loop in the Python programming language is ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › loops-in-python
Loops in Python - GeeksforGeeks
In Python, a while loop is used to execute a block of statements repeatedly until a given condition is satisfied.
Published   1 week ago
🌐
Coursera
coursera.org › tutorials › python-while-loop
How to Write and Use Python While Loops | Coursera
If it is, the continue statement is executed, causing the loop to skip the current iteration and move on to the next one without executing the rest of the loop body. If i is odd, the print statement is executed, outputting the value of i. The pass statement in Python intentionally does nothing. It can be used as a placeholder for future code or when a statement is required by syntax but you don’t want anything to happen. In a while loop, you can use it to ignore a certain condition during an iteration.
🌐
Medium
medium.com › @datasciencejourney100_83560 › while-loops-in-python-2c57f42c7750
While Loop in Python. Loops are required to repeat a block of… | by Rina Mondal | Medium
September 3, 2025 - Once a non-empty name is entered, the loop will break, and the program will print the entered name. name = input("Enter your name: ") while name == '': print("Please provide a name") name = input("Enter your name: ") print("Your name is:", name) name= input("Enter your name") while not name =='Quit': print("Please provide a name") name=input("Enter your name") print(name)
🌐
LogRocket
blog.logrocket.com › home › for and while loops in python
for and while loops in Python - LogRocket Blog
June 4, 2024 - Python allows us to append else statements to our loops as well. The code within the else block executes when the loop terminates. Here is the syntax: # for 'for' loops for i in <collection>: <loop body> else: <code block> # will run when loop halts. # for 'while' loops while <condition>: <loop body> else: <code block> # will run when loop halts
🌐
ScholarHat
scholarhat.com › home
Loops in Python - For, While loop (With Examples)
September 10, 2025 - A nested loop in Python means using one loop inside another loop. It is commonly used when you need to repeat actions in a multi-level structure, like printing patterns or working with multi-dimensional lists (like 2D arrays). for iterating_var in sequence: for iterating_var in sequence: statements(s) statements(s) while expression: while expression: statement(s) statement(s)
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-do-while
Python Do While Loops - GeeksforGeeks
July 23, 2025 - In Python, there is no construct defined for do while loop.