You can use the second argument of iter(), to call a function repeatedly until its return value matches that argument. This would loop forever as 1 will never be equal to 0 (which is the return value of int()):

for _ in iter(int, 1):
    pass

If you wanted an infinite loop using numbers that are incrementing you could use itertools.count:

from itertools import count

for i in count(0):
    ....
Answer from Padraic Cunningham on Stack Overflow
🌐
Scaler
scaler.com › home › topics › what is infinite loop in python?
What is Infinite Loop in Python? | Scaler Topics
May 8, 2024 - A fake infinite loop is a kind ... the loop. For example, we are given a program in which we need to print all the even multiples of 3 that is less than and equal to a given number n....
Discussions

"for i in range()" to do an infinite loop with a counter - Ideas - Discussions on Python.org
Hi, Usually in Python we can avoid the i = 0 … i += 1 paradigm 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. ... More on discuss.python.org
🌐 discuss.python.org
4
August 10, 2022
[Thought experiment] Achieving one-line infinite loops in Python
One-liner infinite print loop, that doesn't use while, append, etc.: for i in __import__('itertools').count(): print(i) More on reddit.com
🌐 r/learnpython
15
5
September 5, 2018
how to detect infinite loops
"Detecting an infinite loop" is equivalent to predicting whether the program will run forever, or if it will eventually terminate on its own. A well-known theorem from theoretical computer science says that doing this reliably is impossible. If you just want an approximate check, there are any number of ways you could do it. For example, you could set a timeout and periodically check whether it's been exceeded: start_time = time.time() deadline = start_time + time_limit while True: if time.time() > deadline: sys.exit(1) # ... More on reddit.com
🌐 r/learnprogramming
17
9
November 10, 2022
Multiprocessing in a while (True) loop
This comment is incorrect. They will not end at the same time. Each will end when it ends and the loop will repeat once the slowest ends. # Joining each process so they end at the same time f1.join() f2.join() f3.join() The more accurate comment would be: # Wait until all processes finish before continuing. More on reddit.com
🌐 r/learnpython
10
3
May 14, 2017
🌐
Runestone Academy
runestone.academy › ns › books › published › py4e-int › iterations › infinite_loops.html
6.3. Infinite loops — Python for Everybody - Interactive
In that case you can write an infinite loop on purpose and then use the break statement to jump out of the loop. This loop is obviously an infinite loop because the logical expression on the while statement is simply the logical constant True: ... As you can see above, the Code Lens gives you a warning because it runs for over 1000 steps. If you make the mistake of running this code, you will learn quickly how to stop a runaway Python process on your system or find where the power-off button is on your computer.
🌐
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 the i = 0 … i += 1 paradigm 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: ... if breaking_condition: break i += 1 Proposal: could we accept that range() without any parameter ...
🌐
Unstop
unstop.com › home › blog › python infinite loop | types, applications & more (+examples)
Python Infinite Loop | Types, Applications & More (+Examples)
October 25, 2024 - Define the Loop Structure: An infinite while loop is a common way to create an endless loop in Python. 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 ...
🌐
Programiz
programiz.com › python-programming › looping-technique
Python Looping Techniques
It can be implemented using an infinite loop along with a conditional break at the end. This is similar to the do...while loop in C. # Python program to illustrate a loop with the condition at the bottom # Roll a dice until the user chooses to exit # import random module import random while True: input("Press enter to roll the dice") # get a number between 1 to 6 num = random.randint(1,6) print("You got",num) option = input("Roll again?(y/n) ") # condition if option == 'n': break
🌐
Scientech Easy
scientecheasy.com › home › blog › infinite loop in python
Infinite Loop in Python - Scientech Easy
January 25, 2026 - It will stop the loop, come out of loop and execute the next statement that is following the infinite loop. Let’s take an example program based on infinite while loop where we will print the sum of squares of numbers from 1 to 20.
Find elsewhere
🌐
Study.com
study.com › computer science courses › computer science 113: programming in python
Infinite Loops in Python: Definition & Examples - Lesson | Study.com
July 18, 2024 - The code here produces the same result as the previous finite loop example using for: ... An infinite loop, on the other hand, is characterized by not having an explicit end, unlike the finite ones exemplified previously, where the control variable i clearly went from 0 to 9 (note that at the end i = 10, but that value of i wasn't printed). In an infinite loop the control is not explicitly clear, as in the example appearing here:
🌐
GeeksforGeeks
geeksforgeeks.org › python › loops-in-python
Loops in Python - GeeksforGeeks
In Python, a 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. In below code, loop runs as long as the condition cnt < 3 is true. It increments the counter by 1 on each iteration and prints "Hello Geek" three times. ... If we want a block of code to execute infinite number of times then we can use the while loop in Python to do so.
Published   February 14, 2026
🌐
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.
🌐
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.

🌐
Gitbooks
buzzcoder.gitbooks.io › codecraft-python › content › while-loop › infinite-loop-and-break.html
Infinite loops and break · CodeCraft-Python - BuzzCoder
Start an infinite loop. Get user input. If input is 0, stop the loop. If input is not 0, do math and continue the loop. ... In this loop, the condition itself is True, so the computer will always continue running the loop. Now we need a way to exit the loop. This can be done with break keyword. break will cause the current loop to end, and the computer will jump to the code directly following the loop. Here is a good example of an infinite loop that works:
🌐
The Python Coding Stack
thepythoncodingstack.com › p › infinite-for-loop-infinite-iterator-python
To Infinity and Beyond • The Infinite `for` Loop
March 7, 2024 - If you need to refresh your memory about what iterators are and the difference between iterators and iterables—they're not the same—you can read A One-Way Stream of Data • Iterators in Python. You can import itertools and update all_positions to assign the infinite cycle iterator to it instead of a finite list: ... Run this code and you'll get an infinite animation. The for loop iterates through all_positions, but all_positions is an infinite iterator.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › python infinite loop
Python Infinite Loop | Top 4 Types of Statements in Python Infinite Loop
May 10, 2024 - Loops are compelling and essential, but an infinite loop is the only pitfall. Python has two types of loops, only ‘While loop’ and ‘For loop.’ While loop works precisely as the IF statement but in the IF statement, we run the block of code just once, whereas, in a while loop, we jump back to the same point from where the code began.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
StudySmarter
studysmarter.co.uk › computer science › computer programming › python infinite loop
Python Infinite Loop: Creation, Example & Error | StudySmarter
August 10, 2023 - Here's an example illustrating an infinite 'for' loop using the 'range()' function: from itertools import count for i in count(): if i > 5: break print("Value of i: ", i)In this example, the loop keeps running and printing the value of 'i' until it reaches a value greater than 5. At that point, ...
🌐
Runestone Academy
runestone.academy › ns › books › published › fopp › MoreAboutIteration › WPInfiniteLoops.html
14.6. 👩‍💻 Infinite Loops — Foundations of Python Programming
Another case where an infinite loop is likely to occur is when you have reassigned the value of the variable used in the while statement in a way that prevents the loop from completing. This is an example below.
🌐
freeCodeCamp
freecodecamp.org › news › python-while-loop-tutorial
Python While Loop Tutorial – While True Syntax Examples and Infinite Loops
November 13, 2020 - You can generate an infinite loop intentionally with while True. The break statement can be used to stop a while loop immediately. I really hope you liked my article and found it helpful. Now you know how to work with While Loops in Python.
🌐
Intellipaat
intellipaat.com › home › blog › python while loop
Python While Loop: Explained with While Loop Flowchart
October 14, 2025 - Python returns to the top to re-check the condition. It continues looping and printing until the condition becomes false when the counter reaches 10. Once counter >= 10, the program exits the while loop and continues with any code after it. Get 100% Hike! Master Most in Demand Skills Now! An infinite while loop refers to a while loop where the while condition never becomes false.