Loops in Python

📘 Python 👁 93 views 📅 Nov 05, 2025
⏱ Estimated reading time: 1 min

Loops in Python

Loops are used to execute a block of code repeatedly based on a condition.


1. for Loop

  • Iterates over a sequence (list, tuple, string, or range).

# Loop through a list fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit) # Loop through numbers using range for i in range(5): print(i) # 0 to 4

2. while Loop

  • Repeats as long as the condition is True.

i = 0 while i < 5: print(i) i += 1

3. Nested Loops

  • Place one loop inside another for complex iterations.

for i in range(1, 4): for j in range(1, 4): print(f"i={i}, j={j}")

4. Loop Control Statements

StatementDescriptionExample
breakExit the loop immediatelyif i == 3: break
continueSkip the current iterationif i % 2 == 0: continue
passDo nothing, acts as a placeholderpass

Example:

for i in range(5): if i == 2: continue # skip 2 elif i == 4: break # stop loop at 4 print(i)

5. else with Loops

  • Executed when the loop completes normally (no break).

for i in range(3): print(i) else: print("Loop finished without break")

6. Key Points

  • for loops are sequence-based, while loops are condition-based.

  • Use nested loops for multi-dimensional data.

  • break stops the loop, continue skips an iteration, pass is a placeholder.

  • else with loops runs only if the loop ends naturally.


🔒 Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes

🤖 AI Quizer Assistant

📝 Quiz
📚 Categories
🏆 Leaderboard
📊 My Score
❓ Help
👋 Hi! I'm your AI quiz assistant for Quizer.in!

I can help you with:
• 📝 Finding quizzes
• 🏆 Checking leaderboard
• 📊 Your performance stats

Type 'help' to get started! 🚀
AI is thinking...