翼度科技»论坛 编程开发 python 查看内容

python笔记:第六章函数&方法

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
1.系统函数

由系统提供,直接拿来用或是导入模块后使用
  1. a = 1.12386
  2. result = round(a,2)
  3. print(result)
  4. > 1.12
复制代码
2.自定义函数


  • 函数是结构化编程的核心
  • 使用关键词def来定义函数
  1. #函数定义
  2. def funcname(parameter_list):
  3.     pass
  4. #1.参数列表可以没有
  5. #2.用 return 返回值value, 若无return 语句,则返回none; 函数内部遇到 return 则停止运行
复制代码
  1. def add(x,y):
  2.     result = x + y
  3.     return result
  4. #定义函数时,不可与系统函数同名
  5. def print_code(code):
  6.     print(code)
复制代码
  1. def hello(name):
  2.         return 'Hello, {}!'.format(name)
复制代码
  1. def hello(name):
  2.         return 'Hello, {}!'.format(name)str1 = input('name = ')print(hello(str1))>name = lmcHello, lmc!
复制代码

  • 为函数添加文档字符串
  1. def hello(name):
  2.         'Welcome for users'  # 文档字符串
  3.         return 'Hello, {}!'.format(name)
  4. print(hello.__doc__)
  5. > 'Welcome for users'
复制代码
2.1 函数的返回值


  • 如果不自定义返回值,则无返回值
  • 关键字 return
  1. def test():
  2.         print('hello')
  3.         return
  4.         print('end')
  5. test()
  6. > hello
复制代码

  • 用明确的变量组来接受函数输出值,便于后期查看(序列解包),不用元组
  1. def damage(skill1,skill2):
  2.     damage1 = skill1 * 3
  3.     damage2 = skill2 * 2 + 10
  4.     return damage1, damage2
  5. skill1_damage, skill2_damage = damage(3,6)
  6. print(skill1_damage,skill2_damage)
  7. > 9 22
复制代码
2.1 函数的参数


  • 指定赋值调用,增加可读性
  1. def add(x,y):
  2.     result = x + y
  3.     return result
  4. c = add(y=3,x=2)  # 指定赋值,与顺序无关,可读性
  5. print(c)
  6. > 5
复制代码

  • 给函数设置默认参数,不传参也能可以调用
  1. # collage已经设置默认参数,不传参即采用默认参数
  2. def print_files(name,age,gender,collage='liaoning University'):
  3.     print('My name is ' + name)
  4.     print('I am ' + age)
  5.     print('My gender is ' + gender)
  6.     print('My school is '+ collage)
  7. print_files('阿衰',str(18),'man')
  8. >
  9. My name is 阿衰
  10. I am 18
  11. My gender is man
  12. My school is liaoning University   # 默认参数
  13. print_files('阿衰',str(18),'man', '怕跌中学')
  14. >
  15. My name is 阿衰
  16. I am 18
  17. My gender is man
  18. My school is 怕跌中学
复制代码

  • 星号参数 类似于序列解包中的星号变量 接收余下位置的参数(或全部接收)
  1. def printer(*ele):
  2.     print(ele)
  3.    
  4. d = 11
  5. printer(d)
  6. > (11,)  # 输出的为元组  
复制代码
这里必须有逗号才能是元组  无逗号(11) 类型为int
  1. a = (11,)
  2. b = (11)
  3. print(type(a))
  4. print(type(b))
  5. >
  6. <class 'tuple'>
  7. <class 'int'>
复制代码

  • 利用*星号接收更多数据
  1. def printer(a, b, *ele):
  2.     return ele
  3.    
  4. tuple1 = printer(1,2,3,4,5)
  5. print(tuple1)
  6. > (3, 4, 5)
复制代码
  1. def printer(a, b, *ele):
  2.     print(ele)
  3.    
  4. tuple1 = (1,2,3,4,5)
  5. printer(*tuple1)
  6. > (3, 4, 5)
复制代码

  • 双星号传递字典
  1. def hello(greeting = 'Hello', name = 'world'):
  2.         print('{}, {}!'.format(greeting, name))
  3. params = {'name': 'bobo', 'greeting': 'well met'}
  4. print()
复制代码

  • 对于星号的使用,能不用最好,一般情况下,也可以达到相同效果
  • 多用于:

    • 定义的函数,允许可变数量的参数
    • 调用函数时,拆分字典或序列使用


来源:https://www.cnblogs.com/lmc7/p/17542633.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具