{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 数值类型"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"4\">Python 3 中可参与数学运算的数值类型主要有三种:</font>\n",
"1. <font color=Red size=\"4\">__整数__</font>(int)\n",
"2. <font color=Red size=\"4\">__浮点数__</font>(float)\n",
"3. <font color=Red size=\"4\">__复数__</font>(complex)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2.1.1 整数"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"4\"><font color=Red size=\"4\">__整数__</font>是不带小数点的数字,包括0、正整数和负整数。整数的4种进制表示如表2.1所示。\n",
"例如:123、-45、0b1101(二进制)、0o17(八进制)、0xff(十六进制)。</font>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"| 进制种类 | 引导符号 | 描述与示例 |\n",
"| :---: | :---: | :---- |\n",
"| 十进制 | 无 | 由字符0到9组成,遇10进1,如:99, 156 |\n",
"| 二进制 | 0b或0B | 由字符0和1组成,遇2进1,如:0b1010,0B1111 |\n",
"| 八进制 | 0o或0O | 由字符0到7组成,遇8进1,如:0o107,0O777 |\n",
"| 十六进制 | 0x或0X | 由字符0到9及a、b、c、d、e、f或A、B、C、D、E、F组成,遇16进1,如0xFF,0X10A |"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 十进制整数\n",
"0,123,-45"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 二进制整数\n",
"0b1101,0B1100"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 八进制整数\n",
"0o17,0O777"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 十六进制整数\n",
"0xff,0X123456789ABCDEF"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"4\">一般来说,Python 3 中<font color=Red>__整数__</font>几乎是<font color=Red>__没有限制大小的__</font>,可以存储计算机内存能够容纳的无限大整数,而且<font color=red>整数永远是精确的。</font></font>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import math # 导入 math 库\n",
"print(math.factorial(100)) # 计算100的阶乘,158位整数\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <font face='楷体' color='red' size=5> 练一练 </font>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 试着仿照上面的例子,试着在下面写代码求解1000的阶乘,看看是否能正确显示\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"4\">除了通常意义上的整数以外,<font color=Red>__布尔值__</font>(bool)属于整数的<font color=Red>__子类型__</font>。布尔值是两个常量对象<font color=Red>__False__</font> 和 <font color=Red>__True__</font>。 它们被用来表示逻辑上的<font color=Red>__真值__</font>或<font color=Red>__假值__</font>。在数字类的上下文中,例如被用作算术运算符的参数时,它们的行为分别类似于<font color=Red>__整数 0 和 1__</font> 。 </font>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"False + 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"True * 2 + 3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <font size=\"4\"><font color=Red>__内置函数 bool()__</font>可被用来将任意值转换为布尔值,只要该值可被解析为一个逻辑值。</font> <font size=\"5\" color=red>包括负数和正数)都会被转换为 True</font>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(bool(3+4))\n",
"print(bool(3-9))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"bool(10-5*2)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.1.2 浮点数"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"4\">浮点数有两种表示方法:</font>\n",
"+ 十进制表示"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"4\">十进制表示的<font color=Red>__浮点数__</font>由整数部分与小数部分组成,其小数部分可以没有值,但<font color=Red>__小数点__</font>必须要有,此时相当于小数部分为0。</font>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"123.45,23.0,0.78,12.,.15\n",
"\n",
"print(.15*10) ## .15 代表的是 0.15\n",
"print(float(3)) ## 必须有小数点 输出为 3.0\n",
"\n",
"import decimal\n",
"b = decimal.Decimal(pow(10.0,23)) \n",
"\n",
"print(b)\n",
"\n",
"\n",
"b = decimal.Decimal(pow(10.0,22))\n",
"\n",
"print(b)\n",
"\n",
"b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"+ 科学计数法"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"4\">科学计数法表示为<x>e<n>,等价于数学中的$x \\times\\ 10^n $。</font>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"0.48e-5,2e3,2.53e3\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"4\">计算机中数字的表示采用的是二进制的方式,十进制与二进制转换过程中可能会引入误差,所以一般来说,<font color=Red>__浮点数无法保证百分之百的精确__</font>。</font>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(43.02 * 7) # 预期输出:301.14 \n",
"\n",
"print(0.1 + 0.2)\n",
"\n",
"## C语言也会出现这样的情况\n",
"# #include <stdio.h>\n",
"\n",
"# int main() {\n",
"# double result = 0.1 + 0.2;\n",
"# printf(\"%.17f\\n\", result); // 输出: 0.30000000000000004\n",
"# return 0;\n",
"# }\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"5.02 / 0.1 # 预期输出:50.20"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"4\">Python 3对于浮点数默认提供<font color=Red>__大约17位数字的精度__</font>。占8 个字节(64位)内存空间,其数字范围为$1.7× 10^{-308} ~1.7× 10^{308}$。</font>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pow(809.0, 106) # 输出809的106次幂的值"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pow(810.0, 106) # 输出810的106次幂的值,引发OverflowError"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"4\">系统会将输入的浮点数只保留<font color=Red>__17位有效数字__</font>,其余的截断<font color=Red>__丢弃__</font>,所以在计算机中浮点数经常<font color=Red>__无法精确表示__</font>。</font>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = 3.1415926535897932384626433832795\n",
"b = 314159.26535897932384626433832795\n",
"c = 31415926535897.932384626433832795\n",
"print(a, b, c, sep='\\n')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font size=\"4\">Python默认的是17位有效数字的精度,当计算需要使用更高的精度(超过17位小数)的时候,可以使用以下方法进行处理:</font>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print('{:.30f}'.format(314159.265358979323846264338327950288419716939937510))\n",
"print('{:.30f}'.format(3.14159265358979323846264338327950288419716939937510))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"输出值:<br>\n",
"314159.2653589793<font color='red'>48093271255493164062</font> <br> \n",
"3.141592653589793<font color='red'>115997963468544</font><br> \n",
"实际值:<br> \n",
"3.141592653589793<font color='red'>23846264338327950288419716939937510</font><br> \n",
"<font size=\"4\">结果表明虽然可以显示指定位数的小数部分,但是结果并不准确,超过16位有效数字后面的数字往往没有精确意义了。</font>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.1.3 复数"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font color=Red>__复数__</font>( (complex))由<font color=Red>__实数__</font>部分和<font color=Red>__虚数__</font>部分构成,可以用a + bj,或者complex(a,b)表示, 复数的实部a和虚部b都是浮点数。\n",
"可以用<font color=Red>__real__</font>和<font color=Red>__imag__</font>分别获取复数的实部和虚部,用abs(a+bj)获得复数的模。\n",
"<font color=red>模=√(实部)²+(虚部)²</font>\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.0\n",
"4.0\n",
"5.0\n"
]
}
],
"source": [
"print((3.0 + 4.0j).real)\n",
"print((3.0 + 4.0j).imag)\n",
"print(abs(3.0 + 4.0j))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = 3\n",
"b = 2\n",
"c = 6\n",
"# 3*x**2 + 2*x + 6 = 0\n",
"# 计算根\n",
"delta = b ** 2 - 4 * a * c\n",
"root1 = (-b + delta**0.5) / (2 * a)\n",
"root2 = (-b - delta**0.5) / (2 * a)\n",
"\n",
"# 输出结果\n",
"print(f\"方程的根为:{root1}\") # 方程的根为:(-0.33333333333333326+1.3743685418725535j)\n",
"print(f\"方程的根为:{root2}\") # 方程的根为:(-0.3333333333333334-1.3743685418725535j)\n",
"\n",
"# 获取实部和虚拟并分别保留2位小数,格式化输出\n",
"print(\"根1 = {:.2f} + {:.2f}j\".format(root1.real, root1.imag))\n",
"print(\"根2 = {:.2f} + {:.2f}j\".format(root2.real, root2.imag))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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
}