As we all know, Python is famous for its syntax brevity, and the same function can be implemented in a single line in Java, which may take a dozen lines.

Python code is so elegant because of its unique features that you can write poetic code if you master them.

Let’s take a look at the dirty operations in Python.

Hello World

For most programmers, the first program should be “Hello World!” Python directly makes the starter into a package.

In [1]: import __hello__
Hello world!
Copy the code

Switching variable

One line of code handles variable swaps, no temporary variables, no xor operations.

In [1]: x,y = y,x
Copy the code

Variables are

For continuous comparisons of variables, Python supports them well.

In [24]: x = 10
​
In [25]: 5 < x < 20
Out[25]: True
​
In [26]: 11 < x < 20
Out[26]: False
Copy the code

List derivation

In [2]: list = list(range(10))
# take an even number
In [3]: even = [x for x in list if x % 2 == 0]
​
In [4]: even
Out[4]: [0, 2, 4, 6, 8]
Copy the code

Merge string

Many languages merge strings through the + sign, but because of the immutability of the string, the string will continue to apply for new memory.

In [5]: x = ['a'.'b'.'c'.'d'.'e'.'f'.'g']
​
In [6]: ' '.join(x)
Out[6]: 'abcdefg'
Copy the code

List slice

In [2]: x
Out[2]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]# select values with indexes 2 through 8 and step size 2
​
In [4]: x[2:8:2]
Out[4]: [2, 4, 6]# If the step size is negative, the value is taken from behind
​
In [6]: x[::-2]
Out[6]: [9, 7, 5, 3, 1]
Copy the code

Inverted string

In [7]: x = 'Hello Python! '
​
In [8]: x[::-1]
Out[8]: '! nohtyP olleH'
Copy the code

Get both the subscript and the value

x = list(range(10))
for index, value in enumerate(x):
    print(index, value)
Copy the code

Zip () function

In [7] : a = [1, 2, 3] In [8] : b = (4 and 6) In [9] : c =,8,9 [7] [16] : In the list (zip (a, b, c)) Out [16] : [(1, 4, 7), (2, 5, 8), (3, 6, 9)]# the inverse operation
In [18]: zz = (zip(a, b, c))
​
In [19]: x, y, z = zip(*zz)
​
In [20]: x,y,z
Out[20]: ((1, 2, 3), (4, 5, 6), (7, 8, 9))
​
# merge list contiguous items
In [22]: a = [1, 2, 3, 4, 5, 6]
​
In [23]: list(zip(a[::2], a[1::2]))
Out[23]: [(1, 2), (3, 4), (5, 6)]
Copy the code

closure

def outer(x):
    def inner(y):
        The inner function uses a variable from the outer function
        nonlocal x
        x += y
        return x + y
​
    The return value of the outer function is a reference to the inner function
    return inner
​
fun = outer(10)
​
print(fun(10)) # 30
print(fun(10)) # 40
print(fun(10)) # 50
Copy the code

Today we learned some Python tricks that you need to work with. Did you get it?

Text source network, only for the use of learning, copyright belongs to the original author, 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.