You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

1_Basics.ipynb 20 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# Python 基础"
  8. ]
  9. },
  10. {
  11. "cell_type": "markdown",
  12. "metadata": {},
  13. "source": [
  14. "## 1. 导入库与Python之禅\n"
  15. ]
  16. },
  17. {
  18. "cell_type": "code",
  19. "execution_count": 1,
  20. "metadata": {},
  21. "outputs": [
  22. {
  23. "data": {
  24. "text/plain": [
  25. "['0-ipython_notebook.ipynb',\n",
  26. " '0-ipython_notebook_EN.ipynb',\n",
  27. " '1_Basics.ipynb',\n",
  28. " '1_Basics_EN.ipynb',\n",
  29. " '2_Print_Statement.ipynb',\n",
  30. " '2_Print_Statement_EN.ipynb',\n",
  31. " '3_Data_Structure_1.ipynb',\n",
  32. " '3_Data_Structure_1_EN.ipynb',\n",
  33. " '4_Data_Structure_2.ipynb',\n",
  34. " '4_Data_Structure_2_EN.ipynb',\n",
  35. " '5_Control_Flow.ipynb',\n",
  36. " '5_Control_Flow_EN.ipynb',\n",
  37. " '6_Function.ipynb',\n",
  38. " '6_Function_EN.ipynb',\n",
  39. " '7_Class.ipynb',\n",
  40. " '7_Class_EN.ipynb',\n",
  41. " 'Python.pdf',\n",
  42. " 'README.md',\n",
  43. " 'README_ENG.md',\n",
  44. " 'images',\n",
  45. " 'test.txt',\n",
  46. " '.ipynb_checkpoints']"
  47. ]
  48. },
  49. "execution_count": 1,
  50. "metadata": {},
  51. "output_type": "execute_result"
  52. }
  53. ],
  54. "source": [
  55. "# 导入库\n",
  56. "import os\n",
  57. "os.listdir('.')"
  58. ]
  59. },
  60. {
  61. "cell_type": "code",
  62. "execution_count": 2,
  63. "metadata": {},
  64. "outputs": [
  65. {
  66. "name": "stdout",
  67. "output_type": "stream",
  68. "text": [
  69. "The Zen of Python, by Tim Peters\n",
  70. "\n",
  71. "Beautiful is better than ugly.\n",
  72. "Explicit is better than implicit.\n",
  73. "Simple is better than complex.\n",
  74. "Complex is better than complicated.\n",
  75. "Flat is better than nested.\n",
  76. "Sparse is better than dense.\n",
  77. "Readability counts.\n",
  78. "Special cases aren't special enough to break the rules.\n",
  79. "Although practicality beats purity.\n",
  80. "Errors should never pass silently.\n",
  81. "Unless explicitly silenced.\n",
  82. "In the face of ambiguity, refuse the temptation to guess.\n",
  83. "There should be one-- and preferably only one --obvious way to do it.\n",
  84. "Although that way may not be obvious at first unless you're Dutch.\n",
  85. "Now is better than never.\n",
  86. "Although never is often better than *right* now.\n",
  87. "If the implementation is hard to explain, it's a bad idea.\n",
  88. "If the implementation is easy to explain, it may be a good idea.\n",
  89. "Namespaces are one honking great idea -- let's do more of those!\n"
  90. ]
  91. }
  92. ],
  93. "source": [
  94. "import this"
  95. ]
  96. },
  97. {
  98. "cell_type": "markdown",
  99. "metadata": {},
  100. "source": [
  101. "**Python 之禅, by Tim Peters**\n",
  102. "```\n",
  103. "优美胜于丑陋(Python 以编写优美的代码为目标)\n",
  104. "明了胜于晦涩(优美的代码应当是明了的,命名规范,风格相似)\n",
  105. "简洁胜于复杂(优美的代码应当是简洁的,不要有复杂的内部实现)\n",
  106. "复杂胜于凌乱(如果复杂不可避免,那代码间也不能有难懂的关系,要保持接口简洁)\n",
  107. "扁平胜于嵌套(优美的代码应当是扁平的,不能有太多的嵌套)\n",
  108. "间隔胜于紧凑(优美的代码有适当的间隔,不要奢望一行代码解决问题)\n",
  109. "可读性很重要(优美的代码是可读的)\n",
  110. "即便假借特例的实用性之名,也不可违背这些规则(这些规则至高无上)\n",
  111. "不要包容所有错误,除非你确定需要这样做(精准地捕获异常,不写except:pass风格的代码)\n",
  112. "当存在多种可能,不要尝试去猜测\n",
  113. "而是尽量找一种,最好是唯一一种明显的解决方案(如果不确定,就用穷举法)\n",
  114. "虽然这并不容易,因为你不是 Python 之父(这里的 Dutch 是指 Guido)\n",
  115. "做也许好过不做,但不假思索就动手还不如不做(动手之前要细思量)\n",
  116. "如果你无法向人描述你的方案,那肯定不是一个好方案;反之亦然(方案测评标准)\n",
  117. "命名空间是一种绝妙的理念,我们应当多加利用(倡导与号召)\n",
  118. "```"
  119. ]
  120. },
  121. {
  122. "cell_type": "markdown",
  123. "metadata": {},
  124. "source": [
  125. "## 2. 变量"
  126. ]
  127. },
  128. {
  129. "cell_type": "markdown",
  130. "metadata": {},
  131. "source": [
  132. "用于表示某物或值的名称称为**变量**。在Python中,可以声明变量并为其赋值,如下所示:"
  133. ]
  134. },
  135. {
  136. "cell_type": "code",
  137. "execution_count": 3,
  138. "metadata": {
  139. "collapsed": true
  140. },
  141. "outputs": [],
  142. "source": [
  143. "x = 2\n",
  144. "y = 5\n",
  145. "xy = 'Hey'"
  146. ]
  147. },
  148. {
  149. "cell_type": "code",
  150. "execution_count": 4,
  151. "metadata": {},
  152. "outputs": [
  153. {
  154. "name": "stdout",
  155. "output_type": "stream",
  156. "text": [
  157. "7 Hey\n"
  158. ]
  159. }
  160. ],
  161. "source": [
  162. "print(x+y, xy)"
  163. ]
  164. },
  165. {
  166. "cell_type": "markdown",
  167. "metadata": {},
  168. "source": [
  169. "多变量可以被赋予相同的名字。"
  170. ]
  171. },
  172. {
  173. "cell_type": "code",
  174. "execution_count": 5,
  175. "metadata": {
  176. "collapsed": true
  177. },
  178. "outputs": [],
  179. "source": [
  180. "x = y = 1"
  181. ]
  182. },
  183. {
  184. "cell_type": "code",
  185. "execution_count": 6,
  186. "metadata": {},
  187. "outputs": [
  188. {
  189. "name": "stdout",
  190. "output_type": "stream",
  191. "text": [
  192. "1 1\n"
  193. ]
  194. }
  195. ],
  196. "source": [
  197. "print(x,y)"
  198. ]
  199. },
  200. {
  201. "cell_type": "markdown",
  202. "metadata": {},
  203. "source": [
  204. "## 3. 运算符"
  205. ]
  206. },
  207. {
  208. "cell_type": "markdown",
  209. "metadata": {},
  210. "source": [
  211. "### 3.1 算数运算符"
  212. ]
  213. },
  214. {
  215. "cell_type": "markdown",
  216. "metadata": {},
  217. "source": [
  218. "| Symbol | Task Performed |\n",
  219. "|----|---|\n",
  220. "| + | Addition |\n",
  221. "| - | Subtraction |\n",
  222. "| / | division |\n",
  223. "| % | mod |\n",
  224. "| * | multiplication |\n",
  225. "| // | floor division |\n",
  226. "| ** | to the power of |"
  227. ]
  228. },
  229. {
  230. "cell_type": "code",
  231. "execution_count": 7,
  232. "metadata": {},
  233. "outputs": [
  234. {
  235. "data": {
  236. "text/plain": [
  237. "3"
  238. ]
  239. },
  240. "execution_count": 7,
  241. "metadata": {},
  242. "output_type": "execute_result"
  243. }
  244. ],
  245. "source": [
  246. "1+2"
  247. ]
  248. },
  249. {
  250. "cell_type": "code",
  251. "execution_count": 8,
  252. "metadata": {},
  253. "outputs": [
  254. {
  255. "data": {
  256. "text/plain": [
  257. "1"
  258. ]
  259. },
  260. "execution_count": 8,
  261. "metadata": {},
  262. "output_type": "execute_result"
  263. }
  264. ],
  265. "source": [
  266. "2-1"
  267. ]
  268. },
  269. {
  270. "cell_type": "code",
  271. "execution_count": 9,
  272. "metadata": {},
  273. "outputs": [
  274. {
  275. "data": {
  276. "text/plain": [
  277. "2"
  278. ]
  279. },
  280. "execution_count": 9,
  281. "metadata": {},
  282. "output_type": "execute_result"
  283. }
  284. ],
  285. "source": [
  286. "1*2"
  287. ]
  288. },
  289. {
  290. "cell_type": "code",
  291. "execution_count": 10,
  292. "metadata": {},
  293. "outputs": [
  294. {
  295. "data": {
  296. "text/plain": [
  297. "0.5"
  298. ]
  299. },
  300. "execution_count": 10,
  301. "metadata": {},
  302. "output_type": "execute_result"
  303. }
  304. ],
  305. "source": [
  306. "1/2"
  307. ]
  308. },
  309. {
  310. "cell_type": "markdown",
  311. "metadata": {},
  312. "source": [
  313. "Python3版本之后,自动将整数的除法转换成浮点数,这样和人类的直觉比较一致。"
  314. ]
  315. },
  316. {
  317. "cell_type": "code",
  318. "execution_count": 11,
  319. "metadata": {},
  320. "outputs": [
  321. {
  322. "data": {
  323. "text/plain": [
  324. "0.5"
  325. ]
  326. },
  327. "execution_count": 11,
  328. "metadata": {},
  329. "output_type": "execute_result"
  330. }
  331. ],
  332. "source": [
  333. "1.0/2"
  334. ]
  335. },
  336. {
  337. "cell_type": "code",
  338. "execution_count": 12,
  339. "metadata": {},
  340. "outputs": [
  341. {
  342. "data": {
  343. "text/plain": [
  344. "0.5"
  345. ]
  346. },
  347. "execution_count": 12,
  348. "metadata": {},
  349. "output_type": "execute_result"
  350. }
  351. ],
  352. "source": [
  353. "1/2.0"
  354. ]
  355. },
  356. {
  357. "cell_type": "code",
  358. "execution_count": 13,
  359. "metadata": {},
  360. "outputs": [
  361. {
  362. "data": {
  363. "text/plain": [
  364. "5"
  365. ]
  366. },
  367. "execution_count": 13,
  368. "metadata": {},
  369. "output_type": "execute_result"
  370. }
  371. ],
  372. "source": [
  373. "15%10"
  374. ]
  375. },
  376. {
  377. "cell_type": "markdown",
  378. "metadata": {},
  379. "source": [
  380. "地板除法(floor divide)就是将这样得到的结果转换成最接近的整数。"
  381. ]
  382. },
  383. {
  384. "cell_type": "code",
  385. "execution_count": 14,
  386. "metadata": {},
  387. "outputs": [
  388. {
  389. "data": {
  390. "text/plain": [
  391. "1.0"
  392. ]
  393. },
  394. "execution_count": 14,
  395. "metadata": {},
  396. "output_type": "execute_result"
  397. }
  398. ],
  399. "source": [
  400. "2.8//2.0"
  401. ]
  402. },
  403. {
  404. "cell_type": "markdown",
  405. "metadata": {},
  406. "source": [
  407. "### 3.2 关系运算符"
  408. ]
  409. },
  410. {
  411. "cell_type": "markdown",
  412. "metadata": {},
  413. "source": [
  414. "| Symbol | Task Performed |\n",
  415. "|----|---|\n",
  416. "| == | True, if it is equal |\n",
  417. "| != | True, if not equal to |\n",
  418. "| < | less than |\n",
  419. "| > | greater than |\n",
  420. "| <= | less than or equal to |\n",
  421. "| >= | greater than or equal to |"
  422. ]
  423. },
  424. {
  425. "cell_type": "code",
  426. "execution_count": 15,
  427. "metadata": {
  428. "collapsed": true
  429. },
  430. "outputs": [],
  431. "source": [
  432. "z = 1"
  433. ]
  434. },
  435. {
  436. "cell_type": "code",
  437. "execution_count": 16,
  438. "metadata": {},
  439. "outputs": [
  440. {
  441. "data": {
  442. "text/plain": [
  443. "True"
  444. ]
  445. },
  446. "execution_count": 16,
  447. "metadata": {},
  448. "output_type": "execute_result"
  449. }
  450. ],
  451. "source": [
  452. "z == 1"
  453. ]
  454. },
  455. {
  456. "cell_type": "code",
  457. "execution_count": 17,
  458. "metadata": {},
  459. "outputs": [
  460. {
  461. "data": {
  462. "text/plain": [
  463. "False"
  464. ]
  465. },
  466. "execution_count": 17,
  467. "metadata": {},
  468. "output_type": "execute_result"
  469. }
  470. ],
  471. "source": [
  472. "z > 1"
  473. ]
  474. },
  475. {
  476. "cell_type": "markdown",
  477. "metadata": {},
  478. "source": [
  479. "### 3.3 位运算符"
  480. ]
  481. },
  482. {
  483. "cell_type": "markdown",
  484. "metadata": {},
  485. "source": [
  486. "| Symbol | Task Performed |\n",
  487. "|----|---|\n",
  488. "| & | Logical And |\n",
  489. "| l | Logical OR |\n",
  490. "| ^ | XOR |\n",
  491. "| ~ | Negate |\n",
  492. "| >> | Right shift |\n",
  493. "| << | Left shift |"
  494. ]
  495. },
  496. {
  497. "cell_type": "code",
  498. "execution_count": 18,
  499. "metadata": {
  500. "collapsed": true
  501. },
  502. "outputs": [],
  503. "source": [
  504. "a = 2 #10\n",
  505. "b = 3 #11"
  506. ]
  507. },
  508. {
  509. "cell_type": "code",
  510. "execution_count": 19,
  511. "metadata": {},
  512. "outputs": [
  513. {
  514. "name": "stdout",
  515. "output_type": "stream",
  516. "text": [
  517. "2\n",
  518. "0b10\n"
  519. ]
  520. }
  521. ],
  522. "source": [
  523. "print(a & b)\n",
  524. "print(bin(a&b))"
  525. ]
  526. },
  527. {
  528. "cell_type": "code",
  529. "execution_count": 20,
  530. "metadata": {},
  531. "outputs": [
  532. {
  533. "data": {
  534. "text/plain": [
  535. "2"
  536. ]
  537. },
  538. "execution_count": 20,
  539. "metadata": {},
  540. "output_type": "execute_result"
  541. }
  542. ],
  543. "source": [
  544. "5 >> 1"
  545. ]
  546. },
  547. {
  548. "cell_type": "markdown",
  549. "metadata": {},
  550. "source": [
  551. "0000 0101 -> 5 \n",
  552. "\n",
  553. "将数字向右移1并填充0。\n",
  554. "\n",
  555. "0000 0010 -> 2"
  556. ]
  557. },
  558. {
  559. "cell_type": "code",
  560. "execution_count": 21,
  561. "metadata": {},
  562. "outputs": [
  563. {
  564. "data": {
  565. "text/plain": [
  566. "10"
  567. ]
  568. },
  569. "execution_count": 21,
  570. "metadata": {},
  571. "output_type": "execute_result"
  572. }
  573. ],
  574. "source": [
  575. "5 << 1"
  576. ]
  577. },
  578. {
  579. "cell_type": "markdown",
  580. "metadata": {},
  581. "source": [
  582. "0000 0101 -> 5 \n",
  583. "\n",
  584. "将数字做移1并填充0。\n",
  585. "\n",
  586. "0000 1010 -> 10"
  587. ]
  588. },
  589. {
  590. "cell_type": "markdown",
  591. "metadata": {},
  592. "source": [
  593. "## 4. 内置函数"
  594. ]
  595. },
  596. {
  597. "cell_type": "markdown",
  598. "metadata": {},
  599. "source": [
  600. "Python带有预先构建的函数"
  601. ]
  602. },
  603. {
  604. "cell_type": "markdown",
  605. "metadata": {},
  606. "source": [
  607. "### 4.1 从一个进制到另一个进制的转换"
  608. ]
  609. },
  610. {
  611. "cell_type": "markdown",
  612. "metadata": {},
  613. "source": [
  614. "从十六进制到十进制的转换是通过向十六进制值添加前缀**0x**来完成的,反之亦然,使用内置的**hex()**,通过在八进制值中添加前缀**0**,从八进制到十进制,或者通过使用内建函数**oct()** 从八进制到十进制。"
  615. ]
  616. },
  617. {
  618. "cell_type": "code",
  619. "execution_count": 22,
  620. "metadata": {},
  621. "outputs": [
  622. {
  623. "data": {
  624. "text/plain": [
  625. "'0xaa'"
  626. ]
  627. },
  628. "execution_count": 22,
  629. "metadata": {},
  630. "output_type": "execute_result"
  631. }
  632. ],
  633. "source": [
  634. "hex(170)"
  635. ]
  636. },
  637. {
  638. "cell_type": "code",
  639. "execution_count": 23,
  640. "metadata": {},
  641. "outputs": [
  642. {
  643. "data": {
  644. "text/plain": [
  645. "170"
  646. ]
  647. },
  648. "execution_count": 23,
  649. "metadata": {},
  650. "output_type": "execute_result"
  651. }
  652. ],
  653. "source": [
  654. "0xAA"
  655. ]
  656. },
  657. {
  658. "cell_type": "code",
  659. "execution_count": 24,
  660. "metadata": {},
  661. "outputs": [
  662. {
  663. "data": {
  664. "text/plain": [
  665. "'0o10'"
  666. ]
  667. },
  668. "execution_count": 24,
  669. "metadata": {},
  670. "output_type": "execute_result"
  671. }
  672. ],
  673. "source": [
  674. "oct(8)"
  675. ]
  676. },
  677. {
  678. "cell_type": "markdown",
  679. "metadata": {},
  680. "source": [
  681. "**int( )** 在用于转换时接受两个值,一个是不同数字系统中的值,另一个是它的基数。注意,不同数字系统中的输入数字应该是字符串类型。"
  682. ]
  683. },
  684. {
  685. "cell_type": "code",
  686. "execution_count": 25,
  687. "metadata": {},
  688. "outputs": [
  689. {
  690. "name": "stdout",
  691. "output_type": "stream",
  692. "text": [
  693. "8\n",
  694. "170\n",
  695. "10\n"
  696. ]
  697. }
  698. ],
  699. "source": [
  700. "print(int('010',8))\n",
  701. "print(int('0xaa',16))\n",
  702. "print(int('1010',2))"
  703. ]
  704. },
  705. {
  706. "cell_type": "markdown",
  707. "metadata": {},
  708. "source": [
  709. "**int( )** 也可用于仅获取浮点数的整数值,或用于将字符串类型的数字转换为整数格式。相似地,函数 **str( )** 可以被用来将整形转化为字符串类型。"
  710. ]
  711. },
  712. {
  713. "cell_type": "code",
  714. "execution_count": 26,
  715. "metadata": {},
  716. "outputs": [
  717. {
  718. "name": "stdout",
  719. "output_type": "stream",
  720. "text": [
  721. "7\n",
  722. "7\n"
  723. ]
  724. }
  725. ],
  726. "source": [
  727. "print(int(7.7))\n",
  728. "print(int('7'))"
  729. ]
  730. },
  731. {
  732. "cell_type": "markdown",
  733. "metadata": {},
  734. "source": [
  735. "同样需要注意的是 **bin( )** 被用作二进制而 **float( )** 被用作十进制、浮点值。**chr( )** 用于将ASCII码转换为等效的字母表,**ord( )** 被反过来使用。"
  736. ]
  737. },
  738. {
  739. "cell_type": "code",
  740. "execution_count": 27,
  741. "metadata": {},
  742. "outputs": [
  743. {
  744. "data": {
  745. "text/plain": [
  746. "'b'"
  747. ]
  748. },
  749. "execution_count": 27,
  750. "metadata": {},
  751. "output_type": "execute_result"
  752. }
  753. ],
  754. "source": [
  755. "chr(98)"
  756. ]
  757. },
  758. {
  759. "cell_type": "code",
  760. "execution_count": 28,
  761. "metadata": {},
  762. "outputs": [
  763. {
  764. "data": {
  765. "text/plain": [
  766. "98"
  767. ]
  768. },
  769. "execution_count": 28,
  770. "metadata": {},
  771. "output_type": "execute_result"
  772. }
  773. ],
  774. "source": [
  775. "ord('b')"
  776. ]
  777. },
  778. {
  779. "cell_type": "markdown",
  780. "metadata": {},
  781. "source": [
  782. "### 4.2 数值函数"
  783. ]
  784. },
  785. {
  786. "cell_type": "markdown",
  787. "metadata": {},
  788. "source": [
  789. "**round( )** 函数将输入值四舍五入到指定的位数或最接近的整数。"
  790. ]
  791. },
  792. {
  793. "cell_type": "code",
  794. "execution_count": 29,
  795. "metadata": {
  796. "scrolled": false
  797. },
  798. "outputs": [
  799. {
  800. "name": "stdout",
  801. "output_type": "stream",
  802. "text": [
  803. "6\n",
  804. "4.56\n"
  805. ]
  806. }
  807. ],
  808. "source": [
  809. "print(round(5.6231))\n",
  810. "print(round(4.55892, 2))"
  811. ]
  812. },
  813. {
  814. "cell_type": "markdown",
  815. "metadata": {},
  816. "source": [
  817. "**complex( )** 被用来定义一个复数 **abs( )** 输出相同的绝对值。"
  818. ]
  819. },
  820. {
  821. "cell_type": "code",
  822. "execution_count": 30,
  823. "metadata": {},
  824. "outputs": [
  825. {
  826. "name": "stdout",
  827. "output_type": "stream",
  828. "text": [
  829. "5.385164807134504\n"
  830. ]
  831. }
  832. ],
  833. "source": [
  834. "c =complex('5+2j')\n",
  835. "print(abs(c))"
  836. ]
  837. },
  838. {
  839. "cell_type": "markdown",
  840. "metadata": {},
  841. "source": [
  842. "**divmod(x,y)** 输出商和余数在元组的格式(商,余数)。"
  843. ]
  844. },
  845. {
  846. "cell_type": "code",
  847. "execution_count": 31,
  848. "metadata": {},
  849. "outputs": [
  850. {
  851. "data": {
  852. "text/plain": [
  853. "(4, 1)"
  854. ]
  855. },
  856. "execution_count": 31,
  857. "metadata": {},
  858. "output_type": "execute_result"
  859. }
  860. ],
  861. "source": [
  862. "divmod(9,2)"
  863. ]
  864. },
  865. {
  866. "cell_type": "markdown",
  867. "metadata": {},
  868. "source": [
  869. "如果第一个参数是该类的实例,**isinstance( )** 则返回True。还可以同时检查多个类。"
  870. ]
  871. },
  872. {
  873. "cell_type": "code",
  874. "execution_count": 32,
  875. "metadata": {},
  876. "outputs": [
  877. {
  878. "name": "stdout",
  879. "output_type": "stream",
  880. "text": [
  881. "True\n",
  882. "False\n",
  883. "True\n"
  884. ]
  885. }
  886. ],
  887. "source": [
  888. "print(isinstance(1, int))\n",
  889. "print(isinstance(1.0,int))\n",
  890. "print(isinstance(1.0,(int,float)))"
  891. ]
  892. },
  893. {
  894. "cell_type": "markdown",
  895. "metadata": {},
  896. "source": [
  897. "**pow(x,y,z)** 可以被用来找到幂函数$x^y$也可以找到第三个指定数字的mod值,即:($x^y$ % z)。"
  898. ]
  899. },
  900. {
  901. "cell_type": "code",
  902. "execution_count": 33,
  903. "metadata": {},
  904. "outputs": [
  905. {
  906. "name": "stdout",
  907. "output_type": "stream",
  908. "text": [
  909. "27\n",
  910. "2\n"
  911. ]
  912. }
  913. ],
  914. "source": [
  915. "print(pow(3,3))\n",
  916. "print(pow(3,3,5))"
  917. ]
  918. },
  919. {
  920. "cell_type": "markdown",
  921. "metadata": {},
  922. "source": [
  923. "**range( )** 函数输出指定范围的整数。它还可以用于通过指定特定范围内的两个数字之间的差来生成一个序列,元素以列表的形式返回。"
  924. ]
  925. },
  926. {
  927. "cell_type": "code",
  928. "execution_count": 38,
  929. "metadata": {},
  930. "outputs": [
  931. {
  932. "name": "stdout",
  933. "output_type": "stream",
  934. "text": [
  935. "[0, 1, 2]\n",
  936. "[2, 3, 4, 5, 6, 7, 8]\n",
  937. "[2, 10, 18, 26]\n"
  938. ]
  939. }
  940. ],
  941. "source": [
  942. "print(list(range(3)))\n",
  943. "print(list(range(2,9)))\n",
  944. "print(list(range(2,27,8)))"
  945. ]
  946. },
  947. {
  948. "cell_type": "markdown",
  949. "metadata": {},
  950. "source": [
  951. "### 4.3 接受用户输入"
  952. ]
  953. },
  954. {
  955. "cell_type": "markdown",
  956. "metadata": {},
  957. "source": [
  958. "**input( )** 接受输入并以字符串的形式保存。"
  959. ]
  960. },
  961. {
  962. "cell_type": "code",
  963. "execution_count": 35,
  964. "metadata": {},
  965. "outputs": [
  966. {
  967. "name": "stdout",
  968. "output_type": "stream",
  969. "text": [
  970. "Type something here and it will be stored in variable abc \thello\n"
  971. ]
  972. }
  973. ],
  974. "source": [
  975. "abc = input(\"Type something here and it will be stored in variable abc \\t\")"
  976. ]
  977. },
  978. {
  979. "cell_type": "code",
  980. "execution_count": 36,
  981. "metadata": {},
  982. "outputs": [
  983. {
  984. "data": {
  985. "text/plain": [
  986. "str"
  987. ]
  988. },
  989. "execution_count": 36,
  990. "metadata": {},
  991. "output_type": "execute_result"
  992. }
  993. ],
  994. "source": [
  995. "type(abc)"
  996. ]
  997. }
  998. ],
  999. "metadata": {
  1000. "kernelspec": {
  1001. "display_name": "Python 3",
  1002. "language": "python",
  1003. "name": "python3"
  1004. },
  1005. "language_info": {
  1006. "codemirror_mode": {
  1007. "name": "ipython",
  1008. "version": 3
  1009. },
  1010. "file_extension": ".py",
  1011. "mimetype": "text/x-python",
  1012. "name": "python",
  1013. "nbconvert_exporter": "python",
  1014. "pygments_lexer": "ipython3",
  1015. "version": "3.7.9"
  1016. }
  1017. },
  1018. "nbformat": 4,
  1019. "nbformat_minor": 1
  1020. }

机器学习越来越多应用到飞行器、机器人等领域,其目的是利用计算机实现类似人类的智能,从而实现装备的智能化与无人化。本课程旨在引导学生掌握机器学习的基本知识、典型方法与技术,通过具体的应用案例激发学生对该学科的兴趣,鼓励学生能够从人工智能的角度来分析、解决飞行器、机器人所面临的问题和挑战。本课程主要内容包括Python编程基础,机器学习模型,无监督学习、监督学习、深度学习基础知识与实现,并学习如何利用机器学习解决实际问题,从而全面提升自我的《综合能力》。