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.

6_Function_EN.ipynb 22 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# Functions"
  8. ]
  9. },
  10. {
  11. "cell_type": "markdown",
  12. "metadata": {},
  13. "source": [
  14. "Most of the times, In a algorithm the statements keep repeating and it will be a tedious job to execute the same statements again and again and will consume a lot of memory and is not efficient. Enter Functions."
  15. ]
  16. },
  17. {
  18. "cell_type": "markdown",
  19. "metadata": {},
  20. "source": [
  21. "This is the basic syntax of a function"
  22. ]
  23. },
  24. {
  25. "cell_type": "markdown",
  26. "metadata": {},
  27. "source": [
  28. "```\n",
  29. "def funcname(arg1, arg2,... argN):\n",
  30. " \n",
  31. " ''' Document String'''\n",
  32. "\n",
  33. " statements\n",
  34. "\n",
  35. "\n",
  36. " return <value>\n",
  37. "```"
  38. ]
  39. },
  40. {
  41. "cell_type": "markdown",
  42. "metadata": {},
  43. "source": [
  44. "Read the above syntax as, A function by name \"funcname\" is defined, which accepts arguements \"arg1,arg2,....argN\". The function is documented and it is '''Document String'''. The function after executing the statements returns a \"value\"."
  45. ]
  46. },
  47. {
  48. "cell_type": "code",
  49. "execution_count": 1,
  50. "metadata": {},
  51. "outputs": [
  52. {
  53. "name": "stdout",
  54. "output_type": "stream",
  55. "text": [
  56. "Hey Rajath!\n",
  57. "Rajath, How do you do?\n"
  58. ]
  59. }
  60. ],
  61. "source": [
  62. "print(\"Hey Rajath!\")\n",
  63. "print(\"Rajath, How do you do?\")"
  64. ]
  65. },
  66. {
  67. "cell_type": "markdown",
  68. "metadata": {},
  69. "source": [
  70. "Instead of writing the above two statements every single time it can be replaced by defining a function which would do the job in just one line. \n",
  71. "\n",
  72. "Defining a function firstfunc()."
  73. ]
  74. },
  75. {
  76. "cell_type": "code",
  77. "execution_count": 2,
  78. "metadata": {},
  79. "outputs": [],
  80. "source": [
  81. "def firstfunc():\n",
  82. " print(\"Hey Rajath!\")\n",
  83. " print(\"Rajath, How do you do?\")"
  84. ]
  85. },
  86. {
  87. "cell_type": "code",
  88. "execution_count": 7,
  89. "metadata": {},
  90. "outputs": [
  91. {
  92. "name": "stdout",
  93. "output_type": "stream",
  94. "text": [
  95. "Hey Rajath!\n",
  96. "Rajath, How do you do?\n",
  97. "Hey Rajath!\n",
  98. "Rajath, How do you do?\n"
  99. ]
  100. }
  101. ],
  102. "source": [
  103. "firstfunc()\n",
  104. "funca=firstfunc\n",
  105. "funca()"
  106. ]
  107. },
  108. {
  109. "cell_type": "markdown",
  110. "metadata": {},
  111. "source": [
  112. "**firstfunc()** every time just prints the message to a single person. We can make our function **firstfunc()** to accept arguements which will store the name and then prints respective to that accepted name. To do so, add a argument within the function as shown."
  113. ]
  114. },
  115. {
  116. "cell_type": "code",
  117. "execution_count": 8,
  118. "metadata": {},
  119. "outputs": [],
  120. "source": [
  121. "def firstfunc(username):\n",
  122. " print(\"Hey\", username + '!')\n",
  123. " print(username + ',' ,\"How do you do?\")"
  124. ]
  125. },
  126. {
  127. "cell_type": "code",
  128. "execution_count": 9,
  129. "metadata": {},
  130. "outputs": [
  131. {
  132. "name": "stdout",
  133. "output_type": "stream",
  134. "text": [
  135. "Please enter your name : Willam\n"
  136. ]
  137. }
  138. ],
  139. "source": [
  140. "name1 = input('Please enter your name : ')"
  141. ]
  142. },
  143. {
  144. "cell_type": "markdown",
  145. "metadata": {},
  146. "source": [
  147. "The name \"Guido\" is actually stored in name1. So we pass this variable to the function **firstfunc()** as the variable username because that is the variable that is defined for this function. i.e name1 is passed as username."
  148. ]
  149. },
  150. {
  151. "cell_type": "code",
  152. "execution_count": 10,
  153. "metadata": {},
  154. "outputs": [
  155. {
  156. "name": "stdout",
  157. "output_type": "stream",
  158. "text": [
  159. "Hey Willam!\n",
  160. "Willam, How do you do?\n"
  161. ]
  162. }
  163. ],
  164. "source": [
  165. "firstfunc(name1)"
  166. ]
  167. },
  168. {
  169. "cell_type": "markdown",
  170. "metadata": {},
  171. "source": [
  172. "Let us simplify this even further by defining another function **secondfunc()** which accepts the name and stores it inside a variable and then calls the **firstfunc()** from inside the function itself."
  173. ]
  174. },
  175. {
  176. "cell_type": "code",
  177. "execution_count": 1,
  178. "metadata": {},
  179. "outputs": [],
  180. "source": [
  181. "def firstfunc(username):\n",
  182. " print(\"Hey\", username + '!')\n",
  183. " print(username + ',' ,\"How do you do?\")\n",
  184. "def secondfunc():\n",
  185. " name = input(\"Please enter your name : \")\n",
  186. " firstfunc(name)"
  187. ]
  188. },
  189. {
  190. "cell_type": "code",
  191. "execution_count": 2,
  192. "metadata": {},
  193. "outputs": [
  194. {
  195. "name": "stdout",
  196. "output_type": "stream",
  197. "text": [
  198. "Please enter your name : Tom\n",
  199. "Hey Tom!\n",
  200. "Tom, How do you do?\n"
  201. ]
  202. }
  203. ],
  204. "source": [
  205. "secondfunc()"
  206. ]
  207. },
  208. {
  209. "cell_type": "markdown",
  210. "metadata": {},
  211. "source": [
  212. "## 1. Return Statement"
  213. ]
  214. },
  215. {
  216. "cell_type": "markdown",
  217. "metadata": {},
  218. "source": [
  219. "When the function results in some value and that value has to be stored in a variable or needs to be sent back or returned for further operation to the main algorithm, return statement is used."
  220. ]
  221. },
  222. {
  223. "cell_type": "code",
  224. "execution_count": 11,
  225. "metadata": {},
  226. "outputs": [],
  227. "source": [
  228. "def times(x,y):\n",
  229. " z = x*y\n",
  230. " return z"
  231. ]
  232. },
  233. {
  234. "cell_type": "markdown",
  235. "metadata": {},
  236. "source": [
  237. "The above defined **times( )** function accepts two arguements and return the variable z which contains the result of the product of the two arguements"
  238. ]
  239. },
  240. {
  241. "cell_type": "code",
  242. "execution_count": 12,
  243. "metadata": {},
  244. "outputs": [
  245. {
  246. "name": "stdout",
  247. "output_type": "stream",
  248. "text": [
  249. "20\n"
  250. ]
  251. }
  252. ],
  253. "source": [
  254. "c = times(4,5)\n",
  255. "print(c)"
  256. ]
  257. },
  258. {
  259. "cell_type": "markdown",
  260. "metadata": {},
  261. "source": [
  262. "The z value is stored in variable c and can be used for further operations."
  263. ]
  264. },
  265. {
  266. "cell_type": "markdown",
  267. "metadata": {},
  268. "source": [
  269. "Instead of declaring another variable the entire statement itself can be used in the return statement as shown."
  270. ]
  271. },
  272. {
  273. "cell_type": "code",
  274. "execution_count": 13,
  275. "metadata": {},
  276. "outputs": [],
  277. "source": [
  278. "def times(x,y):\n",
  279. " '''This multiplies the two input arguments'''\n",
  280. " return x*y"
  281. ]
  282. },
  283. {
  284. "cell_type": "code",
  285. "execution_count": 14,
  286. "metadata": {},
  287. "outputs": [
  288. {
  289. "name": "stdout",
  290. "output_type": "stream",
  291. "text": [
  292. "20\n"
  293. ]
  294. }
  295. ],
  296. "source": [
  297. "c = times(4,5)\n",
  298. "print(c)"
  299. ]
  300. },
  301. {
  302. "cell_type": "markdown",
  303. "metadata": {},
  304. "source": [
  305. "Since the **times( )** is now defined, we can document it as shown above. This document is returned whenever **times( )** function is called under **help( )** function."
  306. ]
  307. },
  308. {
  309. "cell_type": "code",
  310. "execution_count": 15,
  311. "metadata": {},
  312. "outputs": [
  313. {
  314. "name": "stdout",
  315. "output_type": "stream",
  316. "text": [
  317. "Help on function times in module __main__:\n",
  318. "\n",
  319. "times(x, y)\n",
  320. " This multiplies the two input arguments\n",
  321. "\n"
  322. ]
  323. }
  324. ],
  325. "source": [
  326. "help(times)"
  327. ]
  328. },
  329. {
  330. "cell_type": "code",
  331. "execution_count": 16,
  332. "metadata": {},
  333. "outputs": [],
  334. "source": [
  335. "times?"
  336. ]
  337. },
  338. {
  339. "cell_type": "markdown",
  340. "metadata": {},
  341. "source": [
  342. "Multiple variable can also be returned, But keep in mind the order."
  343. ]
  344. },
  345. {
  346. "cell_type": "code",
  347. "execution_count": 17,
  348. "metadata": {},
  349. "outputs": [],
  350. "source": [
  351. "eglist = [10,50,30,12,6,8,100]"
  352. ]
  353. },
  354. {
  355. "cell_type": "code",
  356. "execution_count": 19,
  357. "metadata": {},
  358. "outputs": [],
  359. "source": [
  360. "def egfunc(eglist):\n",
  361. " highest = max(eglist)\n",
  362. " lowest = min(eglist)\n",
  363. " first = eglist[0]\n",
  364. " last = eglist[-1]\n",
  365. " return highest,lowest,first,last"
  366. ]
  367. },
  368. {
  369. "cell_type": "markdown",
  370. "metadata": {},
  371. "source": [
  372. "If the function is just called without any variable for it to be assigned to, the result is returned inside a tuple. But if the variables are mentioned then the result is assigned to the variable in a particular order which is declared in the return statement."
  373. ]
  374. },
  375. {
  376. "cell_type": "code",
  377. "execution_count": 20,
  378. "metadata": {},
  379. "outputs": [
  380. {
  381. "name": "stdout",
  382. "output_type": "stream",
  383. "text": [
  384. "(100, 6, 10, 100)\n"
  385. ]
  386. }
  387. ],
  388. "source": [
  389. "a = egfunc(eglist)\n",
  390. "print(a)"
  391. ]
  392. },
  393. {
  394. "cell_type": "code",
  395. "execution_count": 21,
  396. "metadata": {},
  397. "outputs": [
  398. {
  399. "name": "stdout",
  400. "output_type": "stream",
  401. "text": [
  402. " a = 100 \n",
  403. " b = 6 \n",
  404. " c = 10 \n",
  405. " d = 100\n"
  406. ]
  407. }
  408. ],
  409. "source": [
  410. "a,b,c,d = egfunc(eglist)\n",
  411. "print(' a =',a,'\\n b =',b,'\\n c =',c,'\\n d =',d)"
  412. ]
  413. },
  414. {
  415. "cell_type": "markdown",
  416. "metadata": {},
  417. "source": [
  418. "## 2. Implicit arguments"
  419. ]
  420. },
  421. {
  422. "cell_type": "markdown",
  423. "metadata": {},
  424. "source": [
  425. "When an argument of a function is common in majority of the cases or it is \"implicit\" this concept is used."
  426. ]
  427. },
  428. {
  429. "cell_type": "code",
  430. "execution_count": 25,
  431. "metadata": {},
  432. "outputs": [],
  433. "source": [
  434. "def implicitadd(x,y=3):\n",
  435. " return x+y"
  436. ]
  437. },
  438. {
  439. "cell_type": "markdown",
  440. "metadata": {},
  441. "source": [
  442. "**implicitadd( )** is a function accepts two arguments but most of the times the first argument needs to be added just by 3. Hence the second argument is assigned the value 3. Here the second argument is implicit."
  443. ]
  444. },
  445. {
  446. "cell_type": "markdown",
  447. "metadata": {},
  448. "source": [
  449. "Now if the second argument is not defined when calling the **implicitadd( )** function then it considered as 3."
  450. ]
  451. },
  452. {
  453. "cell_type": "code",
  454. "execution_count": 23,
  455. "metadata": {},
  456. "outputs": [
  457. {
  458. "data": {
  459. "text/plain": [
  460. "7"
  461. ]
  462. },
  463. "execution_count": 23,
  464. "metadata": {},
  465. "output_type": "execute_result"
  466. }
  467. ],
  468. "source": [
  469. "implicitadd(4)"
  470. ]
  471. },
  472. {
  473. "cell_type": "markdown",
  474. "metadata": {},
  475. "source": [
  476. "But if the second argument is specified then this value overrides the implicit value assigned to the argument "
  477. ]
  478. },
  479. {
  480. "cell_type": "code",
  481. "execution_count": 24,
  482. "metadata": {},
  483. "outputs": [
  484. {
  485. "data": {
  486. "text/plain": [
  487. "8"
  488. ]
  489. },
  490. "execution_count": 24,
  491. "metadata": {},
  492. "output_type": "execute_result"
  493. }
  494. ],
  495. "source": [
  496. "implicitadd(4,4)"
  497. ]
  498. },
  499. {
  500. "cell_type": "code",
  501. "execution_count": 20,
  502. "metadata": {},
  503. "outputs": [
  504. {
  505. "data": {
  506. "text/plain": [
  507. "11"
  508. ]
  509. },
  510. "execution_count": 20,
  511. "metadata": {},
  512. "output_type": "execute_result"
  513. }
  514. ],
  515. "source": [
  516. "implicitadd(5, y=6)"
  517. ]
  518. },
  519. {
  520. "cell_type": "markdown",
  521. "metadata": {},
  522. "source": [
  523. "## 3. Any number of arguments"
  524. ]
  525. },
  526. {
  527. "cell_type": "markdown",
  528. "metadata": {},
  529. "source": [
  530. "If the number of arguments that is to be accepted by a function is not known then a asterisk symbol is used before the argument."
  531. ]
  532. },
  533. {
  534. "cell_type": "code",
  535. "execution_count": 29,
  536. "metadata": {},
  537. "outputs": [],
  538. "source": [
  539. "def add_n(*args):\n",
  540. " res = 0\n",
  541. " reslist = []\n",
  542. " for i in args:\n",
  543. " reslist.append(i)\n",
  544. " print(reslist)\n",
  545. " return sum(reslist)"
  546. ]
  547. },
  548. {
  549. "cell_type": "markdown",
  550. "metadata": {},
  551. "source": [
  552. "The above function accepts any number of arguments, defines a list and appends all the arguments into that list and return the sum of all the arguments."
  553. ]
  554. },
  555. {
  556. "cell_type": "code",
  557. "execution_count": 30,
  558. "metadata": {},
  559. "outputs": [
  560. {
  561. "name": "stdout",
  562. "output_type": "stream",
  563. "text": [
  564. "[1, 2, 3, 4, 5]\n"
  565. ]
  566. },
  567. {
  568. "data": {
  569. "text/plain": [
  570. "15"
  571. ]
  572. },
  573. "execution_count": 30,
  574. "metadata": {},
  575. "output_type": "execute_result"
  576. }
  577. ],
  578. "source": [
  579. "add_n(1,2,3,4,5)"
  580. ]
  581. },
  582. {
  583. "cell_type": "code",
  584. "execution_count": 26,
  585. "metadata": {},
  586. "outputs": [
  587. {
  588. "name": "stdout",
  589. "output_type": "stream",
  590. "text": [
  591. "[1, 2, 3]\n"
  592. ]
  593. },
  594. {
  595. "data": {
  596. "text/plain": [
  597. "6"
  598. ]
  599. },
  600. "execution_count": 26,
  601. "metadata": {},
  602. "output_type": "execute_result"
  603. }
  604. ],
  605. "source": [
  606. "add_n(1,2,3)"
  607. ]
  608. },
  609. {
  610. "cell_type": "code",
  611. "execution_count": 31,
  612. "metadata": {},
  613. "outputs": [
  614. {
  615. "name": "stdout",
  616. "output_type": "stream",
  617. "text": [
  618. "[30, 10, 20]\n"
  619. ]
  620. },
  621. {
  622. "data": {
  623. "text/plain": [
  624. "60"
  625. ]
  626. },
  627. "execution_count": 31,
  628. "metadata": {},
  629. "output_type": "execute_result"
  630. }
  631. ],
  632. "source": [
  633. "def add_nd(**kwargs):\n",
  634. " res = 0\n",
  635. " reslist = []\n",
  636. " for (k,v) in kwargs.items():\n",
  637. " reslist.append(v)\n",
  638. " print(reslist)\n",
  639. " return sum(reslist)\n",
  640. "\n",
  641. "add_nd(x=10, y=20, c=30)"
  642. ]
  643. },
  644. {
  645. "cell_type": "markdown",
  646. "metadata": {},
  647. "source": [
  648. "## 4. Global and Local Variables"
  649. ]
  650. },
  651. {
  652. "cell_type": "markdown",
  653. "metadata": {},
  654. "source": [
  655. "Whatever variable is declared inside a function is local variable and outside the function in global variable."
  656. ]
  657. },
  658. {
  659. "cell_type": "code",
  660. "execution_count": 10,
  661. "metadata": {},
  662. "outputs": [],
  663. "source": [
  664. "eg1 = [1,2,3,4,5]"
  665. ]
  666. },
  667. {
  668. "cell_type": "markdown",
  669. "metadata": {},
  670. "source": [
  671. "In the below function we are appending a element to the declared list inside the function. eg2 variable declared inside the function is a local variable."
  672. ]
  673. },
  674. {
  675. "cell_type": "code",
  676. "execution_count": 11,
  677. "metadata": {},
  678. "outputs": [],
  679. "source": [
  680. "def egfunc1():\n",
  681. " eg1 = [1, 2, 3, 4, 5, 7]\n",
  682. " print(\"egfunc1>> eg1: \", eg1)\n",
  683. "\n",
  684. "def egfunc2():\n",
  685. " eg1.append(8)\n",
  686. " print(\"egfunc2>> eg1: \", eg1)\n",
  687. "\n",
  688. "def egfunc3():\n",
  689. " global eg1\n",
  690. " eg1 = [5, 4, 3, 2, 1]\n",
  691. " print(\"egfunc3>> eg1: \", eg1)"
  692. ]
  693. },
  694. {
  695. "cell_type": "code",
  696. "execution_count": 12,
  697. "metadata": {},
  698. "outputs": [
  699. {
  700. "name": "stdout",
  701. "output_type": "stream",
  702. "text": [
  703. "egfunc1>> eg1: [1, 2, 3, 4, 5, 7]\n",
  704. "eg1: [1, 2, 3, 4, 5] \n",
  705. "\n",
  706. "egfunc2>> eg1: [1, 2, 3, 4, 5, 8]\n",
  707. "eg1: [1, 2, 3, 4, 5, 8] \n",
  708. "\n",
  709. "egfunc3>> eg1: [5, 4, 3, 2, 1]\n",
  710. "eg1: [5, 4, 3, 2, 1] \n",
  711. "\n"
  712. ]
  713. }
  714. ],
  715. "source": [
  716. "egfunc1()\n",
  717. "print(\"eg1: \", eg1, \"\\n\")\n",
  718. "\n",
  719. "egfunc2()\n",
  720. "print(\"eg1: \", eg1, \"\\n\")\n",
  721. "\n",
  722. "egfunc3()\n",
  723. "print(\"eg1: \", eg1, \"\\n\")\n"
  724. ]
  725. },
  726. {
  727. "cell_type": "markdown",
  728. "metadata": {},
  729. "source": [
  730. "## 5. Lambda Functions"
  731. ]
  732. },
  733. {
  734. "cell_type": "markdown",
  735. "metadata": {},
  736. "source": [
  737. "These are small functions which are not defined with any name and carry a single expression whose result is returned. Lambda functions comes very handy when operating with lists. These function are defined by the keyword **lambda** followed by the variables, a colon and the respective expression."
  738. ]
  739. },
  740. {
  741. "cell_type": "code",
  742. "execution_count": 48,
  743. "metadata": {},
  744. "outputs": [],
  745. "source": [
  746. "z = lambda x: x * x"
  747. ]
  748. },
  749. {
  750. "cell_type": "code",
  751. "execution_count": 49,
  752. "metadata": {},
  753. "outputs": [
  754. {
  755. "data": {
  756. "text/plain": [
  757. "64"
  758. ]
  759. },
  760. "execution_count": 49,
  761. "metadata": {},
  762. "output_type": "execute_result"
  763. }
  764. ],
  765. "source": [
  766. "z(8)"
  767. ]
  768. },
  769. {
  770. "cell_type": "code",
  771. "execution_count": 50,
  772. "metadata": {},
  773. "outputs": [
  774. {
  775. "data": {
  776. "text/plain": [
  777. "(6, 8)"
  778. ]
  779. },
  780. "execution_count": 50,
  781. "metadata": {},
  782. "output_type": "execute_result"
  783. }
  784. ],
  785. "source": [
  786. "zz = lambda x, y: (x*y, x**y)\n",
  787. "zz(2, 3)"
  788. ]
  789. },
  790. {
  791. "cell_type": "code",
  792. "execution_count": 51,
  793. "metadata": {},
  794. "outputs": [
  795. {
  796. "data": {
  797. "text/plain": [
  798. "function"
  799. ]
  800. },
  801. "execution_count": 51,
  802. "metadata": {},
  803. "output_type": "execute_result"
  804. }
  805. ],
  806. "source": [
  807. "type(z)"
  808. ]
  809. },
  810. {
  811. "cell_type": "code",
  812. "execution_count": 52,
  813. "metadata": {},
  814. "outputs": [
  815. {
  816. "data": {
  817. "text/plain": [
  818. "function"
  819. ]
  820. },
  821. "execution_count": 52,
  822. "metadata": {},
  823. "output_type": "execute_result"
  824. }
  825. ],
  826. "source": [
  827. "def sqr(x):\n",
  828. " return x*x\n",
  829. "\n",
  830. "type(sqr)"
  831. ]
  832. },
  833. {
  834. "cell_type": "markdown",
  835. "metadata": {},
  836. "source": [
  837. "### 5.1 map"
  838. ]
  839. },
  840. {
  841. "cell_type": "markdown",
  842. "metadata": {},
  843. "source": [
  844. "**map( )** function basically executes the function that is defined to each of the list's element separately."
  845. ]
  846. },
  847. {
  848. "cell_type": "code",
  849. "execution_count": 53,
  850. "metadata": {},
  851. "outputs": [],
  852. "source": [
  853. "list1 = [1,2,3,4,5,6,7,8,9]"
  854. ]
  855. },
  856. {
  857. "cell_type": "code",
  858. "execution_count": 54,
  859. "metadata": {},
  860. "outputs": [
  861. {
  862. "name": "stdout",
  863. "output_type": "stream",
  864. "text": [
  865. "[3, 4, 5, 6, 7, 8, 9, 10, 11]\n"
  866. ]
  867. }
  868. ],
  869. "source": [
  870. "eg = map(lambda x:x+2, list1)\n",
  871. "print(list(eg))"
  872. ]
  873. },
  874. {
  875. "cell_type": "code",
  876. "execution_count": 55,
  877. "metadata": {},
  878. "outputs": [
  879. {
  880. "name": "stdout",
  881. "output_type": "stream",
  882. "text": [
  883. "[1, 4, 9, 16, 25, 36, 49, 64, 81]\n"
  884. ]
  885. }
  886. ],
  887. "source": [
  888. "eg = map(sqr, list1)\n",
  889. "print(list(eg))"
  890. ]
  891. },
  892. {
  893. "cell_type": "markdown",
  894. "metadata": {},
  895. "source": [
  896. "You can also add two lists."
  897. ]
  898. },
  899. {
  900. "cell_type": "code",
  901. "execution_count": 34,
  902. "metadata": {},
  903. "outputs": [],
  904. "source": [
  905. "list2 = [9,8,7,6,5,4,3,2,1]"
  906. ]
  907. },
  908. {
  909. "cell_type": "code",
  910. "execution_count": 35,
  911. "metadata": {},
  912. "outputs": [
  913. {
  914. "name": "stdout",
  915. "output_type": "stream",
  916. "text": [
  917. "[10, 10, 10, 10, 10, 10, 10, 10, 10]\n"
  918. ]
  919. }
  920. ],
  921. "source": [
  922. "eg2 = map(lambda x,y:x+y, list1,list2)\n",
  923. "print(eg2)"
  924. ]
  925. },
  926. {
  927. "cell_type": "markdown",
  928. "metadata": {},
  929. "source": [
  930. "Not only lambda function but also other built in functions can also be used."
  931. ]
  932. },
  933. {
  934. "cell_type": "code",
  935. "execution_count": 36,
  936. "metadata": {},
  937. "outputs": [
  938. {
  939. "name": "stdout",
  940. "output_type": "stream",
  941. "text": [
  942. "['10', '10', '10', '10', '10', '10', '10', '10', '10']\n"
  943. ]
  944. }
  945. ],
  946. "source": [
  947. "eg3 = map(str,eg2)\n",
  948. "print(eg3)"
  949. ]
  950. },
  951. {
  952. "cell_type": "markdown",
  953. "metadata": {},
  954. "source": [
  955. "### 5.2 filter"
  956. ]
  957. },
  958. {
  959. "cell_type": "markdown",
  960. "metadata": {},
  961. "source": [
  962. "**filter( )** function is used to filter out the values in a list. Note that **filter()** function returns the result in a new list."
  963. ]
  964. },
  965. {
  966. "cell_type": "code",
  967. "execution_count": 56,
  968. "metadata": {},
  969. "outputs": [],
  970. "source": [
  971. "list1 = [1,2,3,4,5,6,7,8,9]"
  972. ]
  973. },
  974. {
  975. "cell_type": "markdown",
  976. "metadata": {},
  977. "source": [
  978. "To get the elements which are less than 5,"
  979. ]
  980. },
  981. {
  982. "cell_type": "code",
  983. "execution_count": 57,
  984. "metadata": {},
  985. "outputs": [
  986. {
  987. "name": "stdout",
  988. "output_type": "stream",
  989. "text": [
  990. "[1, 2, 3, 4]\n"
  991. ]
  992. }
  993. ],
  994. "source": [
  995. "res = filter(lambda x:x<5,list1)\n",
  996. "print(list(res))"
  997. ]
  998. },
  999. {
  1000. "cell_type": "markdown",
  1001. "metadata": {},
  1002. "source": [
  1003. "Notice what happens when **map()** is used."
  1004. ]
  1005. },
  1006. {
  1007. "cell_type": "code",
  1008. "execution_count": 58,
  1009. "metadata": {},
  1010. "outputs": [
  1011. {
  1012. "data": {
  1013. "text/plain": [
  1014. "<map at 0x7f72a418c780>"
  1015. ]
  1016. },
  1017. "execution_count": 58,
  1018. "metadata": {},
  1019. "output_type": "execute_result"
  1020. }
  1021. ],
  1022. "source": [
  1023. "map(lambda x:x<5, list1)"
  1024. ]
  1025. },
  1026. {
  1027. "cell_type": "markdown",
  1028. "metadata": {},
  1029. "source": [
  1030. "We can conclude that, whatever is returned true in **map( )** function that particular element is returned when **filter( )** function is used."
  1031. ]
  1032. },
  1033. {
  1034. "cell_type": "code",
  1035. "execution_count": 59,
  1036. "metadata": {},
  1037. "outputs": [
  1038. {
  1039. "data": {
  1040. "text/plain": [
  1041. "<filter at 0x7f72a4195240>"
  1042. ]
  1043. },
  1044. "execution_count": 59,
  1045. "metadata": {},
  1046. "output_type": "execute_result"
  1047. }
  1048. ],
  1049. "source": [
  1050. "filter(lambda x:x%4==0,list1)"
  1051. ]
  1052. }
  1053. ],
  1054. "metadata": {
  1055. "kernelspec": {
  1056. "display_name": "Python 3",
  1057. "language": "python",
  1058. "name": "python3"
  1059. },
  1060. "language_info": {
  1061. "codemirror_mode": {
  1062. "name": "ipython",
  1063. "version": 3
  1064. },
  1065. "file_extension": ".py",
  1066. "mimetype": "text/x-python",
  1067. "name": "python",
  1068. "nbconvert_exporter": "python",
  1069. "pygments_lexer": "ipython3",
  1070. "version": "3.6.8"
  1071. }
  1072. },
  1073. "nbformat": 4,
  1074. "nbformat_minor": 1
  1075. }

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