diff --git a/Ovingsforelesninger/felles/lecture2_lf.ipynb b/Ovingsforelesninger/felles/lecture2_lf.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..96cdca4b9f6c791e1e82cca19dbc0c909aadd803 --- /dev/null +++ b/Ovingsforelesninger/felles/lecture2_lf.ipynb @@ -0,0 +1,320 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "kernelspec": { + "display_name": "Python 3.8.3 64-bit", + "language": "python", + "name": "python_defaultSpec_1599133712327" + }, + "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.8.3-final" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "ABwzbFFnFcxI", + "colab_type": "text" + }, + "source": [ + "# Øvingsforelesning 2 Parallell 3" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "n_CAYoW3FcxM", + "colab_type": "text" + }, + "source": [ + "## Oppgave 1: Repetisjon" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jY0bZrvfFcxN", + "colab_type": "text" + }, + "source": [ + "Hva blir resten når $2^7$ deles på $42$ \n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Y2LnvdZXFcxO", + "colab_type": "code", + "colab": {}, + "tags": [] + }, + "source": [ + "print(2**7%42)" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": "2\n" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "va7oTh_RFcxV", + "colab_type": "text" + }, + "source": [ + "## Oppgave 2: Input" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "JJysiWeNFcxW", + "colab_type": "text" + }, + "source": [ + "**Lag et program som gjør følgende:**\n", + "1. Tar inn to tall fra bruker\n", + "2. Multipliserer dem \n", + "3. Skriver ut (tall 1) \\* (tall 2) = (resultat)" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "E6Lw86E_FcxX", + "colab_type": "code", + "colab": {} + }, + "source": [ + "tall1 = int(input('Tall 1: '))\n", + "tall2 = int(input('Tall 2: '))\n", + "resultat = tall1 * tall2\n", + "print(tall1, '*', tall2, '=', resultat)\n" + ], + "execution_count": 22, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ef0TagFtFcxi", + "colab_type": "text" + }, + "source": [ + "## Oppgave 3: Innebygde funksjoner " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cDRLg335Fcxj", + "colab_type": "text" + }, + "source": [ + "Skriv et program som spør brukeren om to tall og printer absoluttverdien av differansen\n", + "<br>\n", + "\n", + "Hint: Bruk den innebygde funksjonen **abs()**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "8Q6Thg0NFcxk", + "colab_type": "code", + "colab": {} + }, + "source": [ + "tall1 = float(input('Tall 1: '))\n", + "tall2 = float(input('Tall 2: '))\n", + "print(abs(tall1-tall2))\n" + ], + "execution_count": 22, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Oppgave 4: Feilretting og variabeltyper" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Følgende program skal regne ut og printe summen av prisen på tre varer. Prisene er lagret som forskjellige variabeltyper. Undersøk feilmeldingene og rett koden slik at den skriver ut riktig totalsum." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "tags": [] + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": "Totalsummen for varene er 49.0\n" + } + ], + "source": [ + "banan_pris = 10.50\n", + "melk_pris = '23.50'\n", + "havregryn_pris = 15\n", + "\n", + "total_sum = banan_pris + float(melk_pris) + havregryn_pris\n", + "\n", + "print('Totalsummen for varene er ' + str(total_sum))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Klt9XugLFcxp", + "colab_type": "text" + }, + "source": [ + "## Oppgave 5: Moduler " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "qOrc_5xRFcxq", + "colab_type": "text" + }, + "source": [ + "Print pi med 10 desimaler <br>\n", + "\n", + "Hint: import math, math.pi, round()" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "mWSU4YS0Fcxr", + "colab_type": "code", + "colab": {}, + "tags": [] + }, + "source": [ + "import math\n", + "print(round(math.pi,10))" + ], + "execution_count": 18, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": "3.1415926536\n" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "iSUDNvFEFcxx", + "colab_type": "text" + }, + "source": [ + "## Oppgave 6: Bruk av variabler" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "VN3hLGTMFcxy", + "colab_type": "text" + }, + "source": [ + "1. Regn ut volumet av en kjegle med radius r = 3 og høyde = 7 <br>\n", + "$$ V = \\frac{\\pi r^2h}{3}$$\n", + "\n", + "Hint til variabler:\n", + "* pi = 3.14\n", + "* r = 3\n", + "* h = 7" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "uFf7GC9kFcx0", + "colab_type": "code", + "colab": {}, + "tags": [] + }, + "source": [ + "PI = 3.14\n", + "r = 3\n", + "h = 7\n", + "V = (PI*r**2*h)/3\n", + "print(V)" + ], + "execution_count": 19, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": "65.94000000000001\n" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0fvSuRO8Fcxb", + "colab_type": "text" + }, + "source": [ + "## Oppgave 7: Variabler og input " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "yaeIkT16Fcxc", + "colab_type": "text" + }, + "source": [ + "1. Endre kjegleprogrammet fra oppgave 6 til å ta inn radius og høyde fra brukeren\n", + "2. Verdiene skal være av typen float " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "HSlztl41Fcxd", + "colab_type": "code", + "colab": {} + }, + "source": [ + "PI = 3.14\n", + "r = float(input('Radius: '))\n", + "h = float(input('Høyde: '))\n", + "V = (PI*r**2*h)/3\n", + "print(V)" + ], + "execution_count": 21, + "outputs": [] + } + ] +} \ No newline at end of file