最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python高階函數(shù)、常用內(nèi)置函數(shù)用法實(shí)例分析

 更新時間:2019年12月26日 10:22:27   作者:隨風(fēng)行云  
這篇文章主要介紹了Python高階函數(shù)、常用內(nèi)置函數(shù)用法,結(jié)合實(shí)例形式分析了Python高階函數(shù)與常用內(nèi)置函數(shù)相關(guān)功能、原理、使用技巧與操作注意事項,需要的朋友可以參考下

本文實(shí)例講述了Python高階函數(shù)、常用內(nèi)置函數(shù)用法。分享給大家供大家參考,具體如下:

高階函數(shù):

  • 允許將函數(shù)作為參數(shù)傳入另一個函數(shù);
  • 允許返回一個函數(shù)。
#返回值為函數(shù)的函數(shù)
sum=lambda x,y:x+y
sub=lambda x,y:x-y
calc_dict={"+":sum,"-":sub}
def calc(x):
  return calc_dict[x]

print(calc('-')(5,6))
print(calc('+')(5,6))

#參數(shù)有函數(shù)的函數(shù)
filter(lambda x:x>5,range(20))

常用內(nèi)置函數(shù):

  • abs(x):求絕對值
  • range([start], stop[, step]) :產(chǎn)生一個序列,默認(rèn)從0開始
    • 注意:返回的不是一個list對象
>>> print(range(20))
range(0, 20)
>>> type(range(20))
<class 'range'>
>>> isinstance(range(20),Iterable)#########是一個可迭代對象
True
>>> from collections import Iterator
>>> isinstance(range(20),Iterator)#不是一個迭代器對象
False
  • oct(x)
    將一個數(shù)字轉(zhuǎn)化為8進(jìn)制
  • hex(x)
    將整數(shù)x轉(zhuǎn)換為16進(jìn)制字符串
  • bin(x)
    將整數(shù)x轉(zhuǎn)換為二進(jìn)制字符串
>>> oct(8)
'0o10'
>>> hex(8)
'0x8'
>>> bin(8)
'0b1000'
  • chr(i):返回整數(shù)i對應(yīng)的Unicode字符
  • ord(x):將字符轉(zhuǎn)換成對應(yīng)的Unicode編址
>>> ord('中')
20013
>>> chr(20013)
'中'
  • enumerate(sequence [, start = 0]):返回一個可枚舉的對象,該對象的next()方法將返回一個tuple
for i, value in enumerate(['A', 'B', 'C']):
  print(i, value)
  • iter(o[, sentinel])  :生成一個對象的迭代器,第二個參數(shù)表示分隔符
from collections import Iterator
#可以被next()函數(shù)調(diào)用并不斷返回下一個值的對象稱為迭代器:Iterator。
print(isinstance([],Iterator))
print(isinstance(iter([]),Iterator))
  • sorted(iterable[, cmp[, key[, reverse]]])  對可迭代對象進(jìn)行排序
>>> l=[8,7,6,5,4,3,2,1]
>>> sorted(l)
[1, 2, 3, 4, 5, 6, 7, 8]
  • cmp(x, y)  :如果x < y ,返回負(fù)數(shù);x == y, 返回0;x > y,返回正數(shù)
  • all(iterable)
    1、可迭代對象中的元素都為真的時候?yàn)檎?
    2、特別的,可迭代對象若為空返回為True
>>> l=[]
>>> all(l)
True
>>> l=[1,2,3,4,5]
>>> all(l)
True
>>> l=[1,2,3,4,5,0]
>>> all(l)
False
  • any(iterable)
    1、可迭代對象中的元素有一個為真的時候?yàn)檎?
    2、特別的,可迭代對象若為空返回為False
>>> l=[]
>>> any(l)
False
>>> l=[0,0,0,0]
>>> any(l)
False
>>> l=[0,0,0,0,5]
>>> any(l)
True
>>>
  • eval(expression [, globals [, locals]])  :計算表達(dá)式expression的值
>>> str1="3+4"
>>> eval(str1)
7
  • exec(object[, globals[, locals]]):執(zhí)行儲存在字符串或文件中的 Python 語句
>>> str1="print('hello world')"
>>> exec(str1)
hello world
  • compile(source, filename, mode[, flags[, dont_inherit]])
    • 將source編譯為代碼或者AST對象。代碼對象能夠通過exec語句來執(zhí)行或者eval()進(jìn)行求值。
      1、參數(shù)source:字符串或者AST(Abstract Syntax Trees)對象。
      2、參數(shù) filename:代碼文件名稱,如果不是從文件讀取代碼則傳遞一些可辨認(rèn)的值。
      3、參數(shù)model:指定編譯代碼的種類??梢灾付?‘exec','eval','single'。
      4、參數(shù)flag和dont_inherit:這兩個參數(shù)暫不介紹
str1 = "print('hello world')"
c2 = compile(str1,'','exec')  
exec(c2)

str2="3+4"
c3=compile(str2,'','eval')
a=eval(c3)
print(a)
  • id(object)  :函數(shù)用于獲取對象的內(nèi)存地址
>>> id(str1)
1514678732384
>>> str2=str1
>>> id(str2)
1514678732384
  • isinstance(object, classinfo):判斷object是否是class的實(shí)例
>>> isinstance(1,int)
True
>>> isinstance(1.0,int)
False
  • len(s)  :返回長度(ascll格式的返回字節(jié)數(shù),unicode返回字符數(shù)/或元素個數(shù))
>>> a=b'abc'
>>> len(a)
3
>>> b="我愛中國"
>>> len(b)
4
>>> c=[1,2,3,4]
>>> len(c)
4
  • repr(object)  :將對象轉(zhuǎn)化為供解釋器讀取的形式,實(shí)質(zhì)是返回一個對象的 string 格式
>>> c=[1,2,3,4]
>>> repr(c)
'[1, 2, 3, 4]'
>>> d={1:2,2:3,3:4}
>>> repr(d)
'{1: 2, 2: 3, 3: 4}'
  • type(object)  :返回該object的類型
>>> type(1)
<class 'int'>
>>> type("123")
<class 'str'>
>>> type((1,2,3))
<class 'tuple'>

關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python函數(shù)使用技巧總結(jié)》、《Python面向?qū)ο蟪绦蛟O(shè)計入門與進(jìn)階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進(jìn)階經(jīng)典教程

希望本文所述對大家Python程序設(shè)計有所幫助。

相關(guān)文章

最新評論

阳谷县| 宜春市| 易门县| 安岳县| 迭部县| 上林县| 江口县| 从江县| 平阴县| 义马市| 佛教| 潍坊市| 镇沅| 凤冈县| 韶关市| 宜兰县| 巴彦县| 大港区| 聂拉木县| 张家川| 久治县| 新晃| 谷城县| 台湾省| 田东县| 新营市| 宜都市| 惠水县| 连云港市| 台北县| 晋江市| 周至县| 南和县| 张家口市| 西丰县| 甘泉县| 即墨市| 洛川县| 南陵县| 尼勒克县| 崇文区|