Skip to content

Instantly share code, notes, and snippets.

@GuillaumeRouja
Created January 17, 2019 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save GuillaumeRouja/a010c43dd90df53f4ced34f4f09ed091 to your computer and use it in GitHub Desktop.
Save GuillaumeRouja/a010c43dd90df53f4ced34f4f09ed091 to your computer and use it in GitHub Desktop.
Functions
Display the source blob
Display the rendered blob
Raw
{
"nbformat_minor": 1,
"cells": [
{
"execution_count": 5,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "1 if you add 1 2\n"
},
{
"execution_count": 5,
"metadata": {},
"data": {
"text/plain": "2"
},
"output_type": "execute_result"
}
],
"source": "# First function example: Add 1 to a and store as b\n\ndef add1(a):\n b=a+1\n print(a,\"if you add 1\",b)\n return b\n\na=1\nadd1(a)\n"
},
{
"execution_count": 6,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "Help on function add1 in module __main__:\n\nadd1(a)\n\n"
}
],
"source": "# Get a help on add function\n\nhelp(add1)"
},
{
"execution_count": 7,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 7,
"metadata": {},
"data": {
"text/plain": "6"
},
"output_type": "execute_result"
}
],
"source": "# Define a function to multiply number\n\ndef mult(a,b):\n c=a*b\n return c\na=2\nb=3\nmult(a,b)"
},
{
"execution_count": 10,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 10,
"metadata": {},
"data": {
"text/plain": "'Michael Jackson Michael Jackson '"
},
"output_type": "execute_result"
}
],
"source": "#Use mult() multiply two different type values together\n\nmult(2, \"Michael Jackson \")"
},
{
"execution_count": 13,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "2 if you square + 1 5\n"
},
{
"execution_count": 13,
"metadata": {},
"data": {
"text/plain": "5"
},
"output_type": "execute_result"
}
],
"source": "#Use of local variable + directly enter a parameter to call the function\n\ndef square(a):\n b=1\n c=a*a+b\n print(a, \"if you square + 1\", c) \n return(c)\n\nsquare(2)"
},
{
"execution_count": 17,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "Michael Jackson\nNone\nMichael Jackson\nNone\n"
}
],
"source": "# Define functions, one with return value None and other without return value -> those functions are equivalent\n\ndef MJ():\n print('Michael Jackson')\n \ndef MJ1():\n print('Michael Jackson')\n return(None)\n\nprint(MJ())\nprint(MJ1())"
},
{
"execution_count": 19,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 19,
"metadata": {},
"data": {
"text/plain": "'guillaume is the best'"
},
"output_type": "execute_result"
}
],
"source": "# Define the function for combining strings\n\ndef con(a, b):\n return(a + b)\n\ncon (\"guillaume\", \" is the best\")"
},
{
"execution_count": 2,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 2,
"metadata": {},
"data": {
"text/plain": "5"
},
"output_type": "execute_result"
}
],
"source": "# Make a Function for the calculation above\n\ndef Equation(a,b):\n c = a + b + 2 * a * b - 1\n if(c < 0):\n c = 0 \n else:\n c = 5\n return(c) \n\nEquation (1,2)"
},
{
"execution_count": 3,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "[10.0, 8.5, 9.5, 7.0, 7.0, 9.5, 9.0, 9.5]\n"
}
],
"source": "# Build-in function print()\n\nalbum_ratings = [10.0, 8.5, 9.5, 7.0, 7.0, 9.5, 9.0, 9.5] \nprint(album_ratings)"
},
{
"execution_count": 4,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 4,
"metadata": {},
"data": {
"text/plain": "70.0"
},
"output_type": "execute_result"
}
],
"source": "# Use sum() to add every element in a list or tuple together\n\nsum(album_ratings)"
},
{
"execution_count": 5,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 5,
"metadata": {},
"data": {
"text/plain": "8"
},
"output_type": "execute_result"
}
],
"source": "# Show the length of the list or tuple\n\nlen(album_ratings)"
},
{
"execution_count": 6,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "Michael Jackson Thriller 1980\nOldie\n"
}
],
"source": "# Function example\n\ndef 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 \nx = type_of_album(\"Michael Jackson\", \"Thriller\", 1980)\nprint(x)"
},
{
"execution_count": 10,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "orange\nblack\ngreen\n"
}
],
"source": "# Print the list using for loop\n\nthe_list=['orange','black','green']\n\ndef PrintList(the_list):\n for element in the_list:\n print(element)\n \nPrintList(the_list)"
},
{
"execution_count": 15,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "this album sucks it's rating is 4\nthis album is good its rating is 10\n"
}
],
"source": "# Example for setting param with default value\n\ndef 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)\n \nisGoodRating()\nisGoodRating(10)"
},
{
"execution_count": 16,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "Michael Jackson is an artist\n"
}
],
"source": "# Example of global variable\n\nartist = \"Michael Jackson\"\ndef printer1(artist):\n internal_var = artist\n print(artist, \"is an artist\")\n \nprinter1(artist)"
},
{
"execution_count": 17,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "Michael Jackson is an artist\nWhitney Houston is an artist\n"
}
],
"source": "# Converting a local variable in a global one so it can be used outside the function:\n\nartist = \"Michael Jackson\"\n\ndef printer(artist):\n global internal_var \n internal_var= \"Whitney Houston\"\n print(artist,\"is an artist\")\n\nprinter(artist) \nprinter(internal_var)"
},
{
"execution_count": 18,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "AC/DC's rating is: 10.0\nDeep Purple's rating is: 0.0\nMy favourite band is: AC/DC\n"
}
],
"source": "# Example of global variable\n\nmyFavouriteBand = \"AC/DC\"\n\ndef getBandRating(bandname):\n if bandname == myFavouriteBand:\n return 10.0\n else:\n return 0.0\n\nprint(\"AC/DC's rating is:\", getBandRating(\"AC/DC\"))\nprint(\"Deep Purple's rating is:\",getBandRating(\"Deep Purple\"))\nprint(\"My favourite band is:\", myFavouriteBand)"
},
{
"execution_count": 20,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "AC/DC's rating is: 0.0\nDeep Purple's rating is: 10.0\nMy favourite band is: AC/DC\n"
}
],
"source": "# Example of global variable and local variable with the same name\n\nmyFavouriteBand = \"AC/DC\"\n\ndef getBandRating(bandname):\n myFavouriteBand = \"Deep Purple\"\n if bandname == myFavouriteBand:\n return 10.0\n else:\n return 0.0\n\nprint(\"AC/DC's rating is:\",getBandRating(\"AC/DC\"))\nprint(\"Deep Purple's rating is: \",getBandRating(\"Deep Purple\"))\nprint(\"My favourite band is:\",myFavouriteBand)"
},
{
"execution_count": 22,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 22,
"metadata": {},
"data": {
"text/plain": "2.0"
},
"output_type": "execute_result"
}
],
"source": "# Come up with a function that divides the first input by the second input:\n\ndef divide(a,b):\n return(a/b)\ndivide (4,2)"
},
{
"execution_count": 26,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 26,
"metadata": {},
"data": {
"text/plain": "4"
},
"output_type": "execute_result"
}
],
"source": "# Use the defined 'con' function\n\ncon(2,2)\n"
},
{
"execution_count": 27,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 27,
"metadata": {},
"data": {
"text/plain": "'guillaume is the best'"
},
"output_type": "execute_result"
}
],
"source": "def con(a, b):\n return(a + b)\n\ncon('guillaume',' is the best')"
},
{
"execution_count": 37,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 37,
"metadata": {},
"data": {
"text/plain": "('hello', 1, '2', 2, 1)"
},
"output_type": "execute_result"
}
],
"source": "tuple1 =(\"hello\",1)\ntuple2=(\"2\",2,1)\ncon(tuple1,tuple2)\n"
},
{
"execution_count": 36,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 36,
"metadata": {},
"data": {
"text/plain": "['orange', 'black', 'white', 'fuschia']"
},
"output_type": "execute_result"
}
],
"source": "list1 =['orange','black','white']\nlist2 = ['fuschia']\ncon(list1,list2)"
},
{
"execution_count": null,
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": ""
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.5",
"name": "python3",
"language": "python"
},
"language_info": {
"mimetype": "text/x-python",
"nbconvert_exporter": "python",
"version": "3.5.5",
"name": "python",
"file_extension": ".py",
"pygments_lexer": "ipython3",
"codemirror_mode": {
"version": 3,
"name": "ipython"
}
}
},
"nbformat": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment