diff --git a/Day_2/07_Lists.ipynb b/Day_2/07_Lists.ipynb index 225b320..a0108e8 100644 --- a/Day_2/07_Lists.ipynb +++ b/Day_2/07_Lists.ipynb @@ -167,8 +167,8 @@ "source": [ "* This makes lists different from strings. \n", "* You cannot change the characters in a [string](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#string) after it has been created.\n", - " * *Immutable*: cannot be changed after creation.\n", - " * In contrast, lists are *mutable*: they can be modified in place." + " * [*Immutable*](https://github.com/dlab-berkeley/python-fundamentals/blob/master/Glossary.md#immutable): cannot be changed after creation.\n", + " * In contrast, lists are [*mutable*](https://github.com/dlab-berkeley/python-fundamentals/blob/master/Glossary.md#mutable): they can be modified in place." ] }, { @@ -677,7 +677,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.1" + "version": "3.7.1" } }, "nbformat": 4, diff --git a/Day_2/08_Loops.ipynb b/Day_2/08_Loops.ipynb index 1a7dc49..e72d68a 100644 --- a/Day_2/08_Loops.ipynb +++ b/Day_2/08_Loops.ipynb @@ -207,7 +207,7 @@ "source": [ "* Read `total = total + number` as:\n", " * Add the current value of the [loop variable](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#loop-variable) `number` to the current value of the accumulator variable `total`.\n", - " * Assign this new value to to `total`, replacing the current value.\n", + " * Assign this new value to `total`, replacing the current value.\n", " \n", "Instead of writing `total = total + number`, this can be simplified to `total += number`. This will reassign total to the current value of total plus the current value of number:" ] @@ -340,7 +340,7 @@ "\n", "Below is a list of presidents. Create a new list that contains only the last name of each president.\n", "\n", - "(HINT: Look at string methods!)" + "(HINT: Use a `for` loop Look at string methods!)" ] }, { @@ -369,29 +369,13 @@ "outputs": [], "source": [] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now sort the list alphabetically by last name (HINT: look up `sorted`):" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Challenge 4: Range\n", "\n", - "Using `range` and a for loop, calculate the factorial of 42 (the product of all integers up to and including 42)." + "Using `range` and a `for` loop to calculate the factorial of 42 (which is the product of all integers up to and including 42)." ] }, { @@ -445,7 +429,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.1" + "version": "3.7.1" } }, "nbformat": 4, diff --git a/Day_2/09_Conditionals.ipynb b/Day_2/09_Conditionals.ipynb index 9c53097..feb3456 100644 --- a/Day_2/09_Conditionals.ipynb +++ b/Day_2/09_Conditionals.ipynb @@ -28,6 +28,33 @@ "* * * * *" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## `bool` data type\n", + "\n", + "A *Boolean* variable is one that can take the values of either `True` or `False`. They are used to indicate the presence or absence of a certain condition. You can test the value of a variable and a *Boolean* value will be returned. In Python, `bool` is the data type for *Boolean* values." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "type(True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "5 > 6" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -37,7 +64,7 @@ "* An `if` statement (more properly called a *conditional* statement)\n", " controls whether some block of code is executed or not.\n", "* Structure is similar to a `for` statement:\n", - " * First line opens with `if` and ends with a colon\n", + " * First line opens with `if`, contains a *Boolean* variable or expression, and ends with a colon\n", " * Body containing one or more statements is indented (usually by 4 spaces)\n" ] }, @@ -278,9 +305,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "presidents_full = [\"George Washington\", \"John Adams\", \"Thomas Jefferson\", \"James Madison\", \"James Monroe\", \\\n", @@ -297,9 +322,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [] }, @@ -322,9 +345,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "song = '''Row, row, row your boat\n", @@ -342,17 +363,19 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Using string methods, for loops, and conditionals, write some code to `print` only the rhyming words (HINT: we'll be simplistic and assume it rhymes if the last 3 characters are the same)" + "Using string methods, for loops, and conditionals, write some code to `print` only the rhyming words. We'll be simplistic and assume it rhymes if the last 3 characters are the same.\n", + "\n", + "(HINT: first remove punctuation using `punctation` below)" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], - "source": [] + "source": [ + "from string import punctuation" + ] }, { "cell_type": "markdown", @@ -365,7 +388,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "In poetry, an acrostic is a message created by taking certain letters in a pattern over lines. One 9th century German writer, Otfrid of Weissenburg, was notorius for his early use of acrostics, one instance of which is in the text below: *Salomoni episcopo Otfridus*. His message can be found by taking the first character of every other line. Print Otfrid's message! \n", + "In poetry, an acrostic is a message created by taking certain letters in a pattern over lines. One 9th century German writer, Otfrid of Weissenburg, was notorius for his early use of acrostics, one of which is in the text below: *Salomoni episcopo Otfridus*. His message can be found by taking the first character of every other line. Print Otfrid's message! \n", + "\n", + "(HINT: remember what % does, lookup \"enumerate\")\n", "\n", "Source: http://titus.uni-frankfurt.de/texte/etcs/germ/ahd/otfrid/otfri.htm" ] @@ -373,9 +398,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "text = '''si sálida gimúati sálomones gúati, \n", @@ -431,32 +454,25 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], - "source": [ - "# HINT: remember what % does, lookup enumerate" - ] + "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Otfrid was more skillful than to settle for the first letter of every other line. What happens if you extract the last letter of the last word of each line, for every other line starting on the second line?" + "Otfrid was more skillful than to settle for the first letter of every other line. What happens if you extract the last letter of the last word of each line, for every other line starting on the second line?\n", + "\n", + "(HINT: first remove punctuation and lookup \"enumerate\")" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], - "source": [ - "# HINT: first remove punctuation, tab is represented by \\t, lookup enumerate\n", - "from string import punctuation" - ] + "source": [] }, { "cell_type": "markdown", @@ -479,9 +495,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [] } @@ -502,7 +516,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.1" + "version": "3.7.1" } }, "nbformat": 4, diff --git a/Day_2/11_Scope.ipynb b/Day_2/11_Scope.ipynb index 0a2e055..f8314e2 100755 --- a/Day_2/11_Scope.ipynb +++ b/Day_2/11_Scope.ipynb @@ -36,9 +36,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "def increment(num):\n", @@ -49,9 +47,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "increment(3)" @@ -61,33 +57,41 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "In this temporary environment, the variables in the [parameter](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#parameter) list (in parentheses in the definition) are set to the values passed in. For example, in `simple_function(3)`, `x` gets set to `3`. Afterwards, you can't access these variables!" + "In this temporary environment, the variables in the [parameter](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#parameter) list (in parentheses in the definition) are set to the values passed in. For example, in `increment(3)`, `num` gets set to `3`. Afterwards, you can't access these variables!" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ - "# temp is no longer defined because simple_function returned (i.e., finished)!\n", "print(num)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the cell above we get an error because `num` is no longer defined. This is because the `increment(num)` function has already returned (i.e., finished) and so it's temporary scope has been destroyed!" + ] + }, { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ - "# and neither is value!\n", "print(incremented_num)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Same with the local variable `incremented_num` that was created within `increment(num)`!" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -100,9 +104,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "pressure = 103.9\n", @@ -115,9 +117,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "print(\"pressure is\", pressure)\n", @@ -150,9 +150,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "x = [1, 2, 3, 5]\n", @@ -161,19 +159,24 @@ " val[0] = val[0] + 3\n", " return val\n", "\n", - "add_3(x)\n", - "# Now, our function is modifying the contents of the list, and both variables still point to the same list" + "add_3(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now, our function is modifying the contents of the list, and both variables still point to the same list.\n", + "\n", + "So the list `x` refers to *is* modified" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ - "# So the list x refers to *is* modified\n", "print(x)" ] }, @@ -198,9 +201,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "def print_hello():\n", @@ -210,9 +211,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "print_hello()" @@ -230,16 +229,12 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ - "## and add a doc [string](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#string) so we can see what it does\n", - "\n", "def send(message, recipient):\n", - " \"\"\" Prints a kind greeting to our input\n", - " returns nothing\"\"\"\n", + " \"\"\" Prints a \"message\" to the \"recipient\"\n", + " The function returns nothing, which defaults to None\"\"\"\n", " print(message, recipient)\n", " \n", "send('Hello','World')" @@ -255,9 +250,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "send(recipient='World', message='Hello')" @@ -280,17 +273,17 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "def send(message, recipient, cc=None, bcc=None):\n", " \"\"\" Prints a kind greeting to our input\n", " returns nothing\"\"\"\n", " print(message, recipient)\n", - " print(\"CC: \", cc)\n", - " print(\"BCC: \", bcc)\n", + " if cc is not None:\n", + " print(\"CC: \", cc)\n", + " if bcc is not None:\n", + " print(\"BCC: \", bcc)\n", " \n", "send('Hello','World')" ] @@ -305,14 +298,44 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "send('Hello','World', \"Rochelle\", \"Laura\")" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "To explicitly use only one optional parameter, specify the name of the parameter followed by an equals sign, and the argument value" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "send('Hello','World', bcc=\"Laura\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "But if we don't explicitly state the option parameter to be used, Python will use the order the parameters are specified in the function declaration." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "send('Hello','World', \"Laura\")" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -325,9 +348,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [] }, @@ -359,9 +380,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [] } @@ -382,7 +401,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.1" + "version": "3.7.1" } }, "nbformat": 4, diff --git a/Day_2/Day_2_Answers/07_Answers.ipynb b/Day_2/Day_2_Answers/07_Answers.ipynb index ee8ad05..da5ac7b 100644 --- a/Day_2/Day_2_Answers/07_Answers.ipynb +++ b/Day_2/Day_2_Answers/07_Answers.ipynb @@ -18,9 +18,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -59,9 +57,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -89,9 +85,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -109,9 +103,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -137,9 +129,7 @@ { "cell_type": "code", "execution_count": 5, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -185,9 +175,7 @@ { "cell_type": "code", "execution_count": 7, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -229,9 +217,7 @@ { "cell_type": "code", "execution_count": 9, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -255,9 +241,7 @@ { "cell_type": "code", "execution_count": 10, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -306,9 +290,7 @@ { "cell_type": "code", "execution_count": 11, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -373,9 +355,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3" + "version": "3.7.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Day_2/Day_2_Answers/08_Answers.ipynb b/Day_2/Day_2_Answers/08_Answers.ipynb index 802e641..0b71079 100644 --- a/Day_2/Day_2_Answers/08_Answers.ipynb +++ b/Day_2/Day_2_Answers/08_Answers.ipynb @@ -69,7 +69,7 @@ "words = [\"red\", \"green\", \"blue\"]\n", "result = \"\"\n", "for c in words:\n", - " result = ''.join(words)\n", + " result += c\n", "print(result)" ] }, @@ -119,9 +119,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "last_names = []\n", @@ -139,31 +137,6 @@ "last_names" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now sort the list alphabetically by last name (HINT: look up `sorted`):" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "sorted(last_names)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "sorted(presidents_full, key=lambda x: x.split()[-1])" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -189,9 +162,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [] } @@ -212,7 +183,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.1" + "version": "3.7.1" } }, "nbformat": 4, diff --git a/Day_2/Day_2_Answers/09_Answers.ipynb b/Day_2/Day_2_Answers/09_Answers.ipynb index a4912ad..73f7a19 100644 --- a/Day_2/Day_2_Answers/09_Answers.ipynb +++ b/Day_2/Day_2_Answers/09_Answers.ipynb @@ -13,19 +13,9 @@ }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[0, 1, 1, 1, 0, 1]\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "original = [-1.5, 0.2, 0.4, 0.0, -1.3, 0.4]\n", "result = []\n", @@ -48,10 +38,8 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, + "execution_count": null, + "metadata": {}, "outputs": [], "source": [ "presidents_full = [\"George Washington\", \"John Adams\", \"Thomas Jefferson\", \"James Madison\", \"James Monroe\", \\\n", @@ -67,19 +55,9 @@ }, { "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['Martin Van Buren', 'James Buchanan', 'George H. W. Bush', 'George W. Bush']\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "b_pres = []\n", "\n", @@ -108,10 +86,8 @@ }, { "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": true - }, + "execution_count": null, + "metadata": {}, "outputs": [], "source": [ "song = '''Row, row, row your boat\n", @@ -129,40 +105,22 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Using string methods, for loops, and conditionals, write some code to `print` only the rhyming words (HINT: we'll be simplistic and assume it rhymes if the last 3 characters are the same)" + "Using string methods, for loops, and conditionals, write some code to `print` only the rhyming words. We'll be simplistic and assume it rhymes if the last 3 characters are the same.\n", + "\n", + "(HINT: first remove punctuation using `punctation` below)" ] }, { "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, + "execution_count": null, + "metadata": {}, "outputs": [], "source": [ + "from string import punctuation\n", + "\n", "for char in \".,\":\n", - " song = song.replace(char, \"\")" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "stream\n", - "dream\n", - "brook\n", - "hook\n" - ] - } - ], - "source": [ + " song = song.replace(char, \"\")\n", + "\n", "for w in song.split():\n", " for other_w in song.split():\n", " if w[-3:] == other_w[-3:] and w.lower() != other_w.lower():\n", @@ -184,15 +142,15 @@ "source": [ "In poetry, an acrostic is a message created by taking certain letters in a pattern over lines. One 9th century German writer, Otfrid of Weissenburg, was notorius for his early use of acrostics, one of which is in the text below: *Salomoni episcopo Otfridus*. His message can be found by taking the first character of every other line. Print Otfrid's message! \n", "\n", + "(HINT: remember what % does, lookup \"enumerate\")\n", + "\n", "Source: http://titus.uni-frankfurt.de/texte/etcs/germ/ahd/otfrid/otfri.htm" ] }, { "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, + "execution_count": null, + "metadata": {}, "outputs": [], "source": [ "text = '''si sálida gimúati sálomones gúati, \n", @@ -247,26 +205,14 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "salomóniepiscopóotfridus\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ - "# HINT: remember what % does, lookup \"enumerate\"\n", "message = \"\"\n", "for i,l in enumerate(text.split(\"\\n\")):\n", - " if (len(l) > 0):\n", - " if (i == 0) or (i % 2 == 0):\n", - " message += l.split()[0][0]\n", + " if i % 2 == 0:\n", + " message += l.split()[0][0]\n", "\n", "print(message)" ] @@ -275,35 +221,24 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Otfrid was more skillful than to settle for the first letter of every other line. What happens if you extract the last letter of the last word of each line, for every other line starting on the second line?" + "Otfrid was more skillful than to settle for the first letter of every other line. What happens if you extract the last letter of the last word of each line, for every other line starting on the second line?\n", + "\n", + "(HINT: first remove punctuation and lookup \"enumerate\")" ] }, { "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "salomoniepiscopootfridus\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ - "# HINT: first remove punctuation, tab is represented by \\t, lookup \"enumerate\"\n", - "from string import punctuation\n", - "\n", "for char in punctuation:\n", " text = text.replace(char, \"\")\n", "\n", "message = \"\"\n", "for i,l in enumerate(text.split(\"\\n\")):\n", - " if len(l) > 0 and i % 2 != 0:\n", - " message += l.split()[-1][-1]\n", + " if i % 2 != 0:\n", + " message += l.split()[-1][-1]\n", "\n", "print(message)" ] @@ -311,9 +246,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [] } @@ -334,9 +267,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.1" + "version": "3.7.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/Day_2/Day_2_Answers/10_Answers.ipynb b/Day_2/Day_2_Answers/10_Answers.ipynb index ac2a6fa..1347545 100644 --- a/Day_2/Day_2_Answers/10_Answers.ipynb +++ b/Day_2/Day_2_Answers/10_Answers.ipynb @@ -12,9 +12,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "def pressure(value):\n", @@ -33,9 +31,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "pressure(22.5)" @@ -53,9 +49,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "def long_function(name_string):\n", @@ -79,9 +73,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "def long_function(name_string):\n", @@ -103,9 +95,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "def print_date(year, month, day):\n", @@ -123,6 +113,15 @@ "Explain why the two lines of output appeared in the order they did." ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Answer\n", + "\n", + "The date was printed first because the `print_date` function actually prints the date, and it was called first. Since there was no `return` statement in the function declaration, the default value of `None` was returned and assigned to the `result` variable. Then, the `print` function was called using the `result` variable, which contains `None`." + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -135,9 +134,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "def print_date(year, month, day):\n", @@ -154,6 +151,15 @@ "When and why is it useful to call functions this way?" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Answer\n", + "\n", + "This short function prints the date in the format \"year/month/day\", and thus the cell above will print the string `2003/2/1` because the arguments passed into the function are explicitly stated (e.g. `day=1`). This is a bit of foreshadowing for the next notebook wherein we'll see how to use this syntax to provide optional parameters." + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -166,9 +172,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "rec_1 = \"143.1 Pursue efforts to ratify international human rights instruments (Kuwait);\" \n", @@ -189,9 +193,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "def get_country(rec):\n", @@ -213,9 +215,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "def get_country(rec):\n", @@ -234,9 +234,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "def get_country_rec(rec):\n", @@ -256,9 +254,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "print(country_and_rec[0])\n", @@ -275,9 +271,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "def get_country_rec(rec):\n", @@ -289,9 +283,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [] } @@ -312,9 +304,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.1" + "version": "3.7.1" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 }