0% found this document useful (0 votes)
10 views1 page

Python While Loop Explained

The document explains the concept of a while loop in Python, which executes a block of code as long as a specified condition is true. An example code snippet demonstrates printing numbers from 1 to 5, incrementing the count until it exceeds 5, at which point the loop ends. The output confirms the loop's functionality by displaying the numbers followed by a message indicating the loop has ended.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views1 page

Python While Loop Explained

The document explains the concept of a while loop in Python, which executes a block of code as long as a specified condition is true. An example code snippet demonstrates printing numbers from 1 to 5, incrementing the count until it exceeds 5, at which point the loop ends. The output confirms the loop's functionality by displaying the numbers followed by a message indicating the loop has ended.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Concept: While Loop

Example Code:
# Example: Print numbers from 1 to 5 using while loop

count = 1
while count <= 5:
print("Number:", count)
count += 1

print("Loop ended.")

Explanation of While Loop

1. A while loop in Python is used to repeatedly execute a block of code as long as a given
condition is True.

2. In this example, the variable count starts at 1.


3. The loop condition while count <= 5 means the loop will continue running as long as count is
less than or equal to 5.
4. Inside the loop, the current value of count is printed, and then it is increased by 1 using count +=
1.
5. When count becomes 6, the condition becomes False and the loop stops.
6. Finally, the program prints "Loop ended." to indicate the end of the loop.

Sample Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Loop ended.

You might also like