From 1441b27228b8809237aae0a899dbc5a3b5654612 Mon Sep 17 00:00:00 2001 From: daisyferreira Date: Tue, 26 Sep 2017 13:40:18 +0000 Subject: [PATCH] added functions --- Functions.ipynb | 301 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 249 insertions(+), 52 deletions(-) diff --git a/Functions.ipynb b/Functions.ipynb index df58b4c..80c0b5f 100644 --- a/Functions.ipynb +++ b/Functions.ipynb @@ -25,6 +25,13 @@ "\n" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -78,16 +85,168 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "15" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "#this function adds two numbers passed as parameters\n", "def add (number1, number2):\n", " answer = number1+number2\n", - " return answer" + " return answer\n", + "\n", + "add(10,5)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#this function subtracts the second number from the first\n", + "def subtract (number1,number2):\n", + " answer = number1-number2\n", + " return answer\n", + "\n", + "subtract(10,5)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def floorDivide (number1, number2):\n", + " answer = number1//number2\n", + " return answer\n", + "\n", + "floorDivide(10,2)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4.5" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def divide (number1,number2):\n", + " answer = number1/number2\n", + " return answer\n", + "divide(9,2)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "40" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def multiply (number1,number2):\n", + " answer = number1*number2\n", + " return answer\n", + "multiply(8,5)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def getRemainder(number1,number2):\n", + " answer = (number1%number2)\n", + " return answer\n", + "getRemainder(9,5)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "81" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def power(number1,number2):\n", + " answer = number1**number2\n", + " return answer\n", + "power(9,2)" ] }, { @@ -101,36 +260,80 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "add(4,5)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "-11" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "add(-6,-5)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "14.6" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "add(5.6, 9)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'helloworld'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "add (\"hello\", \"world\") # ooops this is weird!!" ] @@ -169,22 +372,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "AssertionError", + "evalue": "Houston we've got a problem", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0madd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"Houston we've got a problem\"\u001b[0m \u001b[0;31m#This will give an AssertionError\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mAssertionError\u001b[0m: Houston we've got a problem" + ] + } + ], "source": [ - "assert 2 + 2 == 5, \"Houston we've got a problem\" #This will give an AssertionError\n" + "assert add(2,2) == 5, \"Houston we've got a problem\" #This will give an AssertionError\n" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ - "assert 2 + 2 == 4, \"Houston we've got a problem\" #this won't give an error!" + "assert add(2,2) == 4, \"Houston we've got a problem\" #this won't give an error!" ] }, { @@ -209,22 +422,20 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ - "# subtract function \n", + "# subtract function subtracts second parametre from the first\n", "\n", - "# floorDivide function\n", + "# floorDivide function floor divides second parametre from the first\n", "\n", - "# divide function\n", + "# divide function divides second parametre from the first\n", "\n", - "# multiply function\n", + "# multiply function multiplies first and second parametre\n", "\n", - "# getRemainder function\n", + "# getRemainder function gets the remainder of the first parametre divided by the second\n", "\n", - "# power function" + "# power function multiplies the first function to the power of the second" ] }, { @@ -239,9 +450,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "assert add(4,5)==9, \"add function not working\"" @@ -250,9 +459,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "assert subtract(4,5)==-1, \"subtract function not working\"" @@ -261,9 +468,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "assert multiply(4,5)==20, \"multiply function not working\"" @@ -272,9 +477,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "assert divide(5,5)==1.0, \"divide function not working\"" @@ -283,20 +486,16 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ - "assert floorDivide(1,2)==0.5, \"floor divide function not working\"" + "assert floorDivide(1,2)==0, \"floor divide function not working\"" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "assert getRemainder(5,4)==1, \"getRemainder function not working\"" @@ -305,9 +504,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "assert power(3,2)==9, \"power function not working\""