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 the else clause in Python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to use the else clause in Python. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Else clause

The else clause in Python can be used not only in if statements, but also in for, while, and try statements.

The else block is run only when the for loop or the while loop finishes running normally (rather than through a break statement or a return statement or an exception exits the loop).

For example:

For i in range (3):... Print (I)... Else:... Print ('Iterated over everything'). 012Iterated over everything >

As above, the for loop ends normally, so the subsequent else block is run.

For i in range (3):... If I = = 2RV... Break... Print (I)... Else:... Print ('Iterated over everything'). 01 >

From this, you can see that the for loop will not run the subsequent else block if it does not run properly (such as the one above is the end of the break loop).

The try block is run only if no exception is thrown in the else block. At first, you may not feel the need to use the else clause in the try/except block. After all, in the following code snippet, after_call () executes only if dangerous_call () does not throw an exception, right?

Try: dangerous_call () after_call () except OSError: log ('OSError...')

However, after_call () should not be placed in the try block. For clarity, only statements that throw expected exceptions should be included in the try block. Therefore, it is better to write as follows:

Try: dangerous_call () except OSError: log ('OSError...') else: after_call ()

It is now clear that the try block defends against possible errors in dangerous_call (), not after_call (). And obviously, after_call () is executed only if the try block does not throw an exception. Note, however, that the exception thrown by the else clause is not handled by the previous except clause, which means that if after_call () throws an exception at this point, it will not be caught.

This is the end of the article on "how to use the else clause in Python". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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