{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Functions - Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 4.1.\n", "\n", "In a given university course, the final grade is determined by grade for essay and by the grade for a presentation. The presentation counts for 30% and the essay for 70%. Write two functions: (1) one function which can calculate the final grade based on a set of partial grades. Grades must be rounded to integers. 5.4, for example, becomes 5 and 6.6 becomes 7. (2) Write a second function which can determine whether a given grade is at a pass level (i.e. higher than 6). This function must return a boolean value, in which 'pass' is true and 'fail' equals false. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def calculateMark( essay, presentation ):\n", " partial1 = essay * 0.7\n", " partial2 = presentation * 0.3\n", " final = partial1 + partial2\n", " return round(final)\n", "\n", "def isPass(grade):\n", " eval = 'Fail'\n", " if grade > 6:\n", " eval = 'Pass'\n", " return eval\n", " \n", "\n", "essay = 7.0\n", "presentation = 8.5\n", "final = calculateMark(essay,presentation)\n", "print( \"final grade: {} ({})\".format( final , isPass(final) ) ) \n", "\n", "essay = 4.5\n", "presentation = 5.5\n", "final = calculateMark(essay,presentation)\n", "print( \"final grade: {} ({})\".format( final , isPass(final) ) ) \n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 4.2.\n", "\n", "Import the math library, as follows: \n", "\n", "```\n", "from math import *\n", "```\n", "\n", "This command simply imports all the available functions from the math library. Use the functions ‘log10()’, ‘pow()’, ‘sqrt()’ and ‘cos()’ to generate the following numbers: \n", "\n", "* The base-10 logarithm of 5.\n", "* 3 raised to the power of 4\n", "* The square root of 144\n", "* The cosine of 60 radians." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from math import *\n", "\n", "\n", "print( str( log10(20) ) )\n", "print( str( pow( 3 , 4 ) ) )\n", "print( str( sqrt( 144 ) ) )\n", "print( str( cos( 60 ) ) )\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 4.3.\n", "\n", "Write a function which can convert a given temperature in degrees Celcius into the equivalent in Fahrenheit. Use the following formula: F = 1.8 * C + 32.\n", "\n", "Once the function is ready, test it with a number of values. 20 degrees Celcius ought to be converted into 68 degrees Fahrenheit, and 37 degrees Celcius should equal 98.6 degrees Fahrenheit." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def celciusToFahrentheit( celcius ):\n", " fahrenheit = 1.8 * celcius + 32 \n", " return round( fahrenheit , 1 )\n", "\n", "print( '{} degrees Celcius is {} degrees in Fahrenheit'.format( 20 , celciusToFahrentheit(20) ))\n", "print( '{} degrees Celcius is {} degrees in Fahrenheit'.format( 37 , celciusToFahrentheit(37) )) \n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 4.4.\n", "\n", "Following Pythagoras' theorem (A2 + B2 = C2), calculate the length of the hypothenuse in a right trangle in which the other two sides have a length of 6 and 7. Make use of the math module. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from math import *\n", "\n", "A = 6\n", "B = 7\n", "\n", "C2 = pow( 6 , 2 ) + pow( 7 , 2 )\n", "C = sqrt( C2 )\n", "\n", "print(C)\n" ] } ], "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.7.2" } }, "nbformat": 4, "nbformat_minor": 2 }