From ba1d3be38f01550fca3a74d095a2f4b89fec31a2 Mon Sep 17 00:00:00 2001 From: Mihai <M.Lefter@lumc.nl> Date: Sat, 16 Sep 2017 16:46:49 +0200 Subject: [PATCH] Added hands on exercises for the first introduction lecture. --- .../02_introduction_to_python_1.ipynb | 115 +++++++++++++++--- 1 file changed, 100 insertions(+), 15 deletions(-) diff --git a/introduction/02_introduction_to_python_1.ipynb b/introduction/02_introduction_to_python_1.ipynb index bac0a06..3ba28f0 100644 --- a/introduction/02_introduction_to_python_1.ipynb +++ b/introduction/02_introduction_to_python_1.ipynb @@ -27,6 +27,7 @@ "---\n", "\n", "* Created early 90's by Guido van Rossem at CWI.\n", + " - Name: Monty Python.\n", "* General purpose, high-level programming language.\n", "* Design is driven by code readability." ] @@ -46,7 +47,8 @@ "---\n", "\n", "* Interpreted, no separate compilation step needed.\n", - "* Imperative and object-oriented programming (and some functional programming).\n", + "* Imperative and object-oriented programming.\n", + " - And some functional programming.\n", "* Dynamic type system.\n", "* Automatic memory management.\n", "\n", @@ -612,16 +614,17 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 20, "metadata": {}, "outputs": [], "source": [ - "a = 1.3e20" + "a = 1.3e20\n", + "b = 2" ] }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 21, "metadata": {}, "outputs": [ { @@ -630,7 +633,7 @@ "1.3e+20" ] }, - "execution_count": 16, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } @@ -641,23 +644,23 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "2.84e+20" + "3.2e+20" ] }, - "execution_count": 17, + "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "b = a + 1.2e19\n", - "b * 2" + "c = a + 1.5e19 * b\n", + "c * 2" ] }, { @@ -668,7 +671,7 @@ } }, "source": [ - "Python's type system (1/2)\n", + "Python's type system (1/3)\n", "===\n", "\n", "Every value has a type, view it using `type`:" @@ -769,7 +772,7 @@ } }, "source": [ - "Python's type system (2/2)\n", + "Python's type system (2/3)\n", "===\n", "\n", "Some operations are defined on more than one type, possibly with different meanings." @@ -804,7 +807,47 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1.3e+20" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "float" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -813,7 +856,7 @@ "str" ] }, - "execution_count": 23, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -825,8 +868,15 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, "source": [ + "Python's type system (2/3)\n", + "===\n", + "\n", "Python is strongly typed, meaning that operations on values with incompatible types are forbidden." ] }, @@ -850,6 +900,41 @@ "'beer' + 34" ] }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "<div class=\"alert alert-success\">\n", + "<h1>Hands on!</h1>\n", + "\n", + "<ol>\n", + " <li>We’ve seen that b = 2 is legal.\n", + " <ul>\n", + " <li> What about 2 = b? </li>\n", + " <li>How about a = b = 1?</li>\n", + " </ul>\n", + " </li>\n", + " <li>In math notation you can multiply x and y like this: xy.\n", + " What happens if you try that in Python?</li>\n", + " <li>How many seconds are there in 42 minutes and 42 seconds?</li>\n", + " <li>How many miles are there in 16 kilometers? Hint: there are 1.61 kilometers in a mile.</li>\n", + " <li>Let's assume that you run a 42 kilometer race in 4 hours 42 minutes and 42 seconds.\n", + " <ul>\n", + " <li>What is your average pace (time per mile in minutes and seconds)?</li>\n", + " <li>What is your average speed in miles per hour?</li>\n", + " </ul>\n", + " </li>\n", + " <li>Use string operations to reference string 'tra la la la' in a variable named *song*.</li>\n", + " <li>If an article costs 249 Euros including the 19% Value Added Tax (VAT), what is the actual VAT amount in Euros for the corresponding article?</li>\n", + "</ol>\n", + "\n", + "</div>" + ] + }, { "cell_type": "code", "execution_count": 1, -- GitLab