python3.6 编程技巧总结
定期回顾和总结自己的编程经验,可以让你更好地掌握编程技术。 #生活技巧# #编程开发#
学习python 大半年了,发现有很多很实用却容易被忽视的编程技巧,总结如下。
1. 打印引入模块的文件路径import threading import socket print(threading) print(socket)1234
module ‘threading’ from ‘/Users/taorui/anaconda3/lib/python3.6/threading.py’
module ‘socket’ from ‘/Users/taorui/anaconda3/lib/python3.6/socket.py’
2+11
3
_1
3
print(_)1
3
3. 检查python的对象test=[1,3,5,7] print(dir(test))12
[‘add‘, ‘class‘, ‘contains‘, ‘delattr‘, ‘delitem‘, ‘dir‘, ‘doc‘, ‘eq‘, ‘format‘, ‘ge‘, ‘getattribute‘, ‘getitem‘, ‘gt‘, ‘hash‘, ‘iadd‘, ‘imul‘, ‘init‘, ‘init_subclass‘, ‘iter‘, ‘le‘, ‘len‘, ‘lt‘, ‘mul‘, ‘ne‘, ‘new‘, ‘reduce‘, ‘reduce_ex‘, ‘repr‘, ‘reversed‘, ‘rmul‘, ‘setattr‘, ‘setitem‘, ‘sizeof‘, ‘str‘, ‘subclasshook‘, ‘append’, ‘clear’, ‘copy’, ‘count’, ‘extend’, ‘index’, ‘insert’, ‘pop’, ‘remove’, ‘reverse’, ‘sort’]
4. 检查python版本import sys print(sys.version)12
3.6.3 |Anaconda, Inc.| (default, Oct 6 2017, 12:04:38)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
test=['I','Like','Python','automation'] print(test) ss="" print(ss.join( test))1234
[‘I’, ‘Like’, ‘Python’, ‘automation’]
ILikePythonautomation
testList=[1,3,5] testList.reverse() print(testList)123
[5, 3, 1]
在一个循环中翻转并迭代输出for element in reversed([1,3,5]): print(element)12
5
3
1
"Test Python"[::-1]1
‘nohtyP tseT’
使用切片翻转列表[1,3,5][::-1]1
[5, 3, 1]
6. 玩转枚举testlist=[10,20,30] for i,value in enumerate(testlist): print(i,':',value)123
0 : 10
1 : 20
2 : 30
class Shapes: Circle,Square,Tringle,Quadrangle=range(4) print(Shapes.Circle) print(Shapes.Square) print(Shapes.Tringle) print(Shapes.Quadrangle)123456
0
1
2
3
def test(x,y,z): print(x,y,z) testDict={'x':1,'y':2,'z':3} testList=[10,20,30] test(*testDict) test(**testDict) test(*testList)1234567
x y z
1 2 3
10 20 30
stdcalc={ 'sum':lambda x,y:x+y, 'subtract':lambda x,y:x-y } print(stacalc['sum'](9,3)) print(stacalc['subt'](9,3))123456
12
6
testSet={i*2 for i in range(10)} testDict={i: i*i for i in range(10) } print(testSet) print(testDict)1234
{0, 2, 4, 6, 8, 10, 12, 14, 16, 18}
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
x,y=10,20 print(x,y) x,y=y,x print(x,y)1234
10 20
20 10
n=10 result=1<n<20 print(result) result=1>n>20 print(result)12345
True
False
out=[m**2 if m>10 else m**4 for m in range(50)] print(out)12
[0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401]
14. 存储列表元素到新的变量testList=[1,2,3] x,y,z=testList print(x,y,z)123
1 2 3
15. 一行代码计算任何数的阶乘import functools result=(lambda k:functools.reduce(int.__mul__,range(1,k+1),1))(3) print(result)123
6
16. 找出列表中出现最频繁的数test=[1,2,3,4,2,2,3,4,1,4,4,4,4,] print(max(set(test),key=test.count))12
4
17. 检查一个对象的内存使用import sys x=1 print(sys.getsizeof(x))123
28
18. 从连个相关的序列构建一个字典t1=(1,2,3) t2=(10,20,30) print(dict(zip(t1,t2)))123
{1: 10, 2: 20, 3: 30}
19. 一行代码搜索字符串的多个前后缀print("http://www.google.com".startswith(("http://","https://"))) print("http://www.google.co.uk".endswith((".com",".uk")))12
True
True
import itertools test=[[-1,-2],[30,40],[25,35]] print(list(itertools.chain.from_iterable(test)))123
[-1, -2, 30, 40, 25, 35]
网址:python3.6 编程技巧总结 https://www.yuejiaxmz.com/news/view/612790
相关内容
黑客编程有什么技巧大学学习技巧总结
电脑编程技巧与维护杂志
总结健身锻炼的技巧和经验
化妆技巧分享:一周总结
快速学习和运用工作总结技巧.docx
提高生产力的工作总结技巧.docx
日常用品去除污垢技巧总结
做家务的技巧总结
10年Java面试总结:Java程序员面试必备的面试技巧