本文實例講述了python3.5常見內置方法參數用法。分享給大家供大家參考,具體如下:
python的內置方法參數詳解網站為:https://docs.python.org/3/library/functions.html?highlight=built#ascii
1、abs(x):返回一個數字的絕對值。參數可以是整數或浮點數。如果參數是一個復數,則返回它的大小。
1
2
3
4
|
#內置函數abs() print ( abs ( - 2 )) print ( abs ( 4.5 )) print ( abs ( 0.1 + 7j )) |
運行結果:
2
4.5
7.000714249274855
2、all(iterable):如果可迭代的對象的元素全部為真(即:非零)或可迭代對象為空,返回true,否則返回false
1
2
3
4
|
#內置函數all() print ( all ([ - 1 , 0 , 7.5 ])) print ( all ([ 9 , - 1.6 , 12 ])) print ( all ([])) |
運行結果:
false
true
true
3、any(iterable):如果可迭代的對象的元素中有一個為真(即:非零),返回true,可迭代對象的元素全部為零(全部為假)或者可迭代對象為空時則返回false。
1
2
3
4
|
#內置函數any() print ( any ([ - 1 , 0 , 7.5 ])) print ( any ([ 0 , 0 , 0 ])) print ( any ([])) |
運行結果:
true
false
false
4、ascii(object):將內存對象變成可打印的字符串的形式。
1
2
3
|
#內置函數ascii(object) a = ascii([ 1 , 2 , '你好' ]) print ( type (a),[a]) |
運行結果:
<class 'str'> ["[1, 2, '\\u4f60\\u597d']"]
5、bin(x):將十進制整數轉換成二進制
1
2
3
4
5
|
#內置函數bin() print ( bin ( 0 )) print ( bin ( 2 )) print ( bin ( 8 )) print ( bin ( 255 )) |
運行結果:
0b0
0b10
0b1000
0b11111111
6、bool([x]):返回一個bool值,0:返回false,非0:返回true;空列表:返回false
1
2
3
4
5
|
#內置函數bool() print ( bool ( 0 )) print ( bool ( 1 )) print ( bool ([])) print ( bool ([ 3 ])) |
運行結果:
false
true
false
true
7、bytearray():返回一個新的字節數組,可修改的二進制字節格式。
1
2
3
4
5
6
7
8
|
#內置函數bytearray() a = bytes( "abcde" ,encoding = 'utf-8' ) print (a) b = bytearray( "abcde" ,encoding = 'utf-8' ) print (b) b[ 1 ] = 100 print (b) |
運行結果:
b'abcde'
bytearray(b'abcde')
bytearray(b'adcde')
8、callable(object):判斷是否可調用(函數和類可以調用),列表等不可調用
1
2
3
4
5
|
#內置函數callable def nice(): pass print ( callable (nice)) print ( callable ([])) |
運行結果:
true
false
9、chr(i):返回數字對應的ascii碼對應表;相反地,ord():返回ascii碼對應的數字
1
2
3
|
#內置函數chr()與ord() print ( chr ( 98 )) print ( ord ( 'c' )) |
運行結果:
b
99
10、compile():將字符串編譯成可執行的代碼
1
2
3
4
|
#內置函數compile code = "for i in range(10):print(i)" print ( compile (code,' ',' exec ')) exec (code) |
運行結果:
<code object <module> at 0x008bf700, file "", line 1>
0
1
2
3
4
5
6
7
8
9
11、dir():可以查方法
1
2
3
|
#內置函數dir s = [] print ( dir (s)) |
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__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']
12、divmod(a,b):返回商和余數
1
2
3
|
#內置函數divmod() print ( divmod ( 5 , 3 )) print ( divmod ( 8 , 9 )) |
運行結果:
(1, 2)
(0, 8)
13、enumerate():是枚舉、列舉的意思。
對于一個可迭代的(iterable)/可遍歷的對象(如列表、字符串),enumerate將其組成一個索引序列,
利用它可以同時獲得索引和值;enumerate多用于在for循環中得到計數。
1
2
3
4
|
#內置函數enumerate list = [ '歡' , '迎' , '你' ] for index,item in enumerate ( list ): print (index,item) |
運行結果:
0 歡
1 迎
2 你
13、eval():將字符串str當成有效的表達式來求值并返回計算結果。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#內置函數eval() #字符串轉換成列表 a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]" print ( type (a)) b = eval (a) print (b) print ( type (b)) #字符串轉換成字典 a = "{1: 'a', 2: 'b'}" print ( type (a)) b = eval (a) print (b) print ( type (b)) #字符串轉換成元組 a = "([1,2], [3,4], [5,6], [7,8], (9,0))" print ( type (a)) b = eval (a) print (b) print ( type (b)) |
運行結果:
<class 'str'>
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]
<class 'list'>
<class 'str'>
{1: 'a', 2: 'b'}
<class 'dict'>
<class 'str'>
([1, 2], [3, 4], [5, 6], [7, 8], (9, 0))
<class 'tuple'>
14、filter(function,iterable):過濾序列。
匿名函數用完釋放,不重復使用。
1
2
3
4
5
6
|
#匿名函數 calc = lambda n: print (n) calc( 3 ) res = filter ( lambda n:n> 5 , range ( 10 )) for i in res: print (i) |
運行結果:
3
6
7
8
9
15、map():可以把一個 list 轉換為另一個 list,只需要傳入轉換函數.
1
2
3
|
res = map ( lambda n:n * n, range ( 5 )) #等價于列表生成式[lambda i:i*i for i in range(5)] for i in res: print (i) |
運行結果:
0
1
4
9
16
16、reduce():python 3.0.0.0以后, reduce已經不在built-in function里了, 要用它就得from functools import reduce
.
它可以通過傳給reduce中的函數(必須是二元函數)依次對數據集中的數據進行操作。
凡是要對一個集合進行操作的,并且要有一個統計結果的,能夠用循環或者遞歸方式解決的問題,一般情況下都可以用reduce方式實現。
1
2
3
4
5
|
from functools import reduce res = reduce ( lambda x,y:x + y, range ( 10 )) #求和 res1 = reduce ( lambda x,y:x * y, range ( 1 , 10 )) #階乘 print (res) print (res1) |
運行結果:
45
362880
17、globals():返回的是全局變量的字典,修改其中的內容,值會真正的發生改變。
locals():會以dict類型返回當前位置的全部局部變量。
1
2
3
4
|
def test(): loc_var = 234 print ( locals ()) test() |
運行結果:
{'loc_var': 234}
18、hash():函數返回對象的哈希值。返回的哈希值是使用一個整數表示,通常使用在字典里,以便實現快速查詢鍵值。
1
2
3
4
|
print ( hash ( 'liu' )) print ( hash ( "liu" )) print ( hash ( 'al' )) print ( hash ( 3 )) |
運行結果:
-1221260751
-1221260751
993930640
3
19、hex(x):將一個數字轉換成十六進制
oct(x):將一個數字轉換成八進制
1
2
|
print ( hex ( 15 )) print ( hex ( 32 )) |
運行結果:
0xf
0x20
1
2
3
|
print ( oct ( 8 )) print ( oct ( 16 )) print ( oct ( 31 )) |
運行結果:
0o10
0o20
0o37
20、round():返回浮點數x的四舍五入值
1
|
print ( round ( 1.3457 , 3 )) |
運行結果:
1.346
21、sorted():排序
1
2
3
|
a = { 6 : 2 , 8 : 0 , 1 : 4 , - 5 : 6 , 99 : 11 , 4 : 22 } print ( sorted (a.items())) #按照鍵排序 print ( sorted (a.items(),key = lambda x:x[ 1 ])) #按照鍵值排序 |
運行結果:
[(-5, 6), (1, 4), (4, 22), (6, 2), (8, 0), (99, 11)]
[(8, 0), (6, 2), (1, 4), (-5, 6), (99, 11), (4, 22)]
22、zip():接受任意多個(包括0個和1個)序列作為參數,返回一個tuple列表。
1
2
3
4
|
a = [ 1 , 2 , 3 , 4 ] b = [ 'a' , 'b' , 'c' , 'd' ] for i in zip (a,b): print (i) |
運行結果:
(1, 'a')
(2, 'b')
(3, 'c')
(4, 'd')
23、__import__('decorator')等價于import decorator
希望本文所述對大家python程序設計有所幫助。
原文鏈接:https://blog.csdn.net/loveliuzz/article/details/78087244