You can use this:

while True:
    print("Whatever you want to print")

This is not suggested though, because it will leave your program running forever and that is not good.

Answer from MLavrentyev on Stack Overflow
🌐
Better Programming
betterprogramming.pub › over-engineering-the-infinite-loop-in-python-53450cb52132
Over-Engineering the Infinite Loop in Python
September 20, 2023 - The most Pythonic approach to an infinite loop is to use a while loop with a constantly-true literal value simply: True.
🌐
Gitbooks
buzzcoder.gitbooks.io › codecraft-python › content › while-loop › infinite-loop-and-break.html
Infinite loops and break · CodeCraft-Python
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.
Discussions

How do you make an infinite printing loop in python? - Stack Overflow
I'm new to python, and don't know much about loops. How would I make an infinite printing loop? More on stackoverflow.com
🌐 stackoverflow.com
Infinite While Loop
Hey again everybody! Long time no see! Working on text input and outputs right now. My current code is supposed to count each letter that appears in a text document (“lyrics.txt”) and output the number of times each letter appears. The gets stuck in an infinite while loop though, and I’m ... More on discuss.python.org
🌐 discuss.python.org
0
November 24, 2021
Are infinite for loops possible in Python? - Stack Overflow
Is it possible to get an infinite loop in for loop? My guess is that there can be an infinite for loop in Python. I'd like to know this for future references. More on stackoverflow.com
🌐 stackoverflow.com
"for i in range()" to do an infinite loop with a counter - Ideas - Discussions on Python.org
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 ... More on discuss.python.org
🌐 discuss.python.org
4
August 10, 2022
🌐
Medium
medium.com › analytics-vidhya › the-first-time-i-got-stuck-in-an-infinite-python-loop-bdbf33c7ffb
The first time I got stuck in an infinite Python loop… | by Priscila Brey | Analytics Vidhya | Medium
June 6, 2022 - The mistake I made was not indenting the line where the user has to choose whether to play again or not. If you look closely, the prompt is outside of the outer loop and with nothing being used to break out, and no new inputs being added, the only true statement is that one score is higher than the other, meaning the winner will be printed out endlessly.
🌐
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 ...
🌐
Python.org
discuss.python.org › python help
Infinite While Loop - Python Help - Discussions on Python.org
November 24, 2021 - Hey again everybody! Long time no see! Working on text input and outputs right now. My current code is supposed to count each letter that appears in a text document (“lyrics.txt”) and output the number of times each letter appears. The gets stuck in an infinite while loop though, and I’m ...
Find elsewhere
🌐
Quora
quora.com › Is-it-possible-to-stop-an-infinite-loop-in-Python-without-using-break-or-return-statements-If-so-what-alternative-methods-can-be-used
Is it possible to stop an infinite loop in Python without using break or return statements? If so, what alternative methods can be used? - Quora
Answer (1 of 2): Only two things can break that loop: 1. Keyboard interrupt - You can stop the execution of the program by manually interrupting it with a keyboard interrupt (Ctrl+C) 2. Kill the process - in Windows you can do from the task manager. In Linux you can use the kill command.
🌐
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 ...
🌐
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
🌐
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.
🌐
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.

🌐
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.
🌐
Runestone Academy
runestone.academy › ns › books › published › py4e-int › iterations › infinite_loops.html
6.3. Infinite loops — Python for Everybody - Interactive
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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › loops-in-python
Loops in Python - GeeksforGeeks
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   1 month ago
🌐
Reddit
reddit.com › r/learnpython › a problem with an infinite for-loop in python interacting with a list of 3 values
r/learnpython on Reddit: A problem with an infinite for-loop in python interacting with a list of 3 values
October 17, 2019 -
words = ['cat', 'window', 'defenestrate']

for w in words:
     if len(w) > 6:
         words.insert(0, w)

With for w in words:, the example would attempt to create an infinite list, inserting defenestrate over and over again. I don't understand why the for-loop does it; shouldn't it exit from the loop when it comes to the value defenestrate and the code below the if statement is executed since it evaluates to True.

If the statement was while without any break statement I would understand, but with a for-loop I don't understand. Why is there no end when it doesn't have to go through an infinite list but only a list with 3 values.

In contrast, If I just change the line for w in words: to for w in words[:]: the loop will no longer be infinite and the output will be ['defenestrate', 'cat', 'window', 'defenestrate'] Why is that so?

I am a beginner in programming and would be grateful if you could answer with no advanced terms. Thx.

The example itself can be seen in the Python documentation 3.7.2 under Tutorials, under More Control Flow Tools, under For Statements.

🌐
Python.org
discuss.python.org › python help
Infinite for loop - Python Help - Discussions on Python.org
June 27, 2021 - i am using a set of for loops to update directories to all of their directories, but it has entered a infinite loop and i dont know why. Attr7 = Attr4 Attr4 = [] print("3") for Attr8 in Attr7: print("new") for Attr9 in dir(Key1 + "." + Attr8): print(Attr9) print("5") Attr4.append(Attr8 + "." + Attr9) print("end") if i enter a single directory it prints “new” every cycle but never prints “end”.
🌐
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
🌐
YouTube
youtube.com › shorts › NwjNrKeeS20
How To Create An Infinite Loop In Python #python #code #programming - YouTube
In this python tutorial, I show you how to create an infinite loop in python! Let's get coding!======== Ask Case Digital ========If you have a question you w...
Published   July 15, 2024