diff --git a/intro_python.ipynb b/intro_python.ipynb index 0401968..14b6e58 100644 --- a/intro_python.ipynb +++ b/intro_python.ipynb @@ -112,22 +112,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { "slide_type": "skip" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "int" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Numbers\n", "\n", "x = 3\n", - "y = 3.2" + "y = 3.2\n", + "\n", + "type(x)" ] }, { @@ -135,7 +147,6 @@ "execution_count": null, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { @@ -155,7 +166,6 @@ "execution_count": null, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { @@ -175,7 +185,6 @@ "execution_count": null, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { @@ -194,7 +203,7 @@ "metadata": { "ein.tags": "worksheet-0", "slideshow": { - "slide_type": "-" + "slide_type": "slide" } }, "source": [ @@ -206,6 +215,7 @@ "# Assignment\n", "a = 10 # 10\n", "\n", + "\n", "# Increment/Decrement\n", "a += 1 # 11\n", "a -= 1 # 10\n", @@ -267,17 +277,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { "slide_type": "-" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7.5\n" + ] + }, + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Printing - note how Jupyter automatically prints \"y\" to \"out\", \n", "# but we need to manually print \"x\" if we want to see it!\n", @@ -308,10 +335,9 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { @@ -332,7 +358,6 @@ "execution_count": null, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { @@ -353,7 +378,6 @@ "execution_count": null, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { @@ -424,25 +448,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { "slide_type": "-" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "high\n" + ] + } + ], "source": [ "# Practise control flow: \n", "# write some code that prints \"high\" if\n", "# the number in x is greater than 5, and \"low\"\n", "# if the number is less than 5\n", "\n", - "x = 5\n", + "x = 6\n", "\n", + "if x > 5:\n", + " print(\"high\")\n", + " if x < 5:\n", + " print(\"low\")\n", + "else:\n", + " print(\"equal\")\n", "# your code here! Hint: use if/else and print." ] }, @@ -506,25 +543,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { "slide_type": "-" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This persons age is: 1\n", + "This persons age is: 2\n", + "This persons age is: 10\n", + "Done\n", + "This persons age is: 100\n" + ] + } + ], "source": [ "# A list!\n", "ages = [1, 2, 10, None, 100]\n", - "\n", "# A loop! (note the indentation)\n", "for x in ages:\n", - " print(\"This persons age is: \", x)\n", - "print(\"Done\")" + " if x is not None:\n", + " print(\"This persons age is: \", x)\n", + " else:\n", + "\n", + " print(\"Done\")" ] }, { @@ -532,7 +582,6 @@ "execution_count": null, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { @@ -622,7 +671,6 @@ "execution_count": null, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { @@ -649,7 +697,6 @@ "execution_count": null, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { @@ -663,7 +710,7 @@ "\n", "nums = [30,1,4,3,10.5,100]\n", "\n", - "min_num = x[0] \n", + "min_num = num[0] \n", "\n", "for num in nums: \n", " if num < min_num:\n", @@ -674,17 +721,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { "slide_type": "-" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Challenge!\n", "\n", @@ -696,24 +753,35 @@ "total = 0 \n", "\n", "for num in nums:\n", - " # Your code here\n", + " if num is not None:\n", + " total += 1\n", " \n", "total" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { "slide_type": "-" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[900, 1, 16, 9, 110.25, 10000]" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Applying a function: \n", "# Squaring each number in a list\n", @@ -733,7 +801,6 @@ "execution_count": null, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { @@ -756,17 +823,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { "slide_type": "-" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[21, 45, 97]" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Filter:\n", "# Remove all values less than 18:\n", @@ -783,7 +860,6 @@ "execution_count": null, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { @@ -849,17 +925,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { "slide_type": "-" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "om has scored 100 points\n", + "nandan has scored 10000 points\n", + "arapakis has scored 55 points\n" + ] + } + ], "source": [ "# Destructuring tuples in a for loop:\n", "\n", @@ -874,17 +959,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { "slide_type": "-" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'nandan'" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Aggregating a list of tuples: \n", "\n", @@ -894,7 +989,16 @@ "\n", "scoreboard = [(\"om\", 100), (\"nandan\", 10000), (\"arapakis\", 55)]\n", "\n", - "# Your code here" + "# Your code here\n", + "highest = \"name\"\n", + "highests = 0\n", + "\n", + "for name,score in scoreboard:\n", + " if score > highests:\n", + " highest = name\n", + " highests = score\n", + "\n", + "highest\n" ] }, { @@ -936,17 +1040,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 57, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { "slide_type": "-" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[['statistics', 'more statistics', 'even more statistics'], ['ice cream'], ['R', 'D3']]\n" + ] + } + ], "source": [ "# Challenge: \n", "# Collect all the likes of the teachers into one list: \n", @@ -959,7 +1070,15 @@ " {\"name\": \"nandan\", \"score\": 10000, \"likes\": [\"ice cream\"]},\n", " {\"name\": \"arapakis\", \"score\": 55, \"likes\": [\"R\", \"D3\"]}]\n", "\n", - "# Your code here" + "# Your code here\n", + "likes = []\n", + "\n", + "#for k in teachers:\n", + "# likes = likes + k[\"likes\"]\n", + "\n", + " \n", + "likes = [teachers[\"likes\"] for teachers in teachers]\n", + "print(likes)\n" ] }, { @@ -1011,25 +1130,44 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 73, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { "slide_type": "-" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['hello,', 'i', 'would', 'like', 'a', 'foo.', 'foo', 'went', 'for', 'a', 'walk.', 'foo', 'bar', 'baz.', 'baz', 'to', 'foo', 'foo', 'the', 'foo.']\n", + "6\n" + ] + } + ], "source": [ "# Challenge: \n", "# Count the instances of \"foo\" in the following text, \n", "# ignoring case:\n", "\n", - "x = \"Hello, I would like a foo. Foo went for a walk. Foo bar baz. Baz to the foo.\"\n", + "x = \"Hello, I would like a foo. Foo went for a walk. Foo bar baz. Baz to foo foo the foo.\"\n", "\n", - "# Your code here" + "# Your code here\n", + "count = 0\n", + "foo_list = x.lower().split(\" \")\n", + "\n", + "print(foo_list)\n", + "for word in foo_list:\n", + " if \"foo\" in word:\n", + " count += 1\n", + "\n", + "#count = len(x.lower().split(\"foo\")) - 1\n", + "\n", + "print(count)\n" ] }, { @@ -1090,7 +1228,6 @@ "execution_count": null, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { @@ -1153,7 +1290,6 @@ "execution_count": null, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { @@ -1174,7 +1310,6 @@ "execution_count": null, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { @@ -1195,7 +1330,6 @@ "execution_count": null, "metadata": { "autoscroll": false, - "collapsed": false, "ein.hycell": false, "ein.tags": "worksheet-0", "slideshow": { @@ -1220,7 +1354,7 @@ } }, "source": [ - "## Programming project: basic text analytics\n", + "# Programming project: basic text analytics\n", "\n", "Open the file \"textfile.txt\", which has copied a passage from the book Learning Python\". \n", "\n", @@ -1233,27 +1367,13 @@ "As a first step, and as a simpler exercise, do the above for a single paragraph of the text. Do this first and if you do not manage to finish the larger project submit this as your solution. \n", "\n" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "autoscroll": false, - "collapsed": false, - "ein.hycell": false, - "ein.tags": "worksheet-0", - "slideshow": { - "slide_type": "-" - } - }, - "outputs": [], - "source": [] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python 3", + "language": "python", "name": "python3" }, "language_info": {