In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how Python follows else after for". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how Python connects else after for".
0. Background
Today, I saw a strange way of writing. For followed the else statement directly. At first, I thought it was not indented. After the query, I found that there was such a syntax, so I would like to share it. I have written C++ and Java before, but this is the first time I have seen else after for.
1. Experiment # eg1import numpy as npfor i in np.arange (5): print ielse: print ("hello?") # 0# 1# 2# 3# 4# hello?
You can see that the statements in the for are executed after the break ends normally.
# eg2import numpy as npfor i in np.arange (5): print i if (I = = 3): breakelse: print ("hello?") # 0# 1# 2# 3
In this example, there is a loop in break on iDivery3, and then the statement in else is not executed.
2. Summary
To sum up, it is relatively simple, if the for loop ends normally, the statement in else executes. If it is break, it is not executed.
Engineering code is written less, do not think of a good scene for the time being, in order not to cause interference to other students, this form is better.
Add: the collocation of for and else in python
Use prime numbers as code examples for i in range (2Power10): for n in range (2Poweri): if I% n = 0: # print (I,'=', n,'*', iUnip) break else: print ('found it% s'% I)
Note: the else here does not belong to the if code block
According to the interpretation of the official document: when the iterated object is iterated and empty, the statement at else will be executed, but if there is break in the for loop, the loop will be terminated directly and the code in else will not be executed
Write a simple example to help understand for i in range (10): if I = = 7: print ('found it% slots% I) breakelse: print (' not found')
You can run the code first, take a look at the result, then comment out the break in the code block and run it again. Compare it with the result of the first run, and you will find that it is different.
Add: the use of for-else in python, execute else after for execution
After the end of the for cycle, execute elsefor i in range (5): print (I) else: print ("print else") Thank you for your reading. This is the content of "how Python follows else after for". After the study of this article, I believe you have a deeper understanding of how Python is followed by else after for. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
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.