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

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