{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# DMEP In-class Code July 20th, 2017\n", "\n", "* \"for\" loops\n", "* if/elif/else\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## For loops" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "text = \"it was the best of times, it was the worst of times\"" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "words = text.split()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I want to write some code that display this:\n", "\n", " it\n", " was\n", " the\n", " best\n", " of\n", " times,\n", " it\n", " was\n", " the\n", " worst\n", " of\n", " times\n", " " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "it\n", "was\n", "the\n", "best\n", "of\n", "times,\n", "it\n", "was\n", "the\n", "worst\n", "of\n", "times\n" ] } ], "source": [ "for item in words:\n", " print(item)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "it\n", "was\n", "the\n", "best\n", "of\n", "times,\n" ] } ], "source": [ "item = words[0]\n", "print(item)\n", "item = words[1]\n", "print(item)\n", "item = words[2]\n", "print(item)\n", "item = words[3]\n", "print(item)\n", "item = words[4]\n", "print(item)\n", "item = words[5]\n", "print(item)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "IT\n", "WAS\n", "THE\n", "BEST\n", "OF\n", "TIMES,\n", "IT\n", "WAS\n", "THE\n", "WORST\n", "OF\n", "TIMES\n" ] } ], "source": [ "for item in words:\n", " yelling = item.upper()\n", " print(yelling)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "shopping = []" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "shopping.append(\"apples\")" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['apples']" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shopping" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "shopping.append(\"flour\")" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": true }, "outputs": [], "source": [ "shopping.append(\"sugar\")" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": true }, "outputs": [], "source": [ "shopping.append(\"eggs\")" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['apples', 'flour', 'sugar', 'eggs']" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shopping" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "our task: new list that contains the number of letters in each ingredient on my shopping list.\n", "\n", " [6, 5, 5, 4]\n" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[6, 5, 5, 4]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[len(item) for item in shopping]" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": true }, "outputs": [], "source": [ "food_lengths = []\n", "for item in shopping:\n", " food_lengths.append(len(item))" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[6, 5, 5, 4]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "food_lengths" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": true }, "outputs": [], "source": [ "shopping.extend(['cinnamon', 'butter', 'beer'])" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['apples', 'flour', 'sugar', 'eggs', 'cinnamon', 'butter', 'beer']" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shopping" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## if/elif/else" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": true }, "outputs": [], "source": [ "word = \"bear\"" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "before if statement...\n", "...after if statement\n" ] } ], "source": [ "print(\"before if statement...\")\n", "if len(word) == 8:\n", " print(\"length is eight!\")\n", " print(\"I'm so glad that string had eight characters\")\n", " print(\"let's have a party! an eight-character party. 🎉\")\n", "print(\"...after if statement\")" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": true }, "outputs": [], "source": [ "temperature = 14" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "before if statement...\n", "kinda brisk!\n", "...after if statement\n" ] } ], "source": [ "print(\"before if statement...\")\n", "if temperature > 25:\n", " print(\"wow really hot today huh?\")\n", "elif temperature > 15:\n", " print(\"it's very pleasant today!\")\n", "elif temperature > 5:\n", " print(\"kinda brisk!\")\n", "else:\n", " print(\"it's quite cold\")\n", "print(\"...after if statement\")" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": true }, "outputs": [], "source": [ "temps = [12, 16, 24, 8, 42, 9, 23, 29, -2, 5, 8]" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "now beginning temperature broadcast!\n", "kinda brisk!\n", "it's very pleasant today!\n", "it's very pleasant today!\n", "kinda brisk!\n", "wow really hot today huh?\n", "kinda brisk!\n", "it's very pleasant today!\n", "wow really hot today huh?\n", "it's quite cold\n", "it's quite cold\n", "kinda brisk!\n", "temperature broadcast complete.\n" ] } ], "source": [ "print(\"now beginning temperature broadcast!\")\n", "for item in temps:\n", " if item > 25:\n", " print(\"wow really hot today huh?\")\n", " elif item > 15:\n", " print(\"it's very pleasant today!\")\n", " elif item > 5:\n", " print(\"kinda brisk!\")\n", " else:\n", " print(\"it's quite cold\")\n", "print(\"temperature broadcast complete.\")" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "collapsed": true }, "outputs": [], "source": [ "foo = \"this is a test\"" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "foo.find(\"test\")" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "foo.find(\"blah\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dictionaries!" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "collapsed": true }, "outputs": [], "source": [ "president_states = {\"Obama\": \"Hawaii\",\n", " \"Bush\": \"Texas\",\n", " \"Clinton\": \"Arkansas\",\n", " \"Trump\": \"New York\"}" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(president_states)" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hawaii'" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "president_states[\"Obama\"]" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Arkansas'" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "president_states[\"Clinton\"]" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Arkansas'" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a_president = \"Clinton\"\n", "president_states[a_president]" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "ename": "KeyError", "evalue": "'Franklin'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mpresident_states\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"Franklin\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mKeyError\u001b[0m: 'Franklin'" ] } ], "source": [ "president_states[\"Franklin\"]" ] }, { "cell_type": "code", "execution_count": 68, "metadata": { "collapsed": true }, "outputs": [], "source": [ "shopping = {\"apple\": 2, \"bag of flour\": 1, \"egg\": 3, \"bag of sugar\": 1}" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shopping[\"apple\"]" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"apple\" in shopping" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"banana\" in shopping" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Bush': 'Texas',\n", " 'Clinton': 'Arkansas',\n", " 'Obama': 'Hawaii',\n", " 'Trump': 'New York'}" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "president_states" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"New York\" in president_states" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['apple', 'bag of flour', 'egg', 'bag of sugar'])" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shopping.keys()" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_values([2, 1, 3, 1])" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shopping.values()" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "APPLE\n", "BAG OF FLOUR\n", "EGG\n", "BAG OF SUGAR\n" ] } ], "source": [ "for item in shopping.keys():\n", " print(item.upper())" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_items([('apple', 2), ('bag of flour', 1), ('egg', 3), ('bag of sugar', 1)])" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shopping.items()" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "apple 2\n", "bag of flour 1\n", "egg 3\n", "bag of sugar 1\n" ] } ], "source": [ "for item, value in shopping.items():\n", " print(item, value)" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'apple': 2, 'bag of flour': 1, 'bag of sugar': 1, 'egg': 3}" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shopping" ] }, { "cell_type": "code", "execution_count": 90, "metadata": { "collapsed": true }, "outputs": [], "source": [ "shopping[\"stick of butter\"] = 2" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'apple': 2,\n", " 'bag of flour': 1,\n", " 'bag of sugar': 1,\n", " 'egg': 3,\n", " 'stick of butter': 2}" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shopping" ] }, { "cell_type": "code", "execution_count": 93, "metadata": { "collapsed": true }, "outputs": [], "source": [ "shopping[\"cinnamon stick\"] = 1" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'apple': 2,\n", " 'bag of flour': 1,\n", " 'bag of sugar': 1,\n", " 'cinnamon stick': 1,\n", " 'egg': 3,\n", " 'stick of butter': 2}" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shopping" ] }, { "cell_type": "code", "execution_count": 96, "metadata": { "collapsed": true }, "outputs": [], "source": [ "shopping[\"apple\"] = 4" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'apple': 4,\n", " 'bag of flour': 1,\n", " 'bag of sugar': 1,\n", " 'cinnamon stick': 1,\n", " 'egg': 3,\n", " 'stick of butter': 2}" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shopping" ] }, { "cell_type": "code", "execution_count": 99, "metadata": { "collapsed": true }, "outputs": [], "source": [ "things = []" ] }, { "cell_type": "code", "execution_count": 119, "metadata": { "collapsed": true }, "outputs": [], "source": [ "things.append(\"a\")" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a',\n", " 'a',\n", " 'a',\n", " 'a',\n", " 'a',\n", " 'a',\n", " 'a',\n", " 'a',\n", " 'a',\n", " 'a',\n", " 'a',\n", " 'a',\n", " 'a',\n", " 'a',\n", " 'a',\n", " 'a',\n", " 'a',\n", " 'a',\n", " 'a',\n", " 'a']" ] }, "execution_count": 121, "metadata": {}, "output_type": "execute_result" } ], "source": [ "things" ] }, { "cell_type": "code", "execution_count": 132, "metadata": { "collapsed": true }, "outputs": [], "source": [ "user = {\n", " \"name\": \"Allison\",\n", " \"city\": \"Brooklyn, NY, USA\",\n", " \"favorite_color\": \"blue\",\n", " \"pets\": [\n", " \"Shumai\",\n", " \"Althea\"\n", " ]\n", "}" ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Allison'" ] }, "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ "user[\"name\"]" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Brooklyn, NY, USA'" ] }, "execution_count": 134, "metadata": {}, "output_type": "execute_result" } ], "source": [ "user[\"city\"]" ] }, { "cell_type": "code", "execution_count": 135, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "'blue'" ] }, "execution_count": 135, "metadata": {}, "output_type": "execute_result" } ], "source": [ "user[\"favorite_color\"]" ] }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 137, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(user[\"pets\"])" ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Shumai', 'Althea']" ] }, "execution_count": 139, "metadata": {}, "output_type": "execute_result" } ], "source": [ "user[\"pets\"]" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Shumai'" ] }, "execution_count": 141, "metadata": {}, "output_type": "execute_result" } ], "source": [ "user[\"pets\"][0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sets" ] }, { "cell_type": "code", "execution_count": 142, "metadata": { "collapsed": true }, "outputs": [], "source": [ "countries = set()" ] }, { "cell_type": "code", "execution_count": 143, "metadata": { "collapsed": true }, "outputs": [], "source": [ "countries.add(\"United States\")" ] }, { "cell_type": "code", "execution_count": 144, "metadata": { "collapsed": true }, "outputs": [], "source": [ "countries.add(\"China\")" ] }, { "cell_type": "code", "execution_count": 145, "metadata": { "collapsed": true }, "outputs": [], "source": [ "countries.add(\"Argentina\")" ] }, { "cell_type": "code", "execution_count": 146, "metadata": { "collapsed": true }, "outputs": [], "source": [ "countries.add(\"France\")" ] }, { "cell_type": "code", "execution_count": 147, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Argentina', 'China', 'France', 'United States'}" ] }, "execution_count": 147, "metadata": {}, "output_type": "execute_result" } ], "source": [ "countries" ] }, { "cell_type": "code", "execution_count": 149, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 149, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"United States\" in countries" ] }, { "cell_type": "code", "execution_count": 151, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 151, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Jupiter\" in countries" ] }, { "cell_type": "code", "execution_count": 153, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "France\n", "China\n", "Argentina\n", "United States\n" ] } ], "source": [ "for item in countries:\n", " print(item)" ] }, { "cell_type": "code", "execution_count": 155, "metadata": { "collapsed": true }, "outputs": [], "source": [ "countries.add(\"France\")" ] }, { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Argentina', 'China', 'France', 'United States'}" ] }, "execution_count": 157, "metadata": {}, "output_type": "execute_result" } ], "source": [ "countries" ] }, { "cell_type": "code", "execution_count": 159, "metadata": {}, "outputs": [], "source": [ "temps = [12, 12, 12, 13, 11, 12, 12, 13, 14, 14, 15, 14, 13, 11]" ] }, { "cell_type": "code", "execution_count": 161, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{11, 12, 13, 14, 15}" ] }, "execution_count": 161, "metadata": {}, "output_type": "execute_result" } ], "source": [ "set(temps)" ] }, { "cell_type": "code", "execution_count": 163, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[11, 12, 13, 14, 15]" ] }, "execution_count": 163, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(set(temps))" ] }, { "cell_type": "code", "execution_count": 165, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'set' object does not support indexing", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mcountries\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" ] } ], "source": [ "countries[1]" ] }, { "cell_type": "code", "execution_count": 167, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0,\n", " 1,\n", " 2,\n", " 3,\n", " 4,\n", " 5,\n", " 6,\n", " 7,\n", " 8,\n", " 9,\n", " 10,\n", " 11,\n", " 12,\n", " 13,\n", " 14,\n", " 15,\n", " 16,\n", " 17,\n", " 18,\n", " 19,\n", " 20,\n", " 21,\n", " 22,\n", " 23,\n", " 24,\n", " 25,\n", " 26,\n", " 27,\n", " 28,\n", " 29,\n", " 30,\n", " 31,\n", " 32,\n", " 33,\n", " 34,\n", " 35,\n", " 36,\n", " 37,\n", " 38,\n", " 39,\n", " 40,\n", " 41,\n", " 42,\n", " 43,\n", " 44,\n", " 45,\n", " 46,\n", " 47,\n", " 48,\n", " 49,\n", " 50,\n", " 51,\n", " 52,\n", " 53,\n", " 54,\n", " 55,\n", " 56,\n", " 57,\n", " 58,\n", " 59,\n", " 60,\n", " 61,\n", " 62,\n", " 63,\n", " 64,\n", " 65,\n", " 66,\n", " 67,\n", " 68,\n", " 69,\n", " 70,\n", " 71,\n", " 72,\n", " 73,\n", " 74,\n", " 75,\n", " 76,\n", " 77,\n", " 78,\n", " 79,\n", " 80,\n", " 81,\n", " 82,\n", " 83,\n", " 84,\n", " 85,\n", " 86,\n", " 87,\n", " 88,\n", " 89,\n", " 90,\n", " 91,\n", " 92,\n", " 93,\n", " 94,\n", " 95,\n", " 96,\n", " 97,\n", " 98,\n", " 99]" ] }, "execution_count": 167, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(range(100))" ] }, { "cell_type": "code", "execution_count": 169, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n", "hello\n", "hello\n", "hello\n", "hello\n", "hello\n", "hello\n", "hello\n", "hello\n", "hello\n" ] } ], "source": [ "for i in range(10):\n", " print(\"hello\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sets are faster than lists with `in`" ] }, { "cell_type": "code", "execution_count": 176, "metadata": { "collapsed": true }, "outputs": [], "source": [ "numbers_list = list(range(100000000))" ] }, { "cell_type": "code", "execution_count": 177, "metadata": { "collapsed": true }, "outputs": [], "source": [ "numbers_set = set(numbers_list)" ] }, { "cell_type": "code", "execution_count": 185, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 185, "metadata": {}, "output_type": "execute_result" } ], "source": [ "99999999999999 in numbers_list" ] }, { "cell_type": "code", "execution_count": 187, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 187, "metadata": {}, "output_type": "execute_result" } ], "source": [ "99999999999999 in numbers_set" ] }, { "cell_type": "code", "execution_count": 190, "metadata": { "collapsed": true }, "outputs": [], "source": [ "sonnet_words = set(open(\"sonnets.txt\").read().split())" ] }, { "cell_type": "code", "execution_count": 193, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 193, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"love\" in sonnet_words" ] }, { "cell_type": "code", "execution_count": 195, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 195, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"tyrannosaurus\" in sonnet_words" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tuple" ] }, { "cell_type": "code", "execution_count": 196, "metadata": { "collapsed": true }, "outputs": [], "source": [ "t = (\"a\", \"b\", \"c\", \"d\", \"e\")" ] }, { "cell_type": "code", "execution_count": 197, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('a', 'b', 'c', 'd', 'e')" ] }, "execution_count": 197, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t" ] }, { "cell_type": "code", "execution_count": 199, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 199, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(t)" ] }, { "cell_type": "code", "execution_count": 200, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'d'" ] }, "execution_count": 200, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[3]" ] }, { "cell_type": "code", "execution_count": 201, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'e'" ] }, "execution_count": 201, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[-1]" ] }, { "cell_type": "code", "execution_count": 203, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('b', 'c')" ] }, "execution_count": 203, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[1:3]" ] }, { "cell_type": "code", "execution_count": 204, "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "'tuple' object has no attribute 'append'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"f\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'" ] } ], "source": [ "t.append(\"f\")" ] }, { "cell_type": "code", "execution_count": 205, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'apple': 4,\n", " 'bag of flour': 1,\n", " 'bag of sugar': 1,\n", " 'cinnamon stick': 1,\n", " 'egg': 3,\n", " 'stick of butter': 2}" ] }, "execution_count": 205, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shopping" ] }, { "cell_type": "code", "execution_count": 207, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_items([('apple', 4), ('bag of flour', 1), ('egg', 3), ('bag of sugar', 1), ('stick of butter', 2), ('cinnamon stick', 1)])" ] }, "execution_count": 207, "metadata": {}, "output_type": "execute_result" } ], "source": [ "shopping.items()" ] }, { "cell_type": "code", "execution_count": 208, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, 3, 4, 5)" ] }, "execution_count": 208, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tuple([1, 2, 3, 4, 5])" ] }, { "cell_type": "code", "execution_count": 209, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 209, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list((1, 2, 3, 4, 5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## join method of strings" ] }, { "cell_type": "code", "execution_count": 210, "metadata": { "collapsed": true }, "outputs": [], "source": [ "text = \"this is a test\"" ] }, { "cell_type": "code", "execution_count": 211, "metadata": { "collapsed": true }, "outputs": [], "source": [ "words = text.split()" ] }, { "cell_type": "code", "execution_count": 236, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['this', 'is', 'a', 'test']" ] }, "execution_count": 236, "metadata": {}, "output_type": "execute_result" } ], "source": [ "words" ] }, { "cell_type": "code", "execution_count": 215, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'this and is and a and test'" ] }, "execution_count": 215, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\" and \".join(words)" ] }, { "cell_type": "code", "execution_count": 216, "metadata": { "collapsed": true }, "outputs": [], "source": [ "temp_str = \"14,15,16,14,12,11,16,17,18,20,11\"" ] }, { "cell_type": "code", "execution_count": 219, "metadata": {}, "outputs": [], "source": [ "f_temp = [int(val) * (9 / 5) + 32 for val in temp_str.split(\",\")]" ] }, { "cell_type": "code", "execution_count": 221, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'57.2,59.0,60.8,57.2,53.6,51.8,60.8,62.6,64.4,68.0,51.8'" ] }, "execution_count": 221, "metadata": {}, "output_type": "execute_result" } ], "source": [ "','.join([str(val) for val in f_temp])" ] } ], "metadata": { "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.1" } }, "nbformat": 4, "nbformat_minor": 2 }