python3.6 編程技巧總結,python3.6編程技巧
學習python 大半年了,發現有很多很實用卻容易被忽視的編程技巧,總結如下。
1. 列印引入模組的檔案路徑
import threadingimport socketprint(threading)print(socket)
module ‘threading’ from ‘/Users/taorui/anaconda3/lib/python3.6/threading.py’
module ‘socket’ from ‘/Users/taorui/anaconda3/lib/python3.6/socket.py’
2. 互動環境下的_操作符
2+1
3
_
3
print(_)
3
3. 檢查python的對象
test=[1,3,5,7]print(dir(test))
[‘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 sysprint(sys.version)
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)]
5. 組合多個字串
test=['I','Like','Python','automation']print(test)ss=""print(ss.join( test))
[‘I’, ‘Like’, ‘Python’, ‘automation’]
ILikePythonautomation
5. 四種翻字串/列表的方式翻轉列表本身
testList=[1,3,5]testList.reverse()print(testList)
[5, 3, 1]
在一個迴圈中翻轉并迭代輸出
for element in reversed([1,3,5]): print(element)
5
3
1
一行代碼翻轉字串
"Test Python"[::-1]
‘nohtyP tseT’
使用切片翻轉列表
[1,3,5][::-1]
[5, 3, 1]
6. 玩轉枚舉
testlist=[10,20,30]for i,value in enumerate(testlist): print(i,':',value)
0 : 10
1 : 20
2 : 30
7. 在python中使用枚舉量
class Shapes: Circle,Square,Tringle,Quadrangle=range(4)print(Shapes.Circle)print(Shapes.Square)print(Shapes.Tringle)print(Shapes.Quadrangle)
0
1
2
3
8. 使用*運算子來unpack函數參數
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)
x y z
1 2 3
10 20 30
9. 使用字典來儲存選擇操作
stdcalc={ 'sum':lambda x,y:x+y, 'subtract':lambda x,y:x-y}print(stacalc['sum'](9,3))print(stacalc['subt'](9,3))
12
6
10. 字典集合推導
testSet={i*2 for i in range(10)}testDict={i: i*i for i in range(10) }print(testSet)print(testDict)
{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}
11. 原地交換兩個數字
x,y=10,20print(x,y)x,y=y,xprint(x,y)
10 20
20 10
12. 鏈狀比較操作符
n=10result=1<n<20print(result)result=1>n>20print(result)
True
False
13. 使用三元操作符來進行條件賦值
out=[m**2 if m>10 else m**4 for m in range(50)]print(out)
[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=testListprint(x,y,z)
1 2 3
15. 一行代碼計算任何數的階乘
import functoolsresult=(lambda k:functools.reduce(int.__mul__,range(1,k+1),1))(3)print(result)
6
16. 找出列表中出現最頻繁的數
test=[1,2,3,4,2,2,3,4,1,4,4,4,4,]print(max(set(test),key=test.count))
4
17. 檢查一個對象的記憶體使用量
import sysx=1print(sys.getsizeof(x))
28
18. 從連個相關的序列構建一個字典
t1=(1,2,3)t2=(10,20,30)print(dict(zip(t1,t2)))
{1: 10, 2: 20, 3: 30}
19. 一行程式碼搜尋字串的多個前尾碼
print("http://www.google.com".startswith(("http://","https://")))print("http://www.google.co.uk".endswith((".com",".uk")))
True
True
20.不使用迴圈構造一個switch-case語句
import itertoolstest=[[-1,-2],[30,40],[25,35]]print(list(itertools.chain.from_iterable(test)))
[-1, -2, 30, 40, 25, 35]
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。 //blog.csdn.net/qq_30262201/article/details/78795127