python六函数

mac2022-06-30  32

# #!/usr/bin/env python# #-*- coding:utf-8 -*-# def calc(x,y):# res=x**y# return res# a=3# b=3# v=calc(a,b)# print(v)# #形参x,y,实参a,b,函数里只能有一个return# def test(x,y,z):# print(x)# print(y)# print(z)## test(1,2,3)#过程没有return,函数有return#test(1,2,3)里面的1,2,3就是位置参数,按照位置关系,分别传给上面的x,y,z三个值,# def test(x,y,z):# print(x)# print(y)# print(z)## test(z=2,y=3,x=4)# 以上就是关键词参数,test(z=2,y=3,x=4)里的x,y,z分别对应函数的 xyz# #位置参数和关键字参数可以混搭,但是位置参数必须在关键字参数的左边,但是位置比较也是一一对应。# def test(x,y,z):# print(x)# print(y)# print(z)## test(1,3,z=4)# def handle(x,tpye1='mysql'):# print(x)# print(tpye1)# handle('hello')#默认参数,mysql就是默认参数,当没有给出时就会采用默认参数,如果给出了就不采用了默认参数# def handle(x,tpye1='mysql'):# # print(x)# # print(tpye1)# # handle('hello','sqllite')#参数组: **字典 *列表# def test(x,*args):# print(x)# print(args)# print(args[0:3])## #test(1,2,3,4,5,6)## #test(1,*["name","ales","age",18])# #test(1,*("name","ales","age",18))# test(1,*{"name":"ales","age":18})#位置参数只能用列表*args#关键字参数必须用**kwargs# def test(a,**kwargs):# print(a)# print(kwargs)# test(1,z=2,y=3)#*args和**kwargs混合使用时,*args必须在**kwargs的左边。# def test(a,*args,**kwargs):# print(a)# print(args,args[-1])# print(kwargs)# test(1,6,7,8,9,0,z=2,y=3)# def test(a,*args,**kwargs):# print(a)# print(args,args[-1])# print(kwargs,kwargs.get('y'))# test(1,*[4,5,6,7],**{'z':2,'y':3})

转载于:https://www.cnblogs.com/pjzc/p/11582635.html

最新回复(0)