Learning Python 010 函数 1
来源:互联网 发布:iphone蜂窝数据用户名 编辑:程序博客网 时间:2024/05/29 08:00
Python 函数 1
调用函数
举个例子
多于Python内部的函数,你可以在Python的交互式终端中使用help()
函数来查看函数的使用方法。比如:abs()
函数,如果你不知道如何使用它,你可以在Python的交互式终端中输入:
>>> help(abs)
你会看到下面的输出信息:
Help on built-in function abs in module builtins:abs(x, /) Return the absolute value of the argument.
它说:你随便输入一个数x,会返回x的绝对值:
>>> abs(-20)20>>> abs(-12.32)12.32
数据类型转换
Python内置了数据类型转换的函数:
>>> int('123')123>>> int(12.32)12>>> float('12.32')12.32>>> str(1.22)'1.22'>>> str(10)'10'>>> bool(1)True>>> bool('')False
给函数取“别名”
>>> a = abs # 变量a指向abs函数>>> a(-1) # 所以也可以通过a调用abs函数1
为什么可以这样做:
因为函数名其实就是指向一个函数对象的引用,完全可以把函数名赋给一个变量,就是相当于给这个函数起了一个“别名”。
定义函数
自己写一个函数
下面我们写一个获取x的绝对值的函数,来讲讲Python中函数的书写格式:
def my_abs(x): if x >= 0: return x else: return -x
注意缩进 和
:
。在每一句结尾,你不需要写;
。
空函数
空函数的写法:
def nop(): pass
这里的关键字pass
的意思就是什么都不做。pass
是一个占位符。
空函数怎么用:
如果现在还没想好怎么写函数的代码,就可以先放一个pass
,让代码能运行起来。
举例:
if age >= 18: pass
这样就可以在没有完成
if age >= 18
内部的代码,程序依然可以运行。如果缺少pass
,代码运行时会有语法错误。
参数检查
这个参数检查是一个重点。Python语言才会有参数检查。C/C++是不会有的。所以,这里要学会它。其实,就是下面这段代码:
if not isinstance(x, (int, float)): raise TypeError('bad operand type')
Q:上面这段代码,有什么用啊?
A:我想说的是可有可无。举个例子:
>>> my_abs('A')Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in my_absTypeError: unorderable types: str() >= int()>>> abs('A')Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: bad operand type for abs(): 'str'
你会看到,如果我们个abs()
和my_abs()
函数都参入str数据,得到的错误是不同的。我们写的my_abs()
函数得到的错误是:unorderable types: str() >= int()
,它说:函数内部的这段代码if x >= 0:
正在执行str
数据和int
数据之间的比较。它没有检测到传入参数错误这一直接导致问题的原因,在我们调试程序的时候,这样的报错会误导我们。
所以,如果我们将my_abs()
函数写出下面这个样子:
def my_abs(x): if not isinstance(x, (int, float)): raise TypeError('bad operand type') if x >= 0: return x else: return -x
现在我们在运行程序:
>>> my_abs('A')Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in my_absTypeError: bad operand type
现在就提示出了真正的错误原因:bad operand type
。意思就是说:传入了错误的数据类型。
返回多个值
Python语言还有一个特点,这个特点是C/C++所没有的:函数可以返回对个值。
import mathdef move(x, y, step, angle=0): nx = x + step * math.cos(angle) ny = y - step * math.sin(angle) return nx, ny
相当的简单,就是将你想要返回的数据写在return
语句里面。
我们在使用这个函数的时候,同样的带来获取返回值:
>>> x, y = move(100, 100, 60, math.pi / 6)>>> print(x, y)151.96152422706632 70.0
其实,我们可以使用一个返回值,就可以获取到所有的返回值:
>>> r = move(100, 100, 60, math.pi / 6)>>> print(r)(151.96152422706632, 70.0)
此时,r
是一个元组(tuple)数据类型。返回值按位置赋给元组中对应的元素。
所以,Python函数的返回值其实就是一个元组。
参考网站:
http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014316784721058975e02b46cc45cb836bb0827607738d000
- Learning Python 010 函数 1
- Learning Python 010 函数 2
- Learning Python 012 函数式编程 1 高阶函数
- Learning Python : 2 . property函数
- Learning python 系列1
- learning python(1)
- Learning Python (1)
- OpenCV-Python learning---1
- python learning (day 1)
- Python-learning-SMACH-1-GettingStarted
- Learning Python(1) How python program runs
- Learning Python
- Learning Python
- Learning Python
- Learning Python
- python learning
- Learning Python
- Learning Python
- Leetcode Multiply Strings
- 关于使用开源库或jar包时可能遇到ndk冲突错误
- 最小点割集
- 连接数据库 in clojure ---korma
- 【日志7.19】
- Learning Python 010 函数 1
- 关于mysql存储过程的definer的问题
- Linux进阶命令
- 【手游】少年西游记 美术资源加密分析
- bash 快捷键
- windows NET命令的使用
- 从架构的角度看如何写好代码
- github被墙无法提交代码解决方案
- libjepg相关