{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial A2 – Solutions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__1__ Consider the equation $y = \\frac{3a + 2b}{b^2}$, with $a = 1$ and $b = 2$." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - Calculate $y$ using Python." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.75\n" ] } ], "source": [ "a = 1\n", "b = 2\n", "y = (3*a + 2*b) / b**2\n", "print(y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - Of what type is the result?" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(y))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - How can you force the result to be a whole number?" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 \n" ] } ], "source": [ "# Use integer conversion (flooring)\n", "y_ = int(y)\n", "print(y_, type(y_))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 \n" ] } ], "source": [ "# Use round() (closest integer)\n", "y_ = round(y)\n", "print(y_, type(y_))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - Of what type is the result if $b = 1$?" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5.0 \n" ] } ], "source": [ "a = 1\n", "b = 1\n", "y = (3*a + 2*b) / b**2\n", "print(y, type(y)) # Still a float because of division" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " - What happens if $b = 0$?" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "ename": "ZeroDivisionError", "evalue": "division by zero", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0my\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0ma\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0;36m2\u001b[0m \u001b[0;31m# Raises an error!\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0my\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0my\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" ] } ], "source": [ "a = 1\n", "b = 0\n", "y = (3*a + 2*b) / b**2 # Raises an error!\n", "print(y, type(y))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__2__ Calculate the gravitational force between the moon and the earth at a distance of $r = 3.84\\times 10^8$ m, with masses $m_\\mathrm{earth} = 5.97\\times 10^{24}$ kg, $m_\\mathrm{moon} = 7.35\\times 10^{22}$ kg. Use $G = 6.67 \\times 10^{-11}$ m$^3$ kg$^{-1}$ s$^{-2}$ for the gravitational constant." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Gravitational force: 1.9848379516601562e+20 N\n" ] } ], "source": [ "r = 3.84e8\n", "m_earth = 5.97e24\n", "m_moon = 7.35e22\n", "G = 6.67e-11\n", "\n", "F = (G * m_earth * m_moon) / r**2\n", "print(\"Gravitational force: \", F, \"N\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Advanced 1__ How does Python handle infinity and absent numeric values? What is\n", "the result of a multiplication of these values with 0?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "**Note:** Advanced exercises are not mandatory to get the points.\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nan\n" ] } ], "source": [ "print(float('nan') * 0)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nan\n" ] } ], "source": [ "print(float('inf') * 0)" ] } ], "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.6.10" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }