How to Continue a Loop in Python
Continue statement is used when you want to skip the loop for a certain condition and continue iterating python for loop with the next item on the list. When you use continue in your code it doesn’t break the loop like the break statement does. Instead, it skips the current iterating element and continues with the next item on the iteration.
Continue statement must be used inside a loop (for, while) block. It is generally used after an if conditional statement.
Using Continue Statement In Python
Here is a sample code snippet demonstrating usage of continue statement in Python:
for fruit in ["apple", "banana", "kiwi", "strawberry"]: if fruit == "banana": continue # skip this item print('I <3 ' + fruit) print('The end.')
This sample code snippet outputs the following:
I <3 apple I <3 kiwi I <3 strawberry The end.
Note that, the banana item on the list is skipped using the continue operator.
I hope you liked this tip. Don’t forget to follow me on twitter for more tips like this ;)