How to use for-else syntax structure in python
This article mainly shows you "how python uses for-else syntax structure", the content is simple and easy to understand, organized clearly, I hope to help you solve doubts, let Xiaobian lead you to study and learn "how python uses for-else syntax structure" this article bar.
for - else
What's that? Isn't if and else the original match? You may not know this, but else is a two-timing guy, and for and else are also a pair, and they are legal. Ten pretense B grammar, for-else absolutely can be regarded as Nanwu Bay! If you don't believe me, please see:
>>> for i in [1,2,3,4]:print(i)else:print(i, 'I am else') 12344 I am else12345678910
If a third if intervenes between the for and the else (in the loop), it does not affect the relationship between the for and the else. Because for has a higher rank than if, else is a clingy guy who doesn't care if there is an if and if the statement that satisfies the if condition is executed. The eyes of else are only for. As long as the for is successfully executed, else will run again:
>>> for i in [1,2,3,4]:if i > 2:print(i)else:print(i, 'I am else') 344 I am else123456789
So how do you break up for and else? The else statement is skipped only if the for loop is interrupted by a break statement:
>>> for i in [1,2,3,4]:if i>2:print(i) break else:print(i, 'I am else') 312345678 The above is "How python uses the for-else syntax structure" All the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!