While 1 in Python Example | Code

While 1 is a bit kinder to old versions of Python. While 1 will create an infinite loop in Python.

while True:
    do_something()

The most pythonic way will always be the most readable. Use while True:

while True:
    do_something()

While 1 in Python Example

Simple example code. While 1 will work also in those early versions where True is not yet defined.

while 1:
    print("While 1 Example")
    exit()

Output:

While 1 in Python Example

There is no practical difference. 1 and True are equal in python, and this is guaranteed in the future too, so could use whichever you like the look of best.

Python 2

If you have a tight, long-running loop in Python 2, you probably should use while 1: instead of while True:.

Python 3

Use while True: if you have no condition for breaking out of your loop.

Do comment if you have any doubts and suggestions on this Python While example.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.