Loops
Learn how to repeat code execution using for loops and while loops in Python.
Learn how to repeat code execution using for loops and while loops in Python. This hands-on tutorial focuses on practical implementation of loops concepts.
Loops in Python
Loops allow you to repeat a block of code multiple times. This is essential for tasks like processing items in a list, counting, or keeping a program running until a condition changes.
Which Loop to Use?
The for Loop
The for loop is used to iterate over a sequence (like a list, tuple, string, or range).
Looping through a Range
The range() function generates a sequence of numbers.
Looping through a String
You can loop through each character in a string.
Looping through a List
Looping through a Dictionary
You can iterate over keys, values, or both.
The while Loop
The while loop repeats code as long as a condition is True.
Interactive Example: Sum of Numbers
Let's calculate the sum of numbers from 1 to 10.
AI Mentor
Confused about "Python for and while loops"? Ask our AI mentor for a simplified explanation.
Quiz
Quiz
Question 1 of 5What does range(3) generate?
Key Takeaways
✅ for loops iterate over sequences (lists, strings, ranges)
✅ range(start, stop, step) generates numbers
✅ while loops run as long as a condition is True
✅ Use .items() to loop through dictionary keys and values
What's Next?
In the next lesson, we'll master loop control:
breakto exit loops earlycontinueto skip iterationselseclauses in loops- Nested loops
Keep coding! 🚀