{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# functions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is a function, which accepts arguments in input, uses them for executing some statements and returns a single value (int, float, string, ...). They are used whenever you want to repeat the very same sequence of statements but using different inputs."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"def \\(\\):\n",
"
\\ \\\n",
"
\\ return \\\n",
"\n",
"\\ -> any name starting with a letter and containing letters, numbers and underscore\n",
"
\\ -> a list of expressions separated by commas\n",
"
\\ -> the tab character or 4 spaces\n",
"
\\ -> a column of several statements, all of them must be preceded by a tab\n",
"\n",
"Usage:\n",
"
\\(\\)\n",
"
\\ -> list of several \\ separated by commas"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Suppose you need to convert Celsius degree to Farenheit...\n",
"\n",
"Do not worry, the formula is here: $F=C \\cdot 9/5 + 32$ "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Very important remarks on often forgotten things"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that:\n",
"- things between parentheses are called *parameters* when defining the function and *arguments* when using the function\n",
"- do not forget the colon\n",
"- all statements must be preceeded by a tab or 4 spaces. If you do not put it, they will belong to the ordinary program and not to the function\n",
"- there is almost no reason for writing something after the _return_ , so _return_ should be the last one.\n",
"- variables used inside the function have nothing to do with variables outside. For example, the variable f used inside the function is not the variable f outside. Try to print f outside and you'll see. This means that if a function does not return, it does nothing (except printing if it contains some _print_)\n",
"- it is not mandatory to store the value that you are going to return in a variable, you can directly return everything in one line. However, for long functions this can make your life easier\n",
"- the function defined by _def_ does not run by itself. It must be explicitly used by a program\n",
"- when using a function you pass as arguments < Expression > and thus you may pass values directly, variables or even complex calculations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define a function that accepts as parameters a string and a number and returns the string starting from the index defined by the number"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercises"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What does this function do?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def inc(N):\n",
" N=N+1\n",
" return N"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Which 4 values will this code print at the end of its execution?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"N=5\n",
"def inc(N):\n",
" N=N+1\n",
" return N\n",
"\n",
"print(inc(N))\n",
"print(N)\n",
"print(inc(N+8))\n",
"print(N)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What does this function and what does this piece of code do?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def summe(a,b):\n",
" summe=a+b\n",
" \n",
"print(summe(7,3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What does this piece of code print?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def summe(a,b):\n",
" a=a+b\n",
" return a\n",
" \n",
"print(summe(7,3))\n",
"print(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What does this piece of code print?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def summe(a,b):\n",
" c=a+b\n",
" return c\n",
" \n",
"print(summe(\"Learning \",\"Python\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define a function called **square** which accepts as parameter a number and returns its square. Use it to calculate the square of a number. Then use it twice to calculate the fourth power of a number."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9\n",
"81\n"
]
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Argument order"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Usually the function is called with the arguments in the same order as the parameters in the function definition. However, this is not necessary:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.75\n",
"3.75\n",
"3.75\n"
]
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Making decisions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Logical expressions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\\ \\ \\\n",
"\n",
"Comparison Operators are: \\< \\<= == != \\<> >= >"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Boolean variables (bool)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## If"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"if \\:\n",
"
\\ \\"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercises"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define a function **bigger** that takes as parameters two numbers and returns the greater of the two, not using function min nor max.\n",
"
Example: bigger(3,7) returns 7, bigger(3,1) returns 3, bigger(5,5) returns 5"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## If Else"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"if \\:\n",
"
\\ \\\n",
"
else:\n",
"
\\ \\"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Rewrite the **bigger** function using now else"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Rewrite the **bigger** function putting the result in a variable before returning it"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define function **isFriend** which takes as parameter a string and returns a boolean indicating if that string is the name of a friend. We assume that you are friend with all the people whose name starts with capital P and no one else. \n",
"
Example: isFriend(\"Paolo\") returns True, while isFriend(\"Michael\") returns False."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## And Or Not"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\\ and \\\n",
"
\\ or \\\n",
"
not \\"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Modify **isFriend** to return True also for people whose name starts with M. Use three different methods."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using function isFriend, write a program which starts with a string s containing a name and prints \"Hallo\" if somebody is your friend and \"blurp\" if he/she is not your friend."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercises"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"DIFFICULT\n",
"
Define a function **biggest** that takes as parameters three numbers and returns the biggest of the three without using the min nor the max functions."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Rewrite **biggest** using function **bigger** to make your life easier. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"VERY DIFFICULT\n",
"
Using **bigger**, rewrite **biggest** using a single line in the block!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Else if"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"if \\:\n",
"
\\ \\\n",
"
elif \\:\n",
"
\\ \\\n",
"
else:\n",
"
\\ \\"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}