master
/ 2.4常用数学运算函数.ipynb

2.4常用数学运算函数.ipynb @master

d487d71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
{
 "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
}