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 ... Python Bootcamp 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
DSA Python · Data Science · NumPy · Pandas · Practice · Django · Flask · Last Updated : 3 Jun, 2026 · While Loop is used to execute a block of statements repeatedly until a given condition is satisfied.
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?
It’s just a loop which continues running until the condition becomes false. More on reddit.com
🌐 r/learnpython
76
36
April 10, 2025
While Loops, how do they work?
I’m not new to python as a whole but I’m very bad at it however, I want to learn. I’ve tried to do some coding but I want to know what everything is actually doing rather than just reciting things online so if some of you could explain it to me as if I was like a baby that would make ... More on discuss.python.org
🌐 discuss.python.org
8
0
June 4, 2026
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
🌐
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
March 3, 2025 - In this tutorial, you'll learn about indefinite iteration using the Python while loop. You'll be able to construct basic and complex while loops, interrupt loop execution with break and continue, use the else clause with a while loop, and deal with infinite loops.
🌐
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
Find elsewhere
🌐
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
🌐
Programiz
programiz.com › python-programming › while-loop
Python while Loop (With Examples)
In Python, we use a while loop to repeat a block of code until a certain condition is met.
🌐
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.
🌐
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.
🌐
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 › python-while-loop
How to Write and Use Python While Loops | Coursera
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.
🌐
Python.org
discuss.python.org › python help
While Loops, how do they work? - Python Help - Discussions on Python.org
June 4, 2026 - I’m not new to python as a whole but I’m very bad at it however, I want to learn. I’ve tried to do some coding but I want to know what everything is actually doing rather than just reciting things online so if some of you could explain it to me as if I was like a baby that would make ...
🌐
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 - If the condition evaluates to True, then the loop will run the code within the loop's body and continue to run the code while the condition remains True. It will keep executing the desired set of code statements until that condition is no longer True. Let’s take a hypothetical example. You may ask a user to submit a secret keyword so they can access a specific part of your site. Say that for them to be able to view some content, they first have to enter the keyword ‘Python’.
🌐
GeeksforGeeks
geeksforgeeks.org › python › loops-in-python
Loops in Python - GeeksforGeeks
DSA Python · Data Science · NumPy · Pandas · Practice · Django · Flask · Last Updated : 11 Jun, 2026 · 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   June 11, 2026
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()