Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
A while loop is a type of loop in Python that repeatedly executes a block of code as long as a given condition remains true. This tutorial covers the basics of while loops, including their syntax and common use cases.
Syntax:
The basic syntax of a while loop in Python is as follows:
while condition: # code to be executed
The condition
is a boolean expression that is evaluated before each iteration. If the condition is true, the block of code within the loop is executed. If the condition is false, the loop is terminated, and the program continues with the next statement after the loop.
Example:
Here's an example that demonstrates a simple while loop:
counter = 0 while counter < 5: print(f"Counter value: {counter}") counter += 1
In this example, the loop continues to execute as long as counter
is less than 5. The output is:
Counter value: 0 Counter value: 1 Counter value: 2 Counter value: 3 Counter value: 4
Using else
with while loops:
You can also use an else
block with a while loop. The else
block is executed when the loop condition becomes false.
counter = 0 while counter < 5: print(f"Counter value: {counter}") counter += 1 else: print("Loop has ended.")
This will output:
Counter value: 0 Counter value: 1 Counter value: 2 Counter value: 3 Counter value: 4 Loop has ended.
Infinite loops:
Be cautious when using while loops, as it's possible to create an infinite loop if the condition never becomes false. If you accidentally create an infinite loop, you may need to forcefully terminate the program.
Using break
and continue
:
You can use the break
statement to exit the loop prematurely, and the continue
statement to skip the rest of the current iteration and proceed to the next one.
counter = 0 while counter < 10: counter += 1 if counter % 2 == 0: continue elif counter == 7: break print(f"Counter value: {counter}")
This will output:
Counter value: 1 Counter value: 3 Counter value: 5
In this example, the loop skips even numbers using the continue
statement and terminates the loop when counter
is equal to 7 using the break
statement.
In summary, while loops are a fundamental control structure in Python that allows you to execute a block of code repeatedly as long as a given condition remains true. Be cautious when using while loops to avoid creating infinite loops, and use the break
and continue
statements to control loop execution as needed.
How to use while loops in Python:
# Using a simple while loop count = 0 while count < 5: print(count) count += 1
Python while loop examples:
# Example of using while loop to iterate through a list numbers = [1, 2, 3, 4, 5] index = 0 while index < len(numbers): print(numbers[index]) index += 1
Infinite while loop in Python:
# Creating an infinite while loop while True: print("This is an infinite loop")
Break and continue in while loops Python:
break
statement is used to exit a while loop prematurely, and continue
skips the rest of the code and continues to the next iteration.# Using break and continue in a while loop count = 0 while count < 5: if count == 2: break print(count) count += 1
Nested while loops in Python:
# Example of nested while loops outer_count = 0 while outer_count < 3: inner_count = 0 while inner_count < 2: print(f"Outer: {outer_count}, Inner: {inner_count}") inner_count += 1 outer_count += 1
Python while loop vs for loop:
# Comparison of while loop and for loop count = 0 while count < 5: print(count) count += 1 # Equivalent for loop for i in range(5): print(i)
Conditionals in while loops Python:
# Using conditionals in a while loop count = 0 while count < 5: if count % 2 == 0: print(f"{count} is even") else: print(f"{count} is odd") count += 1
Common mistakes with Python while loops:
# Common mistake: Forgetting to update the loop variable count = 0 while count < 5: print(count) # Forgetting count += 1