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.

05 Control Flow.ipynb 10 kB

6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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": 5,
  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": "markdown",
  224. "metadata": {},
  225. "source": [
  226. "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."
  227. ]
  228. },
  229. {
  230. "cell_type": "code",
  231. "execution_count": 6,
  232. "metadata": {},
  233. "outputs": [
  234. {
  235. "name": "stdout",
  236. "output_type": "stream",
  237. "text": [
  238. "[1, 2, 3]\n",
  239. "[4, 5, 6]\n",
  240. "[7, 8, 9]\n"
  241. ]
  242. }
  243. ],
  244. "source": [
  245. "list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
  246. "for list1 in list_of_lists:\n",
  247. " print(list1)"
  248. ]
  249. },
  250. {
  251. "cell_type": "markdown",
  252. "metadata": {},
  253. "source": [
  254. "A use case of a nested for loop in this case would be,"
  255. ]
  256. },
  257. {
  258. "cell_type": "code",
  259. "execution_count": 7,
  260. "metadata": {},
  261. "outputs": [
  262. {
  263. "name": "stdout",
  264. "output_type": "stream",
  265. "text": [
  266. "1\n",
  267. "2\n",
  268. "3\n",
  269. "4\n",
  270. "5\n",
  271. "6\n",
  272. "7\n",
  273. "8\n",
  274. "9\n"
  275. ]
  276. }
  277. ],
  278. "source": [
  279. "list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
  280. "for list1 in list_of_lists:\n",
  281. " for x in list1:\n",
  282. " print(x)"
  283. ]
  284. },
  285. {
  286. "cell_type": "markdown",
  287. "metadata": {},
  288. "source": [
  289. "### While"
  290. ]
  291. },
  292. {
  293. "cell_type": "markdown",
  294. "metadata": {},
  295. "source": [
  296. "while some_condition:\n",
  297. " \n",
  298. " algorithm"
  299. ]
  300. },
  301. {
  302. "cell_type": "code",
  303. "execution_count": 8,
  304. "metadata": {},
  305. "outputs": [
  306. {
  307. "name": "stdout",
  308. "output_type": "stream",
  309. "text": [
  310. "1\n",
  311. "4\n",
  312. "Bye\n"
  313. ]
  314. }
  315. ],
  316. "source": [
  317. "i = 1\n",
  318. "while i < 3:\n",
  319. " print(i ** 2)\n",
  320. " i = i+1\n",
  321. "print('Bye')"
  322. ]
  323. },
  324. {
  325. "cell_type": "markdown",
  326. "metadata": {},
  327. "source": [
  328. "## Break"
  329. ]
  330. },
  331. {
  332. "cell_type": "markdown",
  333. "metadata": {},
  334. "source": [
  335. "As the name says. It is used to break out of a loop when a condition becomes true when executing the loop."
  336. ]
  337. },
  338. {
  339. "cell_type": "code",
  340. "execution_count": 9,
  341. "metadata": {},
  342. "outputs": [
  343. {
  344. "name": "stdout",
  345. "output_type": "stream",
  346. "text": [
  347. "0\n",
  348. "1\n",
  349. "2\n",
  350. "3\n",
  351. "4\n",
  352. "5\n",
  353. "6\n",
  354. "7\n"
  355. ]
  356. }
  357. ],
  358. "source": [
  359. "for i in range(100):\n",
  360. " print i\n",
  361. " if i>=7:\n",
  362. " break"
  363. ]
  364. },
  365. {
  366. "cell_type": "markdown",
  367. "metadata": {},
  368. "source": [
  369. "## Continue"
  370. ]
  371. },
  372. {
  373. "cell_type": "markdown",
  374. "metadata": {},
  375. "source": [
  376. "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. "
  377. ]
  378. },
  379. {
  380. "cell_type": "code",
  381. "execution_count": 10,
  382. "metadata": {},
  383. "outputs": [
  384. {
  385. "name": "stdout",
  386. "output_type": "stream",
  387. "text": [
  388. "0\n",
  389. "1\n",
  390. "2\n",
  391. "3\n",
  392. "4\n",
  393. "The end.\n",
  394. "The end.\n",
  395. "The end.\n",
  396. "The end.\n",
  397. "The end.\n"
  398. ]
  399. }
  400. ],
  401. "source": [
  402. "for i in range(10):\n",
  403. " if i>4:\n",
  404. " print(\"The end.\")\n",
  405. " continue\n",
  406. " elif i<7:\n",
  407. " print(i)"
  408. ]
  409. },
  410. {
  411. "cell_type": "markdown",
  412. "metadata": {},
  413. "source": [
  414. "## List Comprehensions"
  415. ]
  416. },
  417. {
  418. "cell_type": "markdown",
  419. "metadata": {},
  420. "source": [
  421. "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,"
  422. ]
  423. },
  424. {
  425. "cell_type": "code",
  426. "execution_count": 11,
  427. "metadata": {},
  428. "outputs": [
  429. {
  430. "name": "stdout",
  431. "output_type": "stream",
  432. "text": [
  433. "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]\n"
  434. ]
  435. }
  436. ],
  437. "source": [
  438. "res = []\n",
  439. "for i in range(1,11):\n",
  440. " x = 27*i\n",
  441. " res.append(x)\n",
  442. "print res"
  443. ]
  444. },
  445. {
  446. "cell_type": "markdown",
  447. "metadata": {},
  448. "source": [
  449. "Since you are generating another list altogether and that is what is required, List comprehensions is a more efficient way to solve this problem."
  450. ]
  451. },
  452. {
  453. "cell_type": "code",
  454. "execution_count": 12,
  455. "metadata": {},
  456. "outputs": [
  457. {
  458. "data": {
  459. "text/plain": [
  460. "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]"
  461. ]
  462. },
  463. "execution_count": 12,
  464. "metadata": {},
  465. "output_type": "execute_result"
  466. }
  467. ],
  468. "source": [
  469. "[27*x for x in range(1,11)]"
  470. ]
  471. },
  472. {
  473. "cell_type": "markdown",
  474. "metadata": {},
  475. "source": [
  476. "That's it!. Only remember to enclose it in square brackets"
  477. ]
  478. },
  479. {
  480. "cell_type": "markdown",
  481. "metadata": {},
  482. "source": [
  483. "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."
  484. ]
  485. },
  486. {
  487. "cell_type": "code",
  488. "execution_count": 13,
  489. "metadata": {},
  490. "outputs": [
  491. {
  492. "data": {
  493. "text/plain": [
  494. "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]"
  495. ]
  496. },
  497. "execution_count": 13,
  498. "metadata": {},
  499. "output_type": "execute_result"
  500. }
  501. ],
  502. "source": [
  503. "[27*x for x in range(1,20) if x<=10]"
  504. ]
  505. },
  506. {
  507. "cell_type": "markdown",
  508. "metadata": {},
  509. "source": [
  510. "Let me add one more loop to make you understand better, "
  511. ]
  512. },
  513. {
  514. "cell_type": "code",
  515. "execution_count": 14,
  516. "metadata": {},
  517. "outputs": [
  518. {
  519. "data": {
  520. "text/plain": [
  521. "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]"
  522. ]
  523. },
  524. "execution_count": 14,
  525. "metadata": {},
  526. "output_type": "execute_result"
  527. }
  528. ],
  529. "source": [
  530. "[27*z for i in range(50) if i==27 for z in range(1,11)]"
  531. ]
  532. }
  533. ],
  534. "metadata": {
  535. "kernelspec": {
  536. "display_name": "Python 3",
  537. "language": "python",
  538. "name": "python3"
  539. },
  540. "language_info": {
  541. "codemirror_mode": {
  542. "name": "ipython",
  543. "version": 3
  544. },
  545. "file_extension": ".py",
  546. "mimetype": "text/x-python",
  547. "name": "python",
  548. "nbconvert_exporter": "python",
  549. "pygments_lexer": "ipython3",
  550. "version": "3.5.2"
  551. }
  552. },
  553. "nbformat": 4,
  554. "nbformat_minor": 1
  555. }

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

Contributors (1)