2 Python While Loops

1. What is a While Loop?

A while loop is used to repeat code again and again
as long as a condition is true.

In simple words:
👉 Python checks a condition
👉 If it is True, the loop runs
👉 When it becomes False, the loop stops

2. Simple While Loop Example

Example


 

Output:


 

3. How While Loop Works

  1. Check condition

  2. Run code inside loop

  3. Update value

  4. Check condition again

4. While Loop with Condition

Example


 

5. Infinite While Loop (Be Careful!)

If the condition never becomes false, the loop runs forever.

❌ Example (Infinite Loop)


 

👉 i is never changed.

6. Stop While Loop Using break

Example


 

7. Skip One Loop Using continue

Example


 

8. While Loop with Else

else runs after loop ends normally.

Example


 

9. While Loop with User Input

Example


 

10. Common Beginner Mistakes

❌ Forgetting to Update Variable


 

❌ Infinite loop.

❌ Wrong Indentation


 

✔ Correct:


 

11. Simple Practice Examples

Example 1: Print Even Numbers


 

Example 2: Countdown


 

Example 3: Sum of Numbers


 

12. Summary (Python While Loop)

✔ Repeats code while condition is true
✔ Needs condition and update
✔ Be careful of infinite loops
break stops loop
continue skips one step

📘 Perfect for Beginner eBook

This chapter is ideal for:

  • Python beginner books

  • School & college students

  • Self-learners

If you want next, I can write:

  • For Loop

  • Break & Continue

  • Loop Exercises

  • Real-Life Loop Examples

Just tell me 😊

You may also like...