diff --git a/day_1_exercise.ipynb b/day_1_exercise.ipynb new file mode 100644 index 0000000..2a946a0 --- /dev/null +++ b/day_1_exercise.ipynb @@ -0,0 +1,979 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [], + "authorship_tag": "ABX9TyPI8EYtjU960Bad9ngZYszf", + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "source": [], + "metadata": { + "id": "LjAEo6GNrgZG" + } + }, + { + "cell_type": "markdown", + "source": [ + "# Day 1 Exercises" + ], + "metadata": { + "id": "TOky_EPnu3sq" + } + }, + { + "cell_type": "markdown", + "source": [ + "## Data Types" + ], + "metadata": { + "id": "NTsJE03rvDSo" + } + }, + { + "cell_type": "markdown", + "source": [ + "\n", + "\n", + "1. What types are involved in the following expressions? What data type will each expression evaluate to?\n", + "\n" + ], + "metadata": { + "id": "w5chCWegvHdr" + } + }, + { + "cell_type": "code", + "source": [ + "8 * 2.5" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "WYyIyW0kvRy-", + "outputId": "55926443-1f37-43ed-adc1-cfbd800e9a74" + }, + "execution_count": 62, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "20.0" + ] + }, + "metadata": {}, + "execution_count": 62 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "Data types involved: integer number (Int) (8) and floating point number (Float) (2.5)\n", + "Data type evaluated to: Int (20)" + ], + "metadata": { + "id": "ho6dbCysvYNI" + } + }, + { + "cell_type": "markdown", + "source": [], + "metadata": { + "id": "DxGgXXL7wVaa" + } + }, + { + "cell_type": "code", + "source": [ + "9 / 2" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "YFi2qZ3Hvq6u", + "outputId": "65c07b42-297c-4db1-8c88-4329820261f6" + }, + "execution_count": 63, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "4.5" + ] + }, + "metadata": {}, + "execution_count": 63 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "Data types involved: Ints (9 and 2)\n", + "Data type evaluated to: Float (4.5)" + ], + "metadata": { + "id": "v4vrS-qTvusH" + } + }, + { + "cell_type": "code", + "source": [ + "1.5 * 2 >= 7 - 3" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "IuCXaydnv0Aa", + "outputId": "2a20a56d-1510-4d66-da11-9e048aefd7e9" + }, + "execution_count": 64, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "False" + ] + }, + "metadata": {}, + "execution_count": 64 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "Data types involved: Float (1.5) and Ints (2, 7, and 3)\n", + "Data type evaluated to: Bool (False)" + ], + "metadata": { + "id": "FUVAYClqwHvu" + } + }, + { + "cell_type": "markdown", + "source": [ + "\n", + "2. In which order will the expressions be evaluated?\n" + ], + "metadata": { + "id": "tA-lf6N_wgvT" + } + }, + { + "cell_type": "code", + "source": [ + "6 * 3 + 7 * 4" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "rHMaR2GkwuQ0", + "outputId": "a366c044-c905-42d0-8edc-5cd5c3543670" + }, + "execution_count": 65, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "46" + ] + }, + "metadata": {}, + "execution_count": 65 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "6 * 3 = 18, 7 * 4 = 28, 18 + 28 = 46 (Multiplication followed by addition)" + ], + "metadata": { + "id": "3idBxqMQw0Bv" + } + }, + { + "cell_type": "code", + "source": [ + "5 - 2 * 3 ** 4" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "sNZzjvpFxV6V", + "outputId": "4594bbae-02d9-4e9d-e164-badd0f3aca58" + }, + "execution_count": 66, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "-157" + ] + }, + "metadata": {}, + "execution_count": 66 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "Exponentiation, followed by multiplication, followed by subtraction" + ], + "metadata": { + "id": "eYm1k7mPxe7c" + } + }, + { + "cell_type": "code", + "source": [ + "(5 - 2) * 3 ** 4" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "jmsj_sX1xk9_", + "outputId": "49259cd1-763b-4692-fde8-12332556189d" + }, + "execution_count": 67, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "243" + ] + }, + "metadata": {}, + "execution_count": 67 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "Parantheses first (subtraction), followed by exponentiation, followed by multiplication" + ], + "metadata": { + "id": "gxnUHEJKxqHU" + } + }, + { + "cell_type": "code", + "source": [ + "5 + 2 >= 3 * 4" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "qd4i4dv8x_YT", + "outputId": "ab12232c-5cbc-4073-d2a9-5a28d53674a6" + }, + "execution_count": 68, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "False" + ] + }, + "metadata": {}, + "execution_count": 68 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "Multiplication, addition, greater than or equal to (7 is not greater than or equal to 12)" + ], + "metadata": { + "id": "vTI8o94XyFIH" + } + }, + { + "cell_type": "markdown", + "source": [ + "# Comments and Errors" + ], + "metadata": { + "id": "vi8lCMTryb72" + } + }, + { + "cell_type": "markdown", + "source": [ + "\n", + "\n", + "1. Which of these expressions results in an error? Why does each error occur?\n", + "\n" + ], + "metadata": { + "id": "OYnY9uWNyhbf" + } + }, + { + "cell_type": "code", + "source": [ + "((((5 * 4 ** 7))))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "lN7ZUfk-yk5N", + "outputId": "2581143e-79cd-44a2-c0f9-547dfd7d06b1" + }, + "execution_count": 69, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "81920" + ] + }, + "metadata": {}, + "execution_count": 69 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "No error" + ], + "metadata": { + "id": "L23cxMqizThX" + } + }, + { + "cell_type": "code", + "source": [ + "84 * (0.5 / 7)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "collapsed": true, + "id": "gP7MGGVtzUh6", + "outputId": "8b063258-3644-44e7-c940-a54cc80a1d30" + }, + "execution_count": 70, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "6.0" + ] + }, + "metadata": {}, + "execution_count": 70 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "Syntax error: Does not work as an augmented assignment because it cannot begin with a digit (also, augmented assignments are used to assign a value to a variable)" + ], + "metadata": { + "id": "S_l0J6K80VNn" + } + }, + { + "cell_type": "markdown", + "source": [ + "Solution:" + ], + "metadata": { + "id": "B6UdH-2t1Mft" + } + }, + { + "cell_type": "code", + "source": [ + "84 * (0.5 / 7)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "vALqsqPs1N53", + "outputId": "5be4d402-62fe-4b25-a85f-ae8064cd90aa" + }, + "execution_count": 71, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "6.0" + ] + }, + "metadata": {}, + "execution_count": 71 + } + ] + }, + { + "cell_type": "code", + "source": [ + "(-(-(-(-5 * (4 + 3)))))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "H4aKsMgWzdU9", + "outputId": "409c8529-f271-482c-a4cb-eee613e78001" + }, + "execution_count": 72, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "35" + ] + }, + "metadata": {}, + "execution_count": 72 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "No error" + ], + "metadata": { + "id": "WHhXfQHWziE4" + } + }, + { + "cell_type": "code", + "source": [ + "5 * 3 = weight" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 108 + }, + "collapsed": true, + "id": "zzABhwsTzjhW", + "outputId": "86ff1d10-c8af-4b42-c29f-e7ac41b0f19b" + }, + "execution_count": 73, + "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_1809/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" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "Syntax error: variable names cannot start with a digit" + ], + "metadata": { + "id": "DZ79EodR0BAj" + } + }, + { + "cell_type": "markdown", + "source": [ + "Solution:" + ], + "metadata": { + "id": "ndTx-BRi0O7C" + } + }, + { + "cell_type": "code", + "source": [ + "weight = 5 * 3" + ], + "metadata": { + "id": "vqOlSKkm0MWl" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "73 / -----------5" + ], + "metadata": { + "id": "QDYC11O3zqmx" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "No error" + ], + "metadata": { + "id": "bVpriV01zw3w" + } + }, + { + "cell_type": "markdown", + "source": [ + "\n", + "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." + ], + "metadata": { + "id": "y-AoNVM720yW" + } + }, + { + "cell_type": "code", + "source": [ + "value = 6\n", + "# If value is a string:\n", + "print(\"The value is valid.\")\n", + "# If value is not a string:\n", + "raise Exception(\"The value must be a string.\")" + ], + "metadata": { + "id": "KzwTJLkv3MmL" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "\n", + "3. What are the following errors?" + ], + "metadata": { + "id": "uCIydy0V54j8" + } + }, + { + "cell_type": "code", + "source": [ + "arr = [1, 2, 3]\n", + "print(arr[2])" + ], + "metadata": { + "collapsed": true, + "id": "GcRDhtVd59_U" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "Index error: element [3] does not exist because [1, 2, 3] are assigned 0, 1, and 2 when sliced" + ], + "metadata": { + "id": "v6_LnRF96fqS" + } + }, + { + "cell_type": "markdown", + "source": [ + "Solution:" + ], + "metadata": { + "id": "jGc8PVvZ-CY1" + } + }, + { + "cell_type": "code", + "source": [ + "arr = [1, 2, 3]\n", + "print(arr[2])" + ], + "metadata": { + "id": "80MI1Ode-Dz7" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "3 is the last element of arr, represented by [2]" + ], + "metadata": { + "id": "lHkvknu1-ODz" + } + }, + { + "cell_type": "code", + "source": [ + "fullName = \"Taylor Swift\"\n", + "print(full_name)" + ], + "metadata": { + "collapsed": true, + "id": "t0N6coKV6Dc8" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "Name error: full_name is not defined (variable name written as fullName)" + ], + "metadata": { + "id": "V-ld01ly6ISF" + } + }, + { + "cell_type": "markdown", + "source": [ + "Solution:" + ], + "metadata": { + "id": "Uv7Q_9gi6NKp" + } + }, + { + "cell_type": "code", + "source": [ + "full_name = \"Taylor Swift\"\n", + "print(full_name)" + ], + "metadata": { + "id": "XhljwKWE6Tnp" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "2 + '2' + 4" + ], + "metadata": { + "collapsed": true, + "id": "dpeaVbrl6Voj" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "Type error: integer cannot be added to a string (incompatible)" + ], + "metadata": { + "id": "gGmLCoTO8Q7I" + } + }, + { + "cell_type": "markdown", + "source": [ + "Solutions:" + ], + "metadata": { + "id": "aqQaUuR28lsb" + } + }, + { + "cell_type": "code", + "source": [ + "2 + 2 + 4" + ], + "metadata": { + "id": "OQKGtrht8mb3" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "'2' + '2' + '4'" + ], + "metadata": { + "id": "3ROIaVYy8uRb" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Functions" + ], + "metadata": { + "id": "LOFtebJQ-UVI" + } + }, + { + "cell_type": "markdown", + "source": [ + "\n", + "\n", + "1. Following the function design recipe, define a function that converts kilometers into miles. (Assume there are 1.6 kilometers in a mile.)\n", + "\n" + ], + "metadata": { + "id": "RcjlTMtF-WzO" + } + }, + { + "cell_type": "code", + "source": [ + "def kilometers_to_miles (kilometers) :\n", + " # Convert Kilometers to Miles\n", + " return kilometers / 1.6\n" + ], + "metadata": { + "id": "Bg7YgnOz-c5C" + }, + "execution_count": 77, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "kilometers_to_miles(0)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "RRHehZmc_phT", + "outputId": "43d783e4-a8fa-4975-ba19-d8bf16c72d0e" + }, + "execution_count": 78, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "0.0" + ] + }, + "metadata": {}, + "execution_count": 78 + } + ] + }, + { + "cell_type": "code", + "source": [ + "kilometers_to_miles(1.6)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "swM9wecs_vei", + "outputId": "52971945-37c8-43b8-ec55-a255448f4e09" + }, + "execution_count": 79, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "1.0" + ] + }, + "metadata": {}, + "execution_count": 79 + } + ] + }, + { + "cell_type": "code", + "source": [ + "distance_miles = kilometers_to_miles(1.6)\n", + "distance_miles" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "eXoHJxVt_1nA", + "outputId": "e9ec2580-be60-4140-d1b3-7830a6238de1" + }, + "execution_count": 80, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "1.0" + ] + }, + "metadata": {}, + "execution_count": 80 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "\n", + "2. What value is printed in the below code?" + ], + "metadata": { + "id": "aBC6E4gSTEIE" + } + }, + { + "cell_type": "code", + "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)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "3hQLFYc7TJSc", + "outputId": "6be3e06e-51cd-44a2-c3b8-3d09c77bd509" + }, + "execution_count": 75, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "3\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "Global scope (value defined outside the function) is 3, therefore, the printed value is 3" + ], + "metadata": { + "id": "gODXl8OxTa-c" + } + }, + { + "cell_type": "markdown", + "source": [ + "\n", + "3. Complete the body of the function. You can assume num is always >= 0.\n" + ], + "metadata": { + "id": "pj30q1lWT8FE" + } + }, + { + "cell_type": "code", + "source": [ + "def repeat(string, num):\n", + " ''' Return string repeated num times.\n", + " >>> repeat('yes', 4)\n", + " 'yesyesyesyes'\n", + " >>> repeat('no', 0)\n", + " ''\n", + " >>> repeat('yesnomaybe', 3)\n", + " 'yesnomaybeyesnomaybeyesnomaybe'\n", + " '''\n", + " output = string * num\n", + " return output" + ], + "metadata": { + "id": "YgssEtxhT_EC" + }, + "execution_count": 76, + "outputs": [] + }, + { + "cell_type": "markdown", + "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." + ], + "metadata": { + "id": "e5jistiDVEXc" + } + }, + { + "cell_type": "code", + "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", + " # Clean the string by replacing parantheses, spaces, and hyphens with nothing\n", + " cleaned_number = number.replace('()-,-').replace(',,').replace(',')\n", + "\n", + " # Check if the first three digits of the cleaned string are '416'\n", + " return cleaned_number[:3] == '416'" + ], + "metadata": { + "id": "nNtr5EXkVIKx" + }, + "execution_count": 82, + "outputs": [] + } + ] +} \ No newline at end of file