python基础--函数

#python中函数的定义:函数是逻辑结构化和过程化的一种编程方法,用来实现某些特定的功能.
#python中函数简单定义的方法
# def test(x):
# "this is new function"
# x+=1
# return x

#def:定义函数的关键字
#test函数名
#():括号内可定义形参
#""文档描述(非必要,建议加上)
#x+=1泛指代码块
#return 定义返回值

#调用运行:可以带参数也可以不带,函数名()

#print(test(1))

#过程定义:就是简单特殊没有返回值的函数


# def test02():
# msg="test02"
# print(msg)
# return 1,2,3,4,"a",[1,2],{"name":"tang"},(5,6,7)
#
# dom=test02()
# print(dom[2])
#总结:
#返回值=0 返回None
#返回值=1 返回object
#返回值>1 返回tuple


#函数参数
# def calc(x,y):#形参函数调用是赋值,函数结束就释放内存
# # z=x**y
# # return z
# #
# #
# # calc(2,3)#实参



#位置参数,必须一一对应,关键字参数:位置无序固定,有多个形参,就必须有多少个实参

def rest(x,y,z):
print(x)
print(y)
print(z)
# rest(1,2,3)
#
# rest(y=2,x=3,z=1)

#位置参数必须在关键字参数左边
#rest(1,2,z=3)


# def handle(x,type=None):
# print(x)
# print(type)
# handle("hello",type="select")#关键字参数
# handle("hello","sqlite")#位置参数


#参数组: **字典 *元祖
def test(x,*args):#多的实参会被当做元祖放入*args
print(x)
print(args)
#print(args[0])

#test(1,2,3,4,5,6)
#test(1,*{"name":"alex",'hh':"dhhd"},{'aa':'wss'})
# test(1,*['x','y','z'])
# test(1,*("x","y","z"))
#test(1)


# test(1,{"name":"alex"})

# def test(x,**kwargs):
# print(x)
# print(kwargs)
# test(x=1,y=2,j=3)#会报错:一个参数不能传两个值


def test(x,*args,**kwargs):
print(x,args,kwargs)

test(1,*[1,2,3],**{'q':1})
如果我失败了,至少我尝试过,不会因为痛失机会而后悔
原文地址:https://www.cnblogs.com/tangcode/p/10971315.html