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) 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": [ "Define a function, called **compare_the_two()**, with two arguments. If the first one is greater than the second one, let it print \"Greater\". If the second one is greater, it should print \"Less\". Let it print \"Equal\" if the two values are the same number." ] }, { "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

\"Big\" for x > 200; \n", "

\"Average\" for x > 100 and x <= 200; \n", "

\"Small\" for x <= 100.\n", "

Use the If, Elif, and Else keywords in your code.\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.\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

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

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