What does “while True” mean in Python?

The “while True” is the loop in Python that iterates indefinite times. However, most of the time, the programmer designs the loop in such a way that it increments or iterates a specific number of times until the interrupt or conditional statement is encountered. For instance, programmers terminate the infinite “while True” loop with a terminator like “break” in Python to achieve the desired results.

This article will demonstrate the concept of “while True” in Python. To explain the idea of “while True” different exemplary scenarios are taken for demonstration. The following are topics that will be covered in this article: 

  1. What Does “while True” Mean in Python?
  2. Behavior of “while true” in Python
  3. Use “while True” with if condition
  4. Utilizing “while True” with if-else in Python
  5. Use “while True” with Multiple If Statements in Python
  6. Implement “while True” with if elif combination in Python
  7. One-liner “while True” Approach
  8. Bonus Tip: What is the Difference Between the “while True” Vs. “while 1” in Python?

What does “while True” mean in Python?

The straightforward meaning of “while True” is that the loop will iterate/execute indefinite times. It keeps iterating until and unless the breakpoint or interrupt is invoked within a loop. The user has to manually raise the interruption command like hitting the “CTRL+C” key combination (on Windows) to interrupt the output manually. Following are the different exemplary scenarios to understand the meaning of “while True”: 

Example 1: Behavior of “while true” in Python

The while True means the loop will execute boundlessly infinite times until and unless the manually and forcefully condition is not provided within the loop code body. To learn more about terminating a kernel console in Python, go through our linked article “Terminate Script In Python”. 

Here is how you can utilize the “while True” approach to increment the stopwatch time counter: 

import time
Stopwatch = float(10.00)
#using the while true loop

while True:

    print ("Time Left : ", Stopwatch)

    #invoking sleep function
    time.sleep(0.5)

    #increment the value on each iteration
    Stopwatch += 1

Output

The below output illustrates that the while true loop executes the stopwatch infinite times:

Example 2: Use “while True” With the if Statement

The “while True” with some conditions will act as an interrupt on the infinity loop. Users can break the infinite “while true” loop using the simple if statement with the break statement inside the “while” loop. Whenever the “break” statement executes it will terminate the loop. Following is the code example of the “while True” using the if statement: 

import time
Stopwatch = float(10.00)
#using the while loop to execute the incrementing behavior in Python

while True:

    print ("Time Left : ", Stopwatch)

    #invoking sleep function
    time.sleep(0.5)

    #increment the value on each iteration
    Stopwatch += 1
    if Stopwatch == 20:
        break

Output

Example 3: Utilizing “while True” With if-else in Python

Another practice is to use the “while True” with the “if-else” statement to manually exit out of the “while True” infinity loop. First, the if statement will execute, and upon meeting the specified condition, the “break” statement will trigger and interrupt the infinity loop. Before the condition is met, the else statement becomes True and executes until and unless the if condition triggers the break statement.

Here’s how you can execute the “while True” loop with the if-else statement:

n = 5
while True:
    n += 1
    if n == 9:
        break
    else:
        print(n, '==> iteration done')
print("loop break when condition meet")

Output

Let’s take another example to understand the if-else concept. In the below example, the number is incremented, until and unless the if statement becomes True. When the if statement becomes true the “break” statement under the loop executes and terminates the loop. Otherwise, the “else” statement will execute and break the loop upon executing the else statement. 

Here’s how to use the “while True” with the if-else statement:

n = 5
while True:
    n += 1
    if n <= 3:
        print(n, "\n","n is < 3")
        break
    else:
        print('n is > 3')
        break
print("loop break when condition meet\n")

Output

Example 4: Use “while True” With Multiple If Statements in Python

If you have multiple conditions in a program then utilize the multiple if statement to operate within the “while True” loop. Upon meeting the condition, the particular if statement executes the program within its code body and invokes the break statement to terminate the loop, here’s how you can use the nested if within the while “True” loop:

n = 80

while True:
    if n <= 79:
        print(n, "\n","Good results")
        break
    else:
        print("loop break when condition is unbounded\n")
        if n >= 80:
            print(n, "\n","Excellent results")
            break
        if n<60:
            print(n, "\n","better, need improvement")
            break
        print("bad results\n")

Output

Example 5: Implement “while True” with if elif Combination in Python

Use nested if statements to execute the multiple conditional statements. To break the infinity loop (“while True”) the break statement is considered in the “if-elif” statement code block. The break statement will triggered whenever the condition becomes “True” for the particular if statement:

n = 5
while True:    n += 1    if n <= 3:        print(n, "\n","is < 3")        break    elif n > 4:        print(n, 'is > 4')         break

Output

Example 6: One-liner “while True” Approach 

Python gives the flexibility to write the “while True” loop in one line. The one-liner approach makes a program concise, and more readable. The beauty of one-line code is that when you start reading from left to right you will comprehend the program flow like a narration. 

The while loop executes infinite times on the number that starts from “0” and increments the number in the print() statement:

while True: n=0; n += 1; print(n*"itslinuxfoss\n")

Output

Using a similar one-liner approach you will be able to break the infinity loop by triggering the “break” statement. When the break statement triggers it terminates the infinity “while True” loop. Here’s how you can implement it through a one-liner example:  

while True: n=0; n += 1; print(n*"itslinuxfoss\n"); break

Output

Bonus Tip: What is the Difference Between the “while True” Vs. “while 1” in Python?

while True” and “while 1” both execute the boundless infinity loop until and unless the condition is not invoked. However, the “while 1” is utilized for the legacy version/syntax that is still useful in Python. True in boolean represents “1”. While it is a good practice and a more Pythonic approach to use the “while True” instead of using the “while 1” on the latest versions. 

That is all about the meaning of “while True” in Python. 

Conclusion

The “whileTrue” means in Python that the infinity loop iterations execute on the program. However, depending on the scenario you can manually raise the breakpoints to get out of the indefinite loop. To terminate the infinity loop the “break” statement can be invoked within the loop with the if or if-else statement. This article has demonstrated the meaning of “while True” in different scenarios in Python.