函数其实一段带名字的代码段,咱们能够根据代码段,重复执行某一段代码段,或者有条件的执行某一段代码段。编程
将一段代码定义成函数后,咱们能够很方便的根据本身的需求,随时调用该代码段。遇到需求变化的时候,只须要修改该函数,就能够知足需求,不须要处处修改特定的代码。编程语言
好比咱们定义一个print hello的函数:ide
def say_hello(): print("Hello!") say_hello() ''' Hello! '''
咱们能够看到,咱们使用def来定义函数, 以冒号结尾。 输入回车后,ide会自动缩进,缩进后的代码就是函数的定义体。定义完函数体后,咱们一般空一行表示定义完函数。函数
咱们直接输入函数名,便可调用函数。ui
咱们还能够向函数传递“消息”,在编程语言中咱们称之为参数。好比咱们修改函数,能够向其传递一我的名,并输出想要语句。spa
def say_hello(name): print(name + ", Hello!") say_hello('Ralf') ''' Ralf, Hello! '''
经过上面代码,咱们能够看到,咱们从新修改了函数,增长了一个参数 name, 修改了函数体。这样咱们能够根据本身的须要,向函数传递消息,并输出想要的结果。code
函数定义中的参数,咱们一般称之为 形参,好比上述例子中的变量name, 这个变量只在函数定义中使用,并不具备实际的值。咱们在调用函数时,传递的消息或者说变量,咱们称之为实参,是咱们“实际“上想要使用的参数或者数值,变量。blog
一般状况下,咱们必须按照事先定义好的参数,依次传递给函数体,才能正确得出想要的结果。ci
def say_hello(name, city): print(name + ", Welcome to " + city + "!" ) say_hello('Ralf', "Shanghai")
''' Ralf, Welcome to Shanghai! '''
def say_hello(name, city): print(name + ", Welcome to " + city + "!" ) say_hello('Shanghai', "Ralf")
''' Shanghai, Welcome to Ralf! '''
def say_hello(name, city): print(name + ", Welcome to " + city + "!" ) say_hello('Ralf')
''' Traceback (most recent call last): File "D:/PythonStudy/test.py", line 31, in <module> say_hello('Ralf') TypeError: say_hello() missing 1 required positional argument: 'city' '''
咱们可使用关键字的形式来规避上面例子中出现的错误,即在调用函数时,使用 形参 = value的方式来调用函数:it
def say_hello(name, city): print(name + ", Welcome to " + city + "!" ) say_hello(city = "Shanghai", name = 'Ralf')
'''
输出: Ralf, Welcome to Shanghai! '''
在定义函数时,咱们也能够定义一些参数默认值,这样在调用,若是给了实参,函数就使用实参,没有给值,就是用默认值。
def say_hello(name, city = "Shanghai"): print(name + ", Welcome to " + city + "!" ) # 使用默认值 say_hello("Ralf") #使用实际值 say_hello("Rachel", "Beijing")
''' 输出: Ralf, Welcome to Shanghai! Rachel, Welcome to Beijing! '''
返回值:
函数不只能够接受外部传送的变量参数,也能够输出一个返回值给调用者。
def say_hello(name, city = "Shanghai"): return name + ", Welcome to " + city + "!" out_message = say_hello("Rachel", "Beijing") print(out_message) ''' 输出: Rachel, Welcome to Beijing! '''
返回一个列表:
def born_city(name, city = "Shanghai"): return [name, city] out_message = born_city("Rachel", "Beijing") print(type(out_message)) print(out_message) ''' 输出: <class 'list'> ['Rachel', 'Beijing'] '''
返回一个字典:
def born_city(name, city = "Shanghai"): return {'name': name, 'city': city} out_message = born_city("Rachel", "Beijing") print(type(out_message)) print(out_message) ''' 输出: <class 'dict'> {'name': 'Rachel', 'city': 'Beijing'} '''
传递任意数量的实参,有时候咱们不肯定,实际参数有几个,能够能是一个也多是多个,要怎么定义哪。咱们能够在形参前面加一个星号,表示这个参数能够是多个:
def say_hello(*names): print(names) out_message = say_hello("Rachel", "Ralf", "Terry") print(out_message) ''' 输出: ('Rachel', 'Ralf', 'Terry') '''
注意函数返回了一个元组,即使函数只输入一个参数,返回的也是一个元组。
结合实参和任意数量的实参:
def say_hello(words, *names): for name in names: print(words + ", " + name) out_message = say_hello("hello", "Rachel", "Ralf", "Terry") print(out_message) ''' 输出: hello, Rachel hello, Ralf hello, Terry '''
使用任意数量的关键字实参。有时候咱们须要接受任意数量的实参,可是不知传递给函数的会是什么样的信息。在这种状况下,咱们能够将函数编写成接受任意数量的 键值对,即字典的形式。方式为加两个冒号 **dict:
def pepole_info(name, sex, **others): print(name + ": "+ sex ) for key, value in others.items(): print(key + ": " + value) out_message = pepole_info("Ralf", "male", height = "175", hobby = "football") print(out_message) ''' 输出: Ralf: male height: 175 hobby: football '''