Python內置函數(shù)(4)

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__()抬纸。

內置函數(shù)(原文)
abs() delattr() hash() memoryview() set()
all() dict() help() min() setattr()
any() dir() hex() next() slice()
ascii() divmod() id() object() sorted()
bin() enumerate() input() oct() staticmethod()
bool() eval() int() open() str()
breakpoint() 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()
內置函數(shù)(中文)
abs() delattr() hash() memoryview() set()
all() dict() help() min() setattr()
any() dir() hex() next() slice()
ascii() divmod() id() object() sorted()
bin() enumerate() input() oct() staticmethod()
bool() eval() int() open() str()
breakpoint() 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()
Python內置函數(shù).png

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}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末誊涯,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子蒜撮,更是在濱河造成了極大的恐慌暴构,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,599評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件淀弹,死亡現(xiàn)場離奇詭異丹壕,居然都是意外死亡,警方通過查閱死者的電腦和手機薇溃,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,629評論 3 385
  • 文/潘曉璐 我一進店門菌赖,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人沐序,你說我怎么就攤上這事琉用。” “怎么了策幼?”我有些...
    開封第一講書人閱讀 158,084評論 0 348
  • 文/不壞的土叔 我叫張陵邑时,是天一觀的道長。 經常有香客問我特姐,道長晶丘,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,708評論 1 284
  • 正文 為了忘掉前任唐含,我火速辦了婚禮浅浮,結果婚禮上,老公的妹妹穿的比我還像新娘捷枯。我一直安慰自己滚秩,他們只是感情好,可當我...
    茶點故事閱讀 65,813評論 6 386
  • 文/花漫 我一把揭開白布淮捆。 她就那樣靜靜地躺著郁油,像睡著了一般。 火紅的嫁衣襯著肌膚如雪攀痊。 梳的紋絲不亂的頭發(fā)上桐腌,一...
    開封第一講書人閱讀 50,021評論 1 291
  • 那天,我揣著相機與錄音苟径,去河邊找鬼案站。 笑死,一個胖子當著我的面吹牛涩笤,可吹牛的內容都是我干的嚼吞。 我是一名探鬼主播盒件,決...
    沈念sama閱讀 39,120評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼舱禽!你這毒婦竟也來了炒刁?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 37,866評論 0 268
  • 序言:老撾萬榮一對情侶失蹤誊稚,失蹤者是張志新(化名)和其女友劉穎翔始,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體里伯,經...
    沈念sama閱讀 44,308評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡城瞎,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,633評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了疾瓮。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片脖镀。...
    茶點故事閱讀 38,768評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖狼电,靈堂內的尸體忽然破棺而出蜒灰,到底是詐尸還是另有隱情,我是刑警寧澤肩碟,帶...
    沈念sama閱讀 34,461評論 4 333
  • 正文 年R本政府宣布强窖,位于F島的核電站,受9級特大地震影響削祈,放射性物質發(fā)生泄漏翅溺。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 40,094評論 3 317
  • 文/蒙蒙 一髓抑、第九天 我趴在偏房一處隱蔽的房頂上張望咙崎。 院中可真熱鬧,春花似錦启昧、人聲如沸叙凡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,850評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至跛璧,卻和暖如春严里,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背追城。 一陣腳步聲響...
    開封第一講書人閱讀 32,082評論 1 267
  • 我被黑心中介騙來泰國打工刹碾, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人座柱。 一個月前我還...
    沈念sama閱讀 46,571評論 2 362
  • 正文 我出身青樓迷帜,卻偏偏與公主長得像物舒,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子戏锹,可洞房花燭夜當晚...
    茶點故事閱讀 43,666評論 2 350