master
/ 2.1 数值类型.ipynb

2.1 数值类型.ipynb @master

341084b
 
 
 
 
 
 
 
 
 
 
 
 
d487d71
 
 
 
341084b
 
 
 
 
 
 
 
 
 
 
 
 
d487d71
 
341084b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
d487d71
341084b
 
 
d487d71
 
341084b
 
 
 
d487d71
341084b
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
 
 
 
d487d71
341084b
 
 
 
d487d71
341084b
 
 
 
 
d487d71
 
 
341084b
d487d71
341084b
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
d487d71
341084b
 
 
d487d71
 
 
 
 
 
 
 
 
 
 
 
341084b
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
d487d71
341084b
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
 
 
 
 
 
d487d71
 
341084b
 
 
 
d487d71
 
 
 
 
 
 
 
 
 
 
 
 
341084b
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d487d71
 
 
 
 
 
 
341084b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
{
 "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\">科学计数法表示为&lt;x&gt;e&lt;n&gt;,等价于数学中的$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
}