Python Is a popular and widely used general programming language , Its applications include data science 、 machine learning 、 Scientific computing, etc , And the back end Web Development 、 Mobile and desktop applications . Many well-known companies use Python, Such as Google、Dropbox、Facebook、Mozilla、IBM、Quora、Amazon、Spotify、NASA、Netflix、Reddit etc. .
Python Free and open source , Most of the products related to it are also . Besides , It has a huge 、 A dedicated and friendly community of programmers and users .
Its syntax design goal is simple 、 Easy to read and elegant .
This article will show you 20 This is very practical Python Using skills .
Python zen
Python zen ( also called PEP20) By Tim Peters A short paragraph written , The design and use of Python Guidelines for . You can Python Find this text on the website , It can also be on the console or Jupyter notebook This passage is displayed by a statement in .
>>> import this 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. Readability counts.Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
Chained assignment
If you need to make multiple variables reference the same object , Chain assignment can be used :
>>> x = y = z = 2 >>> x, y, z (2, 2, 2)
Very logical and elegant , Right ?
Chain comparison
Multiple comparison statements can also be combined into one Python expression , Just connect multiple comparison operators . The following expression returns... Only if all comparisons are true True, Otherwise return to False:
>>> x = 5 >>> 2 < x ≤ 8 True >>> 6 < x ≤ 8 False
This expression is equivalent to (2 < x) And (x ≤ 8) And (x ≤ 8), But more compact , And it only needs to be executed once x evaluation .
The following expression is also correct :
>>> 2 < x > 4 True
You can even connect multiple comparisons :
>>> x = 2 >>> y = 8 >>> 0 < x < 4 < y < 16 True
Multiple assignments
You can use tuples to unpack , Assign values to multiple variables in one statement :
>>> x, y, z = 2, 4, 8 >>> x 2 >>> y 4 >>> z 8
Notice the... In the first sentence 2, 4, 8 It's equivalent to a tuple (2, 4, 8).
More advanced multiple assignments
Python Multiple assignments are far more than ordinary assignments . The number of elements on the left and right sides of the assignment can even be different :
>>> x, *y, z = 2, 4, 8, 16 >>> x 2 >>> y [4, 8] >>> z 16
In this code ,x Corresponding to the first value 2, because 2 First appeared .z It's the last one , So take the last value 8.y Take all values in the middle in the form of list , Because it has an asterisk (y*).
Cross variable
Multiple assignments can be used to accurately 、 Exchange any two variables gracefully , And there is no need to introduce a third variable :
>>> x, y = 2, 8 >>> x 2 >>> y 8 >>> x, y = y, x >>> x 8 >>> y 2
Merge dictionaries
One way to merge two dictionaries is to unpack them into a new dictionary :
>>> x = {'u': 1} >>> y = {'v': 2} >>> z = {**x, **y, 'w': 4} >>> z {'u': 1, 'v': 2, 'w': 4}
Connection string
If you need to connect multiple strings , Each string is connected by the same character or group of characters , You can use str.join Method :
>>> x = ['u', 'v', 'w'] >>> y = '-*-'.join(x) >>> y'u-*-v-*-w'
Advanced traversal
If you need to traverse a sequence , You also need each element and the corresponding index , You can use enumerate:
>>> for i, item in enumerate(['u', 'v', 'w']): ... print('index:', i, 'element:', item) ... index: 0 element: u index: 1 element: v index: 2 element: w
Every time you traverse, you get a tuple , This includes the index value and the corresponding element .
Reverse traversal
If you need to traverse a sequence backwards , You can use reversed:
>>> for item in reversed(['u', 'v', 'w']): ... print(item) ... w v u