From ff914cdde12457e959a31339c80abe26e025a04b Mon Sep 17 00:00:00 2001 From: oliver-Waldron Date: Tue, 26 Sep 2017 13:27:55 +0000 Subject: [PATCH] Finnished it --- Functions.ipynb | 205 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 165 insertions(+), 40 deletions(-) diff --git a/Functions.ipynb b/Functions.ipynb index df58b4c..a9cf21f 100644 --- a/Functions.ipynb +++ b/Functions.ipynb @@ -4,6 +4,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "\n", "## Functions in Python\n", "\n", "Functions are \"self contained\" sections of code that accomplish a specific task. Its like a recipe. \n", @@ -78,10 +79,8 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 1, + "metadata": {}, "outputs": [], "source": [ "#this function adds two numbers passed as parameters\n", @@ -90,6 +89,122 @@ " return answer" ] }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def subtract (number1, number2):\n", + " answer = number1-number2\n", + " return answer\n", + "subtract(2,1)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def floorDivide (number1, number2):\n", + " answer = number1//number2\n", + " return answer\n", + "floorDivide(2,1)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2.0" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def divide (number1, number2):\n", + " answer = number1/number2\n", + " return answer\n", + "divide(2,1)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def multiply (number1, number2):\n", + " answer = number1*number2\n", + " return answer\n", + "multiply(2,1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def getRemainder (number1, number2):\n", + " answer = number1%number2\n", + " return answer\n", + "getRemainder(2,1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def power (number1, number2):\n", + " answer = number1**number2\n", + " return answer\n", + "power(2,1)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -132,7 +247,7 @@ "metadata": {}, "outputs": [], "source": [ - "add (\"hello\", \"world\") # ooops this is weird!!" + "add (\"hello \", \"world\") # ooops this is weird!!" ] }, { @@ -173,15 +288,13 @@ "metadata": {}, "outputs": [], "source": [ - "assert 2 + 2 == 5, \"Houston we've got a problem\" #This will give an AssertionError\n" + "assert 2 + 2 == 4, \"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!" @@ -208,10 +321,8 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 8, + "metadata": {}, "outputs": [], "source": [ "# subtract function \n", @@ -224,7 +335,35 @@ "\n", "# getRemainder function\n", "\n", - "# power function" + "# power function\n", + "def add (number1, number2):\n", + " answer = number1+number2\n", + " return answer\n", + "\n", + "def subtract (number1, number2):\n", + " answer = number1-number2\n", + " return answer\n", + "\n", + "def floorDivide (number1, number2):\n", + " answer = number1//number2\n", + " return answer\n", + "\n", + "def divide (number1, number2):\n", + " answer = number1/number2\n", + " return answer\n", + "\n", + "def multiply (number1, number2):\n", + " answer = number1*number2\n", + " return answer\n", + "\n", + "def getRemainder (number1, number2):\n", + " answer = number1%number2\n", + " return answer\n", + "\n", + "\n", + "def power (number1, number2):\n", + " answer = number1**number2\n", + " return answer" ] }, { @@ -239,9 +378,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "assert add(4,5)==9, \"add function not working\"" @@ -250,53 +387,43 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ - "assert subtract(4,5)==-1, \"subtract function not working\"" + "assert subtract(10,5)==5, \"subtract function not working\"" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ - "assert multiply(4,5)==20, \"multiply function not working\"" + "assert multiply(5,5)==25, \"multiply function not working\"" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 13, + "metadata": {}, "outputs": [], "source": [ - "assert divide(5,5)==1.0, \"divide function not working\"" + "assert divide(5,2)==2.5, \"divide function not working\"" ] }, { "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 +432,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "assert power(3,2)==9, \"power function not working\""