{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 常用数学运算函数"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python 内置了一系列与数字运算相关的函数可以直接使用,如下表所示"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"| 函数 | 功能描述 |\n",
"| :--- | :--- |\n",
"| abs(x) | 返回 x的绝对值,x可以是整数或浮点数;当x为复数时返回复数的模|\n",
"| divmod(a, b) | (a // b, a % b),以整数商和余数的二元组形式返回 |\n",
"| pow(x, y[, z]) | 返回 x的y次幂,当z存在时,返回 x的y次幂对z取余 |\n",
"| round(number[, n]) | 返回number舍入到小数点后n位精度的值。当小数点最后几位都是0时,输出其最短表示。当number是整数时,直接返回整数本身|\n",
"| max(iterable)<br>max(arg1,arg2,…) | 从多个参数或一个可迭代对象中返回其最大值,有多个最大值时返回第一个。 |\n",
"| min(iterable)<br>min(arg1,arg2,…) | 从多个参数或一个可迭代对象中返回其最小值,有多个最小值时返回第一个。 | \n",
"| sum(iterable, start=0) | 从 start 开始自左向右对 iterable 的项求和并返回总计值。 iterable 的项通常为数字,而 start 值则不允许为字符串。\n",
"| eval(source) | 计算指定表达式的值source:必选参数,可以是字符串,也可以是一个任意的代码对象实例。常用于将字符串转为数字 |\n",
"| complex([real[, imag]]) | 返回值为 real + imag\\*1j 的复数,或将字符串或数字转换为复数。<br>如果第一个形参是字符串,则它被解释为一个复数,并且函数调用时必须没有第二个形参。第二个形参不能是字符串。|\n",
"| int(x, base=10) | base缺省为10,当其省略时,当x为浮点数或整数字符串时返回整型数字,当x为浮点数字符串时会返回异常。<br>当base为其他值时可将字符串x作为base进制字符串,并将其转成十进制数字。 | \n",
"| float([x]) | 从一个数字或字符串x返回浮点数字 |"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"其中int()与float()函数前面已经讲解过,下面对其他函数进行说明。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" + ### abs(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"4\">返回一个数的<font color=Red>__绝对值__</font>。参数可以是整数、浮点数,返回值的类型与参数x的类型一致。</font>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"3.0\n",
"3.45\n"
]
}
],
"source": [
"print(abs(3)) # 返回绝对值3\n",
"print(abs(-3.0)) # 返回绝对值3.0\n",
"print(abs(-3.45)) # 返回绝对值3.45"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"如果参数x是一个<font color=Red>__复数__</font>,则返回它的<font color=Red>__模__</font>。"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"print(abs(3 + 4j)) # 返回复数3+4j的模5.0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ ### divmod(a, b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"4\">参数a、b可以是整数、浮点数,返回以<font color=Red>__整数商和余数__</font>构成的二元组,相当于(a // b, a % b)</font>"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(3, 1)\n",
"(-4, -2)\n",
"(-4, 2)\n",
"(3, -1)\n"
]
}
],
"source": [
"print(divmod(10, 3)) # 输出(3, 1)\n",
"print(divmod(10, -3)) # 输出(-4, -2)\n",
"print(divmod(-10, 3)) # 输出(-4, 2)\n",
"print(divmod(-10, -3)) # 输出(3, -1)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"print(divmod(11.5, 3.5)) # 输出(3.0, 1.0)\n",
"print(divmod(11.5, -3.5)) # 输出(-4.0, -2.5)\n",
"print(divmod(-11.5, 3.5)) # 输出(-4.0, 2.5)\n",
"print(divmod(-11.5, -3.5)) # divmod()输出(3.0, -1.0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ ### pow(x, y[, z])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"两参数形式 pow(x, y) 返回x的y次<font color=Red>__幂__</font>,等价于乘方运算:x \\** y"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"print(pow(2, 3)) # 等价于2**3,返回8\n",
"print(pow(2, -3)) # 等价于2**-3,返回0.125\n",
"print(pow(2, 1/2)) # 等价于2**0.5,返回1.4142135623730951\n",
"print(pow(-9, 1/2)) # 等价于-9**0.5,返回复数1.8369701987210297e-16+3j,实部近似为0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"如果z存在,则返回x的y次幂对z取余的结果, 比 pow(x, y) % z 更<font color=Red>__高效__</font>。此时三个参数均必须为整数。"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"print(pow(1999, 1998, 1997)) \n",
"print(1999 ** 1998 % 1997) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ ### round(number[, n])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"返回浮点数number保留n 位小的<font color=Red>__最短表示__</font>,若number为整数时,返回整数本身。 "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python 中采用的末位取舍算法为: \n",
"四舍六入五考虑, \n",
"五后非零就进一, \n",
"五后为零看奇偶, \n",
"五前为偶应舍去, \n",
"五前为奇要进一。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"4\">四舍六入五考虑,五后非零就进一,</font>"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.8\n",
"3.9\n",
"3.13\n",
"3.13\n"
]
}
],
"source": [
"print(round(3.84, 1)) # 舍去,输出3.8\n",
"print(round(3.86, 1)) # 进位,输出3.9\n",
"print(round(3.120500001, 2)) # 五后非零应进一,输出3.13\n",
"print('{:.2f}'.format(3.125000001))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"4\">五后为零看奇偶,五前为偶应舍去,五前为奇要进一。</font>"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"print(round(3.12500000, 2)) # 五前为偶应舍去,输出3.12\n",
"print(round(3.11500000, 2)) # 五前为奇要进一,输出3.12"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"如果n被省略或为None,则返回最接近输入值的整数,即<font color=Red>__取整__</font>。"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"print(round(3.14))\n",
"print(round(-3.84))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"结果小数部分的末位为0 或n 超过小数位数时,返回该数的<font color=Red>__最短表示__</font>,舍弃浮点数末尾无意义的零。"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"print(round(3.000006, 2)) # 期望输出3.00,实际输出其浮点数的最短表示3.0\n",
"print(round(3.140000005, 4)) # 期望输出3.1400,实际输出3.14"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"需要注意的是,<font color=Red>__多数浮点数无法精确转为二进制__</font>,会导致部分数字取舍与期望不符。"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"print(round(3.1425, 3)) # 五前为偶应舍去,期望输出3.142,实际输出3.143\n",
"print(round(2.675, 2)) # 五前为奇应进一,期望输出2.68,实际输出2.67"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"使用decimal模块可观察浮点数的精确值,round()函数的舍入实际上是按照其精确值处理的。"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import decimal # 引入decimal模块\n",
"\n",
"\n",
"print(decimal.Decimal(3.1425)) \n",
"print(decimal.Decimal(2.675))\n",
"\n",
"print(decimal.Decimal(14.95)) \n",
"print(decimal.Decimal(14.65))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"当round(number,n)函数对整数number保留小数点后n位时,得到的数据仍为整数类型。"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"print(round(5,4)) # 输出 5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ ### max(iterable)或max(arg1,arg2,…)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"4\">max(arg1,arg2,…) 从<font color=Red>__多个参数__</font>中返回其<font color=Red>__最大值__</font></font>"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1000\n",
"45.9\n"
]
}
],
"source": [
"print(max(80, 100, 1000))\n",
"print(max(32.7, 45.9, -100))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"4\">max(iterable) 从一个<font color=Red>__可迭代对象__</font>中返回其<font color=Red>__最大值__</font></font>"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1000\n",
"45.9\n"
]
}
],
"source": [
"print(max([80, 100, 1000])) # 返回列表中的最大值\n",
"print(max({32.7, 45.9, -100})) # 返回集合中的最大值"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ ### min(iterable)或min(arg1,arg2,…)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"4\">min(arg1,arg2,…) 从<font color=Red>__多个参数__</font>中返回其<font color=Red>__最小值__</font></font>"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"80\n",
"-100\n"
]
}
],
"source": [
"print(min(80, 100, 1000))\n",
"print(min(32.7, 45.9, -100))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"min(iterable) 从一个<font color=Red>__可迭代对象__</font>中返回其<font color=Red>__最小值__</font>"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"80\n",
"-100\n"
]
}
],
"source": [
"print(min([80, 100, 1000])) # 返回列表中的最小值\n",
"print(min({32.7, 45.9, -100})) # 返回集合中的最小值"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ ### sum(iterable, start=0)\n",
"\n",
"<font size=\"4\">省略start时,将元素为数值的可迭代对象iterable中的元素<font color=Red>__累加__</font>。</font>"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"55\n",
"10\n"
]
}
],
"source": [
"print(sum(range(11))) # 1+2+3+......+10,返回55\n",
"print(sum([1, 2, 3, 4])) # 1+2+3+4,返回10\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"4\">若给strat参数传值,将元素为数值的可迭代对象iterable中的元素<font color=Red>__累加到start上__</font></font>"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"print(sum(range(11), start=10)) # 10+1+2+3+......+10,返回65\n",
"print(sum([1, 2, 3, 4], 20)) # 20+1+2+3+4,返回30"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### start参数值非整数时的应用:列表<font color=Red>__降维__</font>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"若一个二维列表包含 n 个一维列表元素,可使用sum()函数将这些子列表拼成一个新的一维列表,起到降维的效果。"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"ls=[[95, 96, 85, 63, 91], [75, 93, 66, 85, 88], [86, 76, 96, 93, 67], [88, 98, 76, 90, 89], [99, 96, 91, 88, 86]]\n",
"print(sum(ls,[]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"在某些使用场景时,不要用sum()。例如当以扩展精度对浮点数求和时,使用sum()可能无法得到精确结果,此时推荐使用 math.fsum()。"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"print(sum([0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1])) # 期望输出1.0,实际结果为0.9999999999999999"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"import math # 引入math模块\n",
"print(math.fsum([0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1])) # 可获得精确的结果,输出1.0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ ### eval(source)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"前面我们讲解过使用eval()进行<font color=Red>__数值类型转换__</font>。 \n",
"实际上,source参数是一个字符串,python会将source当做一个python<font color=Red>__表达式__</font>(从技术上讲,是一个条件列表)进行解析和计算。"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"x = 1\n",
"y = 3\n",
"print(eval('x+1')) # 计算表达式x+1,输出2\n",
"print(eval('pow(y, 2)')) # 计算表达式pow(y, 2),输出9"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"当eval()解析到表达式是不可以计算后,就会查找它是不是一个变量的名字,如果是一个变量的名字,那么它会输出这个变量的内容,否则就会抛异常。"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"abc\n",
"abc\n"
]
}
],
"source": [
"s = 'abc'\n",
"print(eval('s')) # 解析到s,将其当作变量名,输出其值'abc'\n",
"s = '\"abc\"'\n",
"print(eval(s)) # eval去除单引号后得到了字符串“abc”,直接输出。"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"s = 'abc'\n",
"print(eval(s)) # 解析到abcd,将其当作变量名,此时该变量名未定义,抛NameError异常"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ ### complex([real[, imag]])\n",
"\n",
"如果第一个形参real是字符串,则它被解释为一个复数。此时函数调用时必须没有第二个形参,否则将抛异常。"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1+0j)\n",
"(1+2j)\n"
]
}
],
"source": [
"print(complex('1')) # 解释为复数,此时虚部为0,输出(1+0j)\n",
"print(complex('1+2j')) # 解释为复数,输出(1+2j)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"print(complex('1',2)) # 第一个参数为字符串,此时若设置第二个参数,将抛异常"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"如果第一个形参real是数值型(整数、浮点数或复数),则函数调用时可以有第二个形参imag,返回值为 real + imag*1j 的复数。如果省略了imag,则默认值为零。"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"print(complex(1)) # 实部为1,此时虚部为默认值0,输出(1+0j)\n",
"print(complex(1, 2)) # 输出(1+2j)\n",
"print(complex(3.4, 5.6)) # 输出(3.4+5.6j)\n",
"print(complex(1+2j, 3+4j)) # 计算(1+2j)+(3+4j)*j,输出(-3+5j)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"第二个形参imag不能是字符串,否则将抛异常。"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"print(complex(1, '2')) # 第二个参数为字符串,抛异常"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}