In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the way to write Python code". The content 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 "what is the way to write Python code".
Python is famous for its simple and concise syntax, and you only need to master simple English to understand its code. It is very attractive to beginners, it has no declaration, no fancy characters or strange syntax. It is because of this that Python has become popular all over the world.
In addition, Python has some cool features, such as decorators and list parsing. These features do work miracles, but * they also deserve the reputation that small characters can bring about earth-shaking changes.
Let's start with a little trick:
In [1]: first_dict= {'key1':' hello', 'key2':' world'} second_dict= {'key3':'whats',' key4': 'up'} In [2]: # joins the dicts combined_dict= {* * first_dict, * * second_dict} combined_dict Out [2]: {' key1': 'hello',' key2': 'world',' key3':'whats', 'key4':' up'} In []:
This is an ultra-easy way to merge dictionaries! You can clearly see that I have combined the dictionary with only a few asterisks, which I will explain one by one.
Where does the asterisk work?
In addition to the well-known multiplication, the asterisk also allows you to easily accomplish some important tasks, such as unpacking. In general, you can use asterisks to unpack iterable objects, or you can double unpack bi-directional iterable objects (like dictionaries).
In [7]: # unpackingan iterable [xfor x inrange] = = [* range (100)] Out [7]: True In [8]: # unpkacing dict keys d = {'key1':' A'} list (d.keys ()) = = [* d] Out [8]: True In [9]: # unpacking whole dict d = = {* d} Out [9]: True
The power of unpacking
Don't destroy other people's code.
People are more and more aware of this, but there are still people who do not comply with it. Every function written by a developer has its own characteristics. If the function is changed, all code based on your code will be broken.
I'll introduce a simple way to add more functionality to your function without breaking its backward compatibility, and you'll end up with more modular code.
Enter * args and * * kwrags into your code and they will unpack all input into the function. Single asterisk for standard iterable objects, and double asterisks for bi-directional iterable objects of dictionary class, for example:
In [1]: defversion1 (a, b): print (a) print (b) In [2]: version1 (4, 5) 4 5 In [3]: # code breaks version1 (4, 5) 6)-TypeError Traceback (most recent call last) in beautiful code breaks-> 2 version1 6) TypeError: version1 () takes 2 positionalarguments but 3 were given In [4]: defversion2 (a, b, * args): print (a) print (b) # new function. If args: for c in args: print (c) In [5]: version2 (1, code breaks version2, 3, 4, 5) 1, 2, 3, 4, 5, In [6]: # code breaks version2 Extra=10)-TypeError Traceback (most recent call last) in 1 # code breaks-> 2 version2 Extra=10) TypeError: version2 () got an unexpectedkeyword argument 'Extra' In [7]: defversion3 (a, b, * args, * * kwrags): print (a) print (b) # new function. If args: for c in args: print (c) if kwrags: for key, value inzip (kwrags.keys (), kwrags.values ()): print (key,':', value) In [8]: version3 (1, Extra=10) 1 2 3 45 Extra: 10 In []: thank you for reading, this is the content of "how to write Python Code" After the study of this article, I believe you have a deeper understanding of what is the way to write Python code, and the specific use needs to be verified in practice. 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.