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 statement in Python

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "how to use for else sentences in Python". In daily operation, I believe many people have doubts about how to use for else sentences in Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use for else sentences in Python". Next, please follow the editor to study!

Any programming language provides if. Else... Statement, which means that if (if) meets the condition, do something, otherwise (else) do something else:

If axioms:

Print ("true")

Else:

Print ("false")

However, in Python, else can not only be used with if, another unique syntax is for. Else... It can also be used with while, try... Except is used in combination, for example:

For i in range (3):

Print (I)

Else:

Print ("end")

> > >

0

one

two

End

But you will find that for... Else... With if... Else... Behaving differently, according to past experience, code that executes a block of for statements does not execute inside else, and vice versa.

However, what we see is just the opposite. The end of the for loop is followed by the execution of the else statement block, which is interesting, if... Else... Translated into vernacular is if... Otherwise... , and for... Else... Translated into vernacular until... And then... Why not write it as for... Then... What about the sentence pattern?

A for loop traversing an empty list also executes a block of else statements because it is a special case of a normal exit from the for loop.

For i in []: print (I) else: print ("end") > 0

Continue to explore, we use break to prematurely terminate the for loop

For i in range (3):

Print (I)

If I% 2 = 0:

Break

Else:

Print ("end")

> > >

0

When the loop encounters break exiting, the entire statement ends and the else statement block is not executed.

To sum up, we can conclude that the else block executes only if no break is encountered in the loop.

Why did the father of Python come up with such a grammatical candy? This is something that most of us cannot understand. But the Zen of python tells us the answer: "Although that way may not be obvious at first unless you're Dutch."

With this question, I also found an answer in StackOver Flow. There is really very little for in the usual development. Else... However, using for else in a scenario like this is really a use of pythonic.

When you use the for loop to iterate over an element in the list, exit early if you find it, and use for else to notify the caller in a different way if you haven't found the caller after the iteration.

For i in mylist:

If I = = target:

Break

Process (I)

Else:

Raise ValueError ("List argument missing terminal flag.")

If you don't use for... Else... Then you also need to create a temporary tag variable to mark whether it has been found

Found = False

For i in mylist:

If I = = target:

Found = True

Break

Process (I)

If not found:

Raise ValueError ("List argument missing terminal flag.")

When you want to find something in the room, as long as you find it anywhere, stop searching. But if you go through the whole room and haven't found what we're looking for, you need to tell people that there's nothing you're looking for. In such a situation, use for. Else, other than that, it's best not to use it.

At this point, the study on "how to use for else statements in Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Internet Technology

Wechat

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

12
Report