Talk give at Galvanize January 2019 about introductory Python.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

presentation_01042019.ipynb 52 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {
  6. "slideshow": {
  7. "slide_type": "notes"
  8. }
  9. },
  10. "source": [
  11. "Slide conversion: https://echorand.me/presentation-slides-with-jupyter-notebook.html\n",
  12. "\n",
  13. "`$ jupyter-nbconvert --to slides slides.ipynb --reveal-prefix=reveal.js --post serve`"
  14. ]
  15. },
  16. {
  17. "cell_type": "markdown",
  18. "metadata": {
  19. "slideshow": {
  20. "slide_type": "slide"
  21. }
  22. },
  23. "source": [
  24. "# A Brief Python Introduction\n",
  25. "Keith Maull<br/>\n",
  26. "Jan. 03, 2019"
  27. ]
  28. },
  29. {
  30. "cell_type": "markdown",
  31. "metadata": {
  32. "slideshow": {
  33. "slide_type": "slide"
  34. }
  35. },
  36. "source": [
  37. "## Python is a general purpose programming language that is ...\n",
  38. "\n",
  39. "* interpreted\n",
  40. "* dynamically typed (not statically typed)\n",
  41. "* intuitive\n",
  42. "* fun and addictive"
  43. ]
  44. },
  45. {
  46. "cell_type": "markdown",
  47. "metadata": {
  48. "slideshow": {
  49. "slide_type": "notes"
  50. }
  51. },
  52. "source": [
  53. "### strongly typed\n",
  54. "* So to answer your question: another way to look at this that's mostly correct is to say that static typing is compile-time type safety and strong typing is runtime type safety.\n",
  55. "### everything is an object"
  56. ]
  57. },
  58. {
  59. "cell_type": "markdown",
  60. "metadata": {
  61. "slideshow": {
  62. "slide_type": "slide"
  63. }
  64. },
  65. "source": [
  66. "## Python syntax can be learned very quickly\n",
  67. "\n",
  68. "* indentation matters\n",
  69. "* there are no curly braces, semicolons or tricks ...\n",
  70. "* the syntax is one of the true joys of the language once you learn not to fight it\n"
  71. ]
  72. },
  73. {
  74. "cell_type": "markdown",
  75. "metadata": {
  76. "slideshow": {
  77. "slide_type": "subslide"
  78. }
  79. },
  80. "source": [
  81. "## We will learn 80% of the syntax today in 1 hour!\n",
  82. "\n",
  83. "![./1368244924-49523624.jpg](./1368244924-49523624.jpg)"
  84. ]
  85. },
  86. {
  87. "cell_type": "code",
  88. "execution_count": 1,
  89. "metadata": {
  90. "slideshow": {
  91. "slide_type": "subslide"
  92. }
  93. },
  94. "outputs": [
  95. {
  96. "name": "stdout",
  97. "output_type": "stream",
  98. "text": [
  99. "The first item of the list is an `a`\n"
  100. ]
  101. }
  102. ],
  103. "source": [
  104. "a_simple_list = ['a', 'b', 'c', 'd']\n",
  105. "if a_simple_list[0] == 'a':\n",
  106. " print(\"The first item of the list is an `a`\")"
  107. ]
  108. },
  109. {
  110. "cell_type": "code",
  111. "execution_count": 2,
  112. "metadata": {
  113. "slideshow": {
  114. "slide_type": "subslide"
  115. }
  116. },
  117. "outputs": [
  118. {
  119. "name": "stdout",
  120. "output_type": "stream",
  121. "text": [
  122. "a\n",
  123. "b\n",
  124. "c\n",
  125. "d\n"
  126. ]
  127. }
  128. ],
  129. "source": [
  130. "for item in a_simple_list:\n",
  131. " print(item)"
  132. ]
  133. },
  134. {
  135. "cell_type": "code",
  136. "execution_count": 3,
  137. "metadata": {
  138. "slideshow": {
  139. "slide_type": "subslide"
  140. }
  141. },
  142. "outputs": [
  143. {
  144. "name": "stdout",
  145. "output_type": "stream",
  146. "text": [
  147. "0\n",
  148. "1\n",
  149. "2\n",
  150. "3\n",
  151. "4\n"
  152. ]
  153. }
  154. ],
  155. "source": [
  156. "count = 0\n",
  157. "while (count < 5):\n",
  158. " print(count)\n",
  159. " count+=1"
  160. ]
  161. },
  162. {
  163. "cell_type": "code",
  164. "execution_count": 4,
  165. "metadata": {
  166. "slideshow": {
  167. "slide_type": "subslide"
  168. }
  169. },
  170. "outputs": [
  171. {
  172. "data": {
  173. "text/plain": [
  174. "12"
  175. ]
  176. },
  177. "execution_count": 4,
  178. "metadata": {},
  179. "output_type": "execute_result"
  180. }
  181. ],
  182. "source": [
  183. "# functions are easy \n",
  184. "def my_func(x, y):\n",
  185. " return x*y\n",
  186. "\n",
  187. "my_func(3,4)"
  188. ]
  189. },
  190. {
  191. "cell_type": "markdown",
  192. "metadata": {
  193. "slideshow": {
  194. "slide_type": "notes"
  195. }
  196. },
  197. "source": [
  198. "### x, y = y, x"
  199. ]
  200. },
  201. {
  202. "cell_type": "markdown",
  203. "metadata": {
  204. "slideshow": {
  205. "slide_type": "slide"
  206. }
  207. },
  208. "source": [
  209. "## Python has many modules (aka \"libraries\") that do fun things"
  210. ]
  211. },
  212. {
  213. "cell_type": "code",
  214. "execution_count": 5,
  215. "metadata": {
  216. "slideshow": {
  217. "slide_type": "subslide"
  218. }
  219. },
  220. "outputs": [
  221. {
  222. "name": "stdout",
  223. "output_type": "stream",
  224. "text": [
  225. "89\n"
  226. ]
  227. }
  228. ],
  229. "source": [
  230. "# random numbers generator\n",
  231. "import random\n",
  232. "print(random.randint(0,100))"
  233. ]
  234. },
  235. {
  236. "cell_type": "code",
  237. "execution_count": 6,
  238. "metadata": {
  239. "slideshow": {
  240. "slide_type": "subslide"
  241. }
  242. },
  243. "outputs": [
  244. {
  245. "name": "stdout",
  246. "output_type": "stream",
  247. "text": [
  248. "aa\n",
  249. "aah\n",
  250. "aahed\n",
  251. "aahing\n",
  252. "aahs\n",
  253. "aal\n",
  254. "aalii\n",
  255. "aaliis\n",
  256. "aals\n",
  257. "aardvark\n",
  258. "aardvarks\n",
  259. "aardwolf\n",
  260. "aardwolves\n",
  261. "aargh\n",
  262. "aarrgh\n",
  263. "aarrghh\n",
  264. "aarti\n",
  265. "aartis\n",
  266. "aas\n",
  267. "aasvogel\n",
  268. "aasvogels\n",
  269. "ab\n",
  270. "aba\n",
  271. "abac\n",
  272. "abaca\n",
  273. "abacas\n",
  274. "abaci\n",
  275. "aback\n",
  276. "abacs\n",
  277. "abacterial\n",
  278. "abactinal\n",
  279. "abactinally\n",
  280. "abactor\n",
  281. "abactors\n",
  282. "abacus\n",
  283. "abacuses\n",
  284. "abaft\n",
  285. "abaka\n",
  286. "abakas\n",
  287. "abalone\n",
  288. "abalones\n",
  289. "abamp\n",
  290. "abampere\n",
  291. "abamperes\n",
  292. "abamps\n",
  293. "aband\n",
  294. "abanded\n",
  295. "abanding\n",
  296. "abandon\n",
  297. "abandoned\n",
  298. "abandonedly\n",
  299. "abandonee\n",
  300. "abandonees\n",
  301. "abandoner\n",
  302. "abandoners\n",
  303. "abandoning\n",
  304. "abandonment\n",
  305. "abandonments\n",
  306. "abandons\n",
  307. "abandonware\n",
  308. "abandonwares\n",
  309. "abands\n",
  310. "abapical\n",
  311. "abas\n",
  312. "abase\n",
  313. "abased\n",
  314. "abasedly\n",
  315. "abasement\n",
  316. "abasements\n",
  317. "abaser\n",
  318. "abasers\n",
  319. "abases\n",
  320. "abash\n",
  321. "abashed\n",
  322. "abashedly\n",
  323. "abashes\n",
  324. "abashing\n",
  325. "abashless\n",
  326. "abashment\n",
  327. "abashments\n",
  328. "abasia\n",
  329. "abasias\n",
  330. "abasing\n",
  331. "abask\n",
  332. "abatable\n",
  333. "abate\n",
  334. "abated\n",
  335. "abatement\n",
  336. "abatements\n",
  337. "abater\n",
  338. "abaters\n",
  339. "abates\n",
  340. "abating\n",
  341. "abatis\n",
  342. "abatises\n",
  343. "abator\n",
  344. "abators\n",
  345. "abattis\n",
  346. "abattises\n",
  347. "abattoir\n",
  348. "abattoirs\n",
  349. "abattu\n",
  350. "abature\n",
  351. "abatures\n",
  352. "abaxial\n",
  353. "abaxile\n",
  354. "abaya\n",
  355. "abayas\n",
  356. "abb\n",
  357. "abba\n",
  358. "abbacies\n",
  359. "abbacy\n",
  360. "abbas\n",
  361. "abbatial\n",
  362. "abbe\n",
  363. "abbed\n",
  364. "abbes\n",
  365. "abbess\n",
  366. "abbesses\n",
  367. "abbey\n",
  368. "abbeys\n",
  369. "abbot\n",
  370. "abbotcies\n",
  371. "abbotcy\n",
  372. "abbots\n",
  373. "abbotship\n",
  374. "abbotsh\n"
  375. ]
  376. }
  377. ],
  378. "source": [
  379. "# http library\n",
  380. "import requests\n",
  381. "r = requests.get(\"https://raw.githubusercontent.com/atebits/Words/master/Words/en.txt\")\n",
  382. "if r.status_code == 200:\n",
  383. " data = r.text\n",
  384. " print(data[:1000])"
  385. ]
  386. },
  387. {
  388. "cell_type": "markdown",
  389. "metadata": {
  390. "slideshow": {
  391. "slide_type": "subslide"
  392. }
  393. },
  394. "source": [
  395. "## And a strength of the language is text processing ..."
  396. ]
  397. },
  398. {
  399. "cell_type": "code",
  400. "execution_count": 7,
  401. "metadata": {
  402. "slideshow": {
  403. "slide_type": "fragment"
  404. }
  405. },
  406. "outputs": [
  407. {
  408. "data": {
  409. "text/plain": [
  410. "['aa',\n",
  411. " 'aah',\n",
  412. " 'aahed',\n",
  413. " 'aahing',\n",
  414. " 'aahs',\n",
  415. " 'aal',\n",
  416. " 'aalii',\n",
  417. " 'aaliis',\n",
  418. " 'aals',\n",
  419. " 'aardvark']"
  420. ]
  421. },
  422. "execution_count": 7,
  423. "metadata": {},
  424. "output_type": "execute_result"
  425. }
  426. ],
  427. "source": [
  428. "data.split('\\n')[:10]"
  429. ]
  430. },
  431. {
  432. "cell_type": "code",
  433. "execution_count": 8,
  434. "metadata": {
  435. "slideshow": {
  436. "slide_type": "subslide"
  437. }
  438. },
  439. "outputs": [
  440. {
  441. "name": "stdout",
  442. "output_type": "stream",
  443. "text": [
  444. "['aahed', 'aalii', 'aargh', 'aarti', 'abaca', 'abaci', 'aback', 'abacs', 'abaft', 'abaka', 'abamp', 'aband', 'abase', 'abash', 'abask', 'abate', 'abaya', 'abbas', 'abbed', 'abbes', 'abbey', 'abbot', 'abcee', 'abeam', 'abear', 'abele', 'abets', 'abhor', 'abide', 'abies', 'abled', 'abler', 'ables', 'ablet', 'ablow', 'abmho', 'abode', 'abohm', 'aboil', 'aboma', 'aboon', 'abord', 'abore', 'abort', 'about', 'above', 'abram', 'abray', 'abrim', 'abrin', 'abris', 'absey', 'absit', 'abuna', 'abune', 'abuse', 'abuts', 'abuzz', 'abyes', 'abysm', 'abyss', 'acais', 'acari', 'accas', 'accoy', 'acerb', 'acers', 'aceta', 'achar', 'ached', 'aches', 'achoo', 'acids', 'acidy', 'acing', 'acini', 'ackee', 'acker', 'acmes', 'acmic', 'acned', 'acnes', 'acock', 'acold', 'acorn', 'acred', 'acres', 'acrid', 'acted', 'actin', 'acton', 'actor', 'acute', 'acyls', 'adage', 'adapt', 'adaws', 'adays', 'addax', 'added']\n"
  445. ]
  446. }
  447. ],
  448. "source": [
  449. "top_100_five_letter_words = []\n",
  450. "\n",
  451. "for w in data.split('\\n'):\n",
  452. " if len(w) == 5:\n",
  453. " top_100_five_letter_words.append(w)\n",
  454. " if len(top_100_five_letter_words) == 100:\n",
  455. " break\n",
  456. "\n",
  457. "print(top_100_five_letter_words)"
  458. ]
  459. },
  460. {
  461. "cell_type": "code",
  462. "execution_count": 9,
  463. "metadata": {
  464. "slideshow": {
  465. "slide_type": "subslide"
  466. }
  467. },
  468. "outputs": [
  469. {
  470. "name": "stdout",
  471. "output_type": "stream",
  472. "text": [
  473. "['acing', 'aging', 'ahing', 'aking', 'alang', 'almug', 'along', 'among', 'aping', 'awing', 'axing', 'befog', 'being', 'bewig', 'bhang', 'bling', 'boing', 'boong', 'bourg', 'bring', 'brung', 'chang', 'clang', 'cling', 'clung', 'cohog', 'colog', 'craig', 'cuing', 'debag', 'debug', 'defog', 'derig', 'doing', 'droog', 'duing', 'dwang', 'dying', 'ehing', 'eking', 'embog', 'emong', 'ennog', 'ering', 'exing', 'eying', 'fling', 'flong', 'flung', 'glogg', 'going', 'gulag', 'hoing', 'hying', 'hyleg', 'icing', 'incog', 'iring', 'kaing', 'kiang', 'klang', 'klieg', 'klong', 'krang', 'kreng', 'kyang', 'liang', 'lolog', 'lying', 'moong', 'nying', 'obang', 'oflag', 'ohing', 'oping', 'orang', 'owing', 'phang', 'piing', 'pirog', 'pling', 'plong', 'prang', 'prong', 'rejig', 'renig', 'repeg', 'rerig', 'retag', 'rolag', 'ruing', 'scoog', 'scoug', 'scrag', 'scrog', 'shrug', 'skegg', 'slang', 'sling', 'slung']\n"
  474. ]
  475. }
  476. ],
  477. "source": [
  478. "top_100_five_letter_words_ending_in_g = []\n",
  479. "\n",
  480. "for w in data.split('\\n'):\n",
  481. " if len(w) == 5 and w[-1] is 'g':\n",
  482. " top_100_five_letter_words_ending_in_g.append(w)\n",
  483. " if len(top_100_five_letter_words_ending_in_g) == 100:\n",
  484. " break\n",
  485. "\n",
  486. "print(top_100_five_letter_words_ending_in_g)"
  487. ]
  488. },
  489. {
  490. "cell_type": "markdown",
  491. "metadata": {
  492. "slideshow": {
  493. "slide_type": "slide"
  494. }
  495. },
  496. "source": [
  497. "### json support is excellent (as it should be)"
  498. ]
  499. },
  500. {
  501. "cell_type": "code",
  502. "execution_count": 10,
  503. "metadata": {
  504. "slideshow": {
  505. "slide_type": "fragment"
  506. }
  507. },
  508. "outputs": [
  509. {
  510. "name": "stderr",
  511. "output_type": "stream",
  512. "text": [
  513. "IOPub data rate exceeded.\n",
  514. "The notebook server will temporarily stop sending output\n",
  515. "to the client in order to avoid crashing it.\n",
  516. "To change this limit, set the config variable\n",
  517. "`--NotebookApp.iopub_data_rate_limit`.\n",
  518. "\n",
  519. "Current values:\n",
  520. "NotebookApp.iopub_data_rate_limit=1000000.0 (bytes/sec)\n",
  521. "NotebookApp.rate_limit_window=3.0 (secs)\n",
  522. "\n"
  523. ]
  524. }
  525. ],
  526. "source": [
  527. "import json\n",
  528. "import requests\n",
  529. "\n",
  530. "r = requests.get(\"https://raw.githubusercontent.com/LearnWebCode/json-example/master/pets-data.json\")\n",
  531. "if r.status_code == 200:\n",
  532. " pet_data = json.loads(r.text)\n",
  533. "print(data)"
  534. ]
  535. },
  536. {
  537. "cell_type": "code",
  538. "execution_count": 11,
  539. "metadata": {
  540. "slideshow": {
  541. "slide_type": "fragment"
  542. }
  543. },
  544. "outputs": [
  545. {
  546. "data": {
  547. "text/plain": [
  548. "{'name': 'Purrsloud',\n",
  549. " 'species': 'Cat',\n",
  550. " 'favFoods': ['wet food', 'dry food', '<strong>any</strong> food'],\n",
  551. " 'birthYear': 2016,\n",
  552. " 'photo': 'https://learnwebcode.github.io/json-example/images/cat-2.jpg'}"
  553. ]
  554. },
  555. "execution_count": 11,
  556. "metadata": {},
  557. "output_type": "execute_result"
  558. }
  559. ],
  560. "source": [
  561. "pet_data['pets'][0]"
  562. ]
  563. },
  564. {
  565. "cell_type": "markdown",
  566. "metadata": {
  567. "slideshow": {
  568. "slide_type": "subslide"
  569. }
  570. },
  571. "source": [
  572. "### Regular expressions ... also very accessible"
  573. ]
  574. },
  575. {
  576. "cell_type": "code",
  577. "execution_count": 12,
  578. "metadata": {
  579. "slideshow": {
  580. "slide_type": "fragment"
  581. }
  582. },
  583. "outputs": [
  584. {
  585. "name": "stdout",
  586. "output_type": "stream",
  587. "text": [
  588. "tabasheers\n",
  589. "tabers\n",
  590. "tabliers\n",
  591. "taborers\n",
  592. "tabourers\n",
  593. "tacheometers\n",
  594. "tachometers\n",
  595. "tachygraphers\n",
  596. "tachymeters\n",
  597. "tackers\n",
  598. "tackifiers\n",
  599. "tacklers\n",
  600. "taggers\n",
  601. "tailenders\n",
  602. "tailers\n",
  603. "tailgaters\n",
  604. "tailwaters\n",
  605. "taivers\n",
  606. "takeovers\n",
  607. "takers\n",
  608. "talebearers\n",
  609. "talers\n",
  610. "talkers\n",
  611. "talliers\n",
  612. "tambers\n",
  613. "tambourers\n",
  614. "tamers\n",
  615. "tamperers\n",
  616. "tampers\n",
  617. "tanagers\n",
  618. "tanglers\n",
  619. "tankbusters\n",
  620. "tankers\n",
  621. "tanners\n",
  622. "tantalisers\n",
  623. "tantalizers\n",
  624. "taperers\n",
  625. "tapers\n",
  626. "tappers\n",
  627. "tapsters\n",
  628. "targeteers\n",
  629. "tarnishers\n",
  630. "tarpapers\n",
  631. "tarriers\n",
  632. "tarsiers\n",
  633. "taseometers\n",
  634. "tasers\n",
  635. "tasimeters\n",
  636. "taskers\n",
  637. "taskmasters\n",
  638. "tastemakers\n",
  639. "tasters\n",
  640. "taters\n",
  641. "tatlers\n",
  642. "tatters\n",
  643. "tattlers\n",
  644. "tattooers\n",
  645. "taunters\n",
  646. "tautomers\n",
  647. "taverners\n",
  648. "tavers\n",
  649. "tawers\n",
  650. "taxameters\n",
  651. "taxers\n",
  652. "taximeters\n",
  653. "taxonomers\n",
  654. "taxpayers\n",
  655. "teachers\n",
  656. "teamakers\n",
  657. "teamers\n",
  658. "teamsters\n",
  659. "tearers\n",
  660. "tearjerkers\n",
  661. "teaselers\n",
  662. "teasellers\n",
  663. "teasers\n",
  664. "teatasters\n",
  665. "tedders\n",
  666. "teemers\n",
  667. "teenagers\n",
  668. "teeners\n",
  669. "teenyboppers\n",
  670. "teers\n",
  671. "teeters\n",
  672. "teethers\n",
  673. "teetotalers\n",
  674. "teetotallers\n",
  675. "telecasters\n",
  676. "telecommuters\n",
  677. "teleconverters\n",
  678. "telegraphers\n",
  679. "telemarketers\n",
  680. "telemeters\n",
  681. "telephoners\n",
  682. "teleprinters\n",
  683. "teletypewriters\n",
  684. "televiewers\n",
  685. "televisers\n",
  686. "teleworkers\n",
  687. "telewriters\n",
  688. "telfers\n",
  689. "tellers\n",
  690. "tellurometers\n",
  691. "telphers\n",
  692. "temperers\n",
  693. "tempers\n",
  694. "temporisers\n",
  695. "temporizers\n",
  696. "tempters\n",
  697. "tenderers\n",
  698. "tenderisers\n",
  699. "tenderizers\n",
  700. "tenderometers\n",
  701. "tenders\n",
  702. "tenners\n",
  703. "tenoners\n",
  704. "tenpounders\n",
  705. "tensimeters\n",
  706. "tensiometers\n",
  707. "tensioners\n",
  708. "tenters\n",
  709. "tentmakers\n",
  710. "termers\n",
  711. "terminers\n",
  712. "terpolymers\n",
  713. "terriers\n",
  714. "terrifiers\n",
  715. "terrorisers\n",
  716. "terrorizers\n",
  717. "testers\n",
  718. "testifiers\n",
  719. "tethers\n",
  720. "tetramers\n",
  721. "tetrameters\n",
  722. "tetters\n",
  723. "teuchters\n",
  724. "texters\n",
  725. "thalers\n",
  726. "thankers\n",
  727. "thanksgivers\n",
  728. "thatchers\n",
  729. "thawers\n",
  730. "theatergoers\n",
  731. "theaters\n",
  732. "theologasters\n",
  733. "theologers\n",
  734. "theologisers\n",
  735. "theologizers\n",
  736. "theorisers\n",
  737. "theorizers\n",
  738. "theosophers\n",
  739. "thermographers\n",
  740. "thermometers\n",
  741. "thickeners\n",
  742. "thiggers\n",
  743. "thillers\n",
  744. "thimbleriggers\n",
  745. "thinkers\n",
  746. "thinners\n",
  747. "thirsters\n",
  748. "thrashers\n",
  749. "threaders\n",
  750. "threadmakers\n",
  751. "threapers\n",
  752. "threateners\n",
  753. "threepers\n",
  754. "threshers\n",
  755. "thrillers\n",
  756. "thrivers\n",
  757. "throbbers\n",
  758. "thronners\n",
  759. "throttlers\n",
  760. "throwers\n",
  761. "throwsters\n",
  762. "thrummers\n",
  763. "thrusters\n",
  764. "thumpers\n",
  765. "thunderers\n",
  766. "thunders\n",
  767. "thundershowers\n",
  768. "thurifers\n",
  769. "thwackers\n",
  770. "thwarters\n",
  771. "tickers\n",
  772. "ticklers\n",
  773. "tiddlers\n",
  774. "tidewaiters\n",
  775. "tidewaters\n",
  776. "tidiers\n",
  777. "tiebreakers\n",
  778. "tiers\n",
  779. "tigers\n",
  780. "tighteners\n",
  781. "tilers\n",
  782. "tillers\n",
  783. "tilters\n",
  784. "tiltmeters\n",
  785. "timbers\n",
  786. "timekeepers\n",
  787. "timepleasers\n",
  788. "timers\n",
  789. "timesavers\n",
  790. "timeservers\n",
  791. "timeworkers\n",
  792. "timoneers\n",
  793. "tinders\n",
  794. "tinglers\n",
  795. "tinkerers\n",
  796. "tinkers\n",
  797. "tinklers\n",
  798. "tinners\n",
  799. "tinters\n",
  800. "tintometers\n",
  801. "tippers\n",
  802. "tipplers\n",
  803. "tipsters\n",
  804. "titers\n",
  805. "titfers\n",
  806. "tithers\n",
  807. "titleholders\n",
  808. "titlers\n",
  809. "titterers\n",
  810. "titters\n",
  811. "toadeaters\n",
  812. "toasters\n",
  813. "toastmasters\n",
  814. "tobogganers\n",
  815. "tochers\n",
  816. "toddlers\n",
  817. "todgers\n",
  818. "toeraggers\n",
  819. "toggers\n",
  820. "togglers\n",
  821. "toilers\n",
  822. "tokers\n",
  823. "tollers\n",
  824. "tolters\n",
  825. "toners\n",
  826. "tongers\n",
  827. "tongsters\n",
  828. "tonguesters\n",
  829. "tonkers\n",
  830. "tonners\n",
  831. "tonometers\n",
  832. "tontiners\n",
  833. "toolers\n",
  834. "toolholders\n",
  835. "toolmakers\n",
  836. "toolpushers\n",
  837. "tooters\n",
  838. "tootlers\n",
  839. "topers\n",
  840. "topliners\n",
  841. "topmakers\n",
  842. "topnotchers\n",
  843. "topographers\n",
  844. "toppers\n",
  845. "topsiders\n",
  846. "torchbearers\n",
  847. "torchers\n",
  848. "torchiers\n",
  849. "tormenters\n",
  850. "torpedoers\n",
  851. "torquers\n",
  852. "torturers\n",
  853. "toshers\n",
  854. "tossers\n",
  855. "totalisers\n",
  856. "totalizers\n",
  857. "toters\n",
  858. "totterers\n",
  859. "totters\n",
  860. "touchers\n",
  861. "touchpapers\n",
  862. "tougheners\n",
  863. "tourers\n",
  864. "tourneyers\n",
  865. "tousers\n",
  866. "touters\n",
  867. "towers\n",
  868. "towsers\n",
  869. "toyers\n",
  870. "tracers\n",
  871. "trackers\n",
  872. "tracklayers\n",
  873. "trackwalkers\n",
  874. "traders\n",
  875. "traditioners\n",
  876. "traducers\n",
  877. "traffickers\n",
  878. "trailblazers\n",
  879. "trailbreakers\n",
  880. "trailers\n",
  881. "trainbearers\n",
  882. "trainers\n",
  883. "trammelers\n",
  884. "trammellers\n",
  885. "trampers\n",
  886. "tramplers\n",
  887. "trampoliners\n",
  888. "tranquilisers\n",
  889. "tranquilizers\n",
  890. "tranquillisers\n",
  891. "tranquillizers\n",
  892. "transceivers\n",
  893. "transcribers\n",
  894. "transducers\n",
  895. "transferrers\n",
  896. "transfers\n",
  897. "transformers\n",
  898. "transfusers\n",
  899. "transgenders\n",
  900. "transhippers\n",
  901. "transmissometers\n",
  902. "transmitters\n",
  903. "transmuters\n",
  904. "transplanters\n",
  905. "transponders\n",
  906. "transporters\n",
  907. "transposers\n",
  908. "transputers\n",
  909. "transshippers\n",
  910. "transvaluers\n",
  911. "transverters\n",
  912. "tranters\n",
  913. "trapanners\n",
  914. "trappers\n",
  915. "trapshooters\n",
  916. "trashers\n",
  917. "travelers\n",
  918. "travellers\n",
  919. "traversers\n",
  920. "trawlers\n",
  921. "treacherers\n",
  922. "treachers\n",
  923. "treaders\n",
  924. "treadlers\n",
  925. "treasurers\n",
  926. "treaters\n",
  927. "treehoppers\n",
  928. "trekkers\n",
  929. "tremblers\n",
  930. "trenchers\n",
  931. "trendsetters\n",
  932. "trepanners\n",
  933. "trephiners\n",
  934. "trespassers\n",
  935. "triaconters\n",
  936. "tribometers\n",
  937. "tributers\n",
  938. "trickers\n",
  939. "tricksters\n",
  940. "tricyclers\n",
  941. "triers\n",
  942. "triflers\n",
  943. "triggers\n",
  944. "trigonometers\n",
  945. "trillers\n",
  946. "trimers\n",
  947. "trimesters\n",
  948. "trimeters\n",
  949. "trimmers\n",
  950. "trinketers\n",
  951. "triphammers\n",
  952. "trippers\n",
  953. "tripplers\n",
  954. "triumphers\n",
  955. "trochanters\n",
  956. "trocheameters\n",
  957. "trochometers\n",
  958. "troffers\n",
  959. "trollers\n",
  960. "tromometers\n",
  961. "troopers\n",
  962. "troposcatters\n",
  963. "trossers\n",
  964. "trotters\n",
  965. "troublemakers\n",
  966. "troublers\n",
  967. "troubleshooters\n",
  968. "trouncers\n",
  969. "troupers\n",
  970. "trousers\n",
  971. "trouters\n",
  972. "trovers\n",
  973. "trowelers\n",
  974. "trowellers\n",
  975. "trowsers\n",
  976. "truckers\n",
  977. "trucklers\n",
  978. "truckmasters\n",
  979. "trudgers\n",
  980. "trumpeters\n",
  981. "truncheoners\n",
  982. "trundlers\n",
  983. "trussers\n",
  984. "trustbusters\n",
  985. "trusters\n",
  986. "tryers\n",
  987. "trysters\n",
  988. "tubbers\n",
  989. "tubers\n",
  990. "tuckers\n",
  991. "tufters\n",
  992. "tuggers\n",
  993. "tumblers\n",
  994. "tummlers\n",
  995. "tuners\n",
  996. "tunnelers\n",
  997. "tunnellers\n",
  998. "turbidimeters\n",
  999. "turbochargers\n",
  1000. "turcopoliers\n",
  1001. "turners\n",
  1002. "turnovers\n",
  1003. "turtlers\n",
  1004. "tushkers\n",
  1005. "tuskers\n",
  1006. "tussers\n",
  1007. "tutoyers\n",
  1008. "tutworkers\n",
  1009. "tuyers\n",
  1010. "twaddlers\n",
  1011. "twangers\n",
  1012. "twanglers\n",
  1013. "twattlers\n",
  1014. "tweakers\n",
  1015. "tweedlers\n",
  1016. "tweenagers\n",
  1017. "tweeners\n",
  1018. "tweers\n",
  1019. "tweeters\n",
  1020. "tweezers\n",
  1021. "twicers\n",
  1022. "twiddlers\n",
  1023. "twiers\n",
  1024. "twiggers\n",
  1025. "twiners\n",
  1026. "twinflowers\n",
  1027. "twinklers\n",
  1028. "twinters\n",
  1029. "twirlers\n",
  1030. "twisters\n",
  1031. "twitchers\n",
  1032. "twitterers\n",
  1033. "twitters\n",
  1034. "twoccers\n",
  1035. "twockers\n",
  1036. "twoers\n",
  1037. "twofers\n",
  1038. "twoseaters\n",
  1039. "twyers\n",
  1040. "tyers\n",
  1041. "tylers\n",
  1042. "typecasters\n",
  1043. "typefounders\n",
  1044. "typesetters\n",
  1045. "typewriters\n",
  1046. "typifiers\n",
  1047. "typographers\n",
  1048. "tyrannisers\n",
  1049. "tyrannizers\n"
  1050. ]
  1051. }
  1052. ],
  1053. "source": [
  1054. "# regular expressions\n",
  1055. "import re\n",
  1056. "s = r'^t.*ers$' # all words starting with `t` ending in `ers`\n",
  1057. "\n",
  1058. "for w in data.split('\\n'):\n",
  1059. " if re.match(s, w):\n",
  1060. " print(w)"
  1061. ]
  1062. },
  1063. {
  1064. "cell_type": "markdown",
  1065. "metadata": {
  1066. "slideshow": {
  1067. "slide_type": "subslide"
  1068. }
  1069. },
  1070. "source": [
  1071. "### There are many thousands of libraries to get lost in ..."
  1072. ]
  1073. },
  1074. {
  1075. "cell_type": "markdown",
  1076. "metadata": {
  1077. "slideshow": {
  1078. "slide_type": "slide"
  1079. }
  1080. },
  1081. "source": [
  1082. "## Data types in Python are simple and complete ...\n",
  1083. "\n",
  1084. "* You are already familiar with these:\n",
  1085. " * numbers (`1`, `1.87`, `-0.88`, ...)\n",
  1086. " * strings (`\"Hello\"`, `'Hello'`)\n",
  1087. " * Boolean (`True`, `False`)\n",
  1088. " * `None`"
  1089. ]
  1090. },
  1091. {
  1092. "cell_type": "code",
  1093. "execution_count": 13,
  1094. "metadata": {
  1095. "slideshow": {
  1096. "slide_type": "subslide"
  1097. }
  1098. },
  1099. "outputs": [],
  1100. "source": [
  1101. "### No surprises here ..."
  1102. ]
  1103. },
  1104. {
  1105. "cell_type": "code",
  1106. "execution_count": 14,
  1107. "metadata": {
  1108. "slideshow": {
  1109. "slide_type": "fragment"
  1110. }
  1111. },
  1112. "outputs": [
  1113. {
  1114. "data": {
  1115. "text/plain": [
  1116. "3"
  1117. ]
  1118. },
  1119. "execution_count": 14,
  1120. "metadata": {},
  1121. "output_type": "execute_result"
  1122. }
  1123. ],
  1124. "source": [
  1125. "1 + 2"
  1126. ]
  1127. },
  1128. {
  1129. "cell_type": "code",
  1130. "execution_count": 15,
  1131. "metadata": {
  1132. "slideshow": {
  1133. "slide_type": "fragment"
  1134. }
  1135. },
  1136. "outputs": [
  1137. {
  1138. "data": {
  1139. "text/plain": [
  1140. "'helloHello'"
  1141. ]
  1142. },
  1143. "execution_count": 15,
  1144. "metadata": {},
  1145. "output_type": "execute_result"
  1146. }
  1147. ],
  1148. "source": [
  1149. "'hello' + \"Hello\""
  1150. ]
  1151. },
  1152. {
  1153. "cell_type": "markdown",
  1154. "metadata": {
  1155. "slideshow": {
  1156. "slide_type": "slide"
  1157. }
  1158. },
  1159. "source": [
  1160. "## Iterables are an important type category that includes:\n",
  1161. "\n",
  1162. "* lists\n",
  1163. "* tuples\n",
  1164. "* sets\n"
  1165. ]
  1166. },
  1167. {
  1168. "cell_type": "markdown",
  1169. "metadata": {
  1170. "slideshow": {
  1171. "slide_type": "subslide"
  1172. }
  1173. },
  1174. "source": [
  1175. "### Lists are just like arrays ..."
  1176. ]
  1177. },
  1178. {
  1179. "cell_type": "code",
  1180. "execution_count": 16,
  1181. "metadata": {
  1182. "slideshow": {
  1183. "slide_type": "fragment"
  1184. }
  1185. },
  1186. "outputs": [
  1187. {
  1188. "name": "stdout",
  1189. "output_type": "stream",
  1190. "text": [
  1191. "2\n"
  1192. ]
  1193. }
  1194. ],
  1195. "source": [
  1196. "lst = [1, 2, \"three\"]\n",
  1197. "print(lst[1])"
  1198. ]
  1199. },
  1200. {
  1201. "cell_type": "code",
  1202. "execution_count": 17,
  1203. "metadata": {
  1204. "slideshow": {
  1205. "slide_type": "fragment"
  1206. }
  1207. },
  1208. "outputs": [
  1209. {
  1210. "name": "stdout",
  1211. "output_type": "stream",
  1212. "text": [
  1213. "1 2 three "
  1214. ]
  1215. }
  1216. ],
  1217. "source": [
  1218. "for i in lst:\n",
  1219. " print(i, end=\" \")"
  1220. ]
  1221. },
  1222. {
  1223. "cell_type": "code",
  1224. "execution_count": 18,
  1225. "metadata": {
  1226. "slideshow": {
  1227. "slide_type": "fragment"
  1228. }
  1229. },
  1230. "outputs": [
  1231. {
  1232. "data": {
  1233. "text/plain": [
  1234. "[1, 2, 'three', 'a', 'b']"
  1235. ]
  1236. },
  1237. "execution_count": 18,
  1238. "metadata": {},
  1239. "output_type": "execute_result"
  1240. }
  1241. ],
  1242. "source": [
  1243. "lst + ['a', 'b']"
  1244. ]
  1245. },
  1246. {
  1247. "cell_type": "markdown",
  1248. "metadata": {
  1249. "slideshow": {
  1250. "slide_type": "subslide"
  1251. }
  1252. },
  1253. "source": [
  1254. "### Python doesn't require you to keep up with indices ...\n",
  1255. "\n",
  1256. "* but if you need them (which you rarely will) use `enumerate`"
  1257. ]
  1258. },
  1259. {
  1260. "cell_type": "code",
  1261. "execution_count": 19,
  1262. "metadata": {},
  1263. "outputs": [
  1264. {
  1265. "name": "stdout",
  1266. "output_type": "stream",
  1267. "text": [
  1268. "0:1 1:2 2:three "
  1269. ]
  1270. }
  1271. ],
  1272. "source": [
  1273. "for idx, l in enumerate(lst):\n",
  1274. " print(\"{}:{}\".format(idx, l), end=\" \")"
  1275. ]
  1276. },
  1277. {
  1278. "cell_type": "markdown",
  1279. "metadata": {
  1280. "slideshow": {
  1281. "slide_type": "subslide"
  1282. }
  1283. },
  1284. "source": [
  1285. "### You can also do cool things with list access ..."
  1286. ]
  1287. },
  1288. {
  1289. "cell_type": "code",
  1290. "execution_count": 20,
  1291. "metadata": {
  1292. "slideshow": {
  1293. "slide_type": "fragment"
  1294. }
  1295. },
  1296. "outputs": [
  1297. {
  1298. "data": {
  1299. "text/plain": [
  1300. "'three'"
  1301. ]
  1302. },
  1303. "execution_count": 20,
  1304. "metadata": {},
  1305. "output_type": "execute_result"
  1306. }
  1307. ],
  1308. "source": [
  1309. "lst[-1]"
  1310. ]
  1311. },
  1312. {
  1313. "cell_type": "code",
  1314. "execution_count": 21,
  1315. "metadata": {
  1316. "slideshow": {
  1317. "slide_type": "fragment"
  1318. }
  1319. },
  1320. "outputs": [
  1321. {
  1322. "data": {
  1323. "text/plain": [
  1324. "[1, 2]"
  1325. ]
  1326. },
  1327. "execution_count": 21,
  1328. "metadata": {},
  1329. "output_type": "execute_result"
  1330. }
  1331. ],
  1332. "source": [
  1333. "lst[0:2]"
  1334. ]
  1335. },
  1336. {
  1337. "cell_type": "code",
  1338. "execution_count": 22,
  1339. "metadata": {
  1340. "slideshow": {
  1341. "slide_type": "fragment"
  1342. }
  1343. },
  1344. "outputs": [
  1345. {
  1346. "data": {
  1347. "text/plain": [
  1348. "['three', 2, 1]"
  1349. ]
  1350. },
  1351. "execution_count": 22,
  1352. "metadata": {},
  1353. "output_type": "execute_result"
  1354. }
  1355. ],
  1356. "source": [
  1357. "lst[::-1]"
  1358. ]
  1359. },
  1360. {
  1361. "cell_type": "markdown",
  1362. "metadata": {
  1363. "slideshow": {
  1364. "slide_type": "subslide"
  1365. }
  1366. },
  1367. "source": [
  1368. "### Tuples are another useful type\n",
  1369. "* they are just immutable lists\n",
  1370. "* and denoted `(1, 2, 'three`)"
  1371. ]
  1372. },
  1373. {
  1374. "cell_type": "markdown",
  1375. "metadata": {
  1376. "slideshow": {
  1377. "slide_type": "subslide"
  1378. }
  1379. },
  1380. "source": [
  1381. "### Sets are also valuable "
  1382. ]
  1383. },
  1384. {
  1385. "cell_type": "code",
  1386. "execution_count": 23,
  1387. "metadata": {
  1388. "slideshow": {
  1389. "slide_type": "fragment"
  1390. }
  1391. },
  1392. "outputs": [
  1393. {
  1394. "data": {
  1395. "text/plain": [
  1396. "{1, 2, 3, 4, 5}"
  1397. ]
  1398. },
  1399. "execution_count": 23,
  1400. "metadata": {},
  1401. "output_type": "execute_result"
  1402. }
  1403. ],
  1404. "source": [
  1405. "set([1,1,1,1,1,1,2,3,4,5,5,5,5,5,5,5])"
  1406. ]
  1407. },
  1408. {
  1409. "cell_type": "code",
  1410. "execution_count": 24,
  1411. "metadata": {
  1412. "slideshow": {
  1413. "slide_type": "fragment"
  1414. }
  1415. },
  1416. "outputs": [
  1417. {
  1418. "data": {
  1419. "text/plain": [
  1420. "{1, 2, 3, 4, 5}"
  1421. ]
  1422. },
  1423. "execution_count": 24,
  1424. "metadata": {},
  1425. "output_type": "execute_result"
  1426. }
  1427. ],
  1428. "source": [
  1429. "setA = set([1,2])\n",
  1430. "setB = set([1,2,3,4,5])\n",
  1431. "setA.union(setB)"
  1432. ]
  1433. },
  1434. {
  1435. "cell_type": "code",
  1436. "execution_count": 25,
  1437. "metadata": {
  1438. "slideshow": {
  1439. "slide_type": "fragment"
  1440. }
  1441. },
  1442. "outputs": [
  1443. {
  1444. "data": {
  1445. "text/plain": [
  1446. "set()"
  1447. ]
  1448. },
  1449. "execution_count": 25,
  1450. "metadata": {},
  1451. "output_type": "execute_result"
  1452. }
  1453. ],
  1454. "source": [
  1455. "setA.difference(setB)"
  1456. ]
  1457. },
  1458. {
  1459. "cell_type": "code",
  1460. "execution_count": 26,
  1461. "metadata": {
  1462. "slideshow": {
  1463. "slide_type": "fragment"
  1464. }
  1465. },
  1466. "outputs": [
  1467. {
  1468. "data": {
  1469. "text/plain": [
  1470. "{3, 4, 5}"
  1471. ]
  1472. },
  1473. "execution_count": 26,
  1474. "metadata": {},
  1475. "output_type": "execute_result"
  1476. }
  1477. ],
  1478. "source": [
  1479. "setB.difference(setA)"
  1480. ]
  1481. },
  1482. {
  1483. "cell_type": "code",
  1484. "execution_count": 27,
  1485. "metadata": {
  1486. "slideshow": {
  1487. "slide_type": "fragment"
  1488. }
  1489. },
  1490. "outputs": [
  1491. {
  1492. "data": {
  1493. "text/plain": [
  1494. "{1, 2}"
  1495. ]
  1496. },
  1497. "execution_count": 27,
  1498. "metadata": {},
  1499. "output_type": "execute_result"
  1500. }
  1501. ],
  1502. "source": [
  1503. "setA.intersection(setB)"
  1504. ]
  1505. },
  1506. {
  1507. "cell_type": "markdown",
  1508. "metadata": {
  1509. "slideshow": {
  1510. "slide_type": "slide"
  1511. }
  1512. },
  1513. "source": [
  1514. "## Dictionaries are also a crucial type in the language\n",
  1515. "\n",
  1516. "* associative arrays\n",
  1517. "* key/value pairs"
  1518. ]
  1519. },
  1520. {
  1521. "cell_type": "code",
  1522. "execution_count": 28,
  1523. "metadata": {
  1524. "slideshow": {
  1525. "slide_type": "fragment"
  1526. }
  1527. },
  1528. "outputs": [
  1529. {
  1530. "name": "stdout",
  1531. "output_type": "stream",
  1532. "text": [
  1533. "{'a': 5, 'b': 6}\n"
  1534. ]
  1535. }
  1536. ],
  1537. "source": [
  1538. "d = {\"a\": 5, \"b\": 6}\n",
  1539. "print(d)"
  1540. ]
  1541. },
  1542. {
  1543. "cell_type": "code",
  1544. "execution_count": 29,
  1545. "metadata": {
  1546. "slideshow": {
  1547. "slide_type": "fragment"
  1548. }
  1549. },
  1550. "outputs": [
  1551. {
  1552. "name": "stdout",
  1553. "output_type": "stream",
  1554. "text": [
  1555. "{'a': 5, 'b': 6}\n"
  1556. ]
  1557. }
  1558. ],
  1559. "source": [
  1560. "d = dict([('a', 5), ('b', 6)])\n",
  1561. "print(d)"
  1562. ]
  1563. },
  1564. {
  1565. "cell_type": "markdown",
  1566. "metadata": {
  1567. "slideshow": {
  1568. "slide_type": "slide"
  1569. }
  1570. },
  1571. "source": [
  1572. "### Providing the usual suspects ..."
  1573. ]
  1574. },
  1575. {
  1576. "cell_type": "code",
  1577. "execution_count": 30,
  1578. "metadata": {
  1579. "slideshow": {
  1580. "slide_type": "fragment"
  1581. }
  1582. },
  1583. "outputs": [
  1584. {
  1585. "data": {
  1586. "text/plain": [
  1587. "5"
  1588. ]
  1589. },
  1590. "execution_count": 30,
  1591. "metadata": {},
  1592. "output_type": "execute_result"
  1593. }
  1594. ],
  1595. "source": [
  1596. "d['a']"
  1597. ]
  1598. },
  1599. {
  1600. "cell_type": "code",
  1601. "execution_count": 31,
  1602. "metadata": {
  1603. "slideshow": {
  1604. "slide_type": "fragment"
  1605. }
  1606. },
  1607. "outputs": [
  1608. {
  1609. "data": {
  1610. "text/plain": [
  1611. "dict_keys(['a', 'b'])"
  1612. ]
  1613. },
  1614. "execution_count": 31,
  1615. "metadata": {},
  1616. "output_type": "execute_result"
  1617. }
  1618. ],
  1619. "source": [
  1620. "d.keys()"
  1621. ]
  1622. },
  1623. {
  1624. "cell_type": "code",
  1625. "execution_count": 32,
  1626. "metadata": {
  1627. "slideshow": {
  1628. "slide_type": "fragment"
  1629. }
  1630. },
  1631. "outputs": [
  1632. {
  1633. "data": {
  1634. "text/plain": [
  1635. "dict_values([5, 6])"
  1636. ]
  1637. },
  1638. "execution_count": 32,
  1639. "metadata": {},
  1640. "output_type": "execute_result"
  1641. }
  1642. ],
  1643. "source": [
  1644. "d.values()"
  1645. ]
  1646. },
  1647. {
  1648. "cell_type": "code",
  1649. "execution_count": 33,
  1650. "metadata": {
  1651. "slideshow": {
  1652. "slide_type": "fragment"
  1653. }
  1654. },
  1655. "outputs": [
  1656. {
  1657. "name": "stdout",
  1658. "output_type": "stream",
  1659. "text": [
  1660. "a=>5\n",
  1661. "b=>6\n"
  1662. ]
  1663. }
  1664. ],
  1665. "source": [
  1666. "for key, value in d.items():\n",
  1667. " print(\"{}=>{}\".format(key, value))"
  1668. ]
  1669. },
  1670. {
  1671. "cell_type": "markdown",
  1672. "metadata": {
  1673. "slideshow": {
  1674. "slide_type": "slide"
  1675. }
  1676. },
  1677. "source": [
  1678. "## Python logic operators are intuitive\n",
  1679. "\n",
  1680. "* you've seen `in` and `is`\n",
  1681. "* but there is `not`, `not in`, `is not`, `and`, `or`"
  1682. ]
  1683. },
  1684. {
  1685. "cell_type": "code",
  1686. "execution_count": 34,
  1687. "metadata": {
  1688. "slideshow": {
  1689. "slide_type": "subslide"
  1690. }
  1691. },
  1692. "outputs": [
  1693. {
  1694. "data": {
  1695. "text/plain": [
  1696. "False"
  1697. ]
  1698. },
  1699. "execution_count": 34,
  1700. "metadata": {},
  1701. "output_type": "execute_result"
  1702. }
  1703. ],
  1704. "source": [
  1705. "1 is 2"
  1706. ]
  1707. },
  1708. {
  1709. "cell_type": "code",
  1710. "execution_count": 35,
  1711. "metadata": {
  1712. "slideshow": {
  1713. "slide_type": "fragment"
  1714. }
  1715. },
  1716. "outputs": [
  1717. {
  1718. "data": {
  1719. "text/plain": [
  1720. "True"
  1721. ]
  1722. },
  1723. "execution_count": 35,
  1724. "metadata": {},
  1725. "output_type": "execute_result"
  1726. }
  1727. ],
  1728. "source": [
  1729. "1 is not 2"
  1730. ]
  1731. },
  1732. {
  1733. "cell_type": "code",
  1734. "execution_count": 36,
  1735. "metadata": {
  1736. "slideshow": {
  1737. "slide_type": "fragment"
  1738. }
  1739. },
  1740. "outputs": [
  1741. {
  1742. "data": {
  1743. "text/plain": [
  1744. "True"
  1745. ]
  1746. },
  1747. "execution_count": 36,
  1748. "metadata": {},
  1749. "output_type": "execute_result"
  1750. }
  1751. ],
  1752. "source": [
  1753. "True and True"
  1754. ]
  1755. },
  1756. {
  1757. "cell_type": "code",
  1758. "execution_count": 37,
  1759. "metadata": {
  1760. "slideshow": {
  1761. "slide_type": "fragment"
  1762. }
  1763. },
  1764. "outputs": [
  1765. {
  1766. "data": {
  1767. "text/plain": [
  1768. "True"
  1769. ]
  1770. },
  1771. "execution_count": 37,
  1772. "metadata": {},
  1773. "output_type": "execute_result"
  1774. }
  1775. ],
  1776. "source": [
  1777. "True or False"
  1778. ]
  1779. },
  1780. {
  1781. "cell_type": "code",
  1782. "execution_count": 38,
  1783. "metadata": {
  1784. "slideshow": {
  1785. "slide_type": "subslide"
  1786. }
  1787. },
  1788. "outputs": [
  1789. {
  1790. "data": {
  1791. "text/plain": [
  1792. "False"
  1793. ]
  1794. },
  1795. "execution_count": 38,
  1796. "metadata": {},
  1797. "output_type": "execute_result"
  1798. }
  1799. ],
  1800. "source": [
  1801. "False or False"
  1802. ]
  1803. },
  1804. {
  1805. "cell_type": "code",
  1806. "execution_count": 39,
  1807. "metadata": {
  1808. "slideshow": {
  1809. "slide_type": "fragment"
  1810. }
  1811. },
  1812. "outputs": [
  1813. {
  1814. "data": {
  1815. "text/plain": [
  1816. "True"
  1817. ]
  1818. },
  1819. "execution_count": 39,
  1820. "metadata": {},
  1821. "output_type": "execute_result"
  1822. }
  1823. ],
  1824. "source": [
  1825. "False is not True"
  1826. ]
  1827. },
  1828. {
  1829. "cell_type": "code",
  1830. "execution_count": 40,
  1831. "metadata": {
  1832. "slideshow": {
  1833. "slide_type": "fragment"
  1834. }
  1835. },
  1836. "outputs": [],
  1837. "source": [
  1838. "s = 'supercalifragilistic'"
  1839. ]
  1840. },
  1841. {
  1842. "cell_type": "code",
  1843. "execution_count": 41,
  1844. "metadata": {
  1845. "slideshow": {
  1846. "slide_type": "fragment"
  1847. }
  1848. },
  1849. "outputs": [
  1850. {
  1851. "data": {
  1852. "text/plain": [
  1853. "True"
  1854. ]
  1855. },
  1856. "execution_count": 41,
  1857. "metadata": {},
  1858. "output_type": "execute_result"
  1859. }
  1860. ],
  1861. "source": [
  1862. "'u' in s"
  1863. ]
  1864. },
  1865. {
  1866. "cell_type": "code",
  1867. "execution_count": 42,
  1868. "metadata": {
  1869. "slideshow": {
  1870. "slide_type": "fragment"
  1871. }
  1872. },
  1873. "outputs": [
  1874. {
  1875. "data": {
  1876. "text/plain": [
  1877. "True"
  1878. ]
  1879. },
  1880. "execution_count": 42,
  1881. "metadata": {},
  1882. "output_type": "execute_result"
  1883. }
  1884. ],
  1885. "source": [
  1886. "'z' not in s"
  1887. ]
  1888. },
  1889. {
  1890. "cell_type": "code",
  1891. "execution_count": 43,
  1892. "metadata": {
  1893. "slideshow": {
  1894. "slide_type": "subslide"
  1895. }
  1896. },
  1897. "outputs": [],
  1898. "source": [
  1899. "lst = ['a', 'b', 1, 2, 3]"
  1900. ]
  1901. },
  1902. {
  1903. "cell_type": "code",
  1904. "execution_count": 44,
  1905. "metadata": {
  1906. "slideshow": {
  1907. "slide_type": "fragment"
  1908. }
  1909. },
  1910. "outputs": [
  1911. {
  1912. "data": {
  1913. "text/plain": [
  1914. "True"
  1915. ]
  1916. },
  1917. "execution_count": 44,
  1918. "metadata": {},
  1919. "output_type": "execute_result"
  1920. }
  1921. ],
  1922. "source": [
  1923. "'a' in lst"
  1924. ]
  1925. },
  1926. {
  1927. "cell_type": "code",
  1928. "execution_count": 45,
  1929. "metadata": {
  1930. "slideshow": {
  1931. "slide_type": "subslide"
  1932. }
  1933. },
  1934. "outputs": [
  1935. {
  1936. "data": {
  1937. "text/plain": [
  1938. "True"
  1939. ]
  1940. },
  1941. "execution_count": 45,
  1942. "metadata": {},
  1943. "output_type": "execute_result"
  1944. }
  1945. ],
  1946. "source": [
  1947. "s == 'supercalifragilistic'"
  1948. ]
  1949. },
  1950. {
  1951. "cell_type": "code",
  1952. "execution_count": 46,
  1953. "metadata": {
  1954. "slideshow": {
  1955. "slide_type": "fragment"
  1956. }
  1957. },
  1958. "outputs": [
  1959. {
  1960. "data": {
  1961. "text/plain": [
  1962. "True"
  1963. ]
  1964. },
  1965. "execution_count": 46,
  1966. "metadata": {},
  1967. "output_type": "execute_result"
  1968. }
  1969. ],
  1970. "source": [
  1971. "s is 'supercalifragilistic'"
  1972. ]
  1973. },
  1974. {
  1975. "cell_type": "code",
  1976. "execution_count": 47,
  1977. "metadata": {
  1978. "slideshow": {
  1979. "slide_type": "fragment"
  1980. }
  1981. },
  1982. "outputs": [
  1983. {
  1984. "data": {
  1985. "text/plain": [
  1986. "True"
  1987. ]
  1988. },
  1989. "execution_count": 47,
  1990. "metadata": {},
  1991. "output_type": "execute_result"
  1992. }
  1993. ],
  1994. "source": [
  1995. "lst == ['a', 'b', 1, 2, 3]"
  1996. ]
  1997. },
  1998. {
  1999. "cell_type": "code",
  2000. "execution_count": 48,
  2001. "metadata": {
  2002. "slideshow": {
  2003. "slide_type": "fragment"
  2004. }
  2005. },
  2006. "outputs": [
  2007. {
  2008. "data": {
  2009. "text/plain": [
  2010. "False"
  2011. ]
  2012. },
  2013. "execution_count": 48,
  2014. "metadata": {},
  2015. "output_type": "execute_result"
  2016. }
  2017. ],
  2018. "source": [
  2019. "lst is ['a', 'b', 1, 2, 3]"
  2020. ]
  2021. },
  2022. {
  2023. "cell_type": "markdown",
  2024. "metadata": {
  2025. "slideshow": {
  2026. "slide_type": "slide"
  2027. }
  2028. },
  2029. "source": [
  2030. "## Flow control in Python also includes all the usuals ...\n",
  2031. "* `if/elif/else`\n",
  2032. "* `for`\n",
  2033. "* `while`"
  2034. ]
  2035. },
  2036. {
  2037. "cell_type": "code",
  2038. "execution_count": 49,
  2039. "metadata": {
  2040. "slideshow": {
  2041. "slide_type": "subslide"
  2042. }
  2043. },
  2044. "outputs": [
  2045. {
  2046. "name": "stdout",
  2047. "output_type": "stream",
  2048. "text": [
  2049. "the first letter is a 't'\n"
  2050. ]
  2051. }
  2052. ],
  2053. "source": [
  2054. "s = 'this is a test'\n",
  2055. "\n",
  2056. "if s[0] == 't':\n",
  2057. " print(\"the first letter is a 't'\")\n",
  2058. "else:\n",
  2059. " print(\"the first letter is not a 't'\")"
  2060. ]
  2061. },
  2062. {
  2063. "cell_type": "code",
  2064. "execution_count": 50,
  2065. "metadata": {
  2066. "slideshow": {
  2067. "slide_type": "subslide"
  2068. }
  2069. },
  2070. "outputs": [
  2071. {
  2072. "name": "stdout",
  2073. "output_type": "stream",
  2074. "text": [
  2075. "t\n",
  2076. "h\n",
  2077. "i\n",
  2078. "s\n",
  2079. " \n",
  2080. "i\n",
  2081. "s\n",
  2082. " \n",
  2083. "a\n",
  2084. " \n",
  2085. "t\n",
  2086. "e\n",
  2087. "s\n",
  2088. "t\n"
  2089. ]
  2090. }
  2091. ],
  2092. "source": [
  2093. "for c in s:\n",
  2094. " print(\"{}\".format(c))"
  2095. ]
  2096. },
  2097. {
  2098. "cell_type": "code",
  2099. "execution_count": 51,
  2100. "metadata": {
  2101. "slideshow": {
  2102. "slide_type": "subslide"
  2103. }
  2104. },
  2105. "outputs": [
  2106. {
  2107. "name": "stdout",
  2108. "output_type": "stream",
  2109. "text": [
  2110. "t\n",
  2111. "h\n",
  2112. "i\n",
  2113. "s\n",
  2114. " \n",
  2115. "i\n",
  2116. "s\n",
  2117. " \n",
  2118. "a\n",
  2119. " \n",
  2120. "t\n",
  2121. "e\n",
  2122. "s\n",
  2123. "t\n"
  2124. ]
  2125. }
  2126. ],
  2127. "source": [
  2128. "# THIS IS NOT PYTHONIC! for is more elegant\n",
  2129. "\n",
  2130. "i = 0\n",
  2131. "while(i < len(s)):\n",
  2132. " print(\"{}\".format(s[i]))\n",
  2133. " i+=1"
  2134. ]
  2135. },
  2136. {
  2137. "cell_type": "markdown",
  2138. "metadata": {
  2139. "slideshow": {
  2140. "slide_type": "skip"
  2141. }
  2142. },
  2143. "source": [
  2144. "## classes/objects"
  2145. ]
  2146. },
  2147. {
  2148. "cell_type": "markdown",
  2149. "metadata": {
  2150. "slideshow": {
  2151. "slide_type": "slide"
  2152. }
  2153. },
  2154. "source": [
  2155. "## Python's built in functions are excellent\n",
  2156. "* There are about 69 functions\n",
  2157. "* some of the most common/useful are:\n",
  2158. " * `min`, `max`, `all`, `any` \n",
  2159. " * `zip`, `range`, `len`, `map`, `sorted`\n",
  2160. " * `reversed`"
  2161. ]
  2162. },
  2163. {
  2164. "cell_type": "markdown",
  2165. "metadata": {
  2166. "slideshow": {
  2167. "slide_type": "slide"
  2168. }
  2169. },
  2170. "source": [
  2171. "### Let's play:\n",
  2172. "\n",
  2173. "* **PROBLEM**: create a 16 character random password of upper, lower and number\n",
  2174. "* **ONE SOLUTION**: _use what we know about the ASCII table and the `chr()` built in function_"
  2175. ]
  2176. },
  2177. {
  2178. "cell_type": "markdown",
  2179. "metadata": {
  2180. "slideshow": {
  2181. "slide_type": "subslide"
  2182. }
  2183. },
  2184. "source": [
  2185. "![./better_ascii_table.jpg](./better_ascii_table.jpg)"
  2186. ]
  2187. },
  2188. {
  2189. "cell_type": "markdown",
  2190. "metadata": {
  2191. "slideshow": {
  2192. "slide_type": "subslide"
  2193. }
  2194. },
  2195. "source": [
  2196. "**HINT 1:**\n",
  2197. " "
  2198. ]
  2199. },
  2200. {
  2201. "cell_type": "code",
  2202. "execution_count": 52,
  2203. "metadata": {
  2204. "slideshow": {
  2205. "slide_type": "-"
  2206. }
  2207. },
  2208. "outputs": [
  2209. {
  2210. "data": {
  2211. "text/plain": [
  2212. "'z'"
  2213. ]
  2214. },
  2215. "execution_count": 52,
  2216. "metadata": {},
  2217. "output_type": "execute_result"
  2218. }
  2219. ],
  2220. "source": [
  2221. "chr(122)"
  2222. ]
  2223. },
  2224. {
  2225. "cell_type": "code",
  2226. "execution_count": 53,
  2227. "metadata": {
  2228. "slideshow": {
  2229. "slide_type": "subslide"
  2230. }
  2231. },
  2232. "outputs": [
  2233. {
  2234. "name": "stdout",
  2235. "output_type": "stream",
  2236. "text": [
  2237. "uMVKgvYyB114e4rpj"
  2238. ]
  2239. }
  2240. ],
  2241. "source": [
  2242. "import random\n",
  2243. "\n",
  2244. "for i in range(0, 17):\n",
  2245. " # get a number in the ASCII range\n",
  2246. " while(True):\n",
  2247. " rnd_c = random.randint(48, 123)\n",
  2248. " if rnd_c < 58 or (rnd_c > 65 and rnd_c <91) or (rnd_c > 96 and rnd_c < 123):\n",
  2249. " break\n",
  2250. " \n",
  2251. " print(chr(rnd_c), end=\"\")"
  2252. ]
  2253. },
  2254. {
  2255. "cell_type": "markdown",
  2256. "metadata": {
  2257. "slideshow": {
  2258. "slide_type": "slide"
  2259. }
  2260. },
  2261. "source": [
  2262. "## Functions are straightforward and intuitive"
  2263. ]
  2264. },
  2265. {
  2266. "cell_type": "code",
  2267. "execution_count": 54,
  2268. "metadata": {
  2269. "slideshow": {
  2270. "slide_type": "fragment"
  2271. }
  2272. },
  2273. "outputs": [
  2274. {
  2275. "data": {
  2276. "text/plain": [
  2277. "1"
  2278. ]
  2279. },
  2280. "execution_count": 54,
  2281. "metadata": {},
  2282. "output_type": "execute_result"
  2283. }
  2284. ],
  2285. "source": [
  2286. "def a_function():\n",
  2287. " return 1\n",
  2288. "\n",
  2289. "a_function()"
  2290. ]
  2291. },
  2292. {
  2293. "cell_type": "markdown",
  2294. "metadata": {
  2295. "slideshow": {
  2296. "slide_type": "subslide"
  2297. }
  2298. },
  2299. "source": [
  2300. "### Function arguments (parameters) come in three flavors\n",
  2301. "\n",
  2302. "* positional\n",
  2303. "* keyword\n",
  2304. "* mixed\n",
  2305. "\n",
  2306. "Note: default values are allowed!"
  2307. ]
  2308. },
  2309. {
  2310. "cell_type": "code",
  2311. "execution_count": 55,
  2312. "metadata": {
  2313. "slideshow": {
  2314. "slide_type": "subslide"
  2315. }
  2316. },
  2317. "outputs": [
  2318. {
  2319. "data": {
  2320. "text/plain": [
  2321. "6"
  2322. ]
  2323. },
  2324. "execution_count": 55,
  2325. "metadata": {},
  2326. "output_type": "execute_result"
  2327. }
  2328. ],
  2329. "source": [
  2330. "def a_function(a, b, c):\n",
  2331. " return a*b*c\n",
  2332. "\n",
  2333. "a_function(1,2,3)"
  2334. ]
  2335. },
  2336. {
  2337. "cell_type": "code",
  2338. "execution_count": 56,
  2339. "metadata": {
  2340. "slideshow": {
  2341. "slide_type": "subslide"
  2342. }
  2343. },
  2344. "outputs": [
  2345. {
  2346. "data": {
  2347. "text/plain": [
  2348. "192"
  2349. ]
  2350. },
  2351. "execution_count": 56,
  2352. "metadata": {},
  2353. "output_type": "execute_result"
  2354. }
  2355. ],
  2356. "source": [
  2357. "def a_function(a=1, b=1, c=1):\n",
  2358. " return a*b*c\n",
  2359. "\n",
  2360. "a_function(a=3,b=8,c=8)"
  2361. ]
  2362. },
  2363. {
  2364. "cell_type": "code",
  2365. "execution_count": 57,
  2366. "metadata": {
  2367. "slideshow": {
  2368. "slide_type": "fragment"
  2369. }
  2370. },
  2371. "outputs": [
  2372. {
  2373. "data": {
  2374. "text/plain": [
  2375. "4"
  2376. ]
  2377. },
  2378. "execution_count": 57,
  2379. "metadata": {},
  2380. "output_type": "execute_result"
  2381. }
  2382. ],
  2383. "source": [
  2384. "a_function(a=4)"
  2385. ]
  2386. },
  2387. {
  2388. "cell_type": "code",
  2389. "execution_count": 58,
  2390. "metadata": {},
  2391. "outputs": [
  2392. {
  2393. "data": {
  2394. "text/plain": [
  2395. "48"
  2396. ]
  2397. },
  2398. "execution_count": 58,
  2399. "metadata": {},
  2400. "output_type": "execute_result"
  2401. }
  2402. ],
  2403. "source": [
  2404. "a_function(3,b=4,c=4)"
  2405. ]
  2406. },
  2407. {
  2408. "cell_type": "code",
  2409. "execution_count": 59,
  2410. "metadata": {
  2411. "slideshow": {
  2412. "slide_type": "subslide"
  2413. }
  2414. },
  2415. "outputs": [
  2416. {
  2417. "data": {
  2418. "text/plain": [
  2419. "384"
  2420. ]
  2421. },
  2422. "execution_count": 59,
  2423. "metadata": {},
  2424. "output_type": "execute_result"
  2425. }
  2426. ],
  2427. "source": [
  2428. "def a_function(a, b=1, c=1):\n",
  2429. " return a*b*c\n",
  2430. "\n",
  2431. "a_function(6,b=8,c=8)"
  2432. ]
  2433. },
  2434. {
  2435. "cell_type": "code",
  2436. "execution_count": 60,
  2437. "metadata": {},
  2438. "outputs": [
  2439. {
  2440. "data": {
  2441. "text/plain": [
  2442. "384"
  2443. ]
  2444. },
  2445. "execution_count": 60,
  2446. "metadata": {},
  2447. "output_type": "execute_result"
  2448. }
  2449. ],
  2450. "source": [
  2451. "a_function(6, 8, 8)"
  2452. ]
  2453. },
  2454. {
  2455. "cell_type": "markdown",
  2456. "metadata": {
  2457. "slideshow": {
  2458. "slide_type": "subslide"
  2459. }
  2460. },
  2461. "source": [
  2462. "### Functions can return multiple values with tuples"
  2463. ]
  2464. },
  2465. {
  2466. "cell_type": "code",
  2467. "execution_count": 61,
  2468. "metadata": {
  2469. "slideshow": {
  2470. "slide_type": "fragment"
  2471. }
  2472. },
  2473. "outputs": [],
  2474. "source": [
  2475. "def b_function():\n",
  2476. " return (1, 2, 3)"
  2477. ]
  2478. },
  2479. {
  2480. "cell_type": "code",
  2481. "execution_count": 62,
  2482. "metadata": {
  2483. "slideshow": {
  2484. "slide_type": "fragment"
  2485. }
  2486. },
  2487. "outputs": [
  2488. {
  2489. "data": {
  2490. "text/plain": [
  2491. "(1, 2, 3)"
  2492. ]
  2493. },
  2494. "execution_count": 62,
  2495. "metadata": {},
  2496. "output_type": "execute_result"
  2497. }
  2498. ],
  2499. "source": [
  2500. "b_function()"
  2501. ]
  2502. },
  2503. {
  2504. "cell_type": "code",
  2505. "execution_count": 63,
  2506. "metadata": {
  2507. "slideshow": {
  2508. "slide_type": "fragment"
  2509. }
  2510. },
  2511. "outputs": [
  2512. {
  2513. "name": "stdout",
  2514. "output_type": "stream",
  2515. "text": [
  2516. "1 2 3\n"
  2517. ]
  2518. }
  2519. ],
  2520. "source": [
  2521. "x, y, z = b_function()\n",
  2522. "print(x, y, z)"
  2523. ]
  2524. },
  2525. {
  2526. "cell_type": "markdown",
  2527. "metadata": {
  2528. "slideshow": {
  2529. "slide_type": "slide"
  2530. }
  2531. },
  2532. "source": [
  2533. "## Exceptions are valuable for good code!\n",
  2534. "* Python supports `try`/`except`/`finally` and they should be used regularly"
  2535. ]
  2536. },
  2537. {
  2538. "cell_type": "code",
  2539. "execution_count": 64,
  2540. "metadata": {
  2541. "slideshow": {
  2542. "slide_type": "subslide"
  2543. }
  2544. },
  2545. "outputs": [],
  2546. "source": [
  2547. "def c_function(a, b, c):\n",
  2548. " try:\n",
  2549. " if a<0:\n",
  2550. " raise Exception\n",
  2551. " return a*b*c\n",
  2552. " except Exception as e:\n",
  2553. " print(\"The first parameter cannot be less than zero.\")\n",
  2554. " finally:\n",
  2555. " pass # we don't need to clean up anything after the exception"
  2556. ]
  2557. },
  2558. {
  2559. "cell_type": "code",
  2560. "execution_count": 65,
  2561. "metadata": {
  2562. "slideshow": {
  2563. "slide_type": "fragment"
  2564. }
  2565. },
  2566. "outputs": [
  2567. {
  2568. "name": "stdout",
  2569. "output_type": "stream",
  2570. "text": [
  2571. "The first parameter cannot be less than zero.\n"
  2572. ]
  2573. }
  2574. ],
  2575. "source": [
  2576. "c_function(-1, 2, 3) "
  2577. ]
  2578. },
  2579. {
  2580. "cell_type": "markdown",
  2581. "metadata": {
  2582. "slideshow": {
  2583. "slide_type": "subslide"
  2584. }
  2585. },
  2586. "source": [
  2587. "### What did the function _return_ from the exception???"
  2588. ]
  2589. },
  2590. {
  2591. "cell_type": "markdown",
  2592. "metadata": {
  2593. "slideshow": {
  2594. "slide_type": "fragment"
  2595. }
  2596. },
  2597. "source": [
  2598. "ALL PYTHON FUNCTIONS IMPLICITLY RETURN `None` WHEN NO RETURN VALUE IS SPECIFIED"
  2599. ]
  2600. },
  2601. {
  2602. "cell_type": "code",
  2603. "execution_count": 66,
  2604. "metadata": {
  2605. "slideshow": {
  2606. "slide_type": "fragment"
  2607. }
  2608. },
  2609. "outputs": [
  2610. {
  2611. "name": "stdout",
  2612. "output_type": "stream",
  2613. "text": [
  2614. "The first parameter cannot be less than zero.\n"
  2615. ]
  2616. },
  2617. {
  2618. "data": {
  2619. "text/plain": [
  2620. "True"
  2621. ]
  2622. },
  2623. "execution_count": 66,
  2624. "metadata": {},
  2625. "output_type": "execute_result"
  2626. }
  2627. ],
  2628. "source": [
  2629. "c_function(-1, 2, 3) is None"
  2630. ]
  2631. },
  2632. {
  2633. "cell_type": "markdown",
  2634. "metadata": {
  2635. "slideshow": {
  2636. "slide_type": "slide"
  2637. }
  2638. },
  2639. "source": [
  2640. "## Comprehensions"
  2641. ]
  2642. },
  2643. {
  2644. "cell_type": "markdown",
  2645. "metadata": {
  2646. "slideshow": {
  2647. "slide_type": "fragment"
  2648. }
  2649. },
  2650. "source": [
  2651. "### List comprehensions provide shorthand for building lists"
  2652. ]
  2653. },
  2654. {
  2655. "cell_type": "code",
  2656. "execution_count": 67,
  2657. "metadata": {
  2658. "slideshow": {
  2659. "slide_type": "fragment"
  2660. }
  2661. },
  2662. "outputs": [
  2663. {
  2664. "data": {
  2665. "text/plain": [
  2666. "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
  2667. ]
  2668. },
  2669. "execution_count": 67,
  2670. "metadata": {},
  2671. "output_type": "execute_result"
  2672. }
  2673. ],
  2674. "source": [
  2675. "l1 = [x for x in range(0,11)]\n",
  2676. "l1"
  2677. ]
  2678. },
  2679. {
  2680. "cell_type": "code",
  2681. "execution_count": 68,
  2682. "metadata": {
  2683. "slideshow": {
  2684. "slide_type": "subslide"
  2685. }
  2686. },
  2687. "outputs": [
  2688. {
  2689. "data": {
  2690. "text/plain": [
  2691. "'TMbD0xr0Bj'"
  2692. ]
  2693. },
  2694. "execution_count": 68,
  2695. "metadata": {},
  2696. "output_type": "execute_result"
  2697. }
  2698. ],
  2699. "source": [
  2700. "def pw_generator():\n",
  2701. " ascii_pw_range = list(range(48,58)) + list(range(65,91)) + list(range (97, 123))\n",
  2702. " \n",
  2703. " return ''.join([chr(random.choice(ascii_pw_range)) \\\n",
  2704. " for i in range(0,10)])\n",
  2705. "\n",
  2706. "pw_generator()"
  2707. ]
  2708. },
  2709. {
  2710. "cell_type": "code",
  2711. "execution_count": 69,
  2712. "metadata": {
  2713. "slideshow": {
  2714. "slide_type": "subslide"
  2715. }
  2716. },
  2717. "outputs": [
  2718. {
  2719. "data": {
  2720. "text/plain": [
  2721. "['MIO3ewedYc',\n",
  2722. " 'YbFp3ofMhC',\n",
  2723. " 'lgy5GUvod4',\n",
  2724. " 'VfOjbJ1oF3',\n",
  2725. " 'tqYltK37KK',\n",
  2726. " 'E4SzZ0zRjX',\n",
  2727. " 'fdJRpvyHrn',\n",
  2728. " '9cCas9FpyA',\n",
  2729. " '33qhlSo7kd',\n",
  2730. " 'dL1QupAp0V',\n",
  2731. " 'KBciH7ZSaN',\n",
  2732. " '3B1StGRLLe',\n",
  2733. " 'bR6wZCAzsa',\n",
  2734. " '9gq3zcB6lb',\n",
  2735. " 'vmNodUhosK']"
  2736. ]
  2737. },
  2738. "execution_count": 69,
  2739. "metadata": {},
  2740. "output_type": "execute_result"
  2741. }
  2742. ],
  2743. "source": [
  2744. "[pw_generator() for i in range(0,15)]"
  2745. ]
  2746. },
  2747. {
  2748. "cell_type": "markdown",
  2749. "metadata": {
  2750. "slideshow": {
  2751. "slide_type": "subslide"
  2752. }
  2753. },
  2754. "source": [
  2755. "### Dictionary comprehensions are also quite nice ...\n",
  2756. "\n",
  2757. "* Let's say you want to swap the key, value pairs in a dictionary ..."
  2758. ]
  2759. },
  2760. {
  2761. "cell_type": "code",
  2762. "execution_count": 70,
  2763. "metadata": {
  2764. "slideshow": {
  2765. "slide_type": "fragment"
  2766. }
  2767. },
  2768. "outputs": [
  2769. {
  2770. "data": {
  2771. "text/plain": [
  2772. "{1: 'a', 2: 'b', 3: 'c'}"
  2773. ]
  2774. },
  2775. "execution_count": 70,
  2776. "metadata": {},
  2777. "output_type": "execute_result"
  2778. }
  2779. ],
  2780. "source": [
  2781. "d1 = dict([('a', 1), ('b', 2), ('c', 3)])\n",
  2782. "{ v:k for (k,v) in d1.items() }"
  2783. ]
  2784. },
  2785. {
  2786. "cell_type": "markdown",
  2787. "metadata": {
  2788. "slideshow": {
  2789. "slide_type": "fragment"
  2790. }
  2791. },
  2792. "source": [
  2793. "* something more practical might be to filter out all values not meeting a criterion"
  2794. ]
  2795. },
  2796. {
  2797. "cell_type": "code",
  2798. "execution_count": 71,
  2799. "metadata": {
  2800. "slideshow": {
  2801. "slide_type": "fragment"
  2802. }
  2803. },
  2804. "outputs": [
  2805. {
  2806. "data": {
  2807. "text/plain": [
  2808. "{1: 'a', 3: 'c'}"
  2809. ]
  2810. },
  2811. "execution_count": 71,
  2812. "metadata": {},
  2813. "output_type": "execute_result"
  2814. }
  2815. ],
  2816. "source": [
  2817. "d1 = dict([('a', 1), ('b', 2), ('c', 3)])\n",
  2818. "{ v:k for (k,v) in d1.items() if v%2 != 0}"
  2819. ]
  2820. },
  2821. {
  2822. "cell_type": "markdown",
  2823. "metadata": {
  2824. "slideshow": {
  2825. "slide_type": "slide"
  2826. }
  2827. },
  2828. "source": [
  2829. "## LET'S PLAY!"
  2830. ]
  2831. },
  2832. {
  2833. "cell_type": "markdown",
  2834. "metadata": {
  2835. "slideshow": {
  2836. "slide_type": "skip"
  2837. }
  2838. },
  2839. "source": [
  2840. "## file i/o"
  2841. ]
  2842. }
  2843. ],
  2844. "metadata": {
  2845. "celltoolbar": "Slideshow",
  2846. "kernelspec": {
  2847. "display_name": "Python 3",
  2848. "language": "python",
  2849. "name": "python3"
  2850. },
  2851. "language_info": {
  2852. "codemirror_mode": {
  2853. "name": "ipython",
  2854. "version": 3
  2855. },
  2856. "file_extension": ".py",
  2857. "mimetype": "text/x-python",
  2858. "name": "python",
  2859. "nbconvert_exporter": "python",
  2860. "pygments_lexer": "ipython3",
  2861. "version": "3.6.6"
  2862. }
  2863. },
  2864. "nbformat": 4,
  2865. "nbformat_minor": 2
  2866. }