码迷,mamicode.com
首页 > 编程语言 > 详细

python 函数(def)

时间:2018-02-17 22:51:07      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:return   就是   多少   python 函数   body   简单   style   打印   定义   

函数(def)

函数就是给一段程序起一个名字,用这个名字来执行一段程序,反复使用。

关键字‘def‘来定义,identifier(参数)

函数运用例子:

1、

def boy():

  print(”hi,world“)

以上这个函数比较简单,运行这个函数,只是打印出hi,world

2、

def boy(a,b):

  c=a+b

  return c

以上这个函数,运行后,如果给定两个值,即对这两个值进行相加,且返回值

3、

def boy(str,times):

  c=str * times

  return c

以上这个函数,给定一个字符串,和数字,即返回相应的数量

4、

def boy(str,times = 1):

  c=str * times

  return c

以上这个函数,有一个默认值1,如果没有对函数经给多少值,他默认就是输出1次

5、

def boy(a,b=2,c=9)

  print(a,b,c)

boy(111,21)

boy(54,c=8)

boy(k)

运行以上函数,是定义多个默认值

 

python 函数(def)

标签:return   就是   多少   python 函数   body   简单   style   打印   定义   

原文地址:https://www.cnblogs.com/windows3/p/8452278.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!