从零开始学Python-Day15-定义函数

def

定义一个函数,要使用def语句,依次写出函数名、括号、括号中写参数、括号、冒号,然后,在缩进块中编写函数体,也就是计算过程,函数的返回值用return语句返回。我们可以自己定义一个取绝对值的函数my_abs

<code>>>> def my_abs(x):
\tif x>=0:
\t\treturn x
\telse:
\t\treturn -x

>>> my_abs(-2)
2/<code>

在函数体的内部,一旦符合条件执行了return,整个函数体运行就结束了。也就是之前说过的条件判断,一旦符合条件就执行对应的语句结束。如果没有return语句,函数体执行结束也要返回结果,结果为None;retuen None可以直接写为retuen,如下两个函数定义,如果传入参数为0时,我们让它返回None,运行的结果都是None

<code># -*- coding: UTF-8 -*-
# Filename : newf.py
# author by : www.woodmanzhang.com
# 新的函数
def my_function(x):
\tif x>0:
\t\treturn x
\tif x<0: return -x print(my_function(0)) def my_function2(x): if x>0:
\t\treturn x
\tif x<0:
\t\treturn -x
\telse:
\t\treturn
print(my_function2(0))/<code>

我们在存放了newf.py文件的目录下启动python3交互模式,就可以直接调用这个函数my_function:

<code>>>> from newf import my_function
None
None
>>> my_function(100)
100
>>> my_function2(100)

Traceback (most recent call last):
File "", line 1, in
NameError: name 'my_function2' is not defined
>>> from newf import my_function2
>>> my_function2(100)
100/<code>

注意,只有在当前环境调用了的函数才能使用,你可以将自己定义的函数存放在一个文件中,供自己使用调用。调用的方式为 from 文件名 import 函数名

空函数

空函数即为什么都不做,用pass语句完成,跟打牌的pass一个意思,这段我还没想好咋写,先pass,让程序可以运行起来,如果没有pass就会有语法错误,还是上面的例子,我只要非零证书,零我不要,负数我还没想好,修改如下:

<code>def my_function3(x):
\tif x>0:
\t\treturn x
\tif x<0:
\t\tpass
\telse:
\t\treturn/<code>

参数检查

调用函数时,如果参数个数不对,Python解释器会自动检查出来,并给出TypeError如果参数类型不对,自定义函数和内置函数就有区别了,我们的自定义函数是在执行过程中报错,说明了不能在数值和字符串之间进行’>=’的运算;而内置函数直接告诉我们不可以使用字符串:

<code>>>> my_abs(1,2)
Traceback (most recent call last):
File "<pyshell>", line 1, in
my_abs(1,2)
TypeError: my_abs() takes 1 positional argument but 2 were given
>>> my_abs('A')
Traceback (most recent call last):
File "<pyshell>", line 1, in
my_abs('A')
File "<pyshell>", line 2, in my_abs
if x>=0:
TypeError: '>=' not supported between instances of 'str' and 'int'
>>> abs('A')
Traceback (most recent call last):
File "<pyshell>", line 1, in
abs('A')
TypeError: bad operand type for abs(): 'str'/<pyshell>/<pyshell>/<pyshell>/<pyshell>/<code>

我们修改一下my_abs的定义,增加一个参数类型检查,这里只允许整数和浮点数参数传入,参数检查用内置函数isinstance(),报错信息就会直接显示出来:

<code>>>> def my_abs(x):
\tif not isinstance(x,(int,float)):
\t\traise TypeError('bad operand type')
\tif x>=0:
\t\treturn x
\telse:
\t\treturn -x

>>> my_abs('A')
Traceback (most recent call last):
File "<pyshell>", line 1, in
my_abs('A')
File "<pyshell>", line 3, in my_abs
raise TypeError('bad operand type')
TypeError: bad operand type/<pyshell>/<pyshell>/<code>

这里其实就是在函数的定义的函数体前面增加了最初的条件判断,为我们进行参数类型检查。

用tuple返回多个值

一个函数如何返回多个值?我们都知道一元二次方程有两个根,那该如何求解呢?

一元二次方程的求根公式为:

从零开始学Python-Day15-定义函数

计算平方根可以调用math.sqrt()函数

<code>import math
def f2(a,b,c):
\tif not (isinstance(a, (int,float)) and isinstance(b, (int,float)) and isinstance(c, (int,float))):
\t\traise TypeError('bad operand type')
\ty=b**2-4*a*c
\tif a==0:
\t\tif b==0:
\t\t\tprint('无解')
\t\tif b!=0:
\t\t\tx1=-c/b
\t\t\tprint('x=',x1)
\telse:
\t\tif y==0:
\t\t\tx1=-b/(2*a)
\t\t\tprint('x=',x1)
\t\tif y<0:
\t\t\tprint('无解')
\t\telse:
\t\t\tx1=(-b+math.sqrt(y))/(2*a)
\t\t\tx2=(-b-math.sqrt(y))/(2*a)
\t\t\tprint('x1=',x1,'\\n''x2=',x2)/<code>

import导入math函数包,我们定义move为运动后x,y轴数据计算的函数。原来返回值是一个tuple!但是,在语法上,返回一个tuple可以省略括号,而多个变量可以同时接收一个tuple,按位置赋给对应的值,所以,Python的函数返回多值其实就是返回一个tuple,但写起来更方便。


分享到:


相關文章: