python基础操作_方法(函数)

#函数,方法
#普通方法
def hello():
print('hello')
hello()
#带形参的方法
def hello1(name):
print('hello%s'%name)
hello1('布拉德皮特')
#多个参数的方法
def hello2(name1,name2):
print('hello%s和%s'%(name1,name2))
hello2('布拉德皮特','dirk')
#带默认值参数的方法
def hello3(name1,name2,name3='法拉利'):
print('hello%s%s%s'%(name1,name2,name3))
hello3('皮特','dirk',)
#可变参数的方法
def hello4(*args):
print(args)
hello4(1,2,3,4)
#关键字参数,key v 接受进来是一个字典
def hello5 (**kwargs):
print(kwargs)
hello5(name='qiao',age=18)
a={'name':'q','age':12}
hello5(**a)

#关键字传值
#关键字调用参数不用排序。因为指定了关键字
def hello6(name,age):
print(name,age)
hello6(age=1,name='qiao')
#函数 方法的返回值
def plus(num1,num2):
return num1,num2,num1+num2#遇到return立即结束,后面的代码不走了
print(plus(10,2))
a=plus(12,3)
print(a[2])#返回值是个元组
#函数没有返回值时,返回的是None
#传什么类型,返回什么类型

#局部变量,全局变量
#如果在函数里修改全局变量,需要用先声明,用global声明
原文地址:https://www.cnblogs.com/xiaoshidi/p/6979689.html