Python is a popular and widely used general programming language. Its applications include data science, machine learning, scientific computing and other fields, as well as 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 is free and open source, and most products related to it are. In addition, it has a large, dedicated and friendly community of programmers and users.
Its grammatical design goal is simple, easy to read and elegant.
This article will show you 20 very practical Python tips.
1
Zen of Python
Python Zen (also known as PEP20) is a short text written by Tim Peters, which shows the guidelines for designing and using python. You can find this text on the python website or display it through a statement on the console or Jupiter notebook.
>>> 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!
2
Chained assignment
If you want multiple variables to reference the same object, you can use chained assignment:
>>> x = y = z = 2 >>> x, y, z (2, 2, 2)
Very logical and elegant, right?
3
Chain comparison
Multiple comparison statements can also be synthesized into a Python expression by connecting multiple comparison operators. The following expression returns True only when all comparisons are True, otherwise it returns 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 it is more compact and requires only one 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
4
Multiple assignment
You can use tuple unpacking to assign values to multiple variables in one statement:
>>> x, y, z = 2, 4, 8 >>> x 2 >>> y 4 >>> z 8
Note that 2, 4, 8 in the first statement is equivalent to a tuple (2, 4, 8).
5
More advanced multiple assignment
Python's multiple assignment is much more than ordinary assignment. 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 corresponds to the first value 2, because 2 appears first. z is the last, so take the last value 8. Y takes all values in the middle as a list because it has an asterisk (y *).
6
Cross variable
With multiple assignments, any two variables can be exchanged accurately and gracefully without introducing a third variable:
>>> x, y = 2, 8 >>> x 2 >>> y 8 >>> x, y = y, x >>> x 8 >>> y 2
7
Merge dictionary
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}
8
Connection string
If you need to connect multiple strings and use the same character or group of characters between each string, you can use the str.join method:
>>> x = ['u', 'v', 'w'] >>> y = '-*-'.join(x) >>> y'u-*-v-*-w'
9
Advanced traversal
If you need to traverse a sequence and also need each element and 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
Each time you traverse, you will get a tuple, including the index value and the corresponding element.
10
reverse traversal
If you need to traverse a sequence in reverse, you can use reversed:
>>> for item in reversed(['u', 'v', 'w']): ... print(item) ... w v u