In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use Python cool, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!
1. Useful keyboard shortcuts
Press Tab for indentation code.
Press Shift + Tab to unindent the code.
To comment or uncomment a pile of code, select it and press Control + / or Command + (for Mac users).
To enclose quotation marks, select it and press Shift +'or Shift + ".
2.Zip
Zip can be used to iterate over more than two lists together.
3. Column derivation expression
The best thing about Python is that you can do a lot of work with less code. Take the column derivation expression as an example. If you want to create a list of numbers within a certain range, you can easily do so as follows:
You can also easily apply conditions to it.
Practical example:
A cool use case for list derivation is to convert a number into its single list of numbers. The trick is to convert numbers to strings, iterate through characters, convert them to int, and store them in a list. We can follow all the steps.
Or do it together.
This also reminds me of the map () function.
4. Use the * operator
The * operator can be used to repeat a string. For example,
Now you may not want to print "Python is cool" many times, but you should use it for something like this.
The * operator can also be used to extract iterations such as lists.
You can do something similar.
This operator is usually used when we have a function that does not know the number of arguments. We use it with * args and * kwargs.
The arguments we pass to the function are stored in * args. * * kwargs will store named parameters or dictionaries.
5.Partial
The other thing you can do with the function is to create the Partial function. What are these? Suppose we have a function that calculates simple interest. We can set default values for some parameters (from right to left).
However, we cannot set the default value to p in this way.
We can use some functions to implement it. In some functions, we set default values for some parameters from left to right, and then use them as functions. Let's set a default value for p.
Although local functions work from left to right, we can also use named parameters to skip the parameters between them.
6. Assertion
Test-driven development means that you write tests and then write code to pass them. You can use assert to write small tests in Python. For example, you might want to make sure that the shape of an object meets your expectations.
Writing these small tests will be very helpful in ensuring that parts of the code work as expected. It will also help you debug efficiently.
7.Generator
We can use the yield keyword in Python instead of the return keyword to create the generator. The advantage of using generators is that you can generate things and forget them in real time. This saves memory.
The above is all the contents of the article "how to use Python coolly". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.