In case that is what is causing the confusion, the condition is not checked all the time, but only before the first statement in the loop. Therefore, if the value is incremented before the print statement, the condition will only be checked after the value is printed. If you think through the code, … Answer from CAM-Gerlach on discuss.python.org
🌐
W3Schools
w3schools.com › python › python_while_loops.asp
Python While Loops
Python Examples Python Compiler ... Interview Q&A Python Training ... With the while loop we can execute a set of statements as long as a condition is true....
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-while-loop
Python While Loop - GeeksforGeeks
While Loop is used to execute a block of statements repeatedly until a given condition is satisfied. When the condition becomes false, the line immediately after the loop in the program is executed.
Published: June 3, 2026
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
19
0
February 13, 2024
As a beginner how do I understand while loops?
Seems like a pretty straight forward concept to me, what exactly are you strugling with? Any concrete example? More on reddit.com
🌐 r/learnpython
76
36
April 10, 2025
Reddit
Subreddit for posting questions and asking for general advice about all topics related to learning python. More on reddit.com
🌐 r/learnpython
October 29, 2018
[Python] Why would I use a for loop instead of a while loop?
for means "do something with each of these things". while means "keep doing this until something is no longer true". So... say what you mean. Readability counts. More on reddit.com
🌐 r/learnprogramming
58
49
December 20, 2012
People also ask

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 › 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 › computer science courses › computer science 113: programming in python
While Loops in Python | Definition, Syntax & Examples - Lesson ...
🌐
Real Python
realpython.com › python-while-loop
Python while Loops: Repeating Tasks Conditionally – Real Python
July 10, 2026 - Browse Topics Guided Learning Paths ... web-dev web-scraping ... Python’s while loop enables you to execute a block of code repeatedly as long as a given condition remains true....
🌐
GeeksforGeeks
geeksforgeeks.org › python › loops-in-python
Loops in Python - GeeksforGeeks
Loops are used to execute a block of code repeatedly until a condition is met or all items in a sequence are processed. The main types are For loops (iterating over sequences) and While loops (executing code based on a condition).
Published: 3 weeks ago
🌐
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
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-while.html
While Loop
Here is a while loop to print the numbers 0, 1, 2, ... 9 (there are easier ways to do this, but here we're just trying to show the parts of the loop). i = 0 while i < 10: print(i) i = i + 1 print('All done') 0 1 2 3 4 5 6 7 8 9 All done · Very often the last line of the while body has an "increment" role, such as the i = i + 1 line above.
Find elsewhere
🌐
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 - #Syntax of while loop while condition: # Code block to be executed repeatedly · In the below code the loop will prompt the user to enter their name repeatedly until they provide a non-empty input.
🌐
Study.com
study.com › computer science courses › computer science 113: programming in python
While Loops in Python | Definition, Syntax & Examples - Lesson | Study.com
July 24, 2024 - 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 ...
🌐
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
🌐
Programiz
programiz.com › python-programming › while-loop
Python while Loop (With Code Visualization)
In Python, a while loop is used to run a block of code until the specified condition is True.
🌐
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.
🌐
Tulane University Libraries
libguides.tulane.edu › condanditer › while
WHILE Statements - Intermediate Concepts in Python: Conditions and Iteration - Library Guides at Tulane University
August 14, 2026 - This is shown in the "Final output" statement. x = 0 Jupyter Notebook View while x < 6: x+=2 print("Looping output:", x)
🌐
freeCodeCamp
freecodecamp.org › news › while-loops-in-python-while-true-loop-statement-example
While Loops in Python – While True Loop Statement Example
July 19, 2022 - This level of indentation is how Python knows that the code statements you will write are associated with the while statement. Then, the code you want to run goes in the body of the while statement. While the condition evaluates to True, the code inside the body of the while loop will execute.
🌐
Python
wiki.python.org › moin › WhileLoop
While loops - Python Wiki
If a change to this archive is absolutely needed, requests can be made via the infrastructure@python.org mailing list. ... While loops, like the ForLoop, 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 no ...
🌐
Tutorialspoint
tutorialspoint.com › python › python_while_loops.htm
Python - While Loops
Python TechnologiesDatabasesComputer ... ... A while loop in Python programming language repeatedly executes a target statement as long as the specified boolean expression is true....
🌐
Alma Better
almabetter.com › bytes › tutorials › python › while-loop-in-python
While Loop in Python
The Python While Loop 🔁 repeatedly executes a block of statements until a specified condition :thinking: becomes false. Once the condition becomes false, the first line after the Loop executes. A while loop is a type of indefinite iteration . The while loop continues looping as long as the ...
🌐
Coursera
coursera.org › tutorials › how to write and use python while loops
How to Write and Use Python While Loops | Coursera
February 24, 2023 - If the test expression evaluates to true, the body of the while loop will be entered. This process repeats until the test expression evaluates to false. ... 1 2 3 4 5 numbers = [2, 4, 6, 8, 10] total = 0 for num in numbers: total += num print("The total is:", total) What will the interpreter display at the end of the fourth loop? After the fourth loop, the interpreter will print The total is:8. Remember that indentation is crucial in Python.
🌐
Berkeley
pythonnumericalmethods.studentorg.berkeley.edu › notebooks › chapter05.02-While-Loops.html
While Loops — Python Numerical Methods
The code is released under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon! ... A while loop or indefinite loop is a set of instructions that is repeated as long as the associated logical expression is true.
🌐
OpenStax
openstax.org › books › introduction-python-programming › pages › 5-1-while-loop
5.1 While loop - Introduction to Python Programming | OpenStax
March 13, 2024 - A while loop is a code construct that runs a set of statements, known as the loop body, while a given condition, known as the loop expression, is true. At each iteration, once the loop statement is executed, the loop expression is evaluated again.