第三周 day14:内置函数
?. 本節(jié)主要內(nèi)容:
1. 內(nèi)置函數(shù)
什么是內(nèi)置函數(shù)? 就是python給你提供的. 拿來直接?的函數(shù), 比如print., input等等.
截? 到python版本3.6.2 python?共提供了68個內(nèi)置函數(shù). 他們就是python直接提供給我們的. 有 ?些我們已經(jīng)?過了. 有?些還沒有?過. 還有?些需要學(xué)完了?向?qū)ο蟛拍芾^續(xù)學(xué)習(xí)的. 今 天我們就認識?下python的內(nèi)置函數(shù).
abs() dict() help() min() setattr() all() dir() hex() next() slice() any() divmod() id() object() sorted() ascii() enumerate() input() oct() staticmethod() bin() eval() int() open() str() bool() exec() isinstance() ord() sum() bytearray() filter() issubclass() pow() super() bytes() float() iter() print() tuple() callable() format() len() property() type() chr() frozenset() list() range() vars() classmethod() getattr() locals() repr() zip() compile() globals() map() reversed() __import__() complex() hasattr() max() round() delattr() hash() memoryview() set()
2. 作?域相關(guān):
locals() 返回當(dāng)前作?域中的名字
globals() 返回全局作?域中的名字
3迭代器相關(guān):
range() ?成數(shù)據(jù)
next() 迭代器向下執(zhí)??次, 內(nèi)部實際使?了__next__()?法返回迭代器的下?個項?
iter() 獲取迭代器, 內(nèi)部實際使?的是__iter__()?法來獲取迭代器
4.字符串類型代碼的執(zhí)?:
eval() 執(zhí)?字符串類型的代碼. 并返回最終結(jié)果
print(eval("2+2")) # 4 n = 8 print(eval("2+n")) # 10def func():print(666) eval("func()") # 666exec() 執(zhí)?字符串類型的代碼
exec(""" for i in range(10):print(i) """)exec(""" def func():print("我是周杰倫") func() """)compile() 將字符串類型的代碼變異. 代碼對象能夠通過exec語句來執(zhí)?或者eval()進?求 值
'''參數(shù)說明:1. resource 要執(zhí)?的代碼, 動態(tài)代碼?段2. ?件名, 代碼存放的?件名, 當(dāng)傳?了第?個參數(shù)的時候, 這個參數(shù)給空就可以了3. 模式, 取值有3個,1. exec: ?般放?些流程語句的時候2. eval: resource只存放?個求值表達式.3. single: resource存放的代碼有交互的時候. mode應(yīng)為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().? 沒有返回值,復(fù)雜的流程問題的字符串形式的代碼?exec(). ?般很少? 到compile()
?
5.輸入和輸出相關(guān):
input() 獲取?戶輸入的內(nèi)容
print() 打印輸出
6.內(nèi)存相關(guān):
hash()? 獲取到對象的哈希值(int, str, bool, tuple)
id() 獲取到對象的內(nèi)存地址`
7.文件操作相關(guān):
open() ?于?打開?個?件, 創(chuàng)建?個?件句柄
8.模塊相關(guān):
__import__() ?于動態(tài)加載類和函數(shù)
9.幫助:
help() 函數(shù)?于查看函數(shù)或模塊?途的詳細說明
10.調(diào)?相關(guān):
callable() ?于檢查?個對象是否是可調(diào)?的. 如果返回True, object有可能調(diào)?失敗, 但 如果返回False. 那調(diào)?絕對不會成功
11.查看內(nèi)置屬性:
dir() 查看對象的內(nèi)置屬性, ?法. 訪問的是對象中的__dir__()?法
?
12.基礎(chǔ)數(shù)據(jù)類型相關(guān):
(1)數(shù)字相關(guān):
bool() 將給定的數(shù)據(jù)轉(zhuǎn)換成bool值. 如果不給值. 返回False
int() 將給定的數(shù)據(jù)轉(zhuǎn)換成int值. 如果不給值, 返回0
float() 將給定的數(shù)據(jù)轉(zhuǎn)換成float值. 也就是?數(shù)
complex() 創(chuàng)建?個復(fù)數(shù). 第?個參數(shù)為實部, 第?個參數(shù)為虛部. 或者第?個參數(shù)直接 ?字符串來描述復(fù)數(shù)
(2)進制轉(zhuǎn)換:
bin() 將給的參數(shù)轉(zhuǎn)換成?進制
otc() 將給的參數(shù)轉(zhuǎn)換成八進制
ex() 將給的參數(shù)轉(zhuǎn)換成?六進制
(3)數(shù)學(xué)運算:
abs() 返回絕對值
divmode() 返回商和余數(shù)
round() 四舍五入
pow(a, b) 求a的b次冪, 如果有三個參數(shù). 則求完次冪后對第三個數(shù)取余
sum() 求和
min() 求最?值
max() 求最?值
13.和數(shù)據(jù)結(jié)構(gòu)相關(guān):
(1)列表和元組:
list() 將?個可迭代對象轉(zhuǎn)換成列表
tuple() 將?個可迭代對象轉(zhuǎn)換成元組
reversed() 將?個序列翻轉(zhuǎn), 返回翻轉(zhuǎn)序列的迭代器
slice() 列表的切片
st = "?家好, 我是麻花藤" s = slice(1, 5, 2) print(st[s])(2)字符串相關(guān):
str() 將數(shù)據(jù)轉(zhuǎn)化成字符串
format() 與具體數(shù)據(jù)相關(guān), ?于計算各種?數(shù), 精算等
# 字符串 print(format('test', '<20')) # 左對? print(format('test', '>20')) # 右對? print(format('test', '^20')) # 居中 # 數(shù)值 print(format(3, 'b')) # ?進制 print(format(97, 'c')) # 轉(zhuǎn)換成unicode字符 print(format(11, 'd')) # ?進制 print(format(11, 'o')) # ?進制 print(format(11, 'x')) # ?六進制(?寫字?) print(format(11, 'X')) # ?六進制(?寫字?) print(format(11, 'n')) # 和d?樣 print(format(11)) # 和d?樣 # 浮點數(shù) print(format(123456789, 'e')) # 科學(xué)計數(shù)法. 默認保留6位?數(shù) print(format(123456789, '0.2e')) # 科學(xué)計數(shù)法. 保留2位?數(shù)(?寫) print(format(123456789, '0.2E')) # 科學(xué)計數(shù)法. 保留2位?數(shù)(?寫) print(format(1.23456789, 'f')) # ?數(shù)點計數(shù)法. 保留6位?數(shù) print(format(1.23456789, '0.2f')) # ?數(shù)點計數(shù)法. 保留2位?數(shù) print(format(1.23456789, '0.10f')) # ?數(shù)點計數(shù)法. 保留10位?數(shù) print(format(1.23456789e+10000, 'F')) # ?數(shù)點計數(shù)法.bytes() 把字符串轉(zhuǎn)化成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() 返回?個新字節(jié)數(shù)組. 這個數(shù)字?的元素是可變的, 并且每個元素的值得范 圍是[0,256)
ret = bytearray('alex',encoding='utf-8') print(ret[0]) print(ret)memoryview() 查看bytes在內(nèi)存中的情況
# 查看bytes字節(jié)在內(nèi)存中的情況 s = memoryview("麻花藤".encode("utf-8")) print(s)ord() 輸入字符找?guī)ё址幋a的位置
chr() 輸入位置數(shù)字找出對應(yīng)的字符
ascii() 是ascii碼中的返回該值 不是就返回\u...
# 找到對應(yīng)字符的編碼位置 print(ord('a')) print(ord('中'))# 找到對應(yīng)編碼位置的字符 print(chr(97)) print(chr(20013))
# 在ascii中就返回這個值. 如果不在就返回\u... print(ascii('a')) print(ascii('好'))
repr() 返回?個對象的string形式
# repr 就是原封不動的輸出, 引號和轉(zhuǎn)義字符都不起作? print(repr('?家好,\n \t我叫周杰倫')) print('?家好我叫周杰倫')# %r 原封不動的寫出來,但占位符%s會執(zhí)行 name = 'taibai’ print('我叫%r' % name)
(3)數(shù)據(jù)集合:
dict() 創(chuàng)建?個字典
set() 創(chuàng)建?個集合
frozenset() 創(chuàng)建?個凍結(jié)的集合. 凍結(jié)的集合不能進?添加和刪除操作
(4)其他相關(guān):
len() 返回?個對象中的元素的個數(shù)
sorted() 對可迭代對象進?排序操作(講完lamda后再講這個)
enumerate() 獲取集合的枚舉對象
lst = ["alex", "wusir", "taibai"] for index, el in enumerate(lst):print(str(index)+"==>"+el)all() 可迭代對象中全部是True, 結(jié)果才是True
any() 可迭代對象中有?個是True, 結(jié)果就是True
print(all([1,2,True,0])) print(any([1,'',0]))zip() 函數(shù)?于將可迭代的對象作為參數(shù), 將對象中對應(yīng)的元素打包成?個個元組, 然 后返回由這些元組組成的開了表. 如果各個迭代器的元素個數(shù)不?致, 則返回列表?度與最短 的對象相同.
l1 = [1,2,3,] l2 = ['a','b','c',5] l3 = ('*','**',(1,2,3)) for i in zip(l1,l2,l3):print(i)filter() 過濾(講完lamda)
map()? 會根據(jù)提供的函數(shù)對指定序列做映射(lamda)
?
轉(zhuǎn)載于:https://www.cnblogs.com/mwj-blog1/p/9343403.html
總結(jié)
以上是生活随笔為你收集整理的第三周 day14:内置函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android基础_1 四大基本组
- 下一篇: 梦说1+1等于多少