In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "Python development skills of the walrus operator of the use of what", the article explained the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Python development skills of the walrus operator of the use of what" bar!
Catalogue
1. The first usage: if/else
two。 Second usage: while
3. The third usage: deductive
The Python version is developing very fast, and now the latest version is Pyhton 3.9. even so, many people are still stuck at 3.6or 3.7, not even 3.8yet.
Many of the features of Python 3.8 have become old knowledge before they can be understood, such as the walrus operator we are going to talk about today.
The walrus operator was proposed in PEP 572until version 3.8 was incorporated into the release.
Its original English name is Assignment Expressions, which translates to an assignment expression, but now it is more commonly called the walrus operator because it looks so much like a walrus.
1. The first usage: if/else
Some friends may be exposed to this new feature for the first time, so what's the use of simply introducing this walrus operator?
The conditional statement in Golang can directly judge the variable after the variable is obtained in if, which allows you to write one less line of code.
Import "fmt" func main () {if age: = 20 leading age > 18 {fmt.Println ("adult")}}
Before Python 3.8, Python had to be written like this
Age = 20if age > 18: print ("grown up")
But with the walrus operator, you can be like Golang (if you haven't learned Golang, note here that: = in Golang is called a short variable declaration, which means to declare and initialize, it is not the same concept as: = in Python).
Age = 20if age > 18: print ("adult") 2. Second usage: while
You might write this when using a while loop to read a file before using the walrus operator
File = open ("demo.txt", "r") while True: line = file.readline () if not line: break print (line.strip ())
But with the walrus operator, you can do this.
File = open ("demo.txt", "r") while (line: = file.readline ()): print (line.strip ())
It is more amazing to use it to replace the previous infinite while loop.
For example, to implement code that requires command line interaction to enter a password and verify it, you might write something like this
While True: P = input ("Enter the password:") if p = = "youpassword": break
With the walrus operator, it is more comfortable to write.
While (p: = input ("Enter the password:"))! = "youpassword": continue3. The third usage: deductive
Almost every article in this series can be seen as a guide, and this one is still the same.
In the coding process, I like to use deductive, in a simple application scenario, it is simple and efficient.
In the following code, I will use the list derivation to get the bmi index of all obese members.
Members = [{"name": "Xiao Wu", "age": 23, "height": 1.75, "weight": 72}, {"name": "Xiao Li", "age": 17, "height": 1.72, "weight": 63}, {"name": "Chen", "age": 20, "height": 1.78, "weight": 82} ] count = 0def get_bmi (info): global count count + = 1 print (f "executed {count} times") height = info ["height"] weight = info ["weight"] return weight / (height**2) # bmi index of all obese members fat_bmis = [get_bmi (m) for m in members if get_bmi (m) > 24] print (fat_bmis)
The output is as follows
Executed once.
Executed twice.
Executed three times.
Executed four times.
[25.88057063502083]
As you can see, there are only three members, but the get_bmi function is executed four times because it is executed three times in judgment and repeated when constructing a new list.
If all members are obese, it will eventually be executed six times, which is a waste of performance under a large amount of data, so I usually use the traditional for loop + if judgment for this structure.
Fat_bmis = [] # find out the bmi index of all obese members for m in members: bmi = get_bmi (m) if bmi > 24: fat_bmis.append (bmi)
With the walrus operator, you don't have to make compromises in this scenario.
# find out the bmi index of all obese members fat_bmis = [bmi for m in members if (bmi: = get_bmi (m)) > 24]
Finally, as can be seen from the output, it was only executed three times.
Executed once.
Executed twice.
Executed three times.
[25.88057063502083]
Only list derivation is introduced here, but it is also applicable in dictionary derivation and collection derivation. No more demonstrations.
The walrus operator is a novel feature that many people think will destroy the readability of the code. It is true that this is the case when a new thing comes out, but I believe that after the precipitation of time, when more and more people use it and enjoy its convenience, this controversy will gradually disappear in the long river of history.
Thank you for your reading, the above is the "Python development skills of the walrus operator of the use of what" the content, after the study of this article, I believe that you have a deeper understanding of the Python development skills of the walrus operator of the use of this problem, the specific use of the need for practice to verify. 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.