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.

5_Control_Flow_EN.ipynb 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# Control Flow Statements"
  8. ]
  9. },
  10. {
  11. "cell_type": "markdown",
  12. "metadata": {},
  13. "source": [
  14. "## 1. If"
  15. ]
  16. },
  17. {
  18. "cell_type": "markdown",
  19. "metadata": {},
  20. "source": [
  21. "if some_condition:\n",
  22. " \n",
  23. " algorithm"
  24. ]
  25. },
  26. {
  27. "cell_type": "code",
  28. "execution_count": 3,
  29. "metadata": {},
  30. "outputs": [
  31. {
  32. "name": "stdout",
  33. "output_type": "stream",
  34. "text": [
  35. "Welcome!\n"
  36. ]
  37. }
  38. ],
  39. "source": [
  40. "x = 4\n",
  41. "if x >10:\n",
  42. " print(\"Hello\")\n",
  43. "else:\n",
  44. " print(\"Welcome!\")"
  45. ]
  46. },
  47. {
  48. "cell_type": "markdown",
  49. "metadata": {},
  50. "source": [
  51. "## 2. If-else"
  52. ]
  53. },
  54. {
  55. "cell_type": "markdown",
  56. "metadata": {},
  57. "source": [
  58. "if some_condition:\n",
  59. " \n",
  60. " algorithm\n",
  61. " \n",
  62. "else:\n",
  63. " \n",
  64. " algorithm"
  65. ]
  66. },
  67. {
  68. "cell_type": "code",
  69. "execution_count": 2,
  70. "metadata": {},
  71. "outputs": [
  72. {
  73. "name": "stdout",
  74. "output_type": "stream",
  75. "text": [
  76. "hello\n"
  77. ]
  78. }
  79. ],
  80. "source": [
  81. "x = 12\n",
  82. "if x > 10:\n",
  83. " print(\"hello\")\n",
  84. "else:\n",
  85. " print(\"world\")"
  86. ]
  87. },
  88. {
  89. "cell_type": "markdown",
  90. "metadata": {},
  91. "source": [
  92. "## 3. if-elif"
  93. ]
  94. },
  95. {
  96. "cell_type": "markdown",
  97. "metadata": {},
  98. "source": [
  99. "if some_condition:\n",
  100. " \n",
  101. " algorithm\n",
  102. "\n",
  103. "elif some_condition:\n",
  104. " \n",
  105. " algorithm\n",
  106. "\n",
  107. "else:\n",
  108. " \n",
  109. " algorithm"
  110. ]
  111. },
  112. {
  113. "cell_type": "code",
  114. "execution_count": 3,
  115. "metadata": {},
  116. "outputs": [
  117. {
  118. "name": "stdout",
  119. "output_type": "stream",
  120. "text": [
  121. "x<y\n"
  122. ]
  123. }
  124. ],
  125. "source": [
  126. "x = 10\n",
  127. "y = 12\n",
  128. "if x > y:\n",
  129. " print(\"x>y\")\n",
  130. "elif x < y:\n",
  131. " print(\"x<y\")\n",
  132. "else:\n",
  133. " print(\"x=y\")"
  134. ]
  135. },
  136. {
  137. "cell_type": "markdown",
  138. "metadata": {},
  139. "source": [
  140. "if statement inside a if statement or if-elif or if-else are called as nested if statements."
  141. ]
  142. },
  143. {
  144. "cell_type": "code",
  145. "execution_count": 4,
  146. "metadata": {},
  147. "outputs": [
  148. {
  149. "name": "stdout",
  150. "output_type": "stream",
  151. "text": [
  152. "x<y\n",
  153. "x=10\n"
  154. ]
  155. }
  156. ],
  157. "source": [
  158. "x = 10\n",
  159. "y = 12\n",
  160. "if x > y:\n",
  161. " print(\"x>y\")\n",
  162. "elif x < y:\n",
  163. " print(\"x<y\")\n",
  164. " if x==10:\n",
  165. " print(\"x=10\")\n",
  166. " else:\n",
  167. " print(\"invalid\")\n",
  168. "else:\n",
  169. " print(\"x=y\")"
  170. ]
  171. },
  172. {
  173. "cell_type": "markdown",
  174. "metadata": {},
  175. "source": [
  176. "## 4. Loops"
  177. ]
  178. },
  179. {
  180. "cell_type": "markdown",
  181. "metadata": {},
  182. "source": [
  183. "### 4.1 For"
  184. ]
  185. },
  186. {
  187. "cell_type": "markdown",
  188. "metadata": {},
  189. "source": [
  190. "for variable in something:\n",
  191. " \n",
  192. " algorithm"
  193. ]
  194. },
  195. {
  196. "cell_type": "code",
  197. "execution_count": 5,
  198. "metadata": {},
  199. "outputs": [
  200. {
  201. "name": "stdout",
  202. "output_type": "stream",
  203. "text": [
  204. "0\n",
  205. "1\n",
  206. "2\n",
  207. "3\n",
  208. "4\n"
  209. ]
  210. }
  211. ],
  212. "source": [
  213. "for i in range(5):\n",
  214. " print(i)"
  215. ]
  216. },
  217. {
  218. "cell_type": "code",
  219. "execution_count": 6,
  220. "metadata": {},
  221. "outputs": [
  222. {
  223. "name": "stdout",
  224. "output_type": "stream",
  225. "text": [
  226. "1\n",
  227. "2\n",
  228. "5\n",
  229. "6\n"
  230. ]
  231. }
  232. ],
  233. "source": [
  234. "a = [1, 2, 5, 6]\n",
  235. "for i in a:\n",
  236. " print(i)"
  237. ]
  238. },
  239. {
  240. "cell_type": "markdown",
  241. "metadata": {},
  242. "source": [
  243. "In the above example, i iterates over the 0,1,2,3,4. Every time it takes each value and executes the algorithm inside the loop. It is also possible to iterate over a nested list illustrated below."
  244. ]
  245. },
  246. {
  247. "cell_type": "code",
  248. "execution_count": 7,
  249. "metadata": {},
  250. "outputs": [
  251. {
  252. "name": "stdout",
  253. "output_type": "stream",
  254. "text": [
  255. "[1, 2, 3]\n",
  256. "[4, 5, 6]\n",
  257. "[7, 8, 9]\n"
  258. ]
  259. }
  260. ],
  261. "source": [
  262. "list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
  263. "for list1 in list_of_lists:\n",
  264. " print(list1)"
  265. ]
  266. },
  267. {
  268. "cell_type": "markdown",
  269. "metadata": {},
  270. "source": [
  271. "A use case of a nested for loop in this case would be,"
  272. ]
  273. },
  274. {
  275. "cell_type": "code",
  276. "execution_count": 7,
  277. "metadata": {},
  278. "outputs": [
  279. {
  280. "name": "stdout",
  281. "output_type": "stream",
  282. "text": [
  283. "1\n",
  284. "2\n",
  285. "3\n",
  286. "\n",
  287. "4\n",
  288. "5\n",
  289. "6\n",
  290. "\n",
  291. "7\n",
  292. "8\n",
  293. "9\n",
  294. "\n"
  295. ]
  296. }
  297. ],
  298. "source": [
  299. "list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
  300. "for list1 in list_of_lists:\n",
  301. " for x in list1:\n",
  302. " print(x)\n",
  303. " print()"
  304. ]
  305. },
  306. {
  307. "cell_type": "markdown",
  308. "metadata": {},
  309. "source": [
  310. "### 4.2 While"
  311. ]
  312. },
  313. {
  314. "cell_type": "markdown",
  315. "metadata": {},
  316. "source": [
  317. "while some_condition:\n",
  318. " \n",
  319. " algorithm"
  320. ]
  321. },
  322. {
  323. "cell_type": "code",
  324. "execution_count": 8,
  325. "metadata": {},
  326. "outputs": [
  327. {
  328. "name": "stdout",
  329. "output_type": "stream",
  330. "text": [
  331. "1\n",
  332. "4\n",
  333. "Bye\n",
  334. "looping 4\n",
  335. "looping 5\n",
  336. "looping 6\n",
  337. "looping 7\n",
  338. "looping 8\n",
  339. "looping 9\n",
  340. "looping 10\n",
  341. "looping 11\n"
  342. ]
  343. }
  344. ],
  345. "source": [
  346. "i = 1\n",
  347. "while i < 3:\n",
  348. " print(i ** 2)\n",
  349. " i = i+1\n",
  350. "print('Bye')\n",
  351. "\n",
  352. "# do-untile\n",
  353. "while True:\n",
  354. " #do something\n",
  355. " i = i+1\n",
  356. " print('looping %3d' % i)\n",
  357. " \n",
  358. " # check \n",
  359. " if i>10: break"
  360. ]
  361. },
  362. {
  363. "cell_type": "markdown",
  364. "metadata": {},
  365. "source": [
  366. "## 5. Break"
  367. ]
  368. },
  369. {
  370. "cell_type": "markdown",
  371. "metadata": {},
  372. "source": [
  373. "As the name says. It is used to break out of a loop when a condition becomes true when executing the loop."
  374. ]
  375. },
  376. {
  377. "cell_type": "code",
  378. "execution_count": 12,
  379. "metadata": {},
  380. "outputs": [
  381. {
  382. "name": "stdout",
  383. "output_type": "stream",
  384. "text": [
  385. "0\n",
  386. "1\n",
  387. "2\n",
  388. "3\n",
  389. "4\n",
  390. "5\n",
  391. "6\n",
  392. "7\n"
  393. ]
  394. }
  395. ],
  396. "source": [
  397. "for i in range(100):\n",
  398. " print(i)\n",
  399. " if i>=7:\n",
  400. " break"
  401. ]
  402. },
  403. {
  404. "cell_type": "markdown",
  405. "metadata": {},
  406. "source": [
  407. "## 6. Continue"
  408. ]
  409. },
  410. {
  411. "cell_type": "markdown",
  412. "metadata": {},
  413. "source": [
  414. "This continues the rest of the loop. Sometimes when a condition is satisfied there are chances of the loop getting terminated. This can be avoided using continue statement. "
  415. ]
  416. },
  417. {
  418. "cell_type": "code",
  419. "execution_count": 9,
  420. "metadata": {},
  421. "outputs": [
  422. {
  423. "name": "stdout",
  424. "output_type": "stream",
  425. "text": [
  426. "0\n",
  427. "1\n",
  428. "2\n",
  429. "3\n",
  430. "4\n",
  431. "The end.\n",
  432. "The end.\n",
  433. "The end.\n",
  434. "The end.\n",
  435. "The end.\n"
  436. ]
  437. }
  438. ],
  439. "source": [
  440. "for i in range(10):\n",
  441. " if i>4:\n",
  442. " print(\"The end.\")\n",
  443. " continue\n",
  444. " elif i<7:\n",
  445. " print(i)"
  446. ]
  447. },
  448. {
  449. "cell_type": "markdown",
  450. "metadata": {},
  451. "source": [
  452. "## 7. List Comprehensions"
  453. ]
  454. },
  455. {
  456. "cell_type": "markdown",
  457. "metadata": {},
  458. "source": [
  459. "Python makes it simple to generate a required list with a single line of code using list comprehensions. For example If i need to generate multiples of say 27 I write the code using for loop as,"
  460. ]
  461. },
  462. {
  463. "cell_type": "code",
  464. "execution_count": 10,
  465. "metadata": {},
  466. "outputs": [
  467. {
  468. "name": "stdout",
  469. "output_type": "stream",
  470. "text": [
  471. "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]\n"
  472. ]
  473. }
  474. ],
  475. "source": [
  476. "res = []\n",
  477. "for i in range(1,11):\n",
  478. " x = 27*i\n",
  479. " res.append(x)\n",
  480. "print(res)"
  481. ]
  482. },
  483. {
  484. "cell_type": "markdown",
  485. "metadata": {},
  486. "source": [
  487. "Since you are generating another list altogether and that is what is required, List comprehensions is a more efficient way to solve this problem."
  488. ]
  489. },
  490. {
  491. "cell_type": "code",
  492. "execution_count": 11,
  493. "metadata": {},
  494. "outputs": [
  495. {
  496. "data": {
  497. "text/plain": [
  498. "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]"
  499. ]
  500. },
  501. "execution_count": 11,
  502. "metadata": {},
  503. "output_type": "execute_result"
  504. }
  505. ],
  506. "source": [
  507. "[27*x for x in range(1,11)]"
  508. ]
  509. },
  510. {
  511. "cell_type": "markdown",
  512. "metadata": {},
  513. "source": [
  514. "That's it!. Only remember to enclose it in square brackets"
  515. ]
  516. },
  517. {
  518. "cell_type": "markdown",
  519. "metadata": {},
  520. "source": [
  521. "Understanding the code, The first bit of the code is always the algorithm and then leave a space and then write the necessary loop. But you might be wondering can nested loops be extended to list comprehensions? Yes you can."
  522. ]
  523. },
  524. {
  525. "cell_type": "code",
  526. "execution_count": 12,
  527. "metadata": {},
  528. "outputs": [
  529. {
  530. "data": {
  531. "text/plain": [
  532. "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]"
  533. ]
  534. },
  535. "execution_count": 12,
  536. "metadata": {},
  537. "output_type": "execute_result"
  538. }
  539. ],
  540. "source": [
  541. "[27*x for x in range(1,20) if x<=10]"
  542. ]
  543. },
  544. {
  545. "cell_type": "code",
  546. "execution_count": 13,
  547. "metadata": {
  548. "scrolled": true
  549. },
  550. "outputs": [
  551. {
  552. "data": {
  553. "text/plain": [
  554. "{'108': 108,\n",
  555. " '135': 135,\n",
  556. " '162': 162,\n",
  557. " '189': 189,\n",
  558. " '216': 216,\n",
  559. " '243': 243,\n",
  560. " '27': 27,\n",
  561. " '270': 270,\n",
  562. " '54': 54,\n",
  563. " '81': 81}"
  564. ]
  565. },
  566. "execution_count": 13,
  567. "metadata": {},
  568. "output_type": "execute_result"
  569. }
  570. ],
  571. "source": [
  572. "{str(27*x):27*x for x in range(1,20) if x<=10}"
  573. ]
  574. },
  575. {
  576. "cell_type": "code",
  577. "execution_count": 14,
  578. "metadata": {},
  579. "outputs": [
  580. {
  581. "data": {
  582. "text/plain": [
  583. "(27, 54, 81, 108, 135, 162, 189, 216, 243, 270)"
  584. ]
  585. },
  586. "execution_count": 14,
  587. "metadata": {},
  588. "output_type": "execute_result"
  589. }
  590. ],
  591. "source": [
  592. "tuple((27*x for x in range(1,20) if x<=10))"
  593. ]
  594. },
  595. {
  596. "cell_type": "markdown",
  597. "metadata": {},
  598. "source": [
  599. "Let me add one more loop to make you understand better, "
  600. ]
  601. },
  602. {
  603. "cell_type": "code",
  604. "execution_count": 15,
  605. "metadata": {},
  606. "outputs": [
  607. {
  608. "data": {
  609. "text/plain": [
  610. "[1,\n",
  611. " 2,\n",
  612. " 3,\n",
  613. " 4,\n",
  614. " 5,\n",
  615. " 6,\n",
  616. " 7,\n",
  617. " 8,\n",
  618. " 9,\n",
  619. " 10,\n",
  620. " 28,\n",
  621. " 29,\n",
  622. " 30,\n",
  623. " 31,\n",
  624. " 32,\n",
  625. " 33,\n",
  626. " 34,\n",
  627. " 35,\n",
  628. " 36,\n",
  629. " 37,\n",
  630. " 55,\n",
  631. " 56,\n",
  632. " 57,\n",
  633. " 58,\n",
  634. " 59,\n",
  635. " 60,\n",
  636. " 61,\n",
  637. " 62,\n",
  638. " 63,\n",
  639. " 64,\n",
  640. " 82,\n",
  641. " 83,\n",
  642. " 84,\n",
  643. " 85,\n",
  644. " 86,\n",
  645. " 87,\n",
  646. " 88,\n",
  647. " 89,\n",
  648. " 90,\n",
  649. " 91,\n",
  650. " 109,\n",
  651. " 110,\n",
  652. " 111,\n",
  653. " 112,\n",
  654. " 113,\n",
  655. " 114,\n",
  656. " 115,\n",
  657. " 116,\n",
  658. " 117,\n",
  659. " 118]"
  660. ]
  661. },
  662. "execution_count": 15,
  663. "metadata": {},
  664. "output_type": "execute_result"
  665. }
  666. ],
  667. "source": [
  668. "[27*i+z for i in range(50) if i<5 for z in range(1,11)]"
  669. ]
  670. }
  671. ],
  672. "metadata": {
  673. "kernelspec": {
  674. "display_name": "Python 3",
  675. "language": "python",
  676. "name": "python3"
  677. },
  678. "language_info": {
  679. "codemirror_mode": {
  680. "name": "ipython",
  681. "version": 3
  682. },
  683. "file_extension": ".py",
  684. "mimetype": "text/x-python",
  685. "name": "python",
  686. "nbconvert_exporter": "python",
  687. "pygments_lexer": "ipython3",
  688. "version": "3.6.8"
  689. }
  690. },
  691. "nbformat": 4,
  692. "nbformat_minor": 1
  693. }

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