python學習 day013打卡 內建hansh
本節主要內容:
內建函式:
內建函式就是python給你提供的.拿來直接用的函式,比如print,input等等.截止到python版本3.6.2 python一共提供了68個內建函式.他們就是python
直接提供給我們的.有一些我們已經用過了.有一些還沒有用過.還有一些還沒有用過.還有一些需要學完了面向物件才能繼續學習的.今天我們就認識
一下python的內建函式.
https://www.processon.com/mindmap/5bdbfc4be4b0cc9e7ea65a49中的思維導圖是關於內建函式的分類和使用方法的介紹
下面詳細介紹一些之前沒有用到過的內建函式:
一.字串型別程式碼的執行:
eval() 執行字串型別的程式碼.並返回最終結果
print(eval("2+2")) #4 n = 8 print(eval("2+n")) #10 def func(): print(666) eval("func()") #666
exec() 執行字串型別的程式碼
exec(""" for i in range(10): print(i) """) exec(""" def func(): print("我是周杰倫") func() """)
compile()將字串型別的程式碼編譯,程式碼物件能夠通過exec語句來執行或者eval()進行求值
""" 引數說明: 1.resource 要執行的程式碼,動態程式碼片段 2.檔名,程式碼存放的檔名,當傳入了第一個引數的時候,這個引數給空就可以了 3.模式,取值有3個, 1.exce: 一般放一些流程語句的時候 2.eval:resource只存放一個求值表示式 3.single:resource存放的程式碼有互動的時候,mode應為single. """ code1 = "for i in range(10) : print(i)" c1 = compile(code1,"",mode="exec") exec(c1) code2 = "1+2+3" c2= compile(code2,"",mode="eval") a = eval(c2) print(a) code3 = "name = input('請輸入你的名字')" c3 = compile(code3,"",mode="single") exec(c3) print(name)
有返回值的字串形式的程式碼用eval().沒有返回值的字串形式的程式碼用exec().一般很少用到compile()
二.slice() 列表的切片
st = "大家好,我是麻花藤" s = slice(1,5,2) print(st[s])
三.字串相關:
format() 與具體資料相關,用於計算各種小數,精算等
字串 print(format('test', '<20')) # 左對⻬ cener(20) print(format('test', '>20')) # 右對⻬ print(format('test', '^20')) # 居中 # 數值 print(format(3, 'b')) # ⼆進位制 11 print(format(97, 'c')) # 轉換成unicode字元 a print(format(11, 'd')) # ⼗進位制 11 print(format(11, 'o')) # ⼋進位制 13 print(format(11, 'x')) # ⼗六進位制(⼩寫字⺟) b print(format(11, 'X')) # ⼗六進位制(⼤寫字⺟) B print(format(11, 'n')) # 和d⼀樣 11 print(format(11)) # 和d⼀樣 11 # 浮點數 print(format(123456789, 'e')) # 科學計數法. 預設保留6位⼩數 print(format(123456789, '0.2e')) # 科學計數法. 保留2位⼩數(⼩寫) print(format(123456789, '0.2E')) # 科學計數法. 保留2位⼩數(⼤寫) print(format(1.23456789, 'f')) # ⼩數點計數法. 保留6位⼩數 print(format(1.23456789, '0.2f')) # ⼩數點計數法. 保留2位⼩數 print(format(1.23456789, '0.10f')) # ⼩數點計數法. 保留10位⼩數 print(format(1.23456789e+10000, 'F')) # ⼩數點計數法. INF 無窮
bytes()把字串轉化成bytes型別
s = "你好" bs = s.encode("utf-8") print(bs) s1 = bs.decode("utf-8") print(s1) bs = bytes(s,encoding="utf-8") #把字串編碼成utf-8 print(bs)
bytearray() 返回一個新位元組陣列,這個數組裡的元素是可變的,並且每個元素的值的範圍是[0,256)
ret = bytearray("alex",encoding="utf-8") print(ret[0]) print(ret)
memoryview() 檢視bytes在記憶體中的情況
s = memoryview("麻花藤".encode("utf-8")) # 檢視bytes位元組在記憶體中的情況 print(s)
repr() 返回一個物件的官方表示形式
# repr 輸出一個字串的官方表示形式
print(repr("大家好,\n \t我叫周杰倫"))
print("大家好我叫周杰倫")
# 結果 :
# '大家好,\n \t我叫周杰倫'
# 大家好我叫周杰倫
# %r %r用的就是repr
name = "taibai"
print("我叫%r" %name)
# 結果:我叫'taibai'
enumerate() 獲取集合的列舉物件
lst = ["alex","wusir","taibai"] for index,el in enumerate(lst): print(str(index)+"==>"+el) # 結果: # 0==>alex # 1==>wusir # 2==>taibai
all() 可迭代物件中全部是True, 結果才是True
any() 可迭代物件中有一個是True, 結果就是True
print(all([1,2,True,0])) #False print(any([1,"",0]))#True
zip() 函式用於將可迭代的物件作為引數,將物件中對應的元素打包成一個個元組,然後返回由這些元組組成的列表.如果各個
迭代器的元素個數不一致,則返回列表長度與最短的物件相同.
l1 = [1,2,3] l2 = ["a","b","c",5] l3 = ("*","**",(1,2,3)) for i in zip(l1,l2,l3): print(i) # 結果: # (1, 'a', '*') # (2, 'b', '**') # (3, 'c', (1, 2, 3))
.