Python內置函數(shù)(1)— abs()励背、all()、any()砸西、ascii()叶眉、bin()、bool()芹枷、breakpoint()衅疙、bytearray()、bytes()鸳慈、callable()饱溢。
Python內置函數(shù)(2)— chr()、classmethod()走芋、compile()绩郎、complex()、delattr()翁逞、dict()肋杖、dir()、divmod()挖函、enumerate()状植、eval()。
Python內置函數(shù)(3)— exec()、filter()津畸、float()振定、format()、frozenset()肉拓、getattr()吩案、globals()、hasattr()帝簇、hash()、help()靠益。
Python內置函數(shù)(4)— hex()丧肴、id()、input()胧后、int()芋浮、isinstance()、issubclass壳快、iter()纸巷、len()、list()眶痰、locals()瘤旨。
Python內置函數(shù)(5)— map()、max()竖伯、memoryview()存哲、min()、next()七婴、object()祟偷、oct()、open()打厘、ord()修肠、pow()。
Python內置函數(shù)(6)— print()户盯、property()嵌施、range()、repr()莽鸭、reversed()艰管、round()、set()蒋川、setattr()牲芋、slice()、sorted()。
Python內置函數(shù)(7)— staticmethod()缸浦、str()夕冲、sum()、super()裂逐、tuple()歹鱼、type()、vars()卜高、zip()弥姻、__import__()抬纸。
31吕粹、hex()
a)描述
原文:
Return the hexadecimal representation of an integer.
中文:
返回一個整數(shù)的十六進制表示形式。
b)語法
hex 語法:hex(x)
c)參數(shù)
x:一個整數(shù)
d)返回值
返回一個字符串兰迫,以 0x 開頭薪缆。
e)實例
print("hex(255):",hex(255))
print("hex(-42):",hex(-42))
print("hex(12):",hex(12))
print("type(hex(12)):",type(hex(12)))
運行結果:
hex(255): 0xff
hex(-42): -0x2a
hex(12): 0xc
type(hex(12)): <class 'str'> # 字符串
32秧廉、id()
a)描述
原文:
Return the identity of an object.
This is guaranteed to be unique among simultaneously existing objects.(CPython uses the object's memory address.)
中文:
返回對象的標識。
這保證是唯一的同時存在的對象拣帽。(CPython使用對象的內存地址) 疼电。
詮釋:
id() 函數(shù)用于獲取對象的內存地址。
b)語法
id 語法:id([object])
c)參數(shù)
object:對象减拭。
d)返回值
返回對象的內存地址蔽豺。
e)實例
a = 'kevin'
print("id(a):",id(a))
b = 1
print("id(b):",id(b))
運行結果:
id(a): 1740301575472
id(b): 140706674362016
33、input()
a)描述
原文:
Read a string from standard input. The trailing newline is stripped.
The prompt string, if given, is printed to standard output without a trailing newline before reading input.
If the user hits EOF (nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.On nix systems, readline is used if available.
中文:
從標準輸入讀取一個字符串拧粪,尾行換行被剝離茫虽。
提示字符串,如果給定既们,在讀取輸入之前濒析,不帶換行符,打印到標準輸出啥纸。
如果用戶點擊EOF (nix: Ctrl-D, Windows: Ctrl-Z+Return)号杏,則拋出EOFError。在nix系統(tǒng)中斯棒,如果可用盾致,則使用readline。
詮釋:
input() 函數(shù)接受一個標準輸入數(shù)據(jù)荣暮,返回為 string 類型庭惜。
注意:在 Python3.x 中 raw_input() 和 input() 進行了整合,去除了 raw_input( )穗酥,僅保留了input( )函數(shù)护赊,其接收任意輸入惠遏,將所有輸入默認為字符串處理,并返回字符串類型骏啰。
b)語法
input 語法:input([prompt])
c)參數(shù)
prompt:提示信息
d)返回值
返回對應的字符串
e)實例
a = input("please input:")
print("type(a):",type(a))
b = input("please input:")
print("type(b):",type(b))
運行結果:
please input:1228 # 輸入整數(shù)
type(a): <class 'str'> # 字符串
please input:kevin # 輸入字符串
type(b): <class 'str'> # 字符串
34节吮、int()
a)描述
int() 函數(shù)用于將一個字符串或數(shù)字轉換為整型。
b)語法
int() 方法的語法:class int(x, base=10)
c)參數(shù)
x:字符串或數(shù)字判耕。
base:進制數(shù)透绩,默認十進制。
x 有兩種:str / int
1壁熄、若 x 為純數(shù)字帚豪,則不能有 base 參數(shù),否則報錯草丧;其作用為對入?yún)?x 取整狸臣。
print("",int(3.1415926))
print("",int(-11.123))
print("",int(2.5))
print("",int(2.5,10))
運行結果:
3
-11
2
Traceback (most recent call last):
File "D:/Python_Project/Temp.py", line 1251, in <module>
print("",int(2.5,10))
TypeError: int() can't convert non-string with explicit base
2、若 x 為 str方仿,則 base 可略可有。
base 存在時统翩,視 x 為 base 類型數(shù)字仙蚜,并將其轉換為 10 進制數(shù)字。
若 x 不符合 base 規(guī)則厂汗,則報錯委粉。
print('int("9"):',int("9")) # 默認10進制
print('int("1001",2):',int("1001",2)) # "1001"才是2進制格式,并轉化為十進制數(shù)字9
print('int("0xa",16):',int("0xa",16)) # ≥16進制才會允許入?yún)閍,b,c...
print('int("123",8):',int("123",8)) # 視123為8進制數(shù)字娶桦,對應的10進制為83
運行結果:
int("9"): 9
int("1001",2): 9
int("0xa",16): 10
int("123",8): 83
報錯1:
print('int("9",2):',int("9",2)) # 報錯贾节,因為2進制無9
運行結果:
Traceback (most recent call last):
File "D:/Python_Project/Temp.py", line 1254, in <module>
print('int("9",2):',int("9",2)) # 報錯,因為2進制無9
ValueError: invalid literal for int() with base 2: '9'
報錯2:
print('int("3.14",8):',int("3.14",8)) # 報錯衷畦,str須為整數(shù)
運行結果:
Traceback (most recent call last):
File "D:/Python_Project/Temp.py", line 1255, in <module>
print('int("3.14",8):',int("3.14",8)) # 報錯栗涂,str須為整數(shù)
ValueError: invalid literal for int() with base 8: '3.14'
報錯3:
print('int("1.2"):',int("1.2")) # 報錯,str須為整數(shù)
運行結果:
Traceback (most recent call last):
File "D:/Python_Project/Temp.py", line 1256, in <module>
print('int("1.2"):',int("1.2")) # 報錯祈争,str須為整數(shù)
ValueError: invalid literal for int() with base 10: '1.2'
報錯4:
print('int("b",8):',int("b",8)) # 報錯
運行結果:
Traceback (most recent call last):
File "D:/Python_Project/Temp.py", line 1257, in <module>
print('int("b",8):',int("b",8)) # 報錯
ValueError: invalid literal for int() with base 8: 'b'
d)返回值
返回整型數(shù)據(jù)斤程。
e)實例
print("int():",int()) # 不傳入?yún)?shù)時,得到結果0
print("int(3):",int(3))
print("int(3.6):",int(3.6))
print("int('12',16):",int('12',16)) # 如果是帶參數(shù)base的話菩混,12要以字符串的形式進行輸入忿墅,12 為 16進制
print("int('0xa',16):",int('0xa',16))
print("int('10',8):",int('10',8))
運行結果:
int(): 0
int(3): 3
int(3.6): 3
int('12',16): 18
int('0xa',16): 10
int('10',8): 8
35、 isinstance()
a)描述
原文:
Return whether an object is an instance of a class or of a subclass thereof.
A tuple, as in isinstance(x, (A, B, ...))
, may be given as the target to check against. This is equivalent to isinstance(x, A) or isinstance(x, B) or ...
etc.
中文:
返回對象是類的實例還是類的子類的實例沮峡。
一個元組疚脐,如isinstance(x, (A, B邢疙,…)
棍弄,可以作為檢查的目標望薄。這相當于isinstance(x, A)或isinstance(x, B)或…
等等。
詮釋:
isinstance() 函數(shù)來判斷一個對象是否是一個已知的類型照卦,類似 type()式矫。
isinstance() 與 type() 區(qū)別:
type() 不會認為子類是一種父類類型,不考慮繼承關系役耕。
isinstance() 會認為子類是一種父類類型采转,考慮繼承關系。
如果要判斷兩個類型是否相同推薦使用 isinstance()瞬痘。
b)語法
isinstance() 方法的語法:isinstance(object, classinfo)
c)參數(shù)
object:實例對象故慈。
classinfo:可以是直接或間接類名、基本類型或者由它們組成的元組框全,對于基本類型來說 classinfo 可以是:int察绷,float,bool津辩,complex拆撼,str(字符串),list喘沿,dict(字典)闸度,set,tuple蚜印。
d)返回值
如果對象的類型與參數(shù)二的類型(classinfo)相同則返回 True莺禁,否則返回 False。
e)實例
實例1:
a = 2
print("isinstance (a,int):",isinstance (a,int))
print("isinstance (a,str)",isinstance (a,str))
print("isinstance (a,(str,int,list)):",isinstance (a,(str,int,list))) # 是元組中的一個返回 True
運行結果:
isinstance (a,int): True
isinstance (a,str) False
isinstance (a,(str,int,list)): True
實例2(type() 與 isinstance()區(qū)別):
class A:
pass
class B(A):
pass
print("isinstance(A(), A):",isinstance(A(), A))
print("type(A()) == A:",type(A()) == A)
print("isinstance(B(), A):",isinstance(B(), A))
print("type(B()) == A:",type(B()) == A)
運行結果:
isinstance(A(), A): True
type(A()) == A: True
isinstance(B(), A): True
type(B()) == A: False
36窄赋、issubclass()
a)描述
原文:
Return whether 'cls' is a derived from another class or is the same class.
A tuple, as in issubclass(x, (A, B, ...))
, may be given as the target to check against. This is equivalent to issubclass(x, A) or issubclass(x, B) or ...
etc.
中文:
返回“cls”是派生自另一個類還是同一個類哟冬。
A tuple, as in issubclass(x, (A, B忆绰,…))
浩峡,可以作為檢查的目標。這相當于issubclass(x, A)或issubclass(x, B)或…
等等错敢。
詮釋:
issubclass() 方法用于判斷參數(shù) class 是否是類型參數(shù) classinfo 的子類红符。
b)語法
issubclass() 方法的語法:issubclass(class, classinfo)
c)參數(shù)
class:類。
classinfo:類伐债。
d)返回值
如果 class 是 classinfo 的子類返回 True预侯,否則返回 False。
e)實例
class A:
pass
class B(A):
pass
class C:
pass
print("issubclass(B, A):",issubclass(B, A))
print("issubclass(C, A):",issubclass(C, A))
運行結果:
issubclass(B, A): True
issubclass(C, A): False
37峰锁、iter()
a)描述
原文:
iter(iterable) -> iterator
iter(callable, sentinel) -> iterator
Get an iterator from an object. In the first form, the argument must supply its own iterator, or be a sequence.
In the second form, the callable is called until it returns the sentinel.
中文:
iter(iterable) ->iterator
iter(callable, sentinel) ->iterator
從一個對象獲得一個迭代器萎馅。在第一種形式中,參數(shù)必須提供自己的迭代器虹蒋,或者是一個序列糜芳。
在第二種形式中飒货,可調用項被調用,直到它返回標記峭竣。
b)語法
iter() 方法的語法:iter(object[, sentinel])
c)參數(shù)
object:支持迭代的集合對象塘辅。
sentinel:如果傳遞了第二個參數(shù),則參數(shù) object 必須是一個可調用的對象(如皆撩,函數(shù))扣墩,此時,iter 創(chuàng)建了一個迭代器對象扛吞,每次調用這個迭代器對象的next()方法時呻惕,都會調用 object。
d)返回值
迭代器對象滥比。
e)實例
lst = [1, 2, 3]
for i in iter(lst):
print(i)
運行結果:
1
2
3
38亚脆、len()
a)描述
原文:
Return the number of items in a container.
中文:
返回容器中元素的個數(shù)。
詮釋:
len() 返回對象(字符盲泛、列表濒持、元組等)長度或項目個數(shù)。實參可以是序列(如 string寺滚、bytes柑营、tuple、list 或 range 等)或集合(如 dictionary玛迄、set 或 frozen set 等)由境。len()不是字符串類的方法棚亩。
b)語法
len()方法語法:len( s )
c)參數(shù)
s:對象蓖议。
d)返回值
返回對象長度
e)實例
str = "kevin"
print("len(str):",len(str)) # 字符串長度
l = [1,2,3,4]
print("len(l):",len(l))
運行結果:
len(str): 5
len(l): 4
39、list()
a)描述
list() 方法用于將元組或字符串轉換為列表讥蟆。
注:元組與列表是非常類似的勒虾,區(qū)別在于元組的元素值不能修改,元組是放在括號中瘸彤,列表是放于方括號中修然。
b)語法
list()方法語法:list(seq)
c)參數(shù)
seq:要轉換為列表的元組或字符串。
d)返回值
返回列表质况。
e)實例
aTuple = (123, 'Google', 'Kevin', 'Taobao')
list1 = list(aTuple)
print ("list1列表元素 : ", list1)
str="Hello World"
list2=list(str)
print ("list2列表元素 : ", list2)
運行結果:
list1列表元素 : [123, 'Google', 'Kevin', 'Taobao']
list2列表元素 : ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
40愕宋、locals()
a)描述
原文:
Return a dictionary containing the current scope's local variables.
NOTE: Whether or not updates to this dictionary will affect name lookups in the local scope and vice-versa is implementation dependent and not covered by any backwards compatibility guarantees.
中文:
返回一個包含當前作用域的局部變量的字典。
注意:對這個字典的更新是否會影響本地范圍內的名稱查找结榄,反之亦然中贝,這取決于實現(xiàn),并且沒有任何向后兼容性保證臼朗。
詮釋:
locals() 函數(shù)會以字典類型返回當前位置的全部局部變量邻寿。
對于函數(shù), 方法, lambda 函式, 類, 以及實現(xiàn)了 call 方法的類實例, 它都返回 True蝎土。
b)語法
locals() 函數(shù)語法:locals()
c)參數(shù)
無
d)返回值
返回字典類型的局部變量。
e)實例
def kevin(arg): # 兩個局部變量:arg绣否、z
z = 1
print(locals())
kevin(4)
運行結果:
{'arg': 4, 'z': 1}