Loops
for
Loops
Use for
loops to iterate over items in a sequence like a list or range.
Comprehensions: Lists and Dictionaries
Directly create a list or dictionary using a for
loop.
while
Loops
Use while
loops when you want to repeat something until a condition becomes False
.
break
break
exits the loop immediately.
continue
continue
skips the rest of the current loop iteration and moves on to the next.
Looping with enumerate()
Use enumerate()
to get both index and item while looping.
Looping with zip()
Use zip()
to loop over two or more sequences at once.
Infinite Loop Danger
If the while
condition is never False
, the loop will run forever.
Common Errors
Some frequent errors encountered when dealing with Loops in Python.