Sum over a row

One line of code sums integers up to 100, easily implemented using the sum and range functions. Note that range(1,101) returns integers between 1 and 100, excluding 101.

List to heavy

A line of code, the first use of the set of attributes (disorderly not repeated), the list into a set, then use the list function to convert the set into a list, complete the list to duplicate.

List derivation

List derivation can construct a new list very succinctly, using only a concise expression to transform the resulting elements.

[expression for variable in list] or [expression for variable in list if condition];

In the image below, extract the column element greater than 10, multiply the list element by 2, and you get a one-line list derivation.

Quick exchange value

Instead of using an intermediate variable as a medium, swap assignments, as shown below:

Element ordering

Sort by the character length of the list elements. The sort function itself sorts the list by the keyword key=len(length), which modifies the list itself.

Character count statistics

Count the number of occurrences of a character in a string.

Two ways to remove whitespace

The first method, replace, is easy to understand.

The second method first uses the split function to split the string through whitespace. Str.split (” “) returns a list:

[‘ I ‘and’ am ‘and’ hero ‘, ‘ha’, ‘ha’, ‘. ‘], using the join function, will list all the elements together.

Regular matching Chinese

“[\\u4e00-\\u9fa5]+” matches Chinese.

One line doubles the list elements

The map and lambda functions multiply each element of the list by 2 in a single line. Isn’t it neat? The map function maps the specified sequence based on the provided function, returns an iterator, and the list function returns a list.

One line of code outputs the multiplication table

Type in the code box: “print (‘ \ n ‘. Join (‘. Join (= ‘% s * % s % 2 s’ % (y, x, x * y) for y in range (1, x + 1)) for x in range (1, 10)))”

Wenyuan network, only for the use of learning, if there is infringement please contact delete.

You will definitely encounter difficulties in learning Python. Don’t panic, I have a set of learning materials, including 40+ e-books, 800+ teaching videos, covering Python basics, crawlers, frameworks, data analysis, machine learning, etc. Shimo. Im/docs/JWCghr… Python Learning Materials

Follow the Python circle and get good articles delivered daily.