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
🌐
Real Python
realpython.com › python-while-loop
Python while Loops: Repeating Tasks Conditionally – Real Python
March 3, 2025 - You prevent an infinite loop by ensuring that the loop’s condition will eventually become false through proper logic in the loop condition and body. What is the purpose of the break statement in a while loop?Show/Hide · You use the break statement to immediately exit a while loop, regardless of the loop’s condition. Can you use an else clause with a while loop in Python?Show/Hide
Discussions

[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
infinite loops
Hello u/limafadde , I'm a bot that can assist you with code-formatting for reddit. I have detected the following potential issue(s) with your submission: Python code found in submission text but not encapsulated in a code block. If I am correct then please follow these instructions to fix your code formatting. Thanks! More on reddit.com
🌐 r/learnpython
3
2
May 5, 2020
[Python] Why am I getting into an infinite loop?
while rc != "A" or "B" or "C": is the same as: while (rc != "A") or ("B") or ("C"): which is only comparing rc against "A", so this is definitely not what you want. This version: while rc != "A" or rc != "B" or rc != "C": is much closer, but read it closely. If rc was "A" then the phrase rc != "B" will be true. If rc was "B" then the phrase rc != "A" will be true, so at least one of those conditions will always be true, and because your're looping while the first phrase is true or the second phrase is true or the third phrase is true, the loop never ends. You need to use and instead of or. More on reddit.com
🌐 r/learnprogramming
56
372
July 17, 2020
Where can I learn how to efficiently replace an infinite while loop?

The idea of a while loop being ineffective is news to me. While (true) is very common in programming. In fact its so common I've often seen it called a "game loop" or "main loop": http://gameprogrammingpatterns.com/game-loop.html

You can exit the loop with the break or return keywords, there is no need to raise an exception. The behavior of a keyboard interrupt while sleeping seems to be platform dependant, there is some information availaible on stackoverflow: http://stackoverflow.com/questions/5114292/break-interrupt-a-time-sleep-in-python

More on reddit.com
🌐 r/learnpython
12
2
December 16, 2014
People also ask

What causes an infinite loop in a Python program?
An infinite loop in a Python program is typically caused by a loop's termination condition never being met. This may happen if the condition is always true, due to improper initialization, update of loop control variables, or logical errors in the loop's condition.
🌐
vaia.com
vaia.com › python infinite loop
Python Infinite Loop: Definition & Examples | Vaia
How can I detect an infinite loop in my Python program?
Detect an infinite loop by checking for high CPU usage or unresponsive behavior in your program. Use debugging tools like print statements to track loop iterations or integrate timeouts and break conditions to terminate loops exceeding expected execution time. Additionally, test for corner cases that could cause infinite repetition.
🌐
vaia.com
vaia.com › python infinite loop
Python Infinite Loop: Definition & Examples | Vaia
What are the consequences of an infinite loop in Python programs?
An infinite loop can cause a program to become unresponsive, leading to high CPU usage and resource exhaustion. It may prevent other processes from running efficiently and can halt system performance. Debugging can be challenging, potentially resulting in prolonged downtime if not managed promptly.
🌐
vaia.com
vaia.com › python infinite loop
Python Infinite Loop: Definition & Examples | Vaia
🌐
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” ...
🌐
Unstop
unstop.com › home › blog › python infinite loop | types, applications & more (+examples)
Python Infinite Loop | Types, Applications & More (+Examples)
October 25, 2024 - An infinite loop in Python programming language is a sequence that continues indefinitely until it is externally stopped. This type of loop does not have a terminating condition. It often uses conditions that are always true, which causes the ...
🌐
Scaler
scaler.com › home › topics › what is infinite loop in python?
What is Infinite Loop in Python? | Scaler Topics
May 8, 2024 - Now, coming back to the topic, an infinite loop in Python is a continuous repetition of the conditional loop until some external factors like insufficient CPU memory, error in the code, etc.
🌐
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 - An Infinite Loop in Python is a continuous repetitive conditional loop that gets executed until an external factor interferes in the execution flow, like insufficient CPU memory, a failed feature/ error code that stopped the execution, or a ...
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
Find elsewhere
🌐
Runestone Academy
runestone.academy › ns › books › published › py4e-int › iterations › infinite_loops.html
6.3. Infinite loops — Python for Everybody - Interactive
In some loops, like the countdown from last section, we can prove that the loop terminates because we know that the value of n is finite, and we can see that the value of n gets smaller each time through the loop, so eventually we’ll reach 0. Other times a loop is obviously infinite because it has no iteration variable at all.
🌐
The Python Coding Stack
thepythoncodingstack.com › the python coding stack › to infinity and beyond • the infinite `for` loop
To Infinity and Beyond • The Infinite `for` Loop
June 25, 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.
🌐
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 ...
🌐
Vaia
vaia.com › python infinite loop
Python Infinite Loop: Definition & Examples | Vaia
An infinite loop in Python is a loop that continues to execute indefinitely due to the loop's condition always evaluating as true, such as "while True:" or a loop missing a terminating condition. This can be useful for programs or processes that need to run continuously until externally ...
🌐
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.

🌐
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.
🌐
Scientech Easy
scientecheasy.com › home › blog › infinite loop in python
Infinite Loop in Python - Scientech Easy
January 25, 2026 - An infinite loop (also called endless loop) in Python is a sequence of statements in a program which executes endlessly.
🌐
GeeksforGeeks
geeksforgeeks.org › python › loops-in-python
Loops in Python - GeeksforGeeks
When the condition becomes false, the line immediately after the loop in the program is executed. ... An infinite while loop runs continuously because its condition always remains true.
Published   June 11, 2026
🌐
Programiz
programiz.com › python-programming › looping-technique
Python Looping Techniques
Python programming offers two kinds of loop, the for loop and the while loop. Using these loops along with loop control statements like break and continue, we can create various forms of loop. We can create an infinite loop using while statement.
🌐
freeCodeCamp
freecodecamp.org › news › python-while-loop-tutorial
Python While Loop Tutorial – While True Syntax Examples and Infinite Loops
November 13, 2020 - So there is no guarantee that the ... always evaluates to True, then we will have an infinite loop, which is a while loop that runs indefinitely (in theory)....
🌐
Note.nkmk.me
note.nkmk.me › home › python
Python while Loop (Infinite Loop, break, continue) | note.nkmk.me
August 18, 2023 - If the condition in the while statement is always True, the loop will never end, and execution will repeat infinitely. In the following example, the Unix time is acquired using time.time(). The elapsed time is then measured and used to set the condition for when to break out of the loop. Measure elapsed time and time differences in Python
🌐
Gitbooks
buzzcoder.gitbooks.io › codecraft-python › content › while-loop › infinite-loop-and-break.html
Infinite loops and break · CodeCraft-Python - BuzzCoder
But there is a bug here! There is no i += 1 at the end of the loop body, so i will never increase. This means that i < 10 will always be true and the loop will never end. This is called an infinite loop, which can cause your program to freeze.
🌐
Medium
rasiksuhail.medium.com › unraveling-the-infinite-exploring-the-mysteries-and-perils-of-infinite-loops-327d7f46d858
Unraveling the Infinite: Exploring the Mysteries and Perils of Infinite Loops | by Rasiksuhail | Medium
May 26, 2023 - An infinite loop is a loop in a program that never terminates. This can happen for a variety of reasons, such as a logical error in the loop condition, or a failure to update the loop control variable in the loop body.