Skip to content

Instantly share code, notes, and snippets.

@mde-2590
Created April 9, 2019 10:10
Show Gist options
  • Save mde-2590/9a096e38c6df2ef1ade605b1b9662e3b to your computer and use it in GitHub Desktop.
Save mde-2590/9a096e38c6df2ef1ade605b1b9662e3b to your computer and use it in GitHub Desktop.
Created on Cognitive Class Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 if you add one 2\n"
]
},
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# First function example: Add 1 to a and store as b\n",
"\n",
"def add(a):\n",
" b = a + 1\n",
" print(a, \"if you add one\", b)\n",
" return(b)\n",
"\n",
"add(1)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function add in module __main__:\n",
"\n",
"add(a)\n",
"\n"
]
}
],
"source": [
"# Get a help on add function\n",
"\n",
"help(add)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 if you add one 2\n"
]
},
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Call the function add()\n",
"\n",
"add(1)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 if you add one 3\n"
]
},
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Call the function add()\n",
"\n",
"add(2)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# Define a function for multiple two numbers\n",
"\n",
"def Mult(a, b):\n",
" c = a * b\n",
" return(c)\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Use mult() multiply two integers\n",
"\n",
"Mult(2, 3)\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"31.400000000000002"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Use mult() multiply two floats\n",
"\n",
"Mult(10.0, 3.14)\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Michael Jackson Michael Jackson '"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Use mult() multiply two different type values together\n",
"\n",
"Mult(2, \"Michael Jackson \")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 if you square + 1 5\n"
]
}
],
"source": [
"# Function Definition\n",
"\n",
"def square(a):\n",
" \n",
" # Local variable b\n",
" b = 1\n",
" c = a * a + b\n",
" print(a, \"if you square + 1\", c) \n",
" return(c)\n",
"\n",
"x = 2\n",
"z = square(2)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3 if you square + 1 10\n"
]
},
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Initializes Global variable \n",
"\n",
"x = 3\n",
"# Makes function call and return function a y\n",
"y = square(x)\n",
"y\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 if you square + 1 5\n"
]
},
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Directly enter a number as parameter\n",
"\n",
"square(2)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"# Define functions, one with return value None and other without return value\n",
"\n",
"def MJ():\n",
" print('Michael Jackson')\n",
" \n",
"def MJ1():\n",
" print('Michael Jackson')\n",
" return(None)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Michael Jackson\n"
]
}
],
"source": [
"# See the output\n",
"\n",
"MJ()"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Michael Jackson\n"
]
}
],
"source": [
"# See the output\n",
"\n",
"MJ1()"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Michael Jackson\n",
"None\n",
"Michael Jackson\n",
"None\n"
]
}
],
"source": [
"# See what functions returns are\n",
"\n",
"print(MJ())\n",
"print(MJ1())"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"# Define the function for combining strings\n",
"\n",
"def con(a, b):\n",
" return(a + b)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'This is'"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Test on the con() function\n",
"\n",
"con(\"This \", \"is\")"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# a and b calculation block1\n",
"\n",
"a1 = 4\n",
"b1 = 5\n",
"c1 = a1 + b1 + 2 * a1 * b1 - 1\n",
"if(c1 < 0):\n",
" c1 = 0 \n",
"else:\n",
" c1 = 5\n",
"c1"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# a and b calculation block2\n",
"\n",
"a2 = 0\n",
"b2 = 0\n",
"c2 = a2 + b2 + 2 * a2 * b2 - 1\n",
"if(c2 < 0):\n",
" c2 = 0 \n",
"else:\n",
" c2 = 5\n",
"c2 "
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"# Make a Function for the calculation above\n",
"\n",
"def Equation(a,b):\n",
" c = a1 + b1 + 2 * a2 * b2 - 1\n",
" if(c < 0):\n",
" c = 0 \n",
" else:\n",
" c = 5\n",
" return(c) "
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a1 = 4\n",
"b1 = 5\n",
"c1 = Equation(a1, b1)\n",
"c1"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a2 = 0\n",
"b2 = 0\n",
"c2 = Equation(a2, b2)\n",
"c2"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[10.0, 8.5, 9.5, 7.0, 7.0, 9.5, 9.0, 9.5]\n"
]
}
],
"source": [
"# Build-in function print()\n",
"\n",
"album_ratings = [10.0, 8.5, 9.5, 7.0, 7.0, 9.5, 9.0, 9.5] \n",
"print(album_ratings)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"70.0"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Use sum() to add every element in a list or tuple together\n",
"\n",
"sum(album_ratings)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"8"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Show the length of the list or tuple\n",
"\n",
"len(album_ratings)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Michael Jackson Thriller 1980\n",
"Oldie\n"
]
}
],
"source": [
"# Function example\n",
"\n",
"def type_of_album(artist, album, year_released):\n",
" \n",
" print(artist, album, year_released)\n",
" if year_released > 1980:\n",
" return \"Modern\"\n",
" else:\n",
" return \"Oldie\"\n",
" \n",
"x = type_of_album(\"Michael Jackson\", \"Thriller\", 1980)\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"# Print the list using for loop\n",
"\n",
"def PrintList(the_list):\n",
" for element in the_list:\n",
" print(element)"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"1\n",
"the man\n",
"abc\n"
]
}
],
"source": [
"# Implement the printlist function\n",
"\n",
"PrintList(['1', 1, 'the man', \"abc\"])"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"# Example for setting param with default value\n",
"\n",
"def isGoodRating(rating=4): \n",
" if(rating < 7):\n",
" print(\"this album sucks it's rating is\",rating)\n",
" \n",
" else:\n",
" print(\"this album is good its rating is\",rating)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"this album sucks it's rating is 4\n",
"this album is good its rating is 10\n"
]
}
],
"source": [
"# Test the value with default value and with input\n",
"\n",
"isGoodRating()\n",
"isGoodRating(10)"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Michael Jackson is an artist\n"
]
}
],
"source": [
"# Example of global variable\n",
"\n",
"artist = \"Michael Jackson\"\n",
"def printer1(artist):\n",
" internal_var = artist\n",
" print(artist, \"is an artist\")\n",
" \n",
"printer1(artist)"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'internal_var' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-45-0a3947ae8535>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0martist\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"is an artist\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0mprinter1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minternal_var\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'internal_var' is not defined"
]
}
],
"source": [
"# Example of global variable\n",
"\n",
"artist = \"Michael Jackson\"\n",
"def printer1(artist):\n",
" internal_var = artist\n",
" print(artist, \"is an artist\")\n",
" \n",
"printer1(internal_var)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Michael Jackson is an artist\n",
"Whitney Houston is an artist\n"
]
}
],
"source": [
"#create global variables from within a function as follows:\n",
"\n",
"artist = \"Michael Jackson\"\n",
"\n",
"def printer(artist):\n",
" global internal_var \n",
" internal_var= \"Whitney Houston\"\n",
" print(artist,\"is an artist\")\n",
"\n",
"printer(artist) \n",
"printer(internal_var)"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"AC/DC's rating is: 10.0\n",
"Deep Purple's rating is: 0.0\n",
"My favourite band is: AC/DC\n"
]
}
],
"source": [
"# Example of global variable\n",
"\n",
"myFavouriteBand = \"AC/DC\"\n",
"\n",
"def getBandRating(bandname):\n",
" if bandname == myFavouriteBand:\n",
" return 10.0\n",
" else:\n",
" return 0.0\n",
"\n",
"print(\"AC/DC's rating is:\", getBandRating(\"AC/DC\"))\n",
"print(\"Deep Purple's rating is:\",getBandRating(\"Deep Purple\"))\n",
"print(\"My favourite band is:\", myFavouriteBand)"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"AC/DC's rating is: 10.0\n",
"Deep Purple's rating is: 0.0\n",
"My favourite band is AC/DC\n"
]
}
],
"source": [
"# Example of local variable\n",
"\n",
"def getBandRating(bandname):\n",
" myFavouriteBand = \"AC/DC\"\n",
" if bandname == myFavouriteBand:\n",
" return 10.0\n",
" else:\n",
" return 0.0\n",
"\n",
"print(\"AC/DC's rating is: \", getBandRating(\"AC/DC\"))\n",
"print(\"Deep Purple's rating is: \", getBandRating(\"Deep Purple\"))\n",
"print(\"My favourite band is\", myFavouriteBand)"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"AC/DC's rating is: 0.0\n",
"Deep Purple's rating is: 10.0\n",
"My favourite band is: AC/DC\n"
]
}
],
"source": [
"# Example of global variable and local variable with the same name\n",
"\n",
"myFavouriteBand = \"AC/DC\"\n",
"\n",
"def getBandRating(bandname):\n",
" myFavouriteBand = \"Deep Purple\"\n",
" if bandname == myFavouriteBand:\n",
" return 10.0\n",
" else:\n",
" return 0.0\n",
"\n",
"print(\"AC/DC's rating is:\",getBandRating(\"AC/DC\"))\n",
"print(\"Deep Purple's rating is: \",getBandRating(\"Deep Purple\"))\n",
"print(\"My favourite band is:\",myFavouriteBand)"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [],
"source": [
"#Come up with a function that divides the first input by the second input:\n",
"\n",
"def div(a, b):\n",
" return(a/b)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [],
"source": [
"#Use the function con for the following question.\n",
"\n",
"def con(a, b):\n",
" return(a + b)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment