From 8cc076992f7df6fba0d4a7749728cc0589c4c785 Mon Sep 17 00:00:00 2001 From: test Date: Fri, 22 Sep 2017 13:35:25 +0000 Subject: [PATCH 1/2] added functions --- Functions.ipynb | 273 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 267 insertions(+), 6 deletions(-) diff --git a/Functions.ipynb b/Functions.ipynb index df58b4c..02e14c6 100644 --- a/Functions.ipynb +++ b/Functions.ipynb @@ -102,7 +102,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "add(4,5)" @@ -111,7 +113,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "add(-6,-5)" @@ -120,7 +124,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "add(5.6, 9)" @@ -129,7 +135,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "add (\"hello\", \"world\") # ooops this is weird!!" @@ -170,7 +178,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "assert 2 + 2 == 5, \"Houston we've got a problem\" #This will give an AssertionError\n" @@ -310,7 +320,258 @@ }, "outputs": [], "source": [ - "assert power(3,2)==9, \"power function not working\"" + "assert power(3,2)==9, \"power function not working\"\n", + "Jupyter Notebook Logout Control Panelprogramming1_function Last Checkpoint: 21 minutes ago (autosaved) Python 3\n", + "Python 3 Trusted\n", + "File\n", + "Edit\n", + "View\n", + "Insert\n", + "Cell\n", + "Kernel\n", + "Widgets\n", + "Help\n", + "\n", + "In [13]:\n", + "\n", + "age=18 \n", + "adults= True \n", + "​\n", + " \n", + "​\n", + "​\n", + " \n", + " \n", + " File \"\", line 1\n", + " if age > 18\n", + " ^\n", + "SyntaxError: invalid syntax\n", + "\n", + "\n", + "In [16]:\n", + "\n", + "raining = trousers \n", + "sunny = shorts \n", + "windy = coat\n", + "​\n", + "if raining = trousers:\n", + "print(\"wear trousers\")\n", + "​\n", + "elif sunny = shorts \n", + "print(\"wear shorts\")\n", + "​\n", + "else windy == coat \n", + "print(\"wear coat\")\n", + " File \"\", line 5\n", + " if raining = trousers:\n", + " ^\n", + "SyntaxError: invalid syntax\n", + "\n", + "\n", + "In [19]:\n", + "\n", + "gender= boy\n", + "pizza = \"no\"\n", + "​\n", + "pizza = input(\"do you like pizza, answer it in yes or no\")\n", + "​\n", + "if (gender == boy and pizza == \"yes\") \n", + " print(\"you can enter\")\n", + " \n", + " File \"\", line 6\n", + " if (gender == boy and pizza == \"yes\")\n", + " ^\n", + "SyntaxError: invalid syntax\n", + "\n", + "\n", + "In [20]:\n", + "\n", + "for letter in \"PYTHON\":\n", + " print(\"Current letter:\", letter)\n", + "Current letter: P\n", + "Current letter: Y\n", + "Current letter: T\n", + "Current letter: H\n", + "Current letter: O\n", + "Current letter: N\n", + "In [21]:\n", + "\n", + "for x in range (0,9):\n", + " print(\"hello\")\n", + " print(\"goodbye\")\n", + "hello\n", + "goodbye\n", + "hello\n", + "goodbye\n", + "hello\n", + "goodbye\n", + "hello\n", + "goodbye\n", + "hello\n", + "goodbye\n", + "hello\n", + "goodbye\n", + "hello\n", + "goodbye\n", + "hello\n", + "goodbye\n", + "hello\n", + "goodbye\n", + "In [1]:\n", + "\n", + "name=\"fred\"\n", + "for ch in name:\n", + " print(ch, end=””)\n", + " File \"\", line 3\n", + " print(ch, end=””)\n", + " ^\n", + "SyntaxError: invalid character in identifier\n", + "\n", + "\n", + "In [2]:\n", + "\n", + "for x in range (1,101,5):\n", + " print(x)\n", + " \n", + "1\n", + "6\n", + "11\n", + "16\n", + "21\n", + "26\n", + "31\n", + "36\n", + "41\n", + "46\n", + "51\n", + "56\n", + "61\n", + "66\n", + "71\n", + "76\n", + "81\n", + "86\n", + "91\n", + "96\n", + "In [ ]:\n", + "\n", + "​\n", + "In [3]:\n", + "\n", + "name = \"fred\"\n", + "age = \"12\"\n", + "​\n", + "print(\"{} {}\".format (name,age))\n", + "fred 12\n", + "In [6]:\n", + "\n", + "name = \"fred\"\n", + "age = \"12\"\n", + "​\n", + "print(\"{1} {0}\". format(name,age))\n", + "12 fred\n", + "In [8]:\n", + "\n", + "radius = 10 \n", + "pi = 3.141592653589793\n", + "​\n", + "print(\"{:d}\".format (radius))\n", + "print(\"{:f}\".format (pi))\n", + "10\n", + "3.141593\n", + "In [9]:\n", + "\n", + "pi = 3.141592653589793\n", + "print(\"{:0.2f}\".format (pi))\n", + "3.14\n", + "In [11]:\n", + "\n", + "val = 65\n", + "ch = \"b\"\n", + "​\n", + "x=ord(ch)\n", + "y=chr(val)\n", + "​\n", + "print(\"{} , {}\".format (x,y))\n", + "98 , A\n", + "In [17]:\n", + "\n", + "def add (number1, number2):\n", + " answer = number1+number2 \n", + " return answer \n", + "add(50,130)\n", + "Out[17]:\n", + "180\n", + "In [23]:\n", + "\n", + "def subtract (number1, number2):\n", + " answer = number1-number2\n", + " return answer \n", + "subtract(40,2)\n", + "Out[23]:\n", + "38\n", + "In [25]:\n", + "\n", + "def floordivide (number1, number2):\n", + " answer = number1//number2\n", + " return answer\n", + "floordivide(9,2)\n", + "Out[25]:\n", + "4\n", + "In [26]:\n", + "\n", + "def divide (number1, number2):\n", + " answer = number1/number2\n", + " return answer\n", + "divide(9,3)\n", + " \n", + "Out[26]:\n", + "3.0\n", + "In [30]:\n", + "\n", + "def multiply (number1, number2):\n", + " answer = number1*number2\n", + " return answer\n", + "multiply(8,9)\n", + "Out[30]:\n", + "72\n", + "In [31]:\n", + "\n", + "def getRemainder (number1, number2):\n", + " answer = number1/number2\n", + " return answer\n", + "getRemainder(12,7)\n", + "Out[31]:\n", + "1.7142857142857142\n", + "In [32]:\n", + "\n", + "def power (number1, number2):\n", + " answer = number1**number2\n", + " return answer \n", + "power(9,6)\n", + "Out[32]:\n", + "531441\n", + "In [51]:\n", + "\n", + "def function (number1, number2):\n", + " answer = number1-number2\n", + " return answer\n", + "​\n", + "​\n", + "answer = function(4,5)\n", + "print(answer)\n", + "​\n", + "assert function(3,2)==5, \"error in addition\"\n", + "-1\n", + "---------------------------------------------------------------------------\n", + "AssertionError Traceback (most recent call last)\n", + " in ()\n", + " 7 print(answer)\n", + " 8 \n", + "----> 9 assert function(3,2)==5, \"error in addition\"\n", + "\n", + "AssertionError: error in addition\n", + "\n" ] } ], From bd32e29469361e7a214406766796617a7ac59076 Mon Sep 17 00:00:00 2001 From: test Date: Tue, 26 Sep 2017 13:43:45 +0000 Subject: [PATCH 2/2] added functions --- Functions.ipynb | 365 ++++++++++++++++++++---------------------------- 1 file changed, 152 insertions(+), 213 deletions(-) diff --git a/Functions.ipynb b/Functions.ipynb index 02e14c6..082fdb4 100644 --- a/Functions.ipynb +++ b/Functions.ipynb @@ -314,11 +314,18 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "IndentationError", + "evalue": "unindent does not match any outer indentation level (, line 59)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m59\u001b[0m\n\u001b[0;31m File \"\", line 6\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unindent does not match any outer indentation level\n" + ] + } + ], "source": [ "assert power(3,2)==9, \"power function not working\"\n", "Jupyter Notebook Logout Control Panelprogramming1_function Last Checkpoint: 21 minutes ago (autosaved) Python 3\n", @@ -332,246 +339,178 @@ "Widgets\n", "Help\n", "\n", - "In [13]:\n", "\n", - "age=18 \n", - "adults= True \n", - "​\n", " \n", - "​\n", - "​\n", " \n", - " \n", - " File \"\", line 1\n", - " if age > 18\n", - " ^\n", - "SyntaxError: invalid syntax\n", - "\n", - "\n", - "In [16]:\n", - "\n", - "raining = trousers \n", - "sunny = shorts \n", - "windy = coat\n", - "​\n", - "if raining = trousers:\n", - "print(\"wear trousers\")\n", - "​\n", - "elif sunny = shorts \n", - "print(\"wear shorts\")\n", - "​\n", - "else windy == coat \n", - "print(\"wear coat\")\n", - " File \"\", line 5\n", - " if raining = trousers:\n", - " ^\n", - "SyntaxError: invalid syntax\n", - "\n", - "\n", - "In [19]:\n", - "\n", - "gender= boy\n", - "pizza = \"no\"\n", - "​\n", - "pizza = input(\"do you like pizza, answer it in yes or no\")\n", - "​\n", - "if (gender == boy and pizza == \"yes\") \n", - " print(\"you can enter\")\n", - " \n", - " File \"\", line 6\n", - " if (gender == boy and pizza == \"yes\")\n", - " ^\n", - "SyntaxError: invalid syntax\n", - "\n", - "\n", - "In [20]:\n", - "\n", - "for letter in \"PYTHON\":\n", - " print(\"Current letter:\", letter)\n", - "Current letter: P\n", - "Current letter: Y\n", - "Current letter: T\n", - "Current letter: H\n", - "Current letter: O\n", - "Current letter: N\n", - "In [21]:\n", - "\n", - "for x in range (0,9):\n", - " print(\"hello\")\n", - " print(\"goodbye\")\n", - "hello\n", - "goodbye\n", - "hello\n", - "goodbye\n", - "hello\n", - "goodbye\n", - "hello\n", - "goodbye\n", - "hello\n", - "goodbye\n", - "hello\n", - "goodbye\n", - "hello\n", - "goodbye\n", - "hello\n", - "goodbye\n", - "hello\n", - "goodbye\n", - "In [1]:\n", - "\n", - "name=\"fred\"\n", - "for ch in name:\n", - " print(ch, end=””)\n", - " File \"\", line 3\n", - " print(ch, end=””)\n", - " ^\n", - "SyntaxError: invalid character in identifier\n", - "\n", - "\n", - "In [2]:\n", - "\n", - "for x in range (1,101,5):\n", - " print(x)\n", - " \n", - "1\n", - "6\n", - "11\n", - "16\n", - "21\n", - "26\n", - "31\n", - "36\n", - "41\n", - "46\n", - "51\n", - "56\n", - "61\n", - "66\n", - "71\n", - "76\n", - "81\n", - "86\n", - "91\n", - "96\n", - "In [ ]:\n", - "\n", - "​\n", - "In [3]:\n", - "\n", - "name = \"fred\"\n", - "age = \"12\"\n", - "​\n", - "print(\"{} {}\".format (name,age))\n", - "fred 12\n", - "In [6]:\n", - "\n", - "name = \"fred\"\n", - "age = \"12\"\n", - "​\n", - "print(\"{1} {0}\". format(name,age))\n", - "12 fred\n", - "In [8]:\n", - "\n", - "radius = 10 \n", - "pi = 3.141592653589793\n", - "​\n", - "print(\"{:d}\".format (radius))\n", - "print(\"{:f}\".format (pi))\n", - "10\n", - "3.141593\n", - "In [9]:\n", - "\n", - "pi = 3.141592653589793\n", - "print(\"{:0.2f}\".format (pi))\n", - "3.14\n", - "In [11]:\n", - "\n", - "val = 65\n", - "ch = \"b\"\n", - "​\n", - "x=ord(ch)\n", - "y=chr(val)\n", - "​\n", - "print(\"{} , {}\".format (x,y))\n", - "98 , A\n", - "In [17]:\n", - "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "180" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ "def add (number1, number2):\n", " answer = number1+number2 \n", " return answer \n", - "add(50,130)\n", - "Out[17]:\n", - "180\n", - "In [23]:\n", - "\n", + "add(50,130)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ "def subtract (number1, number2):\n", " answer = number1-number2\n", " return answer \n", - "subtract(40,2)\n", - "Out[23]:\n", - "38\n", - "In [25]:\n", - "\n", + "subtract(40,2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ "def floordivide (number1, number2):\n", " answer = number1//number2\n", " return answer\n", - "floordivide(9,2)\n", - "Out[25]:\n", - "4\n", - "In [26]:\n", - "\n", + "floordivide(9,2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ "def divide (number1, number2):\n", " answer = number1/number2\n", " return answer\n", - "divide(9,3)\n", - " \n", - "Out[26]:\n", - "3.0\n", - "In [30]:\n", - "\n", + "divide(9,3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ "def multiply (number1, number2):\n", " answer = number1*number2\n", " return answer\n", - "multiply(8,9)\n", - "Out[30]:\n", - "72\n", - "In [31]:\n", - "\n", + "multiply(8,9)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ "def getRemainder (number1, number2):\n", " answer = number1/number2\n", " return answer\n", - "getRemainder(12,7)\n", - "Out[31]:\n", - "1.7142857142857142\n", - "In [32]:\n", - "\n", + "getRemainder(12,9)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ "def power (number1, number2):\n", " answer = number1**number2\n", " return answer \n", - "power(9,6)\n", - "Out[32]:\n", - "531441\n", - "In [51]:\n", - "\n", - "def function (number1, number2):\n", - " answer = number1-number2\n", - " return answer\n", - "​\n", - "​\n", + "power(9,6)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ "answer = function(4,5)\n", "print(answer)\n", - "​\n", + "\n", "assert function(3,2)==5, \"error in addition\"\n", - "-1\n", - "---------------------------------------------------------------------------\n", - "AssertionError Traceback (most recent call last)\n", + "\n", " in ()\n", " 7 print(answer)\n", " 8 \n", "----> 9 assert function(3,2)==5, \"error in addition\"\n", "\n", - "AssertionError: error in addition\n", - "\n" + "AssertionError: error in addition" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "assert subtract(4,5)==-1, \"subtract function not working\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "assert multiply(4,5)==20, \"multiply function not working\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "assert divide(5,5)==1.0, \"divide function not working\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "assert floorDivide(1,2)==0, \"floor divide function not working\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "assert getRemainder(5,4)==1, \"getRemainder function not working \"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "assert power(3,2)==9, \"power function not working\"" ] } ],