In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what Python skills are used to write better 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 Python skills are used to write better code.
1. F String
The F string provides a simple and convenient way to embed Python expressions in string text for formatting.
First, let's define two variable names and ages to include in the print statement.
Name = "Pavel" age = 23
To avoid dealing with string concatenation or using commas in print statements, you can use Python's improved string format syntax "f-Strings" released in Python 3.6.
Simply place the lowercase or uppercase letter "f" before a string with a variable or expression in curly braces.
F string is a great new way to format strings, which is more readable, faster, more concise and less error-prone than other formatting methods!
two。 Help function
The Python help function is used to find documentation for modules, functions, classes, keywords, etc.
Simply pass an object inside the help function to retrieve the document for that object:
3. Find the size of any object
The default sys module contains a getsizeof function that takes an object and returns its size in bytes. The object can be of any type.
For example:
Consider only the memory consumption directly attributed to the object, regardless of the memory consumption of the object it references.
Another example:
4. Links to compare operators
Typically, to check for more than two conditions, you will have to use logical operators, such as and / or
If a < b and b < c:
In Python, there is a better way to write using the comparison operator chaining.
The chain of operators can be written as follows:
If a < b < c:
For example:
5. List derivation expression
List derivation expressions are another and often more elegant way to create lists.
Instead of creating an empty list and adding each element to the end, you can define both the list and its contents in the following format:
New_list = [expression for item in iterable (if conditional)]
For example:
Another example (conditional):
6. String multiplication
In Python, you can multiply not only by numbers, but also by strings.
For example:
7. Assign multiple variables in a row
You can assign multiple values to multiple variables by separating variables and values with commas:
This also applies when deconstructing / unpacking a sequence, such as a list or tuple, and is a more elegant way to assign the elements of the sequence to individual variables, because you do not need to use loops or individually index each element of the sequence.
8. Exchange variables on the spot
In many other programming languages, the values of two or more variables can only be exchanged by defining additional temp (temporary) variables.
Suppose you want to exchange x and y:
Temp = x x = y y = temp
In Python, there is a simple single-line construct to exchange variables, similar to the concept of assigning values to multiple variables in a single row.
The following code is the same as the above code, but no temporary variables are used:
9. Create an enumeration
Enum is the class used to create enumerations in Python. Enumerations are a set of symbolic names attached to unique, constant values.
To create an enumeration, it is necessary to create a class that is the name of the enumeration you want.
All that's left to do is list the variables and set them equal to the desired values:
To access the enumeration memberPaul, for example, simply execute Person.Paul, and it will return 0.
In Python, you can shorten the above example by listing variables next to each other and setting them equal to the range function:
10.Range
Typically, when you traverse a list, you access not only the index that has a location in the list, but also the actual elements.
Let's define a list of characters:
X = ['await,' baked,'c']
Instead of traversing it in a standard way, access elements and indexes:
Use enumerations
Enumeration is a built-in function of Python that enables us to iterate over an iterable object and have an automatic counter. It essentially matches each element in the list with the corresponding index. Most newbies, even some senior programmers, don't realize this.
You can change the variables I and v to variable names that you might want to use. For example, for indexes, count in enumerate (x).
11.Dir function
Dir () is a powerful built-in function in Python3 that returns a list of properties and methods of any object, that is, functions, modules, strings, lists, dictionaries, and so on.
This is useful when there is little information about the module and helps you learn new modules faster.
For example:
Dir () is usually used for debugging purposes. The ability of dir () to list all the attributes of passed parameters is useful when dealing with many classes and functions separately.
twelve。 Parameter unpacking
Sometimes called the Splat or Scatter operator * they work when you need to decompress the parameters in the list / tuple for function calls that require separate positional arguments.
For example, the built-in range () function requires separate start and stop parameters. When writing function calls, you can use the * operator to unpack parameters from a list or tuple:
Let's define a simple list x = [1, 2, 3, 4, 5]
A common example of a unpacking operator is the use of print:
Print (* x) / / Result: 1 2 3 4 5
This simply prints out each element in the list, separated by spaces, because the unpack operator passes all the elements in the list to them as parameters, so the translation of the above code will be print.
This Python technique is often used in functions to "package" all the parameters received by the method call into a variable.
For example:
The above function func accepts an unlimited number of arguments (args [0] and args [1] provide you with the first and second arguments, respectively).
In a similar manner, dictionaries can use the * operator to pass keyword parameters.
Let's define a Python dictionary called person:
Person = {"name": "Paul", "age": 23, "location": "London"}
You can use the * operator to pass the dictionary to the function.
The incoming dictionary will decompose the key into the function keyword parameter, and then pass the value as the actual value of that parameter.
For example:
Thank you for reading, the above is the content of "what Python skills are used to write better code". After the study of this article, I believe you have a deeper understanding of what Python skills are used to write better 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.