Skip to content

While 1 in Python Example | Code

  • by

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: it 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.3.3 (Community Edition)

Windows 10

Python 3.10.1

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

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading