Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the programming skills of the new version of Python

2025-03-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what programming skills are there in the new version of Python". In daily operation, I believe many people have doubts about the programming skills in the new version of Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what programming skills are there in the new version of Python?" Next, please follow the editor to study!

1. F string (F-Strings)

The F string provides a simple and convenient way to embed Python expressions in string text for formatting.

First, let's define two variables, name and age, which are included in the print statement.

Name = "Pavel" age = 23

Instead of dealing with string concatenation or using commas in print statements, you can use Python's improved string format syntax "f-strings", which has been released with version 3.6 of Python.

Simply put the lowercase or uppercase letter "f" in front of the string and place the variable or expression in curly braces.

F-Strings is a new method of formatting strings, which is more readable, faster, concise, and less error-prone than other formatting methods!

2. Help function

The Help function in Python can be used to find documents 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 byte size. Objects can be of any type.

For example:

Only the memory consumption directly attributed to the object is calculated, not the memory consumption of the object it refers to.

Another example:

4. Links to comparison operators

Typically, to check for more than two conditions, we must use a logical operator, such as and/or.

If a < b and b < c:

In Python, there is a better way to write it using comparison operator chains.

Links to operators can be written as follows:

If a < b < c:

For example:

5. List understanding

List understanding is another more common and elegant way of making lists.

Instead of creating an empty list and adding each element to the end, you just define the list and its contents in the following format:

New_list = [expression for item in iterable (if conditional)]

For example:

Another example:

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 use commas to separate variables and values to assign multiple values to multiple variables:

This also works when deconstructing / unpacking a sequence, such as a list or tuple, and is a more elegant way to assign sequence elements to a single variable, 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 one-line construct to exchange variables, similar to the concept of assigning values to multiple variables in a row.

The following code is the same as above, but does not use any temporary variables:

9. Create enumerations

Enum is the class used to create enumerations in Python, and enumerations are a set of symbolic names attached to unique constant values.

To create an enumeration, you must create a class that is the name of the desired enumeration.

All that's left to do is list the variables and set them to the desired values:

To access the enumeration memberPaul, for example, simply execute Person.Paul, and it will return 0.

In Python, you can simplify the above example by listing variables next to each other and setting them equal to the range function:

10. Enumerate

Typically, when traversing the list, we access not only the index that has a position 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 the names of variables you might like. For example, for indexes, count in the enumeration (x).

11. Dir function

Dir () is a powerful built-in function in Python3 that returns a list of properties and methods for any object (that is, functions, modules, strings, lists, dictionaries, and so on).

This is useful when there is little or no information about modules and helps to learn new modules faster.

For example:

Dir () is usually used for debugging purposes. Dir () is able to list all the attributes of the passed parameters, which is useful when dealing with many classes and functions separately.

twelve。 Parametric decompression

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

All you have to do is print out each element in the list, separated by spaces, because the unpack operator accepts all the elements in the list and passes them as parameters, so the transformation of the above code will be printed out (1 meme 2 meme 3 pencils 4 5).

This Python technique is often used in functions to "package" all the parameters received by a method call into a variable.

For example:

The above function func can accept an unlimited number of arguments (args [0] and args [1] will 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 named person:

Person = {"name": "Paul", "age": 23, "location": "London"}

You can use the * operator to pass the dictionary to the function.

The incoming dictionary decomposes the key into a function keyword parameter and then takes that value as the actual value passed for that parameter.

For example:

At this point, the study of "what are the programming skills of the new version of Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report