我要招人在哪个网站招,公司画册模板,建筑设计门户网站,花型图案设计网站目录
一.前言二.Python 函数定义三.Python 函数的调用四.Python 函数传参 1.Python 函数常规参数2.Python 函数缺省参数3.Python 函数不定长参数 五.Python 函数返回值 return六.Python 函数重点总结七.猜你喜欢 零基础 Python 学习路线推荐 : Python 学习目录 Pytho…目录
一.前言二.Python 函数定义三.Python 函数的调用四.Python 函数传参 1.Python 函数常规参数2.Python 函数缺省参数3.Python 函数不定长参数 五.Python 函数返回值 return六.Python 函数重点总结七.猜你喜欢 零基础 Python 学习路线推荐 : Python 学习目录 Python 基础入门 一.前言
Python 函数是指代码片段可以重复调用比如我们前面文章接触到的 type / id 等等都是函数这些函数是 Python 内置函数Python 底层封装后用于实现某些功能对于所有的 Python 内置函数我们只管调用即可不需要关心具体内部如何实现。
二.Python 函数定义
在 Python 中定义一个函数要使用 **def** 语句依次写出函数名、括号、括号中的参数和冒号然后在缩进块中编写函数体函数的返回值用 return 语句返回如果没有 return 语句默认返回 None
# !usr/bin/env python
# -*- coding:utf-8 _*-Author:猿说编程
Blog(个人博客地址): www.codersrc.com
File:Python 函数使用.py
Time:2021/3/29 08:00
Motto:不积跬步无以至千里不积小流无以成江海程序人生的精彩需要坚持不懈地积累def functionname( parameters ):函数说明function_suitereturn [expression]
例如定义一个函数输出’hello world’def cusom_print():print(hello world)三.Python 函数的调用
当在 py 文件中代码一行一行执行如果遇到函数的定义编译器会自动跳过执行函数之后的代码如果想调用函数直接调用即可。
**注意函数在调用之前必须先声明。Python 中的内置函数如print / type 函数等等已经在 Python 编译器内部声明并且定义好了我们只管调用即可不需要关心具体内部如何实现。**示例代码如下
# !usr/bin/env python
# -*- coding:utf-8 _*-Author:猿说编程
Blog(个人博客地址): www.codersrc.com
File:Python 函数使用.py
Time:2021/3/29 08:00
Motto:不积跬步无以至千里不积小流无以成江海程序人生的精彩需要坚持不懈地积累def custom_print():print(hello world)print(hello world)print(hello world)custom_print()
输出结果
hello world
hello world
hello world代码分析代码执行到第 15 行时编译器发现这是一个函数声明编译器并不会执行会自动跳到函数末尾第 20 行编译器发现 20 行是在调用 custom_print 函数会直接进入 custom_print 函数执行函数内的代码第 16 / 17 / 18 行直到函数结束这就是整个运行过程。
四.Python 函数传参
函数可以通过外部传递参数参数分为形参和实参
def cusom_print(x): //x 是函数的形参 - 用于函数声明print(cusom_print : x{}.format(x))cusom_print(1) //1 是函数的实参 - 用于调用函数时参数传递1.Python 函数常规参数
常规而言函数默认有几个形参在外部调用时就需要传递多少个实参示例代码如下
# !usr/bin/env python
# -*- coding:utf-8 _*-Author:猿说编程
Blog(个人博客地址): www.codersrc.com
File:Python 函数使用.py
Time:2021/3/29 08:00
Motto:不积跬步无以至千里不积小流无以成江海程序人生的精彩需要坚持不懈地积累def cusom_print1(x):print(cusom_print1 : x{}.format(x))def cusom_print2(x,y):print(cusom_print2 : x{}.format(x))print(cusom_print2 : y{}.format(y))def cusom_print3(x,y,z):print(cusom_print3 : x{}.format(x))print(cusom_print3 : y{}.format(y))print(cusom_print3 : z{}.format(z))cusom_print1(1)
cusom_print2(1,2)
cusom_print3(1,2,3)
输出结果:
cusom_print1 : x1
cusom_print2 : x1
cusom_print2 : y2
cusom_print3 : x1
cusom_print3 : y2
cusom_print3 : z32.Python 函数缺省参数
在 Python 函数参数中除了常规参数还有缺省参数即缺省参数有一个默认值
如果外部调用该函数没有给缺省参数传递参数该形参直接取默认参数值** 如果外部调用时给缺省参数传递了参数那么该形参的值应该等于外部传递的参数**
带有缺省参数的函数也被称为缺省函数示例代码如下
# !usr/bin/env python
# -*- coding:utf-8 _*-Author:猿说编程
Blog(个人博客地址): www.codersrc.com
File:Python 函数使用.py
Time:2021/3/29 08:00
Motto:不积跬步无以至千里不积小流无以成江海程序人生的精彩需要坚持不懈地积累def cusom_print4(x,y2,z3): # x2,z3 缺省参数print(cusom_print4 : x{}.format(x))print(cusom_print4 : y{}.format(y))print(cusom_print4 : z{}.format(z))print(****20)cusom_print4(1)
cusom_print4(1,4)
cusom_print4(1,4,3)
输出结果
cusom_print4 : x1
cusom_print4 : y2
cusom_print4 : z3
************************************************************
cusom_print4 : x1
cusom_print4 : y4
cusom_print4 : z3
************************************************************
cusom_print4 : x1
cusom_print4 : y4
cusom_print4 : z3
************************************************************注意 1.缺省参数都有一个默认值如果外部没有给缺省参数传递参数那么直接取默认值否则等于外部传递的参数值 2.缺省参数必须写在函数形参的末尾 错误写法 def cusom_print4(x,y2,z): print(“cusom_print4 : x{}”.format(x))
3.Python 函数不定长参数
除了上面两者在函数的参数中还有一种不定长参数即函数的形参长度/类型都不固定可能听着有点蒙这个问题我们留到下一篇文章 Python 函数不定长参数 *argc , **kargcs 讲解暂时不做过多解释。
五.Python 函数返回值 return
函数的返回值可有可无根据自己的使用需求而定。如果函数没有 return 返回值默认会返回 None即空值与 False 不同它不表示 0也不表示空字符串而表示没有值也就是空值。
六.Python 函数重点总结
1.函数的声明必须在调用之前否则会报错2.注意缺省参数的参数写法3.函数没有使用 return默认返回 None
七.猜你喜欢
Python 配置环境Python 变量Python 运算符Python 条件判断 if/elsePython while 循环Python breakPython continuePython for 循环Python 字符串Python 列表 listPython 元组 tuplePython 字典 dictPython 条件推导式Python 列表推导式Python 字典推导式
未经允许不得转载猿说编程 » Python 函数声明和调用