• Python Basics

  • Python Variables

  • Operators in Python

  • Conditional Statements in Python

  • Python Lists

  • Python Tuples

  • Python Sets

  • Python Dictionaries

  • Loops in Python

  • Python Arrays and Functions

  • Conclusion

Python while Loop

While Loops in Python

In Python, loops are essential constructs that allow you to execute a block of code repeatedly based on certain conditions. This lesson will focus on while loops, which continue to execute as long as a specified condition remains true. You will learn how to use while loops effectively, along with key statements like break, continue, and else.

1. What is a While Loop?

A while loop in Python runs as long as its condition evaluates to true. This type of loop is useful for situations where the number of iterations is not known beforehand and depends on dynamic conditions.

Example: Basic While Loop

python
1# Initializing a variable
2counter = 1
3
4# While loop that continues until counter is less than 6
5while counter < 6:
6    print(counter)  # Output: 1, 2, 3, 4, 5
7    counter += 1  # Incrementing counter

In this example, the loop prints the value of counter until it reaches 6. It's essential to update the variable inside the loop; otherwise, it could lead to an infinite loop.

2. Using the Break Statement

The break statement allows you to exit the loop prematurely, even if the while condition is still true. This is useful when you want to stop execution based on a specific condition.

Example: Exiting the Loop with Break

python
1# Initializing a variable
2counter = 1
3
4# While loop with a break condition
5while counter < 6:
6    print(counter)  # Output: 1, 2, 3
7    if counter == 3:
8        break  # Exit the loop when counter is 3
9    counter += 1

Lesson: Understanding While Loops in Python

In Python, loops are essential constructs that allow you to execute a block of code repeatedly based on certain conditions. This lesson will focus on while loops, which continue to execute as long as a specified condition remains true. You will learn how to use while loops effectively, along with key statements like break, continue, and else.

1. What is a While Loop?

A while loop in Python runs as long as its condition evaluates to true. This type of loop is useful for situations where the number of iterations is not known beforehand and depends on dynamic conditions.

Example: Basic While Loop

python
1# Initializing a variable
2counter = 1
3
4# While loop that continues until counter is less than 6
5while counter < 6:
6    print(counter)  # Output: 1, 2, 3, 4, 5
7    counter += 1  # Incrementing counter

In this example, the loop prints the value of counter until it reaches 6. It's essential to update the variable inside the loop; otherwise, it could lead to an infinite loop.

2. Using the Break Statement

The break statement allows you to exit the loop prematurely, even if the while condition is still true. This is useful when you want to stop execution based on a specific condition.

Example: Exiting the Loop with Break

python
1# Initializing a variable
2counter = 1
3
4# While loop with a break condition
5while counter < 6:
6    print(counter)  # Output: 1, 2, 3
7    if counter == 3:
8        break  # Exit the loop when counter is 3
9    counter += 1

In this example, the loop terminates as soon as counter equals 3, demonstrating how break can control loop execution.

3. Using the Continue Statement

The continue statement allows you to skip the current iteration and move to the next one. This is useful when you want to ignore specific conditions without terminating the entire loop.

Example: Skipping an Iteration with Continue

python
1# Initializing a variable
2counter = 0
3
4# While loop that continues until counter reaches 6
5while counter < 6:
6    counter += 1  # Increment before the condition check
7    if counter == 3:
8        continue  # Skip the iteration when counter is 3
9    print(counter)  # Output: 1, 2, 4, 5, 6

In this case, when counter equals 3, the loop skips the print statement, resulting in only the numbers 1, 2, 4, 5, and 6 being printed.

4. Using Else with While Loops

You can use the else statement with a while loop, which executes a block of code once the loop condition becomes false. This provides a way to perform an action after the loop has finished running.

Example: Using Else with a While Loop

python
1# Initializing a variable
2counter = 1
3
4# While loop with an else statement
5while counter < 6:
6    print(counter)  # Output: 1, 2, 3, 4, 5
7    counter += 1
8else:
9    print("Counter has reached 6.")  # This executes after the loop ends

In this example, the message "Counter has reached 6." is printed after the loop finishes its execution, illustrating how to use the else clause effectively.

Frequently Asked Questions