程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

10 Python skills that programmers must know

編輯:Python

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 .

1

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!

2

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 ?

3

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

4

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).

5

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*).

6

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

7

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}

8

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'

9

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 .

10

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

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved