您好,登錄后才能下訂單哦!
函數(shù):
def test(x): ''' 計(jì)算數(shù)值結(jié)果 :param x: 輸入值int :return: 返回值 ''' y = 2*x +1 return y #參數(shù)組: **字典 * 列表 def test(x,*args): print(x) print(args) test(1,2,3,4,5) #全局變量變量名全部大寫(xiě) name="vivid" #全局變量 def change_name(): #global name #修改為全局變量 name="rain" #局部變量 print("chang_name",name) change_name() #遞歸 def calc(n): print(n) time.sleep(1) if int(n/2)==0: return n return calc(int(n/2)) calc(10) #匿名函數(shù) #lambda x:x+1 def calc(x): return x+1 res=calc(10) func = lambda x:x+1 res1=func(10) print("res1",res1) name = "rain" def chang_name(x): res = name+"_sb" return res res = chang_name(name) #print(res) fname=lambda x:name+"_bow" print(fname(name)) #把函數(shù)當(dāng)作參數(shù)傳給另一個(gè)函數(shù) def foo(n): print(n) def bar(name): print("my name is %s" %name) foo(bar("rain")) #map函數(shù) def add_one(x): return x+1 def reduce_one(x): return x-1 def map_test(func,array): ret=[] for i in array: cc=func(i) ret.append(cc) print(ret) return ret map_test(add_one,num_l) res = map(lambda x:x+1,num_l) print("內(nèi)置函數(shù)map,處理結(jié)果",res) print(list(res)) print("有名函數(shù)",list(map(reduce_one,num_l))) movic_people=["alex","sb_11","sb_22","sb_33","hello","sb_344"] ret = [] # def sb_show(n): # return n.startswith("sb") # def sb_start(n): # return n.endswith("sb") # def filter_test(func,array): # ret = [] # for p in array: # if not func(p): # ret.append(p) # return ret #二 #lambda n:n.startswith("sb") #res = filter_test(lambda n:n.startswith("sb"),movic_people) #print(res) #filter函數(shù) print(list(filter(lambda n:n.startswith("sb"),movic_people))) #reduce函數(shù) from functools import reduce res=0 num1=[1,2,3,4,5,6,100] # for num in num1: # res+=num # print(res) # def reduce_test(array): # res=0 # for num in array: # res+=num # return res # print(reduce_test(num1)) reduce(lambda x,y:x+y,num1,1) divmod(10,3)#分頁(yè): 10: 總數(shù) 3:每頁(yè)數(shù)量 express='1+2*(3/3-1)-2' v=eval(express) print(v) #可hash的數(shù)據(jù)類(lèi)型即不可變數(shù)據(jù)類(lèi)型,不可hash的數(shù)據(jù)類(lèi)型即可變數(shù)據(jù)類(lèi)型 name = "vivid" print(hash(name))
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。