๐ŸŒ
Real Python
realpython.com โ€บ python-while-loop
Python while Loops: Repeating Tasks Conditionally โ€“ Real Python
March 3, 2025 - How does a while loop differ from a for loop?Show/Hide ยท You use a while loop when the number of iterations is uncertain, and a for loop when iterating a known number of times over a sequence.
๐ŸŒ
Python.org
discuss.python.org โ€บ ideas
"for i in range()" to do an infinite loop with a counter - Ideas - Discussions on Python.org
August 10, 2022 - Hi, Usually in Python we can avoid ... that we use in other languages when we need to count things, thanks to enumerate(...), for i in range(100), etc. Along the years I have nearly always found a more โ€œpythonicโ€ replacement for code containing i = 0 โ€ฆ i += 1. There is an exception with this code: an infinite loop with a counter: i = 0 while True: ... ...
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Python while Loop (Infinite Loop, break, continue) | note.nkmk.me
August 18, 2023 - Infinite iterators in Python (itertools.count, cycle, repeat) ... You can specify multiple conditions for the condition part with and or or. ... Use break to break a while loop.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ loops-in-python
Loops in Python - GeeksforGeeks
Code given below uses a 'while' loop with the condition "True", which means that the loop will run infinitely until we break out of it using "break" keyword or some other logic. ... Note: It is suggested not to use this type of loop as it is ...
Published ย  June 7, 2017
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ while-loops
Python's "while" loop - Python Morsels
June 20, 2025 - This loop would never end because we forgot to increment our count variable in the body of our loop! A loop that goes on forever is called an infinite loop. Be careful not to accidentally make infinite loops in Python.
๐ŸŒ
Runestone Academy
runestone.academy โ€บ ns โ€บ books โ€บ published โ€บ py4e-int โ€บ iterations โ€บ infinite_loops.html
6.3. Infinite loops โ€” Python for Everybody - Interactive
For example, suppose you want to take input from the user until they type done. You could write: while True: line = input('Word: ') if line == 'done': break print(line) print ('Done!') The loop condition is True, which is always true, so the loop runs repeatedly until it hits the break statement.
๐ŸŒ
The Python Coding Stack
thepythoncodingstack.com โ€บ p โ€บ infinite-for-loop-infinite-iterator-python
To Infinity and Beyond โ€ข The Infinite `for` Loop
March 7, 2024 - Yes, the `while` loop is what you'd normally use for infinite loops. But you can also use `for` loops, with help from infinite iterators
๐ŸŒ
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. ... Note: remember to increment i, or else the loop will continue forever.
๐ŸŒ
Berkeley
pythonnumericalmethods.studentorg.berkeley.edu โ€บ notebooks โ€บ chapter05.02-While-Loops.html
While Loops โ€” Python Numerical Methods
Answer: The first example will not infinite loop because eventually n will be so small that Python cannot tell the difference between n and 0. More on this in Chapter 9. The second example will infinite loop because n will oscillate between 2 and 3 indefinitely. Now we know two types of loops: for-loops and while-loops. In some cases, either can be used equally well, but sometimes one is better suited for the task than the other.
Find elsewhere
๐ŸŒ
StudySmarter
studysmarter.co.uk โ€บ computer science โ€บ computer programming โ€บ python infinite loop
Python Infinite Loop: Creation, Example & Error | StudySmarter
In Python, an infinite loop can be created using 'while' or 'for' loop structures, with an appropriate condition or iterator that never reaches its stopping point.A Python infinite loop has both advantages and disadvantages, which should be carefully considered before implementing one.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ looping-technique
Python Looping Techniques
# Program to illustrate a loop ... counter # print the sum print("The sum is",sum) ... This kind of loop can be implemented using an infinite loop along with a conditional break in between the body of the loop....
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ [thought experiment] achieving one-line infinite loops in python
r/learnpython on Reddit: [Thought experiment] Achieving one-line infinite loops in Python
September 5, 2018 -

DISCLAIMER: This post is mainly just curious thoughts, it has nothing to do with real-life application or good practice. So please don't actually use any examples provided.

Python is (or at least was) rather famous for its possibilities for one-liners (programs occupying only a single line of code) some time ago. A lot of things can be achieved like this, but among the most puzzling things must be infinite loops; they aren't exactly easy to implement with the tools we have available.

An infinite loop usually requires the use of a while-loop, because for-loops have a beginning and an end. Using a while-loop in one-liners is problematic, though, because you may only use it once, on the top level. This is due to how Python restricts block structures to either be separated by whitespace (and proper indentation), or to only have a single depth level following it. In other words,

while True: print("This works!")

is valid Python, but

while True: if 1 == 1: print("But this doesn't...)

is not.

We do have another "kind" of loop, though; list comprehensions. They are unique in that they may be nested as we see fit, all while using only a single line.

[["Order pizza." for _ in range(6)] for _ in range(42)]

But this doesn't give us an infinite loop; even if we simply input a ridiculously large number to range, it's still technically finite no matter what kind of hardware we're using. Thus, a different approach is required. I mentioned how infinite loops usually require the use of while-loops in Python. We can, however, utilise a certain property of Python to create an infinite loop with for-loops.

nums = [1, 2, 3, 4]
for num in nums:
    print(num)

Okay, that prints out four numbers. Not exactly infinite. But if we tweak our approach a little...

nums = [1]
for num in nums:
    print(num)
    nums.append(num + 1)

We actually get... as many numbers as the computer's memory allows. With this, we can essentially get something like this to work:

nums=[1];[(print(num) and nums.append(num+1)) for num in nums]

(Disclaimer; I never tested if that actually runs.)

It's not a pure one-liner, because it still technically requires two lines (fused together with a semicolon), but it's a proof-of-concept. I initially tried to make it work without having to define a variable, but failed to find a way.

I hope this was mildly interesting, I don't usually write stuff like this. Just found it curious myself, so why not share the thought? Maybe someone can even improve on this.

๐ŸŒ
Python Land
python.land โ€บ home โ€บ introduction to python โ€บ python for loop and while loop
Python For Loop and While Loop โ€ข Python Land Tutorial
September 5, 2025 - We see an expression that follows the while statement: i <= 4. As long as this expression evaluates to True, the block inside of the while-loop executes repeatedly. In the example above, we start with i = 1. In the first iteration of the loop, ...
๐ŸŒ
Unstop
unstop.com โ€บ home โ€บ blog โ€บ python infinite loop | types, applications & more (+examples)
Python Infinite Loop | Types, Applications & More (+Examples)
October 25, 2024 - Initiate the Loop: Use the while True: statement to start it. This means the loop will run indefinitely until a break condition is met. while True: # Code to execute repeatedly print("This loop will run forever")
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ what is infinite loop in python?
What is Infinite Loop in Python? | Scaler Topics
May 8, 2024 - A statement is not evaluated for some results. Example: x = 3. We can use the various states with an infinite loop in Python. Let us understand the various statement used with the infinite loops in Python with example(s). We can use the while loop in Python to create an infinite loop in Python as we have seen in the above examples.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_while_loops.htm
Python while Loop Statements
If it fails to turn false, the loop continues to run, and doesn't stop unless forcefully stopped. Such a loop is called infinite loop, which is undesired in a computer program. The syntax of a while loop in Python programming language is โˆ’ ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-while-loop
Python While Loop - GeeksforGeeks
Therefore, the body of the loop is run infinite times until the memory is full. ... Loop control statements change execution from their normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are ...
Published ย  December 23, 2025
๐ŸŒ
Intellipaat
intellipaat.com โ€บ home โ€บ blog โ€บ python while loop
Python While Loop: Explained with While Loop Flowchart
October 14, 2025 - How can I avoid infinite loops? Yes, a while loop can run infinitely when the condition mentioned in the loop structure will always be true. In order to avoid these infinite loops, you can use break statements.