commit d2fd866a3bbf518331863a7f9cc433827219c24a Author: kmaull Date: Mon Feb 25 19:24:31 2019 -0700 initial commit of asset files diff --git a/1368244924-49523624.jpg b/1368244924-49523624.jpg new file mode 100644 index 0000000..2900fab Binary files /dev/null and b/1368244924-49523624.jpg differ diff --git a/Python Introduction.xmind b/Python Introduction.xmind new file mode 100644 index 0000000..10409c9 Binary files /dev/null and b/Python Introduction.xmind differ diff --git a/README.md b/README.md new file mode 100644 index 0000000..b15456a --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# A BRIEF PYTHON INTRODUCTION +Original talk given at [Galvanize gSchool](http://www.galvanize.com/courses/web-development/#.V9r616351Wo) on Jan 4, 2019. + + +## MIND MAP +A mindmap of this talk was produced with XMind ZEN trial and is in [XMind format](./Python%20Introduction.xmind) and in [PDF](./python_introduction.pdf). + +![this file](./python_introduction.png) + + +## SLIDES +Slides were produced from the Jupyter Notebook using RevealJS. + +* notebook: [presentation_01042019.ipynb](./presentation_01042019.ipynb) +* slides: [presentation_01042019.slides.html](./presentation_01042019.slides.html) + +## LICENSE +Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License. + +If you find anything in this repo useful, don't forget to Say Thanks! \ No newline at end of file diff --git a/better_ascii_table.jpg b/better_ascii_table.jpg new file mode 100644 index 0000000..42eedbe Binary files /dev/null and b/better_ascii_table.jpg differ diff --git a/presentation_01042019.ipynb b/presentation_01042019.ipynb new file mode 100644 index 0000000..3af091d --- /dev/null +++ b/presentation_01042019.ipynb @@ -0,0 +1,2866 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "notes" + } + }, + "source": [ + "Slide conversion: https://echorand.me/presentation-slides-with-jupyter-notebook.html\n", + "\n", + "`$ jupyter-nbconvert --to slides slides.ipynb --reveal-prefix=reveal.js --post serve`" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# A Brief Python Introduction\n", + "Keith Maull
\n", + "Jan. 03, 2019" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Python is a general purpose programming language that is ...\n", + "\n", + "* interpreted\n", + "* dynamically typed (not statically typed)\n", + "* intuitive\n", + "* fun and addictive" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "notes" + } + }, + "source": [ + "### strongly typed\n", + "* 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", + "### everything is an object" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Python syntax can be learned very quickly\n", + "\n", + "* indentation matters\n", + "* there are no curly braces, semicolons or tricks ...\n", + "* the syntax is one of the true joys of the language once you learn not to fight it\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "## We will learn 80% of the syntax today in 1 hour!\n", + "\n", + "![./1368244924-49523624.jpg](./1368244924-49523624.jpg)" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The first item of the list is an `a`\n" + ] + } + ], + "source": [ + "a_simple_list = ['a', 'b', 'c', 'd']\n", + "if a_simple_list[0] == 'a':\n", + " print(\"The first item of the list is an `a`\")" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a\n", + "b\n", + "c\n", + "d\n" + ] + } + ], + "source": [ + "for item in a_simple_list:\n", + " print(item)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n" + ] + } + ], + "source": [ + "count = 0\n", + "while (count < 5):\n", + " print(count)\n", + " count+=1" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "12" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# functions are easy \n", + "def my_func(x, y):\n", + " return x*y\n", + "\n", + "my_func(3,4)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "notes" + } + }, + "source": [ + "### x, y = y, x" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Python has many modules (aka \"libraries\") that do fun things" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "89\n" + ] + } + ], + "source": [ + "# random numbers generator\n", + "import random\n", + "print(random.randint(0,100))" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "aa\n", + "aah\n", + "aahed\n", + "aahing\n", + "aahs\n", + "aal\n", + "aalii\n", + "aaliis\n", + "aals\n", + "aardvark\n", + "aardvarks\n", + "aardwolf\n", + "aardwolves\n", + "aargh\n", + "aarrgh\n", + "aarrghh\n", + "aarti\n", + "aartis\n", + "aas\n", + "aasvogel\n", + "aasvogels\n", + "ab\n", + "aba\n", + "abac\n", + "abaca\n", + "abacas\n", + "abaci\n", + "aback\n", + "abacs\n", + "abacterial\n", + "abactinal\n", + "abactinally\n", + "abactor\n", + "abactors\n", + "abacus\n", + "abacuses\n", + "abaft\n", + "abaka\n", + "abakas\n", + "abalone\n", + "abalones\n", + "abamp\n", + "abampere\n", + "abamperes\n", + "abamps\n", + "aband\n", + "abanded\n", + "abanding\n", + "abandon\n", + "abandoned\n", + "abandonedly\n", + "abandonee\n", + "abandonees\n", + "abandoner\n", + "abandoners\n", + "abandoning\n", + "abandonment\n", + "abandonments\n", + "abandons\n", + "abandonware\n", + "abandonwares\n", + "abands\n", + "abapical\n", + "abas\n", + "abase\n", + "abased\n", + "abasedly\n", + "abasement\n", + "abasements\n", + "abaser\n", + "abasers\n", + "abases\n", + "abash\n", + "abashed\n", + "abashedly\n", + "abashes\n", + "abashing\n", + "abashless\n", + "abashment\n", + "abashments\n", + "abasia\n", + "abasias\n", + "abasing\n", + "abask\n", + "abatable\n", + "abate\n", + "abated\n", + "abatement\n", + "abatements\n", + "abater\n", + "abaters\n", + "abates\n", + "abating\n", + "abatis\n", + "abatises\n", + "abator\n", + "abators\n", + "abattis\n", + "abattises\n", + "abattoir\n", + "abattoirs\n", + "abattu\n", + "abature\n", + "abatures\n", + "abaxial\n", + "abaxile\n", + "abaya\n", + "abayas\n", + "abb\n", + "abba\n", + "abbacies\n", + "abbacy\n", + "abbas\n", + "abbatial\n", + "abbe\n", + "abbed\n", + "abbes\n", + "abbess\n", + "abbesses\n", + "abbey\n", + "abbeys\n", + "abbot\n", + "abbotcies\n", + "abbotcy\n", + "abbots\n", + "abbotship\n", + "abbotsh\n" + ] + } + ], + "source": [ + "# http library\n", + "import requests\n", + "r = requests.get(\"https://raw.githubusercontent.com/atebits/Words/master/Words/en.txt\")\n", + "if r.status_code == 200:\n", + " data = r.text\n", + " print(data[:1000])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "## And a strength of the language is text processing ..." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['aa',\n", + " 'aah',\n", + " 'aahed',\n", + " 'aahing',\n", + " 'aahs',\n", + " 'aal',\n", + " 'aalii',\n", + " 'aaliis',\n", + " 'aals',\n", + " 'aardvark']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data.split('\\n')[:10]" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['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" + ] + } + ], + "source": [ + "top_100_five_letter_words = []\n", + "\n", + "for w in data.split('\\n'):\n", + " if len(w) == 5:\n", + " top_100_five_letter_words.append(w)\n", + " if len(top_100_five_letter_words) == 100:\n", + " break\n", + "\n", + "print(top_100_five_letter_words)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['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" + ] + } + ], + "source": [ + "top_100_five_letter_words_ending_in_g = []\n", + "\n", + "for w in data.split('\\n'):\n", + " if len(w) == 5 and w[-1] is 'g':\n", + " top_100_five_letter_words_ending_in_g.append(w)\n", + " if len(top_100_five_letter_words_ending_in_g) == 100:\n", + " break\n", + "\n", + "print(top_100_five_letter_words_ending_in_g)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "### json support is excellent (as it should be)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "IOPub data rate exceeded.\n", + "The notebook server will temporarily stop sending output\n", + "to the client in order to avoid crashing it.\n", + "To change this limit, set the config variable\n", + "`--NotebookApp.iopub_data_rate_limit`.\n", + "\n", + "Current values:\n", + "NotebookApp.iopub_data_rate_limit=1000000.0 (bytes/sec)\n", + "NotebookApp.rate_limit_window=3.0 (secs)\n", + "\n" + ] + } + ], + "source": [ + "import json\n", + "import requests\n", + "\n", + "r = requests.get(\"https://raw.githubusercontent.com/LearnWebCode/json-example/master/pets-data.json\")\n", + "if r.status_code == 200:\n", + " pet_data = json.loads(r.text)\n", + "print(data)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'name': 'Purrsloud',\n", + " 'species': 'Cat',\n", + " 'favFoods': ['wet food', 'dry food', 'any food'],\n", + " 'birthYear': 2016,\n", + " 'photo': 'https://learnwebcode.github.io/json-example/images/cat-2.jpg'}" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pet_data['pets'][0]" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "### Regular expressions ... also very accessible" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tabasheers\n", + "tabers\n", + "tabliers\n", + "taborers\n", + "tabourers\n", + "tacheometers\n", + "tachometers\n", + "tachygraphers\n", + "tachymeters\n", + "tackers\n", + "tackifiers\n", + "tacklers\n", + "taggers\n", + "tailenders\n", + "tailers\n", + "tailgaters\n", + "tailwaters\n", + "taivers\n", + "takeovers\n", + "takers\n", + "talebearers\n", + "talers\n", + "talkers\n", + "talliers\n", + "tambers\n", + "tambourers\n", + "tamers\n", + "tamperers\n", + "tampers\n", + "tanagers\n", + "tanglers\n", + "tankbusters\n", + "tankers\n", + "tanners\n", + "tantalisers\n", + "tantalizers\n", + "taperers\n", + "tapers\n", + "tappers\n", + "tapsters\n", + "targeteers\n", + "tarnishers\n", + "tarpapers\n", + "tarriers\n", + "tarsiers\n", + "taseometers\n", + "tasers\n", + "tasimeters\n", + "taskers\n", + "taskmasters\n", + "tastemakers\n", + "tasters\n", + "taters\n", + "tatlers\n", + "tatters\n", + "tattlers\n", + "tattooers\n", + "taunters\n", + "tautomers\n", + "taverners\n", + "tavers\n", + "tawers\n", + "taxameters\n", + "taxers\n", + "taximeters\n", + "taxonomers\n", + "taxpayers\n", + "teachers\n", + "teamakers\n", + "teamers\n", + "teamsters\n", + "tearers\n", + "tearjerkers\n", + "teaselers\n", + "teasellers\n", + "teasers\n", + "teatasters\n", + "tedders\n", + "teemers\n", + "teenagers\n", + "teeners\n", + "teenyboppers\n", + "teers\n", + "teeters\n", + "teethers\n", + "teetotalers\n", + "teetotallers\n", + "telecasters\n", + "telecommuters\n", + "teleconverters\n", + "telegraphers\n", + "telemarketers\n", + "telemeters\n", + "telephoners\n", + "teleprinters\n", + "teletypewriters\n", + "televiewers\n", + "televisers\n", + "teleworkers\n", + "telewriters\n", + "telfers\n", + "tellers\n", + "tellurometers\n", + "telphers\n", + "temperers\n", + "tempers\n", + "temporisers\n", + "temporizers\n", + "tempters\n", + "tenderers\n", + "tenderisers\n", + "tenderizers\n", + "tenderometers\n", + "tenders\n", + "tenners\n", + "tenoners\n", + "tenpounders\n", + "tensimeters\n", + "tensiometers\n", + "tensioners\n", + "tenters\n", + "tentmakers\n", + "termers\n", + "terminers\n", + "terpolymers\n", + "terriers\n", + "terrifiers\n", + "terrorisers\n", + "terrorizers\n", + "testers\n", + "testifiers\n", + "tethers\n", + "tetramers\n", + "tetrameters\n", + "tetters\n", + "teuchters\n", + "texters\n", + "thalers\n", + "thankers\n", + "thanksgivers\n", + "thatchers\n", + "thawers\n", + "theatergoers\n", + "theaters\n", + "theologasters\n", + "theologers\n", + "theologisers\n", + "theologizers\n", + "theorisers\n", + "theorizers\n", + "theosophers\n", + "thermographers\n", + "thermometers\n", + "thickeners\n", + "thiggers\n", + "thillers\n", + "thimbleriggers\n", + "thinkers\n", + "thinners\n", + "thirsters\n", + "thrashers\n", + "threaders\n", + "threadmakers\n", + "threapers\n", + "threateners\n", + "threepers\n", + "threshers\n", + "thrillers\n", + "thrivers\n", + "throbbers\n", + "thronners\n", + "throttlers\n", + "throwers\n", + "throwsters\n", + "thrummers\n", + "thrusters\n", + "thumpers\n", + "thunderers\n", + "thunders\n", + "thundershowers\n", + "thurifers\n", + "thwackers\n", + "thwarters\n", + "tickers\n", + "ticklers\n", + "tiddlers\n", + "tidewaiters\n", + "tidewaters\n", + "tidiers\n", + "tiebreakers\n", + "tiers\n", + "tigers\n", + "tighteners\n", + "tilers\n", + "tillers\n", + "tilters\n", + "tiltmeters\n", + "timbers\n", + "timekeepers\n", + "timepleasers\n", + "timers\n", + "timesavers\n", + "timeservers\n", + "timeworkers\n", + "timoneers\n", + "tinders\n", + "tinglers\n", + "tinkerers\n", + "tinkers\n", + "tinklers\n", + "tinners\n", + "tinters\n", + "tintometers\n", + "tippers\n", + "tipplers\n", + "tipsters\n", + "titers\n", + "titfers\n", + "tithers\n", + "titleholders\n", + "titlers\n", + "titterers\n", + "titters\n", + "toadeaters\n", + "toasters\n", + "toastmasters\n", + "tobogganers\n", + "tochers\n", + "toddlers\n", + "todgers\n", + "toeraggers\n", + "toggers\n", + "togglers\n", + "toilers\n", + "tokers\n", + "tollers\n", + "tolters\n", + "toners\n", + "tongers\n", + "tongsters\n", + "tonguesters\n", + "tonkers\n", + "tonners\n", + "tonometers\n", + "tontiners\n", + "toolers\n", + "toolholders\n", + "toolmakers\n", + "toolpushers\n", + "tooters\n", + "tootlers\n", + "topers\n", + "topliners\n", + "topmakers\n", + "topnotchers\n", + "topographers\n", + "toppers\n", + "topsiders\n", + "torchbearers\n", + "torchers\n", + "torchiers\n", + "tormenters\n", + "torpedoers\n", + "torquers\n", + "torturers\n", + "toshers\n", + "tossers\n", + "totalisers\n", + "totalizers\n", + "toters\n", + "totterers\n", + "totters\n", + "touchers\n", + "touchpapers\n", + "tougheners\n", + "tourers\n", + "tourneyers\n", + "tousers\n", + "touters\n", + "towers\n", + "towsers\n", + "toyers\n", + "tracers\n", + "trackers\n", + "tracklayers\n", + "trackwalkers\n", + "traders\n", + "traditioners\n", + "traducers\n", + "traffickers\n", + "trailblazers\n", + "trailbreakers\n", + "trailers\n", + "trainbearers\n", + "trainers\n", + "trammelers\n", + "trammellers\n", + "trampers\n", + "tramplers\n", + "trampoliners\n", + "tranquilisers\n", + "tranquilizers\n", + "tranquillisers\n", + "tranquillizers\n", + "transceivers\n", + "transcribers\n", + "transducers\n", + "transferrers\n", + "transfers\n", + "transformers\n", + "transfusers\n", + "transgenders\n", + "transhippers\n", + "transmissometers\n", + "transmitters\n", + "transmuters\n", + "transplanters\n", + "transponders\n", + "transporters\n", + "transposers\n", + "transputers\n", + "transshippers\n", + "transvaluers\n", + "transverters\n", + "tranters\n", + "trapanners\n", + "trappers\n", + "trapshooters\n", + "trashers\n", + "travelers\n", + "travellers\n", + "traversers\n", + "trawlers\n", + "treacherers\n", + "treachers\n", + "treaders\n", + "treadlers\n", + "treasurers\n", + "treaters\n", + "treehoppers\n", + "trekkers\n", + "tremblers\n", + "trenchers\n", + "trendsetters\n", + "trepanners\n", + "trephiners\n", + "trespassers\n", + "triaconters\n", + "tribometers\n", + "tributers\n", + "trickers\n", + "tricksters\n", + "tricyclers\n", + "triers\n", + "triflers\n", + "triggers\n", + "trigonometers\n", + "trillers\n", + "trimers\n", + "trimesters\n", + "trimeters\n", + "trimmers\n", + "trinketers\n", + "triphammers\n", + "trippers\n", + "tripplers\n", + "triumphers\n", + "trochanters\n", + "trocheameters\n", + "trochometers\n", + "troffers\n", + "trollers\n", + "tromometers\n", + "troopers\n", + "troposcatters\n", + "trossers\n", + "trotters\n", + "troublemakers\n", + "troublers\n", + "troubleshooters\n", + "trouncers\n", + "troupers\n", + "trousers\n", + "trouters\n", + "trovers\n", + "trowelers\n", + "trowellers\n", + "trowsers\n", + "truckers\n", + "trucklers\n", + "truckmasters\n", + "trudgers\n", + "trumpeters\n", + "truncheoners\n", + "trundlers\n", + "trussers\n", + "trustbusters\n", + "trusters\n", + "tryers\n", + "trysters\n", + "tubbers\n", + "tubers\n", + "tuckers\n", + "tufters\n", + "tuggers\n", + "tumblers\n", + "tummlers\n", + "tuners\n", + "tunnelers\n", + "tunnellers\n", + "turbidimeters\n", + "turbochargers\n", + "turcopoliers\n", + "turners\n", + "turnovers\n", + "turtlers\n", + "tushkers\n", + "tuskers\n", + "tussers\n", + "tutoyers\n", + "tutworkers\n", + "tuyers\n", + "twaddlers\n", + "twangers\n", + "twanglers\n", + "twattlers\n", + "tweakers\n", + "tweedlers\n", + "tweenagers\n", + "tweeners\n", + "tweers\n", + "tweeters\n", + "tweezers\n", + "twicers\n", + "twiddlers\n", + "twiers\n", + "twiggers\n", + "twiners\n", + "twinflowers\n", + "twinklers\n", + "twinters\n", + "twirlers\n", + "twisters\n", + "twitchers\n", + "twitterers\n", + "twitters\n", + "twoccers\n", + "twockers\n", + "twoers\n", + "twofers\n", + "twoseaters\n", + "twyers\n", + "tyers\n", + "tylers\n", + "typecasters\n", + "typefounders\n", + "typesetters\n", + "typewriters\n", + "typifiers\n", + "typographers\n", + "tyrannisers\n", + "tyrannizers\n" + ] + } + ], + "source": [ + "# regular expressions\n", + "import re\n", + "s = r'^t.*ers$' # all words starting with `t` ending in `ers`\n", + "\n", + "for w in data.split('\\n'):\n", + " if re.match(s, w):\n", + " print(w)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "### There are many thousands of libraries to get lost in ..." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Data types in Python are simple and complete ...\n", + "\n", + "* You are already familiar with these:\n", + " * numbers (`1`, `1.87`, `-0.88`, ...)\n", + " * strings (`\"Hello\"`, `'Hello'`)\n", + " * Boolean (`True`, `False`)\n", + " * `None`" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [], + "source": [ + "### No surprises here ..." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1 + 2" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'helloHello'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'hello' + \"Hello\"" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Iterables are an important type category that includes:\n", + "\n", + "* lists\n", + "* tuples\n", + "* sets\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "### Lists are just like arrays ..." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "lst = [1, 2, \"three\"]\n", + "print(lst[1])" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 three " + ] + } + ], + "source": [ + "for i in lst:\n", + " print(i, end=\" \")" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 'three', 'a', 'b']" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lst + ['a', 'b']" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "### Python doesn't require you to keep up with indices ...\n", + "\n", + "* but if you need them (which you rarely will) use `enumerate`" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0:1 1:2 2:three " + ] + } + ], + "source": [ + "for idx, l in enumerate(lst):\n", + " print(\"{}:{}\".format(idx, l), end=\" \")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "### You can also do cool things with list access ..." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'three'" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lst[-1]" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2]" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lst[0:2]" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['three', 2, 1]" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lst[::-1]" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "### Tuples are another useful type\n", + "* they are just immutable lists\n", + "* and denoted `(1, 2, 'three`)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "### Sets are also valuable " + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 2, 3, 4, 5}" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "set([1,1,1,1,1,1,2,3,4,5,5,5,5,5,5,5])" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 2, 3, 4, 5}" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "setA = set([1,2])\n", + "setB = set([1,2,3,4,5])\n", + "setA.union(setB)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "set()" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "setA.difference(setB)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{3, 4, 5}" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "setB.difference(setA)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{1, 2}" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "setA.intersection(setB)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Dictionaries are also a crucial type in the language\n", + "\n", + "* associative arrays\n", + "* key/value pairs" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'a': 5, 'b': 6}\n" + ] + } + ], + "source": [ + "d = {\"a\": 5, \"b\": 6}\n", + "print(d)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'a': 5, 'b': 6}\n" + ] + } + ], + "source": [ + "d = dict([('a', 5), ('b', 6)])\n", + "print(d)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "### Providing the usual suspects ..." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d['a']" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['a', 'b'])" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d.keys()" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_values([5, 6])" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d.values()" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a=>5\n", + "b=>6\n" + ] + } + ], + "source": [ + "for key, value in d.items():\n", + " print(\"{}=>{}\".format(key, value))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Python logic operators are intuitive\n", + "\n", + "* you've seen `in` and `is`\n", + "* but there is `not`, `not in`, `is not`, `and`, `or`" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1 is 2" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1 is not 2" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "True and True" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "True or False" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "False or False" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "False is not True" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "s = 'supercalifragilistic'" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'u' in s" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'z' not in s" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [], + "source": [ + "lst = ['a', 'b', 1, 2, 3]" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "'a' in lst" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s == 'supercalifragilistic'" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s is 'supercalifragilistic'" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lst == ['a', 'b', 1, 2, 3]" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lst is ['a', 'b', 1, 2, 3]" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Flow control in Python also includes all the usuals ...\n", + "* `if/elif/else`\n", + "* `for`\n", + "* `while`" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the first letter is a 't'\n" + ] + } + ], + "source": [ + "s = 'this is a test'\n", + "\n", + "if s[0] == 't':\n", + " print(\"the first letter is a 't'\")\n", + "else:\n", + " print(\"the first letter is not a 't'\")" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t\n", + "h\n", + "i\n", + "s\n", + " \n", + "i\n", + "s\n", + " \n", + "a\n", + " \n", + "t\n", + "e\n", + "s\n", + "t\n" + ] + } + ], + "source": [ + "for c in s:\n", + " print(\"{}\".format(c))" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "t\n", + "h\n", + "i\n", + "s\n", + " \n", + "i\n", + "s\n", + " \n", + "a\n", + " \n", + "t\n", + "e\n", + "s\n", + "t\n" + ] + } + ], + "source": [ + "# THIS IS NOT PYTHONIC! for is more elegant\n", + "\n", + "i = 0\n", + "while(i < len(s)):\n", + " print(\"{}\".format(s[i]))\n", + " i+=1" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "skip" + } + }, + "source": [ + "## classes/objects" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Python's built in functions are excellent\n", + "* There are about 69 functions\n", + "* some of the most common/useful are:\n", + " * `min`, `max`, `all`, `any` \n", + " * `zip`, `range`, `len`, `map`, `sorted`\n", + " * `reversed`" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "### Let's play:\n", + "\n", + "* **PROBLEM**: create a 16 character random password of upper, lower and number\n", + "* **ONE SOLUTION**: _use what we know about the ASCII table and the `chr()` built in function_" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "![./better_ascii_table.jpg](./better_ascii_table.jpg)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "**HINT 1:**\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": { + "slideshow": { + "slide_type": "-" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'z'" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "chr(122)" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "uMVKgvYyB114e4rpj" + ] + } + ], + "source": [ + "import random\n", + "\n", + "for i in range(0, 17):\n", + " # get a number in the ASCII range\n", + " while(True):\n", + " rnd_c = random.randint(48, 123)\n", + " if rnd_c < 58 or (rnd_c > 65 and rnd_c <91) or (rnd_c > 96 and rnd_c < 123):\n", + " break\n", + " \n", + " print(chr(rnd_c), end=\"\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Functions are straightforward and intuitive" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def a_function():\n", + " return 1\n", + "\n", + "a_function()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "### Function arguments (parameters) come in three flavors\n", + "\n", + "* positional\n", + "* keyword\n", + "* mixed\n", + "\n", + "Note: default values are allowed!" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def a_function(a, b, c):\n", + " return a*b*c\n", + "\n", + "a_function(1,2,3)" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "192" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def a_function(a=1, b=1, c=1):\n", + " return a*b*c\n", + "\n", + "a_function(a=3,b=8,c=8)" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a_function(a=4)" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "48" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a_function(3,b=4,c=4)" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "384" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def a_function(a, b=1, c=1):\n", + " return a*b*c\n", + "\n", + "a_function(6,b=8,c=8)" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "384" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a_function(6, 8, 8)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "### Functions can return multiple values with tuples" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "def b_function():\n", + " return (1, 2, 3)" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(1, 2, 3)" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b_function()" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 2 3\n" + ] + } + ], + "source": [ + "x, y, z = b_function()\n", + "print(x, y, z)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Exceptions are valuable for good code!\n", + "* Python supports `try`/`except`/`finally` and they should be used regularly" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [], + "source": [ + "def c_function(a, b, c):\n", + " try:\n", + " if a<0:\n", + " raise Exception\n", + " return a*b*c\n", + " except Exception as e:\n", + " print(\"The first parameter cannot be less than zero.\")\n", + " finally:\n", + " pass # we don't need to clean up anything after the exception" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The first parameter cannot be less than zero.\n" + ] + } + ], + "source": [ + "c_function(-1, 2, 3) " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "### What did the function _return_ from the exception???" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "ALL PYTHON FUNCTIONS IMPLICITLY RETURN `None` WHEN NO RETURN VALUE IS SPECIFIED" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The first parameter cannot be less than zero.\n" + ] + }, + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "c_function(-1, 2, 3) is None" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Comprehensions" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "### List comprehensions provide shorthand for building lists" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" + ] + }, + "execution_count": 67, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "l1 = [x for x in range(0,11)]\n", + "l1" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'TMbD0xr0Bj'" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def pw_generator():\n", + " ascii_pw_range = list(range(48,58)) + list(range(65,91)) + list(range (97, 123))\n", + " \n", + " return ''.join([chr(random.choice(ascii_pw_range)) \\\n", + " for i in range(0,10)])\n", + "\n", + "pw_generator()" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['MIO3ewedYc',\n", + " 'YbFp3ofMhC',\n", + " 'lgy5GUvod4',\n", + " 'VfOjbJ1oF3',\n", + " 'tqYltK37KK',\n", + " 'E4SzZ0zRjX',\n", + " 'fdJRpvyHrn',\n", + " '9cCas9FpyA',\n", + " '33qhlSo7kd',\n", + " 'dL1QupAp0V',\n", + " 'KBciH7ZSaN',\n", + " '3B1StGRLLe',\n", + " 'bR6wZCAzsa',\n", + " '9gq3zcB6lb',\n", + " 'vmNodUhosK']" + ] + }, + "execution_count": 69, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[pw_generator() for i in range(0,15)]" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "### Dictionary comprehensions are also quite nice ...\n", + "\n", + "* Let's say you want to swap the key, value pairs in a dictionary ..." + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{1: 'a', 2: 'b', 3: 'c'}" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d1 = dict([('a', 1), ('b', 2), ('c', 3)])\n", + "{ v:k for (k,v) in d1.items() }" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "* something more practical might be to filter out all values not meeting a criterion" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{1: 'a', 3: 'c'}" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d1 = dict([('a', 1), ('b', 2), ('c', 3)])\n", + "{ v:k for (k,v) in d1.items() if v%2 != 0}" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## LET'S PLAY!" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "skip" + } + }, + "source": [ + "## file i/o" + ] + } + ], + "metadata": { + "celltoolbar": "Slideshow", + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/presentation_01042019.slides.html b/presentation_01042019.slides.html new file mode 100644 index 0000000..2a39387 --- /dev/null +++ b/presentation_01042019.slides.html @@ -0,0 +1,16638 @@ + + + + + + + + + + + + +presentation_01042019 slides + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
+
+
+

A Brief Python Introduction

Keith Maull
+Jan. 03, 2019

+ +
+
+
+
+
+
+

Python is a general purpose programming language that is ...

    +
  • interpreted
  • +
  • dynamically typed (not statically typed)
  • +
  • intuitive
  • +
  • fun and addictive
  • +
+ +
+
+
+
+
+
+

Python syntax can be learned very quickly

    +
  • indentation matters
  • +
  • there are no curly braces, semicolons or tricks ...
  • +
  • the syntax is one of the true joys of the language once you learn not to fight it
  • +
+ +
+
+
+
+
+
+

We will learn 80% of the syntax today in 1 hour!

./1368244924-49523624.jpg

+ +
+
+
+
+
+
In [1]:
+
+
+
a_simple_list = ['a', 'b', 'c', 'd']
+if a_simple_list[0] == 'a':
+    print("The first item of the list is an `a`")
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
The first item of the list is an `a`
+
+
+
+ +
+
+ +
+
+
+
In [2]:
+
+
+
for item in a_simple_list:
+    print(item)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
a
+b
+c
+d
+
+
+
+ +
+
+ +
+
+
+
In [3]:
+
+
+
count = 0
+while (count < 5):
+    print(count)
+    count+=1
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
0
+1
+2
+3
+4
+
+
+
+ +
+
+ +
+
+
+
In [4]:
+
+
+
# functions are easy 
+def my_func(x, y):
+    return x*y
+
+my_func(3,4)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[4]:
+ + + + +
+
12
+
+ +
+ +
+
+ +
+
+
+
+

Python has many modules (aka "libraries") that do fun things

+
+
+
+
+
+
In [5]:
+
+
+
# random numbers generator
+import random
+print(random.randint(0,100))
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
89
+
+
+
+ +
+
+ +
+
+
+
In [6]:
+
+
+
# http library
+import requests
+r = requests.get("https://raw.githubusercontent.com/atebits/Words/master/Words/en.txt")
+if r.status_code == 200:
+    data = r.text
+    print(data[:1000])
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
aa
+aah
+aahed
+aahing
+aahs
+aal
+aalii
+aaliis
+aals
+aardvark
+aardvarks
+aardwolf
+aardwolves
+aargh
+aarrgh
+aarrghh
+aarti
+aartis
+aas
+aasvogel
+aasvogels
+ab
+aba
+abac
+abaca
+abacas
+abaci
+aback
+abacs
+abacterial
+abactinal
+abactinally
+abactor
+abactors
+abacus
+abacuses
+abaft
+abaka
+abakas
+abalone
+abalones
+abamp
+abampere
+abamperes
+abamps
+aband
+abanded
+abanding
+abandon
+abandoned
+abandonedly
+abandonee
+abandonees
+abandoner
+abandoners
+abandoning
+abandonment
+abandonments
+abandons
+abandonware
+abandonwares
+abands
+abapical
+abas
+abase
+abased
+abasedly
+abasement
+abasements
+abaser
+abasers
+abases
+abash
+abashed
+abashedly
+abashes
+abashing
+abashless
+abashment
+abashments
+abasia
+abasias
+abasing
+abask
+abatable
+abate
+abated
+abatement
+abatements
+abater
+abaters
+abates
+abating
+abatis
+abatises
+abator
+abators
+abattis
+abattises
+abattoir
+abattoirs
+abattu
+abature
+abatures
+abaxial
+abaxile
+abaya
+abayas
+abb
+abba
+abbacies
+abbacy
+abbas
+abbatial
+abbe
+abbed
+abbes
+abbess
+abbesses
+abbey
+abbeys
+abbot
+abbotcies
+abbotcy
+abbots
+abbotship
+abbotsh
+
+
+
+ +
+
+ +
+
+
+
+

And a strength of the language is text processing ...

+
+
+
+
+
+
In [7]:
+
+
+
data.split('\n')[:10]
+
+ +
+
+
+ +
+
+ + +
+ +
Out[7]:
+ + + + +
+
['aa',
+ 'aah',
+ 'aahed',
+ 'aahing',
+ 'aahs',
+ 'aal',
+ 'aalii',
+ 'aaliis',
+ 'aals',
+ 'aardvark']
+
+ +
+ +
+
+ +
+
+
+
In [8]:
+
+
+
top_100_five_letter_words = []
+
+for w in data.split('\n'):
+    if len(w) == 5:
+        top_100_five_letter_words.append(w)
+    if len(top_100_five_letter_words) == 100:
+        break
+
+print(top_100_five_letter_words)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
['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']
+
+
+
+ +
+
+ +
+
+
+
In [9]:
+
+
+
top_100_five_letter_words_ending_in_g = []
+
+for w in data.split('\n'):
+    if len(w) == 5 and w[-1] is 'g':
+        top_100_five_letter_words_ending_in_g.append(w)
+    if len(top_100_five_letter_words_ending_in_g) == 100:
+        break
+
+print(top_100_five_letter_words_ending_in_g)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
['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']
+
+
+
+ +
+
+ +
+
+
+
+

json support is excellent (as it should be)

+
+
+
+
+
+
In [10]:
+
+
+
import json
+import requests
+
+r = requests.get("https://raw.githubusercontent.com/LearnWebCode/json-example/master/pets-data.json")
+if r.status_code == 200:
+    pet_data = json.loads(r.text)
+print(data)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
IOPub data rate exceeded.
+The notebook server will temporarily stop sending output
+to the client in order to avoid crashing it.
+To change this limit, set the config variable
+`--NotebookApp.iopub_data_rate_limit`.
+
+Current values:
+NotebookApp.iopub_data_rate_limit=1000000.0 (bytes/sec)
+NotebookApp.rate_limit_window=3.0 (secs)
+
+
+
+
+ +
+
+ +
+
+
+
In [11]:
+
+
+
pet_data['pets'][0]
+
+ +
+
+
+ +
+
+ + +
+ +
Out[11]:
+ + + + +
+
{'name': 'Purrsloud',
+ 'species': 'Cat',
+ 'favFoods': ['wet food', 'dry food', '<strong>any</strong> food'],
+ 'birthYear': 2016,
+ 'photo': 'https://learnwebcode.github.io/json-example/images/cat-2.jpg'}
+
+ +
+ +
+
+ +
+
+
+
+

Regular expressions ... also very accessible

+
+
+
+
+
+
In [12]:
+
+
+
# regular expressions
+import re
+s = r'^t.*ers$' # all words starting with `t` ending in `ers`
+
+for w in data.split('\n'):
+    if re.match(s, w):
+        print(w)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
tabasheers
+tabers
+tabliers
+taborers
+tabourers
+tacheometers
+tachometers
+tachygraphers
+tachymeters
+tackers
+tackifiers
+tacklers
+taggers
+tailenders
+tailers
+tailgaters
+tailwaters
+taivers
+takeovers
+takers
+talebearers
+talers
+talkers
+talliers
+tambers
+tambourers
+tamers
+tamperers
+tampers
+tanagers
+tanglers
+tankbusters
+tankers
+tanners
+tantalisers
+tantalizers
+taperers
+tapers
+tappers
+tapsters
+targeteers
+tarnishers
+tarpapers
+tarriers
+tarsiers
+taseometers
+tasers
+tasimeters
+taskers
+taskmasters
+tastemakers
+tasters
+taters
+tatlers
+tatters
+tattlers
+tattooers
+taunters
+tautomers
+taverners
+tavers
+tawers
+taxameters
+taxers
+taximeters
+taxonomers
+taxpayers
+teachers
+teamakers
+teamers
+teamsters
+tearers
+tearjerkers
+teaselers
+teasellers
+teasers
+teatasters
+tedders
+teemers
+teenagers
+teeners
+teenyboppers
+teers
+teeters
+teethers
+teetotalers
+teetotallers
+telecasters
+telecommuters
+teleconverters
+telegraphers
+telemarketers
+telemeters
+telephoners
+teleprinters
+teletypewriters
+televiewers
+televisers
+teleworkers
+telewriters
+telfers
+tellers
+tellurometers
+telphers
+temperers
+tempers
+temporisers
+temporizers
+tempters
+tenderers
+tenderisers
+tenderizers
+tenderometers
+tenders
+tenners
+tenoners
+tenpounders
+tensimeters
+tensiometers
+tensioners
+tenters
+tentmakers
+termers
+terminers
+terpolymers
+terriers
+terrifiers
+terrorisers
+terrorizers
+testers
+testifiers
+tethers
+tetramers
+tetrameters
+tetters
+teuchters
+texters
+thalers
+thankers
+thanksgivers
+thatchers
+thawers
+theatergoers
+theaters
+theologasters
+theologers
+theologisers
+theologizers
+theorisers
+theorizers
+theosophers
+thermographers
+thermometers
+thickeners
+thiggers
+thillers
+thimbleriggers
+thinkers
+thinners
+thirsters
+thrashers
+threaders
+threadmakers
+threapers
+threateners
+threepers
+threshers
+thrillers
+thrivers
+throbbers
+thronners
+throttlers
+throwers
+throwsters
+thrummers
+thrusters
+thumpers
+thunderers
+thunders
+thundershowers
+thurifers
+thwackers
+thwarters
+tickers
+ticklers
+tiddlers
+tidewaiters
+tidewaters
+tidiers
+tiebreakers
+tiers
+tigers
+tighteners
+tilers
+tillers
+tilters
+tiltmeters
+timbers
+timekeepers
+timepleasers
+timers
+timesavers
+timeservers
+timeworkers
+timoneers
+tinders
+tinglers
+tinkerers
+tinkers
+tinklers
+tinners
+tinters
+tintometers
+tippers
+tipplers
+tipsters
+titers
+titfers
+tithers
+titleholders
+titlers
+titterers
+titters
+toadeaters
+toasters
+toastmasters
+tobogganers
+tochers
+toddlers
+todgers
+toeraggers
+toggers
+togglers
+toilers
+tokers
+tollers
+tolters
+toners
+tongers
+tongsters
+tonguesters
+tonkers
+tonners
+tonometers
+tontiners
+toolers
+toolholders
+toolmakers
+toolpushers
+tooters
+tootlers
+topers
+topliners
+topmakers
+topnotchers
+topographers
+toppers
+topsiders
+torchbearers
+torchers
+torchiers
+tormenters
+torpedoers
+torquers
+torturers
+toshers
+tossers
+totalisers
+totalizers
+toters
+totterers
+totters
+touchers
+touchpapers
+tougheners
+tourers
+tourneyers
+tousers
+touters
+towers
+towsers
+toyers
+tracers
+trackers
+tracklayers
+trackwalkers
+traders
+traditioners
+traducers
+traffickers
+trailblazers
+trailbreakers
+trailers
+trainbearers
+trainers
+trammelers
+trammellers
+trampers
+tramplers
+trampoliners
+tranquilisers
+tranquilizers
+tranquillisers
+tranquillizers
+transceivers
+transcribers
+transducers
+transferrers
+transfers
+transformers
+transfusers
+transgenders
+transhippers
+transmissometers
+transmitters
+transmuters
+transplanters
+transponders
+transporters
+transposers
+transputers
+transshippers
+transvaluers
+transverters
+tranters
+trapanners
+trappers
+trapshooters
+trashers
+travelers
+travellers
+traversers
+trawlers
+treacherers
+treachers
+treaders
+treadlers
+treasurers
+treaters
+treehoppers
+trekkers
+tremblers
+trenchers
+trendsetters
+trepanners
+trephiners
+trespassers
+triaconters
+tribometers
+tributers
+trickers
+tricksters
+tricyclers
+triers
+triflers
+triggers
+trigonometers
+trillers
+trimers
+trimesters
+trimeters
+trimmers
+trinketers
+triphammers
+trippers
+tripplers
+triumphers
+trochanters
+trocheameters
+trochometers
+troffers
+trollers
+tromometers
+troopers
+troposcatters
+trossers
+trotters
+troublemakers
+troublers
+troubleshooters
+trouncers
+troupers
+trousers
+trouters
+trovers
+trowelers
+trowellers
+trowsers
+truckers
+trucklers
+truckmasters
+trudgers
+trumpeters
+truncheoners
+trundlers
+trussers
+trustbusters
+trusters
+tryers
+trysters
+tubbers
+tubers
+tuckers
+tufters
+tuggers
+tumblers
+tummlers
+tuners
+tunnelers
+tunnellers
+turbidimeters
+turbochargers
+turcopoliers
+turners
+turnovers
+turtlers
+tushkers
+tuskers
+tussers
+tutoyers
+tutworkers
+tuyers
+twaddlers
+twangers
+twanglers
+twattlers
+tweakers
+tweedlers
+tweenagers
+tweeners
+tweers
+tweeters
+tweezers
+twicers
+twiddlers
+twiers
+twiggers
+twiners
+twinflowers
+twinklers
+twinters
+twirlers
+twisters
+twitchers
+twitterers
+twitters
+twoccers
+twockers
+twoers
+twofers
+twoseaters
+twyers
+tyers
+tylers
+typecasters
+typefounders
+typesetters
+typewriters
+typifiers
+typographers
+tyrannisers
+tyrannizers
+
+
+
+ +
+
+ +
+
+
+
+

There are many thousands of libraries to get lost in ...

+
+
+
+
+
+
+

Data types in Python are simple and complete ...

    +
  • You are already familiar with these:
      +
    • numbers (1, 1.87, -0.88, ...)
    • +
    • strings ("Hello", 'Hello')
    • +
    • Boolean (True, False)
    • +
    • None
    • +
    +
  • +
+ +
+
+
+
+
+
In [13]:
+
+
+
### No surprises here ...
+
+ +
+
+
+ +
+
+
+
In [14]:
+
+
+
1 + 2
+
+ +
+
+
+ +
+
+ + +
+ +
Out[14]:
+ + + + +
+
3
+
+ +
+ +
+
+ +
+
+
+
In [15]:
+
+
+
'hello' + "Hello"
+
+ +
+
+
+ +
+
+ + +
+ +
Out[15]:
+ + + + +
+
'helloHello'
+
+ +
+ +
+
+ +
+
+
+
+

Iterables are an important type category that includes:

    +
  • lists
  • +
  • tuples
  • +
  • sets
  • +
+ +
+
+
+
+
+
+

Lists are just like arrays ...

+
+
+
+
+
+
In [16]:
+
+
+
lst = [1, 2, "three"]
+print(lst[1])
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
2
+
+
+
+ +
+
+ +
+
+
+
In [17]:
+
+
+
for i in lst:
+    print(i, end=" ")
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1 2 three 
+
+
+ +
+
+ +
+
+
+
In [18]:
+
+
+
lst + ['a', 'b']
+
+ +
+
+
+ +
+
+ + +
+ +
Out[18]:
+ + + + +
+
[1, 2, 'three', 'a', 'b']
+
+ +
+ +
+
+ +
+
+
+
+

Python doesn't require you to keep up with indices ...

    +
  • but if you need them (which you rarely will) use enumerate
  • +
+ +
+
+
+
+
+
In [19]:
+
+
+
for idx, l in enumerate(lst):
+    print("{}:{}".format(idx, l), end=" ")
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
0:1 1:2 2:three 
+
+
+ +
+
+ +
+
+
+
+

You can also do cool things with list access ...

+
+
+
+
+
+
In [20]:
+
+
+
lst[-1]
+
+ +
+
+
+ +
+
+ + +
+ +
Out[20]:
+ + + + +
+
'three'
+
+ +
+ +
+
+ +
+
+
+
In [21]:
+
+
+
lst[0:2]
+
+ +
+
+
+ +
+
+ + +
+ +
Out[21]:
+ + + + +
+
[1, 2]
+
+ +
+ +
+
+ +
+
+
+
In [22]:
+
+
+
lst[::-1]
+
+ +
+
+
+ +
+
+ + +
+ +
Out[22]:
+ + + + +
+
['three', 2, 1]
+
+ +
+ +
+
+ +
+
+
+
+

Tuples are another useful type

    +
  • they are just immutable lists
  • +
  • and denoted (1, 2, 'three)
  • +
+ +
+
+
+
+
+
+

Sets are also valuable

+
+
+
+
+
+
In [23]:
+
+
+
set([1,1,1,1,1,1,2,3,4,5,5,5,5,5,5,5])
+
+ +
+
+
+ +
+
+ + +
+ +
Out[23]:
+ + + + +
+
{1, 2, 3, 4, 5}
+
+ +
+ +
+
+ +
+
+
+
In [24]:
+
+
+
setA = set([1,2])
+setB = set([1,2,3,4,5])
+setA.union(setB)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[24]:
+ + + + +
+
{1, 2, 3, 4, 5}
+
+ +
+ +
+
+ +
+
+
+
In [25]:
+
+
+
setA.difference(setB)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[25]:
+ + + + +
+
set()
+
+ +
+ +
+
+ +
+
+
+
In [26]:
+
+
+
setB.difference(setA)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[26]:
+ + + + +
+
{3, 4, 5}
+
+ +
+ +
+
+ +
+
+
+
In [27]:
+
+
+
setA.intersection(setB)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[27]:
+ + + + +
+
{1, 2}
+
+ +
+ +
+
+ +
+
+
+
+

Dictionaries are also a crucial type in the language

    +
  • associative arrays
  • +
  • key/value pairs
  • +
+ +
+
+
+
+
+
In [28]:
+
+
+
d = {"a": 5, "b": 6}
+print(d)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
{'a': 5, 'b': 6}
+
+
+
+ +
+
+ +
+
+
+
In [29]:
+
+
+
d = dict([('a', 5), ('b', 6)])
+print(d)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
{'a': 5, 'b': 6}
+
+
+
+ +
+
+ +
+
+
+
+

Providing the usual suspects ...

+
+
+
+
+
+
In [30]:
+
+
+
d['a']
+
+ +
+
+
+ +
+
+ + +
+ +
Out[30]:
+ + + + +
+
5
+
+ +
+ +
+
+ +
+
+
+
In [31]:
+
+
+
d.keys()
+
+ +
+
+
+ +
+
+ + +
+ +
Out[31]:
+ + + + +
+
dict_keys(['a', 'b'])
+
+ +
+ +
+
+ +
+
+
+
In [32]:
+
+
+
d.values()
+
+ +
+
+
+ +
+
+ + +
+ +
Out[32]:
+ + + + +
+
dict_values([5, 6])
+
+ +
+ +
+
+ +
+
+
+
In [33]:
+
+
+
for key, value in d.items():
+    print("{}=>{}".format(key, value))
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
a=>5
+b=>6
+
+
+
+ +
+
+ +
+
+
+
+

Python logic operators are intuitive

    +
  • you've seen in and is
  • +
  • but there is not, not in, is not, and, or
  • +
+ +
+
+
+
+
+
In [34]:
+
+
+
1 is 2
+
+ +
+
+
+ +
+
+ + +
+ +
Out[34]:
+ + + + +
+
False
+
+ +
+ +
+
+ +
+
+
+
In [35]:
+
+
+
1 is not 2
+
+ +
+
+
+ +
+
+ + +
+ +
Out[35]:
+ + + + +
+
True
+
+ +
+ +
+
+ +
+
+
+
In [36]:
+
+
+
True and True
+
+ +
+
+
+ +
+
+ + +
+ +
Out[36]:
+ + + + +
+
True
+
+ +
+ +
+
+ +
+
+
+
In [37]:
+
+
+
True or False
+
+ +
+
+
+ +
+
+ + +
+ +
Out[37]:
+ + + + +
+
True
+
+ +
+ +
+
+ +
+
+
+
In [38]:
+
+
+
False or False
+
+ +
+
+
+ +
+
+ + +
+ +
Out[38]:
+ + + + +
+
False
+
+ +
+ +
+
+ +
+
+
+
In [39]:
+
+
+
False is not True
+
+ +
+
+
+ +
+
+ + +
+ +
Out[39]:
+ + + + +
+
True
+
+ +
+ +
+
+ +
+
+
+
In [40]:
+
+
+
s = 'supercalifragilistic'
+
+ +
+
+
+ +
+
+
+
In [41]:
+
+
+
'u' in s
+
+ +
+
+
+ +
+
+ + +
+ +
Out[41]:
+ + + + +
+
True
+
+ +
+ +
+
+ +
+
+
+
In [42]:
+
+
+
'z' not in s
+
+ +
+
+
+ +
+
+ + +
+ +
Out[42]:
+ + + + +
+
True
+
+ +
+ +
+
+ +
+
+
+
In [43]:
+
+
+
lst = ['a', 'b', 1, 2, 3]
+
+ +
+
+
+ +
+
+
+
In [44]:
+
+
+
'a' in lst
+
+ +
+
+
+ +
+
+ + +
+ +
Out[44]:
+ + + + +
+
True
+
+ +
+ +
+
+ +
+
+
+
In [45]:
+
+
+
s == 'supercalifragilistic'
+
+ +
+
+
+ +
+
+ + +
+ +
Out[45]:
+ + + + +
+
True
+
+ +
+ +
+
+ +
+
+
+
In [46]:
+
+
+
s is 'supercalifragilistic'
+
+ +
+
+
+ +
+
+ + +
+ +
Out[46]:
+ + + + +
+
True
+
+ +
+ +
+
+ +
+
+
+
In [47]:
+
+
+
lst == ['a', 'b', 1, 2, 3]
+
+ +
+
+
+ +
+
+ + +
+ +
Out[47]:
+ + + + +
+
True
+
+ +
+ +
+
+ +
+
+
+
In [48]:
+
+
+
lst is ['a', 'b', 1, 2, 3]
+
+ +
+
+
+ +
+
+ + +
+ +
Out[48]:
+ + + + +
+
False
+
+ +
+ +
+
+ +
+
+
+
+

Flow control in Python also includes all the usuals ...

    +
  • if/elif/else
  • +
  • for
  • +
  • while
  • +
+ +
+
+
+
+
+
In [49]:
+
+
+
s = 'this is a test'
+
+if s[0] == 't':
+    print("the first letter is a 't'")
+else:
+    print("the first letter is not a 't'")
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
the first letter is a 't'
+
+
+
+ +
+
+ +
+
+
+
In [50]:
+
+
+
for c in s:
+    print("{}".format(c))
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
t
+h
+i
+s
+ 
+i
+s
+ 
+a
+ 
+t
+e
+s
+t
+
+
+
+ +
+
+ +
+
+
+
In [51]:
+
+
+
# THIS IS NOT PYTHONIC! for is more elegant
+
+i = 0
+while(i < len(s)):
+    print("{}".format(s[i]))
+    i+=1
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
t
+h
+i
+s
+ 
+i
+s
+ 
+a
+ 
+t
+e
+s
+t
+
+
+
+ +
+
+ +
+
+
+
+

Python's built in functions are excellent

    +
  • There are about 69 functions
  • +
  • some of the most common/useful are:
      +
    • min, max, all, any
    • +
    • zip, range, len, map, sorted
    • +
    • reversed
    • +
    +
  • +
+ +
+
+
+
+
+
+

Let's play:

    +
  • PROBLEM: create a 16 character random password of upper, lower and number
  • +
  • ONE SOLUTION: use what we know about the ASCII table and the chr() built in function
  • +
+ +
+
+
+
+
+
+

./better_ascii_table.jpg

+ +
+
+
+
+
+
+

HINT 1:

+ +
+
+
+
+
+
In [52]:
+
+
+
chr(122)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[52]:
+ + + + +
+
'z'
+
+ +
+ +
+
+ +
+
+
+
In [53]:
+
+
+
import random
+
+for i in range(0, 17):
+    # get a number in the ASCII range
+    while(True):
+        rnd_c = random.randint(48, 123)
+        if rnd_c < 58 or (rnd_c > 65 and rnd_c <91) or (rnd_c > 96 and rnd_c < 123):
+            break
+    
+    print(chr(rnd_c), end="")
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
uMVKgvYyB114e4rpj
+
+
+ +
+
+ +
+
+
+
+

Functions are straightforward and intuitive

+
+
+
+
+
+
In [54]:
+
+
+
def a_function():
+    return 1
+
+a_function()
+
+ +
+
+
+ +
+
+ + +
+ +
Out[54]:
+ + + + +
+
1
+
+ +
+ +
+
+ +
+
+
+
+

Function arguments (parameters) come in three flavors

    +
  • positional
  • +
  • keyword
  • +
  • mixed
  • +
+

Note: default values are allowed!

+ +
+
+
+
+
+
In [55]:
+
+
+
def a_function(a, b, c):
+    return a*b*c
+
+a_function(1,2,3)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[55]:
+ + + + +
+
6
+
+ +
+ +
+
+ +
+
+
+
In [56]:
+
+
+
def a_function(a=1, b=1, c=1):
+    return a*b*c
+
+a_function(a=3,b=8,c=8)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[56]:
+ + + + +
+
192
+
+ +
+ +
+
+ +
+
+
+
In [57]:
+
+
+
a_function(a=4)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[57]:
+ + + + +
+
4
+
+ +
+ +
+
+ +
+
+
+
In [58]:
+
+
+
a_function(3,b=4,c=4)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[58]:
+ + + + +
+
48
+
+ +
+ +
+
+ +
+
+
+
In [59]:
+
+
+
def a_function(a, b=1, c=1):
+    return a*b*c
+
+a_function(6,b=8,c=8)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[59]:
+ + + + +
+
384
+
+ +
+ +
+
+ +
+
+
+
In [60]:
+
+
+
a_function(6, 8, 8)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[60]:
+ + + + +
+
384
+
+ +
+ +
+
+ +
+
+
+
+

Functions can return multiple values with tuples

+
+
+
+
+
+
In [61]:
+
+
+
def b_function():
+    return (1, 2, 3)
+
+ +
+
+
+ +
+
+
+
In [62]:
+
+
+
b_function()
+
+ +
+
+
+ +
+
+ + +
+ +
Out[62]:
+ + + + +
+
(1, 2, 3)
+
+ +
+ +
+
+ +
+
+
+
In [63]:
+
+
+
x, y, z = b_function()
+print(x, y, z)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
1 2 3
+
+
+
+ +
+
+ +
+
+
+
+

Exceptions are valuable for good code!

    +
  • Python supports try/except/finally and they should be used regularly
  • +
+ +
+
+
+
+
+
In [64]:
+
+
+
def c_function(a, b, c):
+    try:
+        if a<0:
+            raise Exception
+        return a*b*c
+    except Exception as e:
+        print("The first parameter cannot be less than zero.")
+    finally:
+        pass # we don't need to clean up anything after the exception
+
+ +
+
+
+ +
+
+
+
In [65]:
+
+
+
c_function(-1, 2, 3) 
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
The first parameter cannot be less than zero.
+
+
+
+ +
+
+ +
+
+
+
+

What did the function return from the exception???

+
+
+
+
+
+
+

ALL PYTHON FUNCTIONS IMPLICITLY RETURN None WHEN NO RETURN VALUE IS SPECIFIED

+ +
+
+
+
+
+
In [66]:
+
+
+
c_function(-1, 2, 3) is None
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
The first parameter cannot be less than zero.
+
+
+
+ +
+ +
Out[66]:
+ + + + +
+
True
+
+ +
+ +
+
+ +
+
+
+
+

Comprehensions

+
+
+
+
+
+
+

List comprehensions provide shorthand for building lists

+
+
+
+
+
+
In [67]:
+
+
+
l1 = [x for x in range(0,11)]
+l1
+
+ +
+
+
+ +
+
+ + +
+ +
Out[67]:
+ + + + +
+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+
+ +
+ +
+
+ +
+
+
+
In [68]:
+
+
+
def pw_generator():
+    ascii_pw_range = list(range(48,58)) + list(range(65,91)) + list(range (97, 123))
+    
+    return ''.join([chr(random.choice(ascii_pw_range)) \
+                    for i in range(0,10)])
+
+pw_generator()
+
+ +
+
+
+ +
+
+ + +
+ +
Out[68]:
+ + + + +
+
'TMbD0xr0Bj'
+
+ +
+ +
+
+ +
+
+
+
In [69]:
+
+
+
[pw_generator() for i in range(0,15)]
+
+ +
+
+
+ +
+
+ + +
+ +
Out[69]:
+ + + + +
+
['MIO3ewedYc',
+ 'YbFp3ofMhC',
+ 'lgy5GUvod4',
+ 'VfOjbJ1oF3',
+ 'tqYltK37KK',
+ 'E4SzZ0zRjX',
+ 'fdJRpvyHrn',
+ '9cCas9FpyA',
+ '33qhlSo7kd',
+ 'dL1QupAp0V',
+ 'KBciH7ZSaN',
+ '3B1StGRLLe',
+ 'bR6wZCAzsa',
+ '9gq3zcB6lb',
+ 'vmNodUhosK']
+
+ +
+ +
+
+ +
+
+
+
+

Dictionary comprehensions are also quite nice ...

    +
  • Let's say you want to swap the key, value pairs in a dictionary ...
  • +
+ +
+
+
+
+
+
In [70]:
+
+
+
d1 = dict([('a', 1), ('b', 2), ('c', 3)])
+{ v:k for (k,v) in d1.items() }
+
+ +
+
+
+ +
+
+ + +
+ +
Out[70]:
+ + + + +
+
{1: 'a', 2: 'b', 3: 'c'}
+
+ +
+ +
+
+ +
+
+
+
+
    +
  • something more practical might be to filter out all values not meeting a criterion
  • +
+ +
+
+
+
+
+
In [71]:
+
+
+
d1 = dict([('a', 1), ('b', 2), ('c', 3)])
+{ v:k for (k,v) in d1.items() if v%2 != 0}
+
+ +
+
+
+ +
+
+ + +
+ +
Out[71]:
+ + + + +
+
{1: 'a', 3: 'c'}
+
+ +
+ +
+
+ +
+
+
+
+

LET'S PLAY!

+
+
+
+
+
+ + + + + + + diff --git a/python_introduction.pdf b/python_introduction.pdf new file mode 100644 index 0000000..72cc94a Binary files /dev/null and b/python_introduction.pdf differ diff --git a/python_introduction.png b/python_introduction.png new file mode 100644 index 0000000..bf68eee Binary files /dev/null and b/python_introduction.png differ