Python学习笔记(5):函数
2010-08-28 15:30
686 查看
在前面我们已经见过一些Python内建函数,比如len和rang。现在我们来看看自定义函数,函数是通过def关键字来定义,后面跟函数名称和圆括号,括号内可以包含参数,该行以冒号结束,接下来是语句块,即函数体。
运行结果为:
x is 50
Changed local x to 2
x is still 50
运行结果为:
5 is maximum.
Prints the maximum of two numbers.
The two values must be integers.
Help on function printMax in module __main__:
printMax(x, y)
Prints the maximum of two numbers.
The two values must be integers.
1. 简单的sayHello函数
def printMax(a, b): if a >]3. 局部变量
def func(x): print("x is ", x) x = 2 print("Changed local x to ", x) x = 50 func(x) print("x is still ", x)
运行结果为:
x is 50
Changed local x to 2
x is still 50
4. 默认参数值
运行结果为:
5 is maximum.
Prints the maximum of two numbers.
The two values must be integers.
Help on function printMax in module __main__:
printMax(x, y)
Prints the maximum of two numbers.
The two values must be integers.
相关文章推荐
- python 3 学习笔记:函数-lambda
- 【python学习笔记】函数式编程:返回函数
- Python 学习笔记-函数(一)传参
- python 学习笔记 -- 函数
- python学习笔记(六)——函数
- Python学习笔记(函数)
- OpenCV_Python学习笔记--OpenCV中的绘图函数
- Python学习笔记(三):函数
- Python学习笔记——函数
- [python]学习笔记2-字符串内建函数1
- 学习笔记-小甲鱼Python3学习第二十二讲:函数:递归是神马
- Python 学习笔记 - 3 创建函数
- python学习笔记:第七天(函数)
- 小甲鱼:Python学习笔记003_函数
- python学习笔记-------函数
- Python学习笔记(三)函数初识和内置函数
- [python]学习笔记3-字符串内建函数2
- python3学习笔记:函数作为返回值&匿名函数lambda
- Python学习笔记(九)返回函数
- Python学习笔记7-把函数当参数传递、指定可变参数