Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 165 additions & 40 deletions Functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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": {},
Expand Down Expand Up @@ -132,7 +247,7 @@
"metadata": {},
"outputs": [],
"source": [
"add (\"hello\", \"world\") # ooops this is weird!!"
"add (\"hello \", \"world\") # ooops this is weird!!"
]
},
{
Expand Down Expand Up @@ -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!"
Expand All @@ -208,10 +321,8 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# subtract function \n",
Expand All @@ -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"
]
},
{
Expand All @@ -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\""
Expand All @@ -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\""
Expand All @@ -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\""
Expand Down