In this post, we’ll learn what Python continue statement is and how to properly use it in Python code.
A step by step explanation will be provided with proper practical examples for better understanding.
After reading this post, I’m sure you will have a detailed practical knowledge of how to use Python continue in your own Python code.
So without any delay, lets get right into its implementation.
What is Python Continue Statement?
The continue statement can be used in an iteration. It is used to skip the current iteration but not termination the whole loop. Its used to skip that specific iteration for which it is specified.
Let’s understand the use of Python continue statement using proper practical Python code examples. But first, let’s understand its syntax.
Syntax of Continue
continue
You can see that its a simple continue keyword. You can implement it like this.
Implementing Python Continue Statement (Multiple Examples)
Below examples will show you the practical use of Python continue in code.
Example 1: For Loop on String
val='Purple' for i in val: if i=='l': continue print(i)
Output
P u r p e
You can see that we have used a string and passed it to the for loop. Every item of that string will be passed to the variable of for loop. Inside its body, we’ve used an if condition as well which specifies that if the variable is equal to alphabet L(l) then execute the continue statement or else just print the item.
You can see that when the value got a match, the Python continue statement got triggered and that specific iteration was skipped. As a result, no alphabet L got printed.
So this is how you can skip some specific items in a loop. You can customize it in your own way.
Example 2: For Loop on Integer
for i in range(10): if(i==4 or i==7): continue // it will skip if the value is 4 or 7 print(i)
Output
0
1
2
3
5
6
8
9
You can see that by using Python continue statement, we have successfully skipped printing the integers 4 and 7.
So this is how you can use Python continue statement in your own code with ease. Hope you like this article.
Do ask questions if you have any, regarding the implementation of Python continue. I’ll be happy to answer them.
Conclusion
As conclusion, we’ve practically discussed the usage of Python continue statement with the help of multiple Python code examples. I’d be very glad to have your feedback. Thank you for reading this post.