From bc3e6baf55dc26c5028f216fe4c7584b86dd33a8 Mon Sep 17 00:00:00 2001 From: klaidas-predeinas Date: Tue, 26 Sep 2017 13:21:33 +0000 Subject: [PATCH 1/2] functions task completed --- .ipynb_checkpoints/Functions-checkpoint.ipynb | 387 ++++++++++++++++++ Functions.ipynb | 163 +++++--- 2 files changed, 493 insertions(+), 57 deletions(-) create mode 100644 .ipynb_checkpoints/Functions-checkpoint.ipynb diff --git a/.ipynb_checkpoints/Functions-checkpoint.ipynb b/.ipynb_checkpoints/Functions-checkpoint.ipynb new file mode 100644 index 0000000..1ceb04f --- /dev/null +++ b/.ipynb_checkpoints/Functions-checkpoint.ipynb @@ -0,0 +1,387 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Functions in Python\n", + "\n", + "Functions are \"self contained\" sections of code that accomplish a specific task. Its like a recipe. \n", + "\n", + "In python the keyword that starts a function is\n", + "\n", + "```python \n", + " def function_name(dataIn):\n", + "```\n", + "\n", + "Functions usually \"take in\" data, process it, and \"return\" a result. In python this looks like:\n", + "```python\n", + " def function_name(dataIn):\n", + " \n", + " #do stuff with the data to get a result\n", + " \n", + " return result\n", + "```\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Once a function is written, it can be used over and over and over again. \n", + "\n", + "Functions must be \"called\" with actual data in order to be executed. Back to the recipe analogy - a recipe is not the cake, you actually have to follow the recipe with actual ingredients to end up with a resulting cake!\n", + "\n", + "```python\n", + "result = function_name(3) # the data taken in is the number 3\n", + "answer = function_name(78) # the data taken in is the number 78\n", + "start=77\n", + "final = function_name(start) # the data taken in is the value of the variable start \n", + "```\n", + "\n", + "[Read more about functions here](http://www.cs.utah.edu/~germain/PPS/Topics/functions.html)\n", + "\n", + "remember to make you own notes as you learn new things." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Task - write a library of maths functions\n", + "\n", + "1. add - takes 2 parameters: the two numbers to add, returns the sum\n", + "\n", + "2. subtract - takes 2 parameters: the two numbers to subtract, returns the difference\n", + "\n", + "3. floorDivide - takes 2 parameters: the two numbers to divide, returns the whole quotient\n", + "\n", + "4. divide - takes 2 parameters: the two numbers to divide, returns the quotient (as a decimal)\n", + "\n", + "4. multiply - takes 2 parameters: the two numbers to multiply, returns the product\n", + "\n", + "5. getRemainder - takes 2 parameters: the two numbers to divide, returns the remainder\n", + "\n", + "6. power takes 2 parameters: the two numbers one is the base the other the power, returns base ^ power\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## function definitions\n", + "\n", + "In the next cell we are defining the function and the input values (called parameters). These input values are simply placeholders for actual values that will be used when calling the function. In the cake analogy, the egg in a recipe does not exisit until you actually make a cake. Then you need to use a real egg." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "#this function adds two numbers passed as parameters\n", + "def add (number1, number2):\n", + " answer = number1+number2\n", + " return answer" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## calling functions\n", + "\n", + "In the cells below we are CALLING the function with actual values (ARGUMENTS)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "add(4,5)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-11" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "add(-6,-5)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "14.6" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "add(5.6, 9)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'helloworld'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "add (\"hello\", \"world\") # ooops this is weird!!" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Testing code with asserts\n", + "\n", + "The assert statement exists in almost every programming language. When you do...\n", + "\n", + "\n", + "```python\n", + "assert \n", + "```\n", + "\n", + "you're telling the program to test that condition, and trigger an error if the condition is false.\n", + "\n", + "Here is a simple example;\n", + "```python\n", + "assert 2 + 2 == 5, \"Houston we've got a problem\"\n", + "```\n", + "\n", + "Since 2+2 doed **NOT** equal 5, this code will print out the message \"Houston we've got a problem\" - you can test it out below\n", + "\n", + "[Much more on testing here](http://docs.python-guide.org/en/latest/writing/tests/)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## test the assert code below and fix it so that it doesn't throw an AssertionError" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "ename": "AssertionError", + "evalue": "2+2 does not equal 5", + "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[0;36m2\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m2\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"2+2 does not equal 5\"\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: 2+2 does not equal 5" + ] + } + ], + "source": [ + "assert 2 + 2 == 5, \"2+2 does not equal 5\" #This will give an AssertionError\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "assert 2 + 2 == 4, \"Houston we've got a problem\" #this won't give an error!" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Task: Write the function definitions \n", + "\n", + "subtract - takes 2 parameters: the two numbers to subtract, returns the difference\n", + "\n", + "floorDivide - takes 2 parameters: the two numbers to divide, returns the whole quotient\n", + "\n", + "divide - takes 2 parameters: the two numbers to divide, returns the quotient (as a decimal)\n", + "\n", + "multiply - takes 2 parameters: the two numbers to multiply, returns the product\n", + "\n", + "getRemainder - takes 2 parameters: the two numbers to divide, returns the remainder\n", + "\n", + "power - takes 2 parameters: the two numbers one is the base the other the power, returns base ^ power\n" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [], + "source": [ + "# subtract function \n", + "def subtract (num1, num2):\n", + " total = num1 - num2\n", + " return total\n", + "# floorDivide function\n", + "def floorDivide (num1, num2):\n", + " total = num1 // num2\n", + " return total\n", + "# divide function\n", + "def divide (num1, num2):\n", + " total = num1 / num2\n", + " return total\n", + "# multiply function\n", + "def multiply (num1, num2):\n", + " total = num1 * num2\n", + " return total\n", + "# getRemainder function\n", + "def getRemainder (num1, num2):\n", + " total = num1 % num2\n", + " return total\n", + "# power function\n", + "def power (num1, num2):\n", + " total = num1 ** num2\n", + " return total" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Task: Test the code you have written using the following asserts\n", + "\n", + "To test your code with all these asserts go to **Kernel** (on the menu) and choose **restart and run all**" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "assert add(4,5)==9, \"add function not working\"" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "assert subtract(4,5)==-1, \"subtract function not working\"" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "assert multiply(4,5)==20, \"multiply function not working\"" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "assert divide(5,5)==1.0, \"divide function not working\"" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [], + "source": [ + "assert floorDivide(1,2)==0, \"floor divide function not working\"" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "assert getRemainder(5,4)==1, \"getRemainder function not working\"" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [], + "source": [ + "assert power(3,2)==9, \"power function not working\"" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.5.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Functions.ipynb b/Functions.ipynb index df58b4c..1ceb04f 100644 --- a/Functions.ipynb +++ b/Functions.ipynb @@ -78,10 +78,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", @@ -101,36 +99,80 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "add(4,5)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "-11" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "add(-6,-5)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "14.6" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "add(5.6, 9)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'helloworld'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "add (\"hello\", \"world\") # ooops this is weird!!" ] @@ -169,19 +211,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "AssertionError", + "evalue": "2+2 does not equal 5", + "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[0;36m2\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m2\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"2+2 does not equal 5\"\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: 2+2 does not equal 5" + ] + } + ], "source": [ - "assert 2 + 2 == 5, \"Houston we've got a problem\" #This will give an AssertionError\n" + "assert 2 + 2 == 5, \"2+2 does not equal 5\" #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,23 +260,34 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 33, + "metadata": {}, "outputs": [], "source": [ "# subtract function \n", - "\n", + "def subtract (num1, num2):\n", + " total = num1 - num2\n", + " return total\n", "# floorDivide function\n", - "\n", + "def floorDivide (num1, num2):\n", + " total = num1 // num2\n", + " return total\n", "# divide function\n", - "\n", + "def divide (num1, num2):\n", + " total = num1 / num2\n", + " return total\n", "# multiply function\n", - "\n", + "def multiply (num1, num2):\n", + " total = num1 * num2\n", + " return total\n", "# getRemainder function\n", - "\n", - "# power function" + "def getRemainder (num1, num2):\n", + " total = num1 % num2\n", + " return total\n", + "# power function\n", + "def power (num1, num2):\n", + " total = num1 ** num2\n", + " return total" ] }, { @@ -238,10 +301,8 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 14, + "metadata": {}, "outputs": [], "source": [ "assert add(4,5)==9, \"add function not working\"" @@ -249,10 +310,8 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 16, + "metadata": {}, "outputs": [], "source": [ "assert subtract(4,5)==-1, \"subtract function not working\"" @@ -260,10 +319,8 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 17, + "metadata": {}, "outputs": [], "source": [ "assert multiply(4,5)==20, \"multiply function not working\"" @@ -271,10 +328,8 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 18, + "metadata": {}, "outputs": [], "source": [ "assert divide(5,5)==1.0, \"divide function not working\"" @@ -282,21 +337,17 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, + "execution_count": 34, + "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 +355,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\"" From bd9593d2d29e3d6abd455ca7084f03cf212baac4 Mon Sep 17 00:00:00 2001 From: klaidas-predeinas Date: Tue, 26 Sep 2017 13:29:04 +0000 Subject: [PATCH 2/2] Finished --- .ipynb_checkpoints/Functions-checkpoint.ipynb | 18 +++--------------- Functions.ipynb | 18 +++--------------- 2 files changed, 6 insertions(+), 30 deletions(-) diff --git a/.ipynb_checkpoints/Functions-checkpoint.ipynb b/.ipynb_checkpoints/Functions-checkpoint.ipynb index 1ceb04f..12dc1f4 100644 --- a/.ipynb_checkpoints/Functions-checkpoint.ipynb +++ b/.ipynb_checkpoints/Functions-checkpoint.ipynb @@ -211,23 +211,11 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 1, "metadata": {}, - "outputs": [ - { - "ename": "AssertionError", - "evalue": "2+2 does not equal 5", - "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[0;36m2\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m2\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"2+2 does not equal 5\"\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: 2+2 does not equal 5" - ] - } - ], + "outputs": [], "source": [ - "assert 2 + 2 == 5, \"2+2 does not equal 5\" #This will give an AssertionError\n" + "#assert 2 + 2 == 5, \"2+2 does not equal 5\" #This will give an AssertionError\n" ] }, { diff --git a/Functions.ipynb b/Functions.ipynb index 1ceb04f..12dc1f4 100644 --- a/Functions.ipynb +++ b/Functions.ipynb @@ -211,23 +211,11 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 1, "metadata": {}, - "outputs": [ - { - "ename": "AssertionError", - "evalue": "2+2 does not equal 5", - "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[0;36m2\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m2\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"2+2 does not equal 5\"\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: 2+2 does not equal 5" - ] - } - ], + "outputs": [], "source": [ - "assert 2 + 2 == 5, \"2+2 does not equal 5\" #This will give an AssertionError\n" + "#assert 2 + 2 == 5, \"2+2 does not equal 5\" #This will give an AssertionError\n" ] }, {