diff --git a/Functions.ipynb b/Functions.ipynb index df58b4c..c511022 100644 --- a/Functions.ipynb +++ b/Functions.ipynb @@ -67,6 +67,186 @@ "\n" ] }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "33" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def add(number1, number2):\n", + " answer=number1+number2\n", + " return answer\n", + "# define the functions first\n", + "add(5,5)\n", + "add(-4,9)\n", + "add(67,-34)\n", + "#a sum can now be carried out" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "150" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def multiply(number1, number2):\n", + " answer=number1*number2\n", + " return answer\n", + "\n", + "multiply(10,5)\n", + "multiply(50,3)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "98" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def subtract(number1, number2):\n", + " answer=number1-number2\n", + " return answer\n", + "\n", + "subtract(50,20)\n", + "subtract(80,-30)\n", + "subtract(99,1)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def floorDivide(number1, number2):\n", + " answer=number1//number2\n", + " return answer\n", + "\n", + "floorDivide(10,2)\n", + "floorDivide(5,2)\n", + "floorDivide(30,15)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5.75" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def divide(number1, number2):\n", + " answer=number1/number2\n", + " return answer\n", + "\n", + "divide(50,2)\n", + "divide(70,7)\n", + "divide(23,4)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def getRemainder(number1, number2):\n", + " answer=number1%number2\n", + " return answer\n", + "\n", + "getRemainder(10,8)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "64" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def power(number1, number2):\n", + " answer=number1**number2\n", + " return answer\n", + "\n", + "power(7,2)\n", + "power(3,3)\n", + "power(2,6)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -78,10 +258,8 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 8, + "metadata": {}, "outputs": [], "source": [ "#this function adds two numbers passed as parameters\n", @@ -101,36 +279,80 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "add(4,5)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "-11" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "add(-6,-5)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "14.6" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "add(5.6, 9)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'helloworld'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "add (\"hello\", \"world\") # ooops this is weird!!" ] @@ -169,19 +391,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Houston we've got a problem\n" + ] + } + ], "source": [ - "assert 2 + 2 == 5, \"Houston we've got a problem\" #This will give an AssertionError\n" + "assert 2 + 2 == 4\n", + "print(\"Houston we've got a problem\") #This will give an AssertionError" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 14, + "metadata": {}, "outputs": [], "source": [ "assert 2 + 2 == 4, \"Houston we've got a problem\" #this won't give an error!" @@ -208,23 +437,71 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "64" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# subtract function \n", + "def subtract(number1, number2):\n", + " answer=number1-number2\n", + " return answer\n", + "\n", + "subtract(50,20)\n", + "subtract(80,-30)\n", + "subtract(99,1)\n", "\n", "# floorDivide function\n", + "def floorDivide(number1, number2):\n", + " answer=number1//number2\n", + " return answer\n", + "\n", + "floorDivide(10,2)\n", + "floorDivide(5,2)\n", + "floorDivide(30,15)\n", "\n", "# divide function\n", + "def divide(number1, number2):\n", + " answer=number1/number2\n", + " return answer\n", + "\n", + "divide(50,2)\n", + "divide(70,7)\n", + "divide(23,4)\n", "\n", "# multiply function\n", + "def multiply(number1, number2):\n", + " answer=number1*number2\n", + " return answer\n", + "\n", + "multiply(10,5)\n", + "multiply(50,3)\n", "\n", "# getRemainder function\n", + "def getRemainder(number1, number2):\n", + " answer=number1%number2\n", + " return answer\n", + "\n", + "getRemainder(10,8)\n", "\n", - "# power function" + "# power function\n", + "def power(number1, number2):\n", + " answer=number1**number2\n", + " return answer\n", + "\n", + "power(7,2)\n", + "power(3,3)\n", + "power(2,6)\n" ] }, { @@ -238,10 +515,8 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 16, + "metadata": {}, "outputs": [], "source": [ "assert add(4,5)==9, \"add function not working\"" @@ -249,10 +524,8 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 17, + "metadata": {}, "outputs": [], "source": [ "assert subtract(4,5)==-1, \"subtract function not working\"" @@ -260,10 +533,8 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 18, + "metadata": {}, "outputs": [], "source": [ "assert multiply(4,5)==20, \"multiply function not working\"" @@ -271,10 +542,8 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 19, + "metadata": {}, "outputs": [], "source": [ "assert divide(5,5)==1.0, \"divide function not working\"" @@ -282,21 +551,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 20, + "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 - }, + "execution_count": 21, + "metadata": {}, "outputs": [], "source": [ "assert getRemainder(5,4)==1, \"getRemainder function not working\"" @@ -304,10 +569,8 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 22, + "metadata": {}, "outputs": [], "source": [ "assert power(3,2)==9, \"power function not working\""