While Loop in Python#

A while loop is a powerful tool in Python that lets you repeatedly run a block of code as long as a certain condition is true. It’s useful when you want to run code based on a condition that might not be known beforehand.

A while loop has an expression followed by a colon and an indented block of code. The loop keeps running as long as the expression is True.

while expression:
    print("message")

This code will keep printing “message” as long as the expression is true.

A common mistake with while loops is creating an infinite loop, where the loop never stops. To avoid this, make sure the expression in the while loop will eventually become false, or use the break keyword to exit the loop.

While True - Creating an Infinite Loop#

Sometimes, you might want to create an infinite loop that runs until a certain condition is met. You can do this with a while True loop and a break statement inside it.

while True:
    user_input = input("Enter 'exit' to quit: ")
    if user_input == 'exit':
        break
    print("You are still inside the loop!")

In this case, the loop keeps running until the condition is met, and then the break statement exits the loop.

Nesting Loops#

You can also nest while loops, meaning you can have a while loop inside another while loop or even a for loop inside a while loop. This is useful for handling complex control flow and working with multi-dimensional data.

For vs. While Loops#

While loops are similar to for loops. They both support the break, continue, and else statements for controlling the loop’s flow.

  • For loops are typically used when you want to iterate over a collection, like a list or range of numbers.

  • While loops are more event-based and are used when you have specific conditions to start and stop the loop.

While loops are essential in Python for running code until a certain condition is met. Understanding how to use them can help you control the flow of your programs. They are a valuable tool in your programming toolkit.