日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

這就是菜鳥和高手的區(qū)別!完全不同的兩個世界!Python必然會爆火

 靜幻堂 2018-09-11
程序員的聚集地 2018-09-10 14:46:30
這就是菜鳥和高手的區(qū)別!完全不同的兩個世界!Python必然會爆火

私信小編01.02.03.04都可以獲取到不同的PDF哦!是非常實(shí)用的PDF哦!

01. 變量交換

Bad

tmp = a
a = b
b = tmp

Pythonic

a,b = b,a


02. 列表推導(dǎo)

Bad

my_list = []
for i in range(10):
my_list.append(i*2)

Pythonic

my_list = [i*2 for i in range(10)]


03. 單行表達(dá)式

雖然列表推導(dǎo)式由于其簡潔性及表達(dá)性,被廣受推崇。但是有許多可以寫成單行的表達(dá)式,并不是好的做法。

Bad

print 'one'; print 'two'
if x == 1: print 'one'
if <complex comparison> and <other complex comparison>:
# do something

Pythonic

print 'one'
print 'two'
if x == 1:
print 'one'
cond1 = <complex comparison>
cond2 = <other complex comparison>
if cond1 and cond2:
# do something

04. 帶索引遍歷

Bad

for i in range(len(my_list)):
print(i, "-->", my_list[i])

Pythonic

for i,item in enumerate(my_list):
print(i, "-->",item)


05. 序列解包

Pythonic

a, *rest = [1, 2, 3]
# a = 1, rest = [2, 3]
a, *middle, c = [1, 2, 3, 4]
# a = 1, middle = [2, 3], c = 4

06. 字符串拼接

Bad

letters = ['s', 'p', 'a', 'm']
s=""
for let in letters:
s += let

Pythonic

letters = ['s', 'p', 'a', 'm']
word = ''.join(letters)


07. 真假判斷

Bad

if attr == True:
print 'True!'
if attr == None:
print 'attr is None!'

Pythonic

if attr:
print 'attr is truthy!'
if not attr:
print 'attr is falsey!'
if attr is None:
print 'attr is None!'

08. 訪問字典元素

Bad

d = {'hello': 'world'}
if d.has_key('hello'):
print d['hello'] # prints 'world'
else:
print 'default_value'

Pythonic

d = {'hello': 'world'}
print d.get('hello', 'default_value') # prints 'world'
print d.get('thingy', 'default_value') # prints 'default_value'
# Or:
if 'hello' in d:
print d['hello']

09. 操作列表

Bad

a = [3, 4, 5]
b = []
for i in a:
if i > 4:
b.append(i)

Pythonic

a = [3, 4, 5]
b = [i for i in a if i > 4]
# Or:
b = filter(lambda x: x > 4, a)

Bad

a = [3, 4, 5]
for i in range(len(a)):
a[i] += 3

Pythonic

a = [3, 4, 5]
a = [i + 3 for i in a]
# Or:
a = map(lambda i: i + 3, a)

10. 文件讀取

Bad

f = open('file.txt')
a = f.read()
print a
f.close()

Pythonic

with open('file.txt') as f:
for line in f:
print line

11. 代碼續(xù)行

Bad

my_very_big_string = """For a long time I used to go to bed early. Sometimes, 
when I had put out my candle, my eyes would close so quickly that I had not even
time to say “I’m going to sleep.”"""
from some.deep.module.inside.a.module import a_nice_function, another_nice_function,
yet_another_nice_function

Pythonic

my_very_big_string = (
"For a long time I used to go to bed early. Sometimes, "
"when I had put out my candle, my eyes would close so quickly "
"that I had not even time to say “I’m going to sleep.”"
)
from some.deep.module.inside.a.module import (
a_nice_function, another_nice_function, yet_another_nice_function)

12. 顯式代碼

Bad

def make_complex(*args):
x, y = args
return dict(**locals())

Pythonic

def make_complex(x, y):
return {'x': x, 'y': y}


13. 使用占位符

Pythonic

filename = 'foobar.txt'
basename, _, ext = filename.rpartition('.')


14. 鏈?zhǔn)奖容^

Bad

if age > 18 and age < 60:
print("young man")

Pythonic

if 18 < age < 60:
print("young man")

理解了鏈?zhǔn)奖容^操作,那么你應(yīng)該知道為什么下面這行代碼輸出的結(jié)果是 False

>>> False == False == True 
False


15. 三目運(yùn)算

這個保留意見。隨使用習(xí)慣就好。

Bad

if a > 2:
b = 2
else:
b = 1
#b = 2

Pythonic

a = 3 
b = 2 if a > 2 else 1
#b = 2

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多