{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define a function that prints the value of the argument accompanied by the phrase \"raised to the power of 2:\" **and** also returns a value equal to its argument raised to the power of 2. This time, use a new variable, called result, in the body of the Function. \n",
"Call the function with some argument to verify it works properly.\n",
"\n",
"*Example: if argument is 7, it prints \"7 raised to the power of 2:\" and then it returns 49*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define a function **says** that takes as parameter a string **S** and returns your name, followed by a colon and a space and the input string **S** enclosed by double quotations. \n",
"
Example: says('Hallo') returns
Paolo: \"Hallo\"
written in this way\n",
"\n",
"Test function **says** with three different input strings, one of them with an apostrophe and one of them with a double quotation inside."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define a function **F** with three parameters which tells the user the values of the parameters and then returns a (anyone, but please not all sums or all multiplications!) mathematical operation on the parameters."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Try function **F** with the parameters in order."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Try function **F** with the same parameters but not in order, giving obviously still the same result."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Consider the function **mystery** here below which uses the function **test** which returns a boolean and for which you do not have any idea of what does it do."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def mystery(a,b):\n",
" if test(a):\n",
" return b\n",
" return a"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Which of the following functions returns always the same as the function **mystery**, for every possible function test and every possible input (obviously same input for mystery and for m1 m2 m3 m4)? For each one write a comment convincing me of your answer."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def m1(x,y):\n",
" if test(x):\n",
" return y\n",
" else:\n",
" return x\n",
" \n",
"def m2(a,b):\n",
" if not test(b):\n",
" return a\n",
" else:\n",
" return b\n",
" \n",
"def m3(a,b):\n",
" result=a\n",
" if test(a):\n",
" result=b\n",
" return result\n",
"\n",
"def m4(a,b):\n",
" if not test(a):\n",
" b=\"Paolo\"\n",
" else:\n",
" return b\n",
" return a"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"m1:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"m2:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"m3:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"m4:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create function **category** that receives a number x and prints\n",
"
\"Big\" when x is larger than 200; \n",
"
\"Small\" when x is smaller than 100;\n",
"
\"Average\" in the other cases. \n",
"
Build it first without using Elif and then using Elif.\n",
"
Test it!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define function **median** which takes as parameters three numbers and returns the median, i.e. the number in the middle according to numerical order. You may use other functions we built in class, but in this case write them here. You may not use min and max functions.\n",
"
Examples: median(1,2,3) returns 2, median(9,3,6) returns 6, median(7,8,7) returns 7.\n",
"\n",
"Hint: There are a lot of different solutions to this problem. Think how would you do it manually slowly step by step if your brain were able to analyse only two numbers at the same time (and not three, as human brain is able to do). Do it on paper slowly with a practical example, do not try to write the code if you are not able to do it on paper!\n",
"\n",
"Test the function with the three examples above."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define a function daysOfMonth that takes as input parameter a number between 1 and 12, prints bad words if the number is not in this range and returns an int which is the number of days in that month.\n",
"
I hope that at elementary school you studied the rhyme \"30 days has September...\" https://youtu.be/BYshA2ybFdY"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define a function isLeapYear that takes as input parameter a year and returns a bool indicating whether that year is leap or not. \n",
"
In case you do not know, the rule for leap years is that a year is leap if it is divisible by 4 (such as 2016), with the exception of years which are divisible by 100 which are not leap (such as 1900) except when divisible by 400 which are leap (for example 2000).\n",
"
To check whether a number n is divisible by 4 the trick is n%4==0 (the operator % gives the remainder of the integer division)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Redefine a function daysOfMonth that takes as input parameter a number between 1 and 12 and a year, prints bad words if the number is not in this range and returns an int which is the number of days in that month. This time you have the year, so you can write correctly 28 or 29 depending on whether the year is leap. This function must use isLeapYear function, obviously."
]
},
{
"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
}