but on the other hand it creates a completely useless list of integers just to loop over them. Isn't it a waste of memory, especially as far as big numbers of iterations are concerned?

That is what xrange(n) is for. It avoids creating a list of numbers, and instead just provides an iterator object.

In Python 3, xrange() was renamed to range() - if you want a list, you have to specifically request it via list(range(n)).

Answer from Amber on Stack Overflow
🌐
W3Schools
w3schools.com › python › python_while_loops.asp
Python While Loops
With the break statement we can stop the loop even if the while condition is true: ... Note: The else block will NOT be executed if the loop is stopped by a break statement. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
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
Discussions

python - for or while loop to do something n times - Stack Overflow
In Python you have two fine ways to repeat some action more than once. One of them is while loop and the other - for loop. So let's have a look on two simple pieces of code: for i in range(n): More on stackoverflow.com
🌐 stackoverflow.com
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
February 13, 2024
Absolute beginner; please help. How does while loop function?
I think a lot of the proble is this syntax…but your format makes it hard to be 100% here. One blank line above code block, then four leading spaces on every line. if checker == 2 == True: If checker is 2, I believe it runs like this. if checker == (2 == True): if 2 == 2 == True: #2 != True if 2 == False: #which is always gonna be Flase So it never gets to the break, hard to tell formatting is bad. You simple remove the erroneous ‘==True’ from those comparisons. ‘If’ always asks if the thing is Truthy, we use ‘not’ to make Flasey things Truthy. More on reddit.com
🌐 r/learnpython
18
3
October 3, 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
🌐
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:
🌐
Simplilearn
simplilearn.com › home › resources › software development › your ultimate python tutorial for beginners › introduction to python while loop
Introduction to Python While Loop
May 27, 2024 - Python is an object-oriented programming language consisting of three types of loops. Learn about the Python While loop, break and continue statements, & more.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
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:
🌐
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
Find elsewhere
🌐
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
Python computes n >= 1 or 0.5 >= 1, which is false so the while-loop ends with i = 4. You may have asked, “What if the logical expression is true and never changes?” and this is indeed a very good question. If the logical expression is true, and nothing in the while-loop code changes the expression, then the result is known as an infinite loop.
🌐
DataCamp
datacamp.com › tutorial › do-while-loop-python
Emulating a Do-While Loop in Python: A Comprehensive Guide for Beginners | DataCamp
January 31, 2024 - In some programming languages, a "do-while" loop ensures that the code within the loop executes at least once before checking the condition. Python does not have a built-in "do-while" loop, but you can emulate its behavior.
🌐
Python documentation
docs.python.org › 3 › tutorial › controlflow.html
4. More Control Flow Tools — Python 3.14.3 documentation
In a for loop, the else clause is executed after the loop finishes its final iteration, that is, if no break occurred. In a while loop, it’s executed after the loop’s condition becomes false.
🌐
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…
🌐
IONOS
ionos.com › digital guide › websites › web development › python while loop
How to use while loops in Python - IONOS
September 26, 2022 - A while loop is clearly not the best solution for this problem: word = 'Python' letters = iter(word) letter = '' while letter is not None: letter = next(letters, None) if letter: print(letter)
🌐
Quora
quora.com › How-do-I-use-for-and-while-loops-in-Python
How to use for and while loops in Python - Quora
Answer (1 of 10): > [[ Warning: this answer is for beginners and learners. . . . Proficient programmers need not read. ]] —— It’s day ten on planet Uiton. Jan and two colleagues set out in their spaceships, early in the morning, but by noon, something had hit Jan’s ship, causing it to crash-...
🌐
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 ...
🌐
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)
🌐
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!
🌐
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 (:).
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-construct-while-loops-in-python-3
How To Construct While Loops in Python 3 | DigitalOcean
August 20, 2021 - A while loop implements the repeated execution of code based on a given Boolean condition. The code that is in a while block will execute as long as the whil…
🌐
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.