函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。
函数能提高应用的模块性,和代码的重复利用率。Python提供了许多内建函数,比如print()。但也可以自己创建函数,这被叫做用户自定义函数。
一 作用
- 减少重复代码
- 方便修改,更易扩展
- 保持代码一致性
二 语法
def 函数名(): # def是define的缩写,定义的意思 "注释:函数的功能" 执行语句 reture 返回值 函数名() # 函数调用
实例(Python3.0+):
使用函数来输出"Hello World!"
def hello():
print('hello')
hello()
# 运行结果
# hello
三 命名规则
- 函数明必须以下划线或字母开头,可以包含任意字母、数字,下划线自由组合
- 函数区分大小写
- 函数明不能使用保留字
四 参数
- 形参:定义函数时使用的参数是形参
- 实参:调用函数时,传入的参数是实参
五 参数分类
- 位置参数 --> 必须位置一一对应,多一个不行,少一个也不行
- 关键字参数
- 默认参数
- 不定长参数(• 不定长无命名参数 *args --> 元组 • 不定长有命名参数 **kwargs --> 字典)
位置参数
- 形参和实参必须一一对应,多一个不行,少一个也不行
实例(Python3.0+):
def count(name,age,sex):
print(name)
print(age)
print(sex)
count("xiaow",30,"man")
#运行结果:
#xiaow
#30
#man
# 参数对应错误实例
def count(name,age,sex):
print(name)
print(age)
print(sex)
count('xiaow',30,'man','student')
# 运行结果
# TypeError: count() takes 3 positional arguments but 4 were given
关键字参数
- 关键字参数,无须一一对应,缺一不行多一也不行
实例(Python3.0+):
# 参数一一对应
def foo(name,age):
print('my name is %s age is %s' %(name,age))
foo(name='xiaow',age=19)
# 运行结果:
# my name is xiaow age is 19
# 参数不对应
def foo(name,age):
print('my name is %s age is %s' %(name,age))
foo(age=19,name='xiaow')
# 运行结果:
# my name is xiaow age is 19
# 参数缺失
def foo(name,age):
print('my name is %s age is %s' %(name,age))
foo(name='xiaow')
# 运行结果:
# TypeError: foo() missing 1 required positional argument: 'age'
小提示:位置参数必须在关键字参数左边
实例(Python3.0+):
# 位置参数在关键字参数左边
def foo(name,age,gender):
print('my name is %s age is % gender is %s' %(name,age,gender))
foo('xiaow',age=19,gender='girl')
# 运行结果:
# my name is xiaow age is 19ender is girl
# 位置参数在关键字参数右边
def foo(name,age,gender):
print('my name is %s age is % gender is %s' %(name,age,gender))
foo(name='xiaow',19,girl)
# 运行结果:
# SyntaxError: positional argument follows keyword argument(关键字参数必须跟随在位置参数后边)
默认参数
- 调用函数时,如果没有传递参数,则会使用默认参数。
实例(Python3.0+):
# 传递默认参数
def foo(name,age=19):
print('my name is %s age is %s' %(name,age))
foo('xiaow',20)
# 运行结果:
# my name is xiaow age is 20
# 不传递默认参数
def foo(name,age=19):
print('my name is %s age is %s' %(name,age))
foo('xiaow')
# 运行结果:
# my name is xiaow age is 19
不定长参数
- 不定长无命名参数 *args(加了星号 * 的参数会以元组(tuple)的形式导入,存放所有未命名的变量参数)
- 不定长有命名参数 **kwargs(加了两个星号 ** 的参数会以字典的形式导入)
*args
实例(Python3.0+):
# 传递多个参数
def foo(*args):
print(args)
for i in args:
print(i)
foo('xiaow',20,'girl')
# 运行结果
# ('xiaow', 20, 'girl')
# xiaow
# 20
# girl
# 传递元组
def foo(x,y,*args):
print('x is %s' %x)
print('y is %s' %y)
print('args is %s' %args)
foo('study','python',('xiaow',20,'girl'))
# 运行结果:
# x is study
# y is python
# args is ('xiaow', 20, 'girl')
# 传参时加*与不加*的区别
# 不加*
def foo(*args):
print(args)
for i in args:
print(i)
foo(1,2,('a','b'))
# 运行结果:
# (1, 2, ('a', 'b'))
# 1
# 2
# ('a', 'b')
# 加*
def foo(*args):
print(args)
for i in args:
print(i)
foo(1,2,*('a','b'))
# 运行结果:
# (1, 2, 'a', 'b')
# 1
# 2
# a
# b
**kwargs
实例(Python3.0+):
# 传递多个参数
def foo(**kwargs):
print(kwargs)
for i in kwargs:
print(i)
foo(name='xiaow',age=20)
# 运行结果:
# {'name': 'xiaow', 'age': 20}
# name
# age
# 传递字典
def foo(x,y,**kwargs):
print('x is %s' %x)
print('y is %s' %y)
print('kwargs is %s' %kwargs)
dict1={'name':'xiaow','age':20}
foo('study','python',**dict1) # 传递字典时,不加**会报错
# 运行结果:
# x is study
# y is python
# kwargs is {'name': 'xiaow', 'age': 20}
六 参数总结
- 当位置参数、关键字参数、默认参数混用时,三者的顺序是:位置参数,关键字参数,默认参数(且默认参数不能传值)
- 当args、位置参数、默认参数混用时,三者的顺序是:位置参数、默认参数、*args 或 位置参数、*args、默认参数
- 当位置参数、默认参数、**kwargs混用时,三者的顺序必须是:位置参数、默认参数、**kwargs
- 当位置参数、*args、**kwargs混用时,三者的顺序必须是:位置参数、*args、**kwargs