In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The main content of this article is to explain "an example analysis of the special features of for else in python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "an example analysis of the special features of for else in python".
No matter which programming language we use, we will write "if-else" statements, but what about "for-else"?
For many languages, such as c, c + +, and Java, it would be completely wrong to use "else" after the loop. However, Python, as an elegant language, has this strange but useful feature. If we use it correctly, our code will become cleaner.
Basic knowledge of For-Else features
When Python developers first encounter the "for-else" feature, it looks strange and difficult to understand. But in fact, its use is surprisingly simple. One sentence is enough to explain the problem: the "else" block is executed only if there is no break in the loop.
Too simple to be true? Let's test it with an example:
Leaders = ["Elon", "Tim", "Warren"] for i in leaders: if I = = "Yang": print ("Yang is a leader!") Breakelse: print ("Not found Yang!") # Not found Yang!
As shown above, the list leaders does not contain "Yang", so there is no break in the for loop. Therefore, the "else" block executes and prints the information.
What happens if "Yang" is included in the list leaders?
Leaders = ["Yang", "Elon", "Tim", "Warren"] for i in leaders: if I = = "Yang": print ("Yang is a leader!") Breakelse: print ("Not found Yang!") # Yang is a leader!
As shown above, because "Yang" is in the leaders list, the for loop is broken and the "else" block is not executed.
In short, the for-else feature is not difficult to understand, but it is not easy to use it correctly and skillfully.
Three scenarios using the For-Else feature
We don't have to use the for-else feature in Python programs. To be honest, we can do the same thing without it, but using it can make our code more elegant.
1. Iterate and find items without flag variables
Iterating through a list to find a specific item is the basic solution for using loops. Usually, when we find a project, there is no point in continuing to iterate, and we need to break the cycle. The question is: how do we know if this item has been found?
The traditional solution is to define a "flag" variable and set it to True when a specific item is found.
Leaders = ["Yang", "Elon", "Tim", "Warren"] have_yang = Falsefor i in leaders: if I = = "Yang": have_yang = True # Do something breakif have_yang = = False: # no yang. # Do others
This approach is good enough, but if you want to take full advantage of Python. Using the for-else feature is another option:
Leaders = ["Yang", "Elon", "Tim", "Warren"] for i in leaders: if I = = "Yang": have_yang = True # Do something breakelse: # no yang. # Do others
It's more simple and convenient.
two。 Help break the nesting cycle
The for-else feature can also help when there are nested loops.
For i in range (5): for j in range (5): if j = = 2 and I = = 0: break if not (j = = 2 and I = = 0): continue break
As shown above, breaking a nested loop is a bit difficult because we have to know if the internal loop is broken.
The above code shows a clumsy solution to determine whether the internal loop has been broken. It works, of course, but we can make it cleaner with the for-else feature:
# use the for-else syntaxfor i in range (5): for j in range (5): if j = = 2 and I = 0: break else: # only execute when it's no break in the inner loop continue break3. Help handle exceptions
Exception handling is very important for programming, and it can also be helpful if we use the for-else feature correctly. For example:
Nums = [1,3,0,5] for denominator in nums: try: 20/denominator except ZeroDivisionError: breakelse: # no found ZeroDivisionError... # Do others
As shown above, if there is no ZeroDivisionError in the for loop, we can perform the corresponding operation in the "else" block.
At this point, I believe you have a deeper understanding of the "example analysis of the special features of for else in python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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: 207
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.