这就是菜鸟和高手的区别!完全不同的两个世界!Python必然会爆火
发布时间:2020-12-17 01:00:04 所属栏目:Python 来源:网络整理
导读:进群:548377875 ?都可以获取到不同的PDF哦!是非常实用的PDF哦! 01. 变量交换 Bad tmp = aa = bb = tmp Pythonic a,b = b,a 02. 列表推导 Bad my_list = []for i in range(10): my_list.append(i*2) Pythonic my_list = [i*2 for i in range(10)] 03. 单行
进群:548377875 ?都可以获取到不同的PDF哦!是非常实用的PDF哦!01. 变量交换Bad tmp = a a = b b = tmp Pythonic a,b = b,a 02. 列表推导Bad my_list = [] for i in range(10): my_list.append(i*2) Pythonic my_list = [i*2 for i in range(10)] 03. 单行表达式虽然列表推导式由于其简洁性及表达性,被广受推崇。但是有许多可以写成单行的表达式,并不是好的做法。 Bad print 'one'; print 'two' if x == 1: print 'one' if Pythonic print 'one' print 'two' if x == 1: print 'one' cond1 = 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,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','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,5] b = [i for i in a if i > 4] # Or: b = filter(lambda x: x > 4,a) Bad a = [3,5] for i in range(len(a)): a[i] += 3 Pythonic a = [3,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. 代码续行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,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. 链式比较Bad if age > 18 and age < 60: print("young man") Pythonic if 18 < age < 60: print("young man") 理解了链式比较操作,那么你应该知道为什么下面这行代码输出的结果是 False >>> False == False == True False 15. 三目运算这个保留意见。随使用习惯就好。 Bad if a > 2: b = 2 else: b = 1 #b = 2 Pythonic a = 3 b = 2 if a > 2 else 1 #b = 2 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 使用Python 3.5的OpenCV 3.1.0中的`CV_HAAR_SCALE_IMAGE`在
- 在Python中,如何使用“ strip()for file in line”格式仅读
- python基础学习13----生成器&迭代器
- 如何使用GtkBuilder从glade文件重用小部件树?
- 利用Python的Twisted框架实现webshell密码扫描器的教程
- Python基于动态规划算法解决01背包问题实例
- 使用PIP安装PIL时出错 – Django,Python
- python修改字典内key对应值的方法
- python – 以4个为一组从HUGE文本文件中读取行
- 在哪里有一个很好的“python惯例和最佳实践”总结?