Get the App
SLTechnology News&Howtos  ›  Development  › 

What skills are hidden in Python

Shulou Source: shulou.com Published: 2022-06-02 09:18:41 10月03日 Update

This article mainly explains "what skills are hidden in Python". The content of the explanation 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 skills are hidden in Python.

1. Use re.DEBUG to view the matching process of regular expressions

Regular expressions are a feature of Python, but it can be painful to debug and it's easy to get a bug. Fortunately, Python can print out a parsing tree of regular expressions and use re.debug to show the complete process of re.compile.

Once you understand the grammar, you can find your mistakes. Here we can see that [/ font] forgot to remove []

2. The enumerate function is used to traverse the elements in the list and their subscripts

3. Be more careful with default arguments

Instead, you should use a tag value for "undefined" instead of "[]".

4. For C developers who prefer parentheses to indentation, you only need to use the following command:

From _ _ future__ import braces

5. Tricks in slicing operation

A = [1, 2, 3, 4, 5] > > a [:: 2] [1, 1, 3, 5]

A special example is x [::-1], which reverses the list

> a [::-1] [5, 4, 3, 2, 1]

6. Decorator

The decorator implements calling other functions or methods in a function to increase functionality, thus modifying parameters or results, etc., adding a decorator before the function definition, with only an "@" symbol.

The following example shows the use of a print_args decorator:

7. Trick for fetching parameters

You can use * or * * to fetch a list or dictionary as a function argument

8. Exception else statement

Using "else" is better than adding extra code to "try" statements, because it avoids accidentally getting exceptions that are not protected by try statements. Except for the statement.

9. Nested list derivation and generator expressions

[(iMagnej) for i in range (3) for j in range (I)] ((iMagnej) for i in range (4) for j in range (I))

These statements can replace a large number of nested loop code blocks

10. Main sentence patterns

Import this

Let's read the essentials of Python Zen again (The Zen of Python, by Tim Peters):

Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Thank you for your reading, these are the contents of "what skills are hidden in Python". After the study of this article, I believe you have a deeper understanding of what skills are hidden in Python, 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!

Tags: Functions techniques expressions statements regularities learning code content parameters just need procedures special lucky unexpected painful subscript examples elements functions functionality Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MariaDB vpn Docker Apple