🔁 Python Loops for Beginners

📚 Table of Contents

  • What are Loops?
  • Why Use Loops?
  • for Loop
  • while Loop
  • range() Function
  • break Statement
  • continue Statement
  • Nested Loops
  • Practice Examples
  • FAQ

💡 Quick Tip

Loops help you repeat the same task multiple times without writing the same code again and again.

⚠️ Common Mistakes

  • Creating infinite loops.
  • Wrong indentation.
  • Forgetting to update while loop condition.

✍️ By Roshni Code Charm

📅 July 2026 | ⏱️ 6 min read

🔁 What are Loops?

Loops are used to execute a block of code repeatedly until a condition becomes false.

⭐ Why Use Loops?

🔢 Python for Loop

A for loop is used to iterate over a sequence like list, string, or range.


for i in range(5):
    print(i)

Output:


0
1
2
3
4

⏳ Python while Loop

A while loop runs as long as the condition is true.


count = 1

while count <= 5:
    print(count)
    count += 1

🔢 range() Function

The range() function generates a sequence of numbers.


for number in range(1,6):
    print(number)

🛑 break Statement

break is used to stop the loop immediately.


for i in range(10):

    if i == 5:
        break

    print(i)

⏭️ continue Statement

continue skips the current iteration and moves to the next one.


for i in range(5):

    if i == 2:
        continue

    print(i)

🔄 Nested Loops

A loop inside another loop is called a nested loop.


for i in range(3):

    for j in range(2):

        print(i,j)

🚀 Practice Examples

❓ Frequently Asked Questions

1. How many types of loops are in Python?

Python mainly has two loops: for loop and while loop.

2. What is the use of range()?

range() creates a sequence of numbers for looping.

3. Can loops be nested in Python?

Yes, one loop can be placed inside another loop.