Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use for-else syntax structure in python

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report