14.python函数

原文:https://www.cnblogs.com/linhaifeng/articles/6113086.html

1.python中函数定义:函数是逻辑结构化和过程化的一种编程方法。

python中函数定义方法:
def test(x):
    "The function definitions"
    x+=1
    return x
     
def:定义函数的关键字
test:函数名
():内可定义形参
"":文档描述(非必要,但是强烈建议为你的函数添加描述信息)
x+=1:泛指代码块或程序处理逻辑
return:定义返回值


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

2.为什么使用函数:

  1.代码重用

  2.保持一致性,易维护

  3.可扩展性

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

当一个函数/过程没有使用return显示的定义返回值时,python解释器会隐式的返回None,

所以在python中即便是过程也可以算作函数。

总结:

   返回值数=0:返回None

   返回值数=1:返回object

   返回值数>1:返回tuple

#函数
'''
y=2*x+1
x=3
y->7
x=3
y->7
'''
# def test(x):
#     '''
#     2*x+1
#     :param x:整形数字
#     :return: 返回计算结果
#     '''
#     y=2*x+1
#     return y
#
# def test():
#     '''
#     2*x+1
#     :param x:整形数字
#     :return: 返回计算结果
#     '''
#     x=3
#     y=2*x+1
#     return y
# a=test()
# print(a)
#函数返回值
# def test01():
#     msg = 'test01'
#     print(msg)
#
#
# def test02():
#     msg = 'test02'
#     print(msg)
#     return msg
#
# def test03():
#     msg = 'test03'
#     print(msg)
#     return 1,2,3,4,'a',['alex'],{'name':'alex'},None
#
# def test04():
#     msg = 'test03'
#     print(msg)
#     return {'name':'alex'}
# t1=test01()
# t2=test02()
# t3=test03()
# t4=test04()
# print(t1) #None
# print(t2) #print(t2)
# print(t3) #(1, 2, 3, 4, 'a', ['alex'], {'name': 'alex'}, None)
# print(t4) #{'name': 'alex'}

4.函数参数

  

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

#位置参数,必须一一对应,缺一不行多一也不行
# test(1,2,3)

#关键字参数,无须一一对应,缺一不行多一也不行
# test(y=1,x=3,z=4)

#位置参数必须在关键字参数左边
# test(1,y=2,3)#报错
# test(1,3,y=2)#报错
# test(1,3,z=2)
# test(1,3,z=2,y=4)#报错
# test(z=2,1,3)#报错
#参数组:**字典 *列表
def test(x,*args):
    print(x)
    print(args)


# test(1)
# test(1,2,3,4,5)
# test(1,{'name':'alex'})
# test(1,['x','y','z'])
# test(1,*['x','y','z'])
# test(1,*('x','y','z'))
# def calc(x,y): #x=2,y=3
#     res=x**y
#     return x
#     return y
# res=calc(2,3)
# # print(x)
# # print(y)
# print(res)
# # a=10
# # b=10
# # calc(a,b)

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

#位置参数,必须一一对应,缺一不行多一也不行
# test(1,2,3)

#关键字参数,无须一一对应,缺一不行多一也不行
# test(y=1,x=3,z=4)

#位置参数必须在关键字参数左边
# test(1,y=2,3)#报错
# test(1,3,y=2)#报错
# test(1,3,z=2)
# test(1,3,z=2,y=4)#报错
# test(z=2,1,3)#报错

# def handle(x,type='mysql'):
#     print(x)
#     print(type)
# handle('hello')
# handle('hello',type='sqlite')
# handle('hello','sqlite')

# def install(func1=False,func2=True,func3=True):
#     pass

#参数组 *列表:**字典
# def test(x,*args):
#     print(x)
#     print(args)

# test(1)
# test(1,2,3,4,5)
# test(1,{'name':'alex'})
# test(1,['x','y','z'])
# test(1,*['x','y','z'])
# test(1,*('x','y','z'))

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

# def test(x,*args,**kwargs):
#     print(x)
#     print(args,args[-1])
#     print(kwargs,kwargs.get('y'))
# test(1,1,2,1,1,11,1,x=1,y=2,z=3) #报错
# test(1,1,2,1,1,11,1,y=2,z=3)
# test(1,*[1,2,3],**{'y':1})

  1.形参变量只有在被调用时才分配内存单元,在调用结束时,即刻释放所分配的内存单元。因此,形参只在函数内部有效。函数调用结束返回主调用函数后则不能再使用该形参变量

  2.实参可以是常量、变量、表达式、函数等,无论实参是何种类型的量,在进行函数调用时,它们都必须有确定的值,以便把这些值传送给形参。因此应预先用赋值,输入等办法使参数获得确定值

  3.位置参数和关键字(标准调用:实参与形参位置一一对应;关键字调用:位置无需固定)

  4.默认参数

  5.参数组

5.变量

变量有局部变量和全局变量,在子程序中定义的变量称为局部变量,在程序的一开始定义的变量称为全局变量。

全局变量作用域是整个程序,局部变量作用域是定义该变量的子程序。
当全局变量与局部变量同名时:
在定义局部变量的子程序内,局部变量起作用;在其它地方全局变量起作用。
原文地址:https://www.cnblogs.com/raitorei/p/11669281.html