Python学习笔记之函数

来源:互联网 发布:百度读书软件 编辑:程序博客网 时间:2024/05/22 12:56

Python学习笔记之函数

函数

在大多数的编程语言中,函数都是十分重要的存在,有了函数,我们可以让代码更加的精简,同时可以让函数被重复的使用,大大的加速了开发的速度。好的函数设计,能够在后续的开发中极大的增加效率,同时还能够减少维护的费用。

定义函数

关键字:def

def say_hello():    print('hello')say_hello()

在函数中传递参数

  • 基本传递

    def say_something(the_words):  print(the_words)say_something('hello world')
  • 形参和实参

    这两个概念是函数的基础,形参就是函数定义中使用的参数,实参是指函数调用中传递的参数,清楚了这两个概念,接下来的函数定义以及参数传递才会清楚。

  • 位置实参

    根据形参的位置定义实参的传递。

    def say_something(first_word,second_word):  print(first_word + ' ' + second_word)say_something('Good','Morning')
  • 关键字实参

    根据形参的名字传递实参。

    def say_something_new(first_word, second_word):  print(first_word + ' ' + second_word)say_something_new(second_word='Morning', first_word='Good')
  • 默认值以及参数可选

    如果传递的单数给了默认值,那么在Python中这个参数就相当于定义为可选参数。

    def say_something_new(first_word, second_word='Morning'):  print(first_word + ' ' + second_word)say_something_new('Good')
  • 传递列表

    def say_something_new(sentence):  for word in sentence:      print(word)say_something_new(['I','am','ten','years','old'])

    在上例中,如果在函数中修改了列表中的元素的值,列表的内容会被更改,如下例:

    def say_something_change_list(sentence):  for word in sentence:      if word=='ten':          sentence[2]='eleven'imp_list = ['I','am','ten','years','old']print(imp_list)say_something_change_list(imp_list)print(imp_list)

    如果需要避免列表被修改,在调用时需要复制一个列表作为参数传递

    imp_list = ['I','am','ten','years','old']print(imp_list)say_something_change_list(imp_list[:])print(imp_list)
  • 传递任意数量的参数

    在函数定义时,只需要在形参前加星号:

    def say_any_thing(*word):  for something in word:      print(something)say_any_thing('Good','Morning','to','you')

函数的返回值

和其他编程语言类似,Python中使用return关键字进行函数的返回,函数可以返回任何类型的数据,在函数定义中无需提前声明返回参数的类型。

def sum(a,b,c):    return(a+b+c)print(sum(1,2,3))

总结

Python的函数十分的灵活,至少和其他大多数编程语言相比较,不过需要注意的就是Python编程中的代码缩进,这关系到代码的逻辑运行与功能。

所有代码附录:

print('定义函数')def say_hello():    print('hello')say_hello()print('在函数中传递参数')def say_something(the_words):    print(the_words)say_something('hello world')print('在函数中传递参数 - 位置实参')def say_something(first_word, second_word):    print(first_word + ' ' + second_word)say_something('Good', 'Morning')print('在函数中传递参数 - 关键字实参')def say_something_new(first_word, second_word):    print(first_word + ' ' + second_word)say_something_new(second_word='Morning', first_word='Good')print('在函数中传递参数 - 默认值以及参数可选')def say_something_new(first_word, second_word='Morning'):    print(first_word + ' ' + second_word)say_something_new('Good')print('在函数中传递参数 - 传递列表')def say_something_list(sentence):    for word in sentence:        print(word)say_something_list(['I','am','ten','years','old'])print('在函数中传递参数 - 修改列表')def say_something_change_list(sentence):    for word in sentence:        if word=='ten':            sentence[2]='eleven'imp_list = ['I','am','ten','years','old']print(imp_list)say_something_change_list(imp_list)print(imp_list)print('在函数中传递参数 - 避免修改列表')imp_list = ['I','am','ten','years','old']print(imp_list)say_something_change_list(imp_list[:])print(imp_list)print('在函数中传递参数 - 传递任意数量的参数')def say_any_thing(*word):    for something in word:        print(something)say_any_thing('Good','Morning','to','you')print('函数的返回值')def sum(a,b,c):    return(a+b+c)print(sum(1,2,3))
原创粉丝点击