成都创新互联网站制作重庆分公司

python基础知识(四)函数

函数:

南县网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设等网站项目制作,到程序开发,运营维护。创新互联成立于2013年到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。

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))

当前名称:python基础知识(四)函数
当前链接:http://cxhlcq.com/article/ghgioe.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部