您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
函数:
def test(x): ''' 计算数值结果 :param x: 输入值int :return: 返回值 ''' y = 2*x +1 return y #参数组: **字典 * 列表 def test(x,*args): print(x) print(args) test(1,2,3,4,5) #全局变量变量名全部大写 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) #匿名函数 #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)) #把函数当作参数传给另一个函数 def foo(n): print(n) def bar(name): print("my name is %s" %name) foo(bar("rain")) #map函数 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("内置函数map,处理结果",res) print(list(res)) print("有名函数",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函数 print(list(filter(lambda n:n.startswith("sb"),movic_people))) #reduce函数 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)#分页: 10: 总数 3:每页数量 express='1+2*(3/3-1)-2' v=eval(express) print(v) #可hash的数据类型即不可变数据类型,不可hash的数据类型即可变数据类型 name = "vivid" print(hash(name))
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。