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

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