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
Note: remember to increment i, or else the loop will continue forever. The while loop requires relevant variables to be ready, in this example we need to define an indexing variable, i, which we set to 1.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-while-loop
Python While Loop - GeeksforGeeks
Python ยท count = 0 while count < 3: count = count + 1 print("Hello Geek") Output ยท Hello Geek Hello Geek Hello Geek ยท while expression: statement(s) Parameters: condition a boolean expression. If it evaluates to True, code inside the loop will execute. statement(s) that will be executed during each iteration of the loop.
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
loops - When to use "while" or "for" in Python - Stack Overflow
When I should use a while loop or a for loop in Python? It looks like people prefer using a for loop (for brevity?). Is there any specific situation which I should use one or the other? Is it a mat... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Reddit
Hello everyone just learning python and need to create a while loop that shows interest and years Iโ€™m struggling with operands please help. More on reddit.com
๐ŸŒ r/learnpython
October 29, 2018
๐ŸŒ
Stanford CS
cs.stanford.edu โ€บ people โ€บ nick โ€บ py โ€บ python-while.html
While Loop
The earlier for-loop is very handy to loop over a collection, but that collection needs to be known ahead of time. ... The while-loop uses a boolean test expression to control the run of the body lines. The for-loop is great of looping over a collection.
๐ŸŒ
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
๐ŸŒ
Real Python
realpython.com โ€บ python-while-loop
Python while Loops: Repeating Tasks Conditionally โ€“ Real Python
July 10, 2026 - The execution jumps back to the loop header, and the loop condition is evaluated to determine whether the loop will execute again. Pythonโ€™s while loops also have additional syntax. They have an else clause that runs when the loop terminates naturally because the condition becomes true.
Find elsewhere
๐ŸŒ
Berkeley
pythonnumericalmethods.studentorg.berkeley.edu โ€บ notebooks โ€บ chapter05.02-While-Loops.html
While Loops โ€” Python Numerical Methods
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 loop is true or false. If the expression is true, the code block will be executed, and after it is executed, the program returns to the logical expression at the beginning of the while statement.
๐ŸŒ
YouTube
youtube.com โ€บ watch
While Loop | Python | Tutorial 20 - YouTube
Source Code - http://www.giraffeacademy.com/programming-languages/python/ This video is one in a series of videos where we'll be looking at programming in py...
Published: October 22, 2017
๐ŸŒ
Python
wiki.python.org โ€บ moin โ€บ WhileLoop.html
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 ...
๐ŸŒ
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 - In this example, the WHILE loop has been updated to run until x reaches 12 while the IF statement breaks the loop at 6.
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ while-loop
Master Python While Loops: A Comprehensive Guide
Similar to the Python for loop, the Python while loop allows you to repeat code without having to type out the repetitions. But while loops are particularly great when the number of iterations is unknown at the time the loop starts, making them a valuable concept for beginners learning Python.
๐ŸŒ
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.
๐ŸŒ
Server Academy
serveracademy.com โ€บ blog โ€บ python-while-loop-tutorial
Python While Loop Tutorial Blog | Server Academy
July 23, 2026 - The loop is a fundamental tool in Python for executing a block of code repeatedly as long as a given condition remains true. This type of loop is useful when tโ€ฆ
๐ŸŒ
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.
Top answer
1 of 11
93

Yes, there is a huge difference between while and for.

The for statement iterates through a collection or iterable object or generator function.

The while statement simply loops until a condition is False.

It isn't preference. It's a question of what your data structures are.

Often, we represent the values we want to process as a range (an actual list), or xrange (which generates the values) (Edit: In Python 3, range is now a generator and behaves like the old xrange function. xrange has been removed from Python 3). This gives us a data structure tailor-made for the for statement.

Generally, however, we have a ready-made collection: a set, tuple, list, map or even a string is already an iterable collection, so we simply use a for loop.

In a few cases, we might want some functional-programming processing done for us, in which case we can apply that transformation as part of iteration. The sorted and enumerate functions apply a transformation on an iterable that fits naturally with the for statement.

If you don't have a tidy data structure to iterate through, or you don't have a generator function that drives your processing, you must use while.

2 of 11
24

while is useful in scenarios where the break condition doesn't logically depend on any kind of sequence. For example, consider unpredictable interactions:

while user_is_sleeping():
    wait()

Of course, you could write an appropriate iterator to encapsulate that action and make it accessible via for โ€“ but how would that serve readability?ยน

In all other cases in Python, use for (or an appropriate higher-order function which encapsulate the loop).

ยน assuming the user_is_sleeping function returns False when false, the example code could be rewritten as the following for loop:

for _ in iter(user_is_sleeping, False):
    wait()
๐ŸŒ
YouTube
youtube.com โ€บ alex the analyst
While Loops in Python | Python for Beginners - YouTube
Take my Full Python Course Here: https://bit.ly/48O581RIn this series we will be walking through everything you need to know to get started in Python! In thi...
Published: November 29, 2022
Views: 54K
๐ŸŒ
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 - While loops are utilized to iteratively execute a block of code until a specific condition is met, ensuring the repetition continues as long as the condition remains true. #Syntax of while loop while condition: # Code block to be executed repeatedly
๐ŸŒ
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.
๐ŸŒ
The Knowledge Academy
theknowledgeacademy.com โ€บ blog โ€บ python-while-loop
Python While Loop: Everything You Should Know
February 25, 2026 - A Python While Loop repeatedly executes a block of code as long as a specified condition is true, and stops only when the condition becomes false.