Use 10 hidden eggs in Python

Source: Internet
Author: User

1, using RE. Debug to view the matching process of regular expressions

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

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

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


3. Be careful with the default arguments


Instead, you should replace "[]" with a tag value that means "no definition".

4. For C-series developers who prefer parentheses rather than indentation, you only need to use one of the following commands:

From __future__ import Braces

5. Tricks in slicing operation

A = [1,2,3,4,5] >>> a[::2] [1,3,5]
The special example is x[::-1], which can reverse the list
>>> a[::-1] [5,4,3,2,1]

6, the decorative device

The adorner implements calling other functions or methods in one function to increase functionality, modifying parameters or results, and then adding adorners to the function definition with an "@" symbol.
The following example shows the use of a Print_args adorner:

7. The trick of taking the parameter

You can use * or * * To remove a list or dictionary as a function parameter

8. Exception Else statement


It is better to use "else" than to add extra code in a "try" statement because it avoids accidentally getting an exception that is not protected by a try statement ... In addition to the Declaration.

9. Nested list derivation and generator expression

[(I,J) for I in range (3) for J in Range (i)]
((I,J) 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 again the essence of the Zen of Python (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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.