diff --git a/02_exercises/Day-1.ipynb b/02_exercises/Day-1.ipynb index 152ff22..ba49ad6 100644 --- a/02_exercises/Day-1.ipynb +++ b/02_exercises/Day-1.ipynb @@ -1,467 +1,1067 @@ { - "cells": [ - { - "cell_type": "markdown", - "id": "0db63e99", - "metadata": {}, - "source": [ - "# Day 1 Exercises\n", - "## Data Types" - ] + "cells": [ + { + "cell_type": "markdown", + "id": "0db63e99", + "metadata": { + "id": "0db63e99" + }, + "source": [ + "# Day 1 Exercises\n", + "## Data Types" + ] + }, + { + "cell_type": "markdown", + "id": "16317370", + "metadata": { + "id": "16317370" + }, + "source": [ + "#### 1. What types are involved in the following expressions? What data type will each expression evaluate to?\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "ef11b015", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "ef11b015", + "outputId": "ee591f32-033d-4c79-d29d-58291ee292cf" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "20.0" + ] + }, + "metadata": {}, + "execution_count": 1 + } + ], + "source": [ + "8 * 2.5" + ] + }, + { + "cell_type": "markdown", + "id": "f6d66006", + "metadata": { + "id": "f6d66006" + }, + "source": [ + "The operant \"*\" signifies multiplication so this line of code multiples 8 by 2.5 which is 20" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "65d27d1b", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "65d27d1b", + "outputId": "ac9773f3-f267-4da0-d416-5cd93ccf0cc1" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "4.5" + ] + }, + "metadata": {}, + "execution_count": 2 + } + ], + "source": [ + "9 / 2" + ] + }, + { + "cell_type": "markdown", + "id": "98b7953d", + "metadata": { + "id": "98b7953d" + }, + "source": [ + "The operator \"/\" signifies division so 9/2 should return 4.5" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "a43f75d9", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "a43f75d9", + "outputId": "bf85e6fc-6fac-40f5-e780-41d1bdd4f2e1" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "False" + ] + }, + "metadata": {}, + "execution_count": 3 + } + ], + "source": [ + "1.5 * 2 >= 7 - 3" + ] + }, + { + "cell_type": "markdown", + "id": "8aa61750", + "metadata": { + "id": "8aa61750" + }, + "source": [ + "multiplication, greater than or equal to and subtraction. It should run in the order multiplication, subtraction and then greater than or equal to. 3 >= 4 and it should return \"false\"" + ] + }, + { + "cell_type": "markdown", + "id": "456935f7", + "metadata": { + "id": "456935f7" + }, + "source": [ + "#### 2. In which order will the expressions be evaluated?" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "81d95cdf", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "81d95cdf", + "outputId": "ec369aca-e5e4-455f-90aa-40e3ffbf6ae0" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "46" + ] + }, + "metadata": {}, + "execution_count": 4 + } + ], + "source": [ + "6 * 3 + 7 * 4" + ] + }, + { + "cell_type": "markdown", + "id": "e01f5db8", + "metadata": { + "id": "e01f5db8" + }, + "source": [ + "1) 6 x 3 = 18\n", + "2) 7 x 4 = 28\n", + "3) 18 + 28 = 46" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "e461c8e3", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "e461c8e3", + "outputId": "d97eed2f-0f26-46ec-f446-56016645a216" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "-157" + ] + }, + "metadata": {}, + "execution_count": 5 + } + ], + "source": [ + "5 - 2 * 3 ** 4" + ] + }, + { + "cell_type": "markdown", + "id": "13b6e209", + "metadata": { + "id": "13b6e209" + }, + "source": [ + "1) 3 ^ 4 = 81\n", + "2) 81 x 2 = 162\n", + "3) 5 - 162 = -157" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "8f90ba30", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "8f90ba30", + "outputId": "961c1aaf-a61e-47e4-ee92-4f1cf6aecbee" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "243" + ] + }, + "metadata": {}, + "execution_count": 6 + } + ], + "source": [ + "(5 - 2) * 3 ** 4" + ] + }, + { + "cell_type": "markdown", + "id": "13b2d323", + "metadata": { + "id": "13b2d323" + }, + "source": [ + "1) (5-2) = 3\n", + "2) 3 ^ 4 = 81\n", + "3) 81 x 3 = 243" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "124e77f2", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "124e77f2", + "outputId": "d5f88650-b843-4cae-bf3d-92ca64cccc8c" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "False" + ] + }, + "metadata": {}, + "execution_count": 7 + } + ], + "source": [ + "5 + 2 >= 3 * 4" + ] + }, + { + "cell_type": "markdown", + "id": "98d3dae4", + "metadata": { + "id": "98d3dae4" + }, + "source": [ + "1) 3 x 4 = 12\n", + "2) 5 + 2 = 7\n", + "3) 7 >= 12?\n", + "Final result should give us \"False\"" + ] + }, + { + "cell_type": "markdown", + "id": "e1535cd4", + "metadata": { + "id": "e1535cd4" + }, + "source": [ + "## Comments and Errors" + ] + }, + { + "cell_type": "markdown", + "id": "54e16d1c", + "metadata": { + "id": "54e16d1c" + }, + "source": [ + "#### 1. Which of these expressions results in an error? Why does each error occur?" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "b523b4c5", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "b523b4c5", + "outputId": "18caa609-93f1-4837-b438-01cade465d52" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "81920" + ] + }, + "metadata": {}, + "execution_count": 8 + } + ], + "source": [ + "((((5 * 4 ** 7))))" + ] + }, + { + "cell_type": "markdown", + "id": "27db6591", + "metadata": { + "id": "27db6591" + }, + "source": [ + "There wouldn't be an error, the extra parentheses are likely ignored and the mathematics rules apply inside" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "267c3acf", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 106 + }, + "id": "267c3acf", + "outputId": "cf7f98f8-bd0e-415f-a172-3c219923f06f" + }, + "outputs": [ + { + "output_type": "error", + "ename": "SyntaxError", + "evalue": "'literal' is an illegal expression for augmented assignment (868797654.py, line 1)", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"/tmp/ipykernel_15482/868797654.py\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m 84 *= 0.5 / 7\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m 'literal' is an illegal expression for augmented assignment\n" + ] + } + ], + "source": [ + "84 *= 0.5 / 7" + ] + }, + { + "cell_type": "markdown", + "id": "5308d0c8", + "metadata": { + "id": "5308d0c8" + }, + "source": [ + "The \"*=\" is not a recognized operator so this should restult in an error because Python doesn't recognize it" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "5395cdfc", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "5395cdfc", + "outputId": "c5467efb-b500-4950-8204-19df00caa1de" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "35" + ] + }, + "metadata": {}, + "execution_count": 10 + } + ], + "source": [ + "(-(-(-(-5 * (4 + 3)))))" + ] + }, + { + "cell_type": "markdown", + "id": "8faa6637", + "metadata": { + "id": "8faa6637" + }, + "source": [ + "Should not result in an error, it would just deal with the inner-most parentheses first and work its way outward" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "bcb621cb", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 106 + }, + "id": "bcb621cb", + "outputId": "14a0fbf5-dc07-48b2-b9d6-c6fb9f275009" + }, + "outputs": [ + { + "output_type": "error", + "ename": "SyntaxError", + "evalue": "cannot assign to expression here. Maybe you meant '==' instead of '='? (3700621706.py, line 1)", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"/tmp/ipykernel_15482/3700621706.py\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m 5 * 3 = weight\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m cannot assign to expression here. Maybe you meant '==' instead of '='?\n" + ] + } + ], + "source": [ + "5 * 3 = weight" + ] + }, + { + "cell_type": "markdown", + "id": "13b1f2e5", + "metadata": { + "id": "13b1f2e5" + }, + "source": [ + "This should result in an error because a line of coding cannot begin with an integer" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "00785f55", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "00785f55", + "outputId": "c13c4cca-e1ef-4834-adca-409b0bab5058" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "-14.6" + ] + }, + "metadata": {}, + "execution_count": 12 + } + ], + "source": [ + "73 / -----------5" + ] + }, + { + "cell_type": "markdown", + "id": "ff5a54ad", + "metadata": { + "id": "ff5a54ad" + }, + "source": [ + "I don't anticipate an error: it should deal with the negations before doing the division" + ] + }, + { + "cell_type": "markdown", + "id": "e0e9ebea", + "metadata": { + "id": "e0e9ebea" + }, + "source": [ + "#### 2. Write a block of code to check if the value is a string. If it is, print out a message saying it is valid. Otherwise, raise an exception with a message saying the value must be a string." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "e2d51d70", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "e2d51d70", + "outputId": "15bdcd1c-2fc3-4f5d-a0c5-f573da6c464d" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Value must be a string\n" + ] + } + ], + "source": [ + "value = 6\n", + "\n", + "# Your code goes here\n", + "if type(value) == str:\n", + " print(\"Valid\")\n", + "else:\n", + " print(\"Value must be a string\")" + ] + }, + { + "cell_type": "markdown", + "id": "5c90263b", + "metadata": { + "id": "5c90263b" + }, + "source": [ + "#### 3. What are the following errors?" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "d843090e", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 159 + }, + "id": "d843090e", + "outputId": "f4333c60-f670-44e0-b95b-9d4cd5891e6e" + }, + "outputs": [ + { + "output_type": "error", + "ename": "IndexError", + "evalue": "list index out of range", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/tmp/ipykernel_15482/2963248189.py\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0marr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marr\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mIndexError\u001b[0m: list index out of range" + ] + } + ], + "source": [ + "arr = [1, 2, 3]\n", + "print(arr[3])" + ] + }, + { + "cell_type": "markdown", + "id": "75d2859b", + "metadata": { + "id": "75d2859b" + }, + "source": [ + " The list is not long enough to fetch [3]" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "17248f59", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 159 + }, + "id": "17248f59", + "outputId": "721dc44a-0eb7-43fb-dac2-dcc846390145" + }, + "outputs": [ + { + "output_type": "error", + "ename": "NameError", + "evalue": "name 'full_name' is not defined", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/tmp/ipykernel_15482/3996353418.py\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mfullName\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"Taylor Swift\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfull_name\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mNameError\u001b[0m: name 'full_name' is not defined" + ] + } + ], + "source": [ + "fullName = \"Taylor Swift\"\n", + "print(full_name)" + ] + }, + { + "cell_type": "markdown", + "id": "d2ea2086", + "metadata": { + "id": "d2ea2086" + }, + "source": [ + " 'fullName' definition and 'full_name' return don't match" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a77e5ca9", + "metadata": { + "id": "a77e5ca9" + }, + "outputs": [], + "source": [ + "2 + '2' + 4" + ] + }, + { + "cell_type": "markdown", + "id": "75b171d5", + "metadata": { + "id": "75b171d5" + }, + "source": [ + "You can't add an integer to a string and use an operator. Operators function differently when dealing with all strings or all integers but they can't be mixed." + ] + }, + { + "cell_type": "markdown", + "id": "b51b05f3", + "metadata": { + "id": "b51b05f3" + }, + "source": [ + "#### 1. Following the function design recipe, define a function that converts kilometers into miles. (Assume there are 1.6 kilometers in a mile.)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "6bcde5c5", + "metadata": { + "id": "6bcde5c5" + }, + "outputs": [], + "source": [ + "# Your code goes here\n", + "def convert_to_miles(kilometers):\n", + " miles = kilometers / 1.6\n", + " return miles\n", + " convert_to_miles(5)" + ] + }, + { + "cell_type": "code", + "source": [ + "convert_to_miles(5)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "sTDEwCY4CQy7", + "outputId": "bc8bbc4f-5b45-415a-8d7b-5e56d28d893a" + }, + "id": "sTDEwCY4CQy7", + "execution_count": 22, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "3.125" + ] + }, + "metadata": {}, + "execution_count": 22 + } + ] + }, + { + "cell_type": "markdown", + "id": "032ffa80", + "metadata": { + "id": "032ffa80" + }, + "source": [ + "#### 2. What value is printed in the below code?" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "a5d51b99", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "a5d51b99", + "outputId": "4ccc6ad9-ea2e-43ce-e516-5ffd760c7082" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "3\n" + ] + } + ], + "source": [ + "answer = 3\n", + "\n", + "def answer_to_everything():\n", + " '''Return the answer to life, the universe, and everything'''\n", + " answer = 42\n", + " return answer\n", + "\n", + "answer_to_everything()\n", + "\n", + "print(answer)\n", + "# it should be 3 as we defined it in the first line which was the global variable\n", + "# the local variable inside is over-ridden by the global" + ] + }, + { + "cell_type": "markdown", + "id": "8f4255d4", + "metadata": { + "id": "8f4255d4" + }, + "source": [ + "#### 3. Complete the body of the function. You can assume num is always >= 0." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "f00465cc", + "metadata": { + "id": "f00465cc" + }, + "outputs": [], + "source": [ + "def repeat(string, num):\n", + " ''' Return string repeated num times.\n", + " >>> repeat('yes', 4)\n", + " 'yesyesyesyes'\n", + " >>> repeat('no', 0)\n", + " # your expected output\n", + " >>> repeat('yesnomaybe', 3)\n", + " # your expected output here\n", + " '''\n", + "\n", + " return (string * num)" + ] + }, + { + "cell_type": "code", + "source": [ + "repeat(\"hi\", 3)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "id": "s__ozMB-F76S", + "outputId": "723d5d58-e94a-4c10-c7c4-59e2f8466a90" + }, + "id": "s__ozMB-F76S", + "execution_count": 2, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'hihihi'" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 2 + } + ] + }, + { + "cell_type": "code", + "source": [ + "repeat(\"yes\", 4)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "id": "qiSqCsyALy4G", + "outputId": "84267214-0016-4c26-d63a-f57aa61b7ed0" + }, + "id": "qiSqCsyALy4G", + "execution_count": 3, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'yesyesyesyes'" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 3 + } + ] + }, + { + "cell_type": "code", + "source": [ + "repeat(\"no\", 0)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "id": "gUBR9M6UL3Yp", + "outputId": "cad6461d-04ab-4cda-d272-75351fae6fd5" + }, + "id": "gUBR9M6UL3Yp", + "execution_count": 5, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "''" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 5 + } + ] + }, + { + "cell_type": "code", + "source": [ + "repeat(\"yesnomaybe\", 3)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "id": "n1IIKQFwMKhM", + "outputId": "846f1e88-9155-410c-9c0c-a6dc2ea326d7" + }, + "id": "n1IIKQFwMKhM", + "execution_count": 6, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'yesnomaybeyesnomaybeyesnomaybe'" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 6 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "I tested the code as it was given but it didn't run for me so I tweaked it and ran the repeat requests to see if it would work!" + ], + "metadata": { + "id": "CuecM6n8MT2b" + }, + "id": "CuecM6n8MT2b" + }, + { + "cell_type": "markdown", + "id": "53fcfb06", + "metadata": { + "id": "53fcfb06" + }, + "source": [ + "#### 4. Complete the function below. You can assume that number will always be a string with 10 digits and that the first 3 digits will always be the area code." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "bdd60bd7", + "metadata": { + "id": "bdd60bd7" + }, + "outputs": [], + "source": [ + "def is_416_number(number):\n", + " ''' Check whether the number has a 416 area code.\n", + " >>> is_416_number('(416)-555-5555')\n", + " True\n", + " >>> is_416_number('514 416 5555')\n", + " False\n", + " >>> is_416_number('4165554160')\n", + " True\n", + " '''\n", + " # Your code goes here\n", + " if number[0:3] == \"416\":\n", + " return True\n", + " elif number[1:4] == \"416\":\n", + " return True\n", + " else:\n", + " return False\n", + " # Hint: replace () and spaces with nothing\n" + ] + }, + { + "cell_type": "code", + "source": [ + "is_416_number('(416)-555-5555')\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Objl3z3QbhCY", + "outputId": "dd120e44-6095-40bd-bf49-e2c57148b981" + }, + "id": "Objl3z3QbhCY", + "execution_count": 8, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "True" + ] + }, + "metadata": {}, + "execution_count": 8 + } + ] + }, + { + "cell_type": "code", + "source": [ + "is_416_number('514 416 5555')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "btrx2zsibtyn", + "outputId": "c60338cc-044a-4efd-8d6b-03ea1e5a7a8d" + }, + "id": "btrx2zsibtyn", + "execution_count": 9, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "False" + ] + }, + "metadata": {}, + "execution_count": 9 + } + ] + }, + { + "cell_type": "code", + "source": [ + "is_416_number('4165554160')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "qyyROx3Cb09W", + "outputId": "e155d35f-bebf-42c6-ea72-f0111ffd1dc2" + }, + "id": "qyyROx3Cb09W", + "execution_count": 10, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "True" + ] + }, + "metadata": {}, + "execution_count": 10 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "yay!" + ], + "metadata": { + "id": "9doENU2OcAKZ" + }, + "id": "9doENU2OcAKZ" + } + ], + "metadata": { + "kernelspec": { + "display_name": "lcr-env", + "language": "python", + "name": "python3" + }, + "language_info": { + "name": "python", + "version": "3.11.13" + }, + "colab": { + "provenance": [] + } }, - { - "cell_type": "markdown", - "id": "16317370", - "metadata": {}, - "source": [ - "#### 1. What types are involved in the following expressions? What data type will each expression evaluate to?\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "ef11b015", - "metadata": {}, - "outputs": [], - "source": [ - "8 * 2.5" - ] - }, - { - "cell_type": "markdown", - "id": "f6d66006", - "metadata": {}, - "source": [ - "*Your answer here*" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "65d27d1b", - "metadata": {}, - "outputs": [], - "source": [ - "9 / 2" - ] - }, - { - "cell_type": "markdown", - "id": "98b7953d", - "metadata": {}, - "source": [ - "*Your answer here*" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a43f75d9", - "metadata": {}, - "outputs": [], - "source": [ - "1.5 * 2 >= 7 - 3" - ] - }, - { - "cell_type": "markdown", - "id": "8aa61750", - "metadata": {}, - "source": [ - "*Your answer here*" - ] - }, - { - "cell_type": "markdown", - "id": "456935f7", - "metadata": {}, - "source": [ - "#### 2. In which order will the expressions be evaluated?" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "81d95cdf", - "metadata": {}, - "outputs": [], - "source": [ - "6 * 3 + 7 * 4" - ] - }, - { - "cell_type": "markdown", - "id": "e01f5db8", - "metadata": {}, - "source": [ - "*Your answer here*" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "e461c8e3", - "metadata": {}, - "outputs": [], - "source": [ - "5 - 2 * 3 ** 4" - ] - }, - { - "cell_type": "markdown", - "id": "13b6e209", - "metadata": {}, - "source": [ - "*Your answer here*" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "8f90ba30", - "metadata": {}, - "outputs": [], - "source": [ - "(5 - 2) * 3 ** 4" - ] - }, - { - "cell_type": "markdown", - "id": "13b2d323", - "metadata": {}, - "source": [ - "*Your answer here*" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "124e77f2", - "metadata": {}, - "outputs": [], - "source": [ - "5 + 2 >= 3 * 4" - ] - }, - { - "cell_type": "markdown", - "id": "98d3dae4", - "metadata": {}, - "source": [ - "*Your answer here*" - ] - }, - { - "cell_type": "markdown", - "id": "e1535cd4", - "metadata": {}, - "source": [ - "## Comments and Errors" - ] - }, - { - "cell_type": "markdown", - "id": "54e16d1c", - "metadata": {}, - "source": [ - "#### 1. Which of these expressions results in an error? Why does each error occur?" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "b523b4c5", - "metadata": {}, - "outputs": [], - "source": [ - "((((5 * 4 ** 7))))" - ] - }, - { - "cell_type": "markdown", - "id": "27db6591", - "metadata": {}, - "source": [ - "*Your answer here*" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "267c3acf", - "metadata": {}, - "outputs": [], - "source": [ - "84 *= 0.5 / 7" - ] - }, - { - "cell_type": "markdown", - "id": "5308d0c8", - "metadata": {}, - "source": [ - "*Your answer here*" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "5395cdfc", - "metadata": {}, - "outputs": [], - "source": [ - "(-(-(-(-5 * (4 + 3)))))" - ] - }, - { - "cell_type": "markdown", - "id": "8faa6637", - "metadata": {}, - "source": [ - "*Your answer here*" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "bcb621cb", - "metadata": {}, - "outputs": [], - "source": [ - "5 * 3 = weight" - ] - }, - { - "cell_type": "markdown", - "id": "13b1f2e5", - "metadata": {}, - "source": [ - "*Your answer here*" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "00785f55", - "metadata": {}, - "outputs": [], - "source": [ - "73 / -----------5" - ] - }, - { - "cell_type": "markdown", - "id": "ff5a54ad", - "metadata": {}, - "source": [ - "*Your answer here*" - ] - }, - { - "cell_type": "markdown", - "id": "e0e9ebea", - "metadata": {}, - "source": [ - "#### 2. Write a block of code to check if the value is a string. If it is, print out a message saying it is valid. Otherwise, raise an exception with a message saying the value must be a string." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "e2d51d70", - "metadata": {}, - "outputs": [], - "source": [ - "value = 6\n", - "\n", - "# Your code goes here" - ] - }, - { - "cell_type": "markdown", - "id": "5c90263b", - "metadata": {}, - "source": [ - "#### 3. What are the following errors?" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "d843090e", - "metadata": {}, - "outputs": [], - "source": [ - "arr = [1, 2, 3]\n", - "print(arr[3])" - ] - }, - { - "cell_type": "markdown", - "id": "75d2859b", - "metadata": {}, - "source": [ - "*Your answer here*" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "17248f59", - "metadata": {}, - "outputs": [], - "source": [ - "fullName = \"Taylor Swift\"\n", - "print(full_name)" - ] - }, - { - "cell_type": "markdown", - "id": "d2ea2086", - "metadata": {}, - "source": [ - "*Your answer here*" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a77e5ca9", - "metadata": {}, - "outputs": [], - "source": [ - "2 + '2' + 4" - ] - }, - { - "cell_type": "markdown", - "id": "75b171d5", - "metadata": {}, - "source": [ - "*Your answer here*" - ] - }, - { - "cell_type": "markdown", - "id": "06f7a0d0", - "metadata": {}, - "source": [ - "## Functions" - ] - }, - { - "cell_type": "markdown", - "id": "b51b05f3", - "metadata": {}, - "source": [ - "#### 1. Following the function design recipe, define a function that converts kilometers into miles. (Assume there are 1.6 kilometers in a mile.)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "6bcde5c5", - "metadata": {}, - "outputs": [], - "source": [ - "# Your code goes here" - ] - }, - { - "cell_type": "markdown", - "id": "032ffa80", - "metadata": {}, - "source": [ - "#### 2. What value is printed in the below code?" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a5d51b99", - "metadata": {}, - "outputs": [], - "source": [ - "answer = 3\n", - "\n", - "def answer_to_everything():\n", - " '''Return the answer to life, the universe, and everything'''\n", - " answer = 42\n", - " return answer\n", - "\n", - "answer_to_everything()\n", - "\n", - "print(answer)" - ] - }, - { - "cell_type": "markdown", - "id": "8f4255d4", - "metadata": {}, - "source": [ - "#### 3. Complete the body of the function. You can assume num is always >= 0." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f00465cc", - "metadata": {}, - "outputs": [], - "source": [ - "def repeat(string, num):\n", - " ''' Return string repeated num times.\n", - " >>> repeat('yes', 4)\n", - " 'yesyesyesyes'\n", - " >>> repeat('no', 0)\n", - " # your expected output here\n", - " >>> repeat('yesnomaybe', 3)\n", - " # your expected output here\n", - " '''\n", - "\n", - " return output" - ] - }, - { - "cell_type": "markdown", - "id": "53fcfb06", - "metadata": {}, - "source": [ - "#### 4. Complete the function below. You can assume that number will always be a string with 10 digits and that the first 3 digits will always be the area code." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "bdd60bd7", - "metadata": {}, - "outputs": [], - "source": [ - "def is_416_number(number):\n", - " ''' Check whether the number has a 416 area code.\n", - " >>> is_416_number('(416)-555-5555')\n", - " True\n", - " >>> is_416_number('514 416 5555')\n", - " False\n", - " >>> is_416_number('4165554160')\n", - " True\n", - " '''\n", - " # Your code goes here\n", - " # Hint: replace () and spaces with nothing" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "lcr-env", - "language": "python", - "name": "python3" - }, - "language_info": { - "name": "python", - "version": "3.11.13" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} + "nbformat": 4, + "nbformat_minor": 5 +} \ No newline at end of file