{ "cells": [ { "cell_type": "markdown", "metadata": { "tags": [ "remove_cell" ] }, "source": [ "
\n", "
\n", "

Ingenieurinformatik – Übung

\n", " Lehrstuhl Computational Civil Engineering
\n", " Kontakt: Email senden | Individuelle Kontakte siehe Webseite des Lehrstuhls
\n", " Links: \n", " Vorlesungsskript | \n", " Webseite des Lehrstuhls\n", "
\n", "
\n", " \n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Operationen" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Operationen sind die Grundstruktur eines Python-Programms. In dieser Aufgabe werden die vorgestellten Operationen vertieft." ] }, { "cell_type": "markdown", "metadata": { "tags": [ "remove_cell" ] }, "source": [ "## Grundlagen und Beispiele" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Aufgabenteil A" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Berechnen Sie folgende Ausdrücke in Python. Prüfen Sie manuell die Richtigkeit der Ergebnisse.\n", "\n", "1. Verhältnis von 7 zu 5.\n", "1. Ganzzahlige Division von -9 und 4 und der Rest dieser.\n", "1. $\\sf 1 + 2^6/5$\n", "1. $\\sf \\frac{2\\cdot 4}{4^4}$\n", "1. Ist der Rest der Division $\\sf 144/32$ größer als 16?\n", "1. Stimmt es, dass 7 größer ist als 5 und auch 8 kleiner als 12?" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "loesung", "hide-cell" ] }, "source": [ "### Lösungsvorschlag" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [ "loesung", "hide-cell" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.4\n" ] } ], "source": [ "print( 7/5 )" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "tags": [ "loesung", "hide-cell" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-3\n", "3\n" ] } ], "source": [ "print( -9//4 )\n", "print( -9 % 4 )" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "tags": [ "loesung", "hide-cell" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "13.8\n" ] } ], "source": [ "print( 1 + 2**6/5 )" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "tags": [ "loesung", "hide-cell" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.03125\n" ] } ], "source": [ "print( (2*4) / 4**4 )" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "tags": [ "loesung", "hide-cell" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print( 144 % 32 > 16 )" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "tags": [ "loesung", "hide-cell" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print( 7> 5 and 8 < 12 )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Aufgabenteil B" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Welche der folgenden Kombinationen von Operanden und Operatoren sind in Python möglich?\n", "\n", "1. Liste, ganze Zahl, Multiplikation\n", "1. Liste, ganze Zahl, Addition\n", "1. Liste, Liste, Addition\n", "1. Liste, Gleitkommazahl, Addition" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "loesung", "hide-cell" ] }, "source": [ "### Lösungsvorschlag" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "loesung", "hide-cell", "remove_cell" ] }, "source": [ "Zu 1: Ja, das ist möglich, hierbei wird die Liste um den ganzahligen Faktor vervielfältigt." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "tags": [ "loesung", "hide-cell" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4, 5, 6, 4, 5, 6, 4, 5, 6]\n" ] } ], "source": [ "l1 = [4, 5, 6]\n", "n = 3\n", "print( l1 * n )" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "loesung", "hide-cell", "remove_cell" ] }, "source": [ "Zu 2: Nein, das ist nicht möglich. Eine Addition würde nur elementweise Sinn machen, was mit Listen aber nicht direkt möglich ist." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "tags": [ "loesung", "hide-cell" ] }, "outputs": [], "source": [ "# Zum ausprobieren, einfach folgende Zeile auskommentieren\n", "# print( l1 + n )\n" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "loesung", "hide-cell", "remove_cell" ] }, "source": [ "Zu 3: Ja, hierbei entsteht eine neue Liste, welche die Vereinigung der beiden Operanden enthält." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "tags": [ "loesung", "hide-cell" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4, 5, 6, 14, 15, 16]\n" ] } ], "source": [ "l2 = [14, 15, 16]\n", "print( l1 + l2 )" ] }, { "cell_type": "markdown", "metadata": { "tags": [ "loesung", "hide-cell", "remove_cell" ] }, "source": [ "Zu 4: Nein, genauso wie die Addition einer ganzzahligen Zahl nicht möglich ist." ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "tags": [ "loesung", "hide-cell" ] }, "outputs": [], "source": [ "# Zum ausprobieren, einfach folgende Zeilen auskommentieren\n", "# g = 4.7\n", "# print( l1 + g )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Aufgabenteil C\n", "\n", "Schreiben Sie ein Skript, welches das Quadrat des Abstandes und den Mittelpunkt zwischen zwei Punkten $\\sf (x_1, y_1) = (1, −7)$ und $\\sf (x_2, y_2) = (1, 5)$ berechnet. Die Ergebnisse sollen in einem Antwortsatz ausgegeben werden." ] }, { "cell_type": "markdown", "metadata": { "tags": [ "loesung", "hide-cell", "remove_cell" ] }, "source": [ "### Lösungsvorschlag" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "tags": [ "loesung", "hide-cell" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Punkt 1: ( 1 , -7 )\n", "Punkt 2: ( 1 , 5 )\n", "Das Abstandsquadrat ist: 144\n", "Die Koordinaten des Mittelpunkts sind: ( 1.0 , -1.0 )\n" ] } ], "source": [ "# Definition des ersten Punktes\n", "x1 = 1\n", "y1 = -7\n", "\n", "# Definition des zweiten Punktes\n", "x2 = 1\n", "y2 = 5\n", "\n", "# Berechne Distanzquadrat\n", "dist2 = (x1 - x2)**2 + (y1 - y2)**2\n", "\n", "# Berechne Koordinaten des Mittelpunkts\n", "mx = (x1 + x2) / 2\n", "my = (y1 + y2) / 2\n", "\n", "print(\"Punkt 1: (\", x1, \",\", y1, \")\")\n", "print(\"Punkt 2: (\", x2, \",\", y2, \")\")\n", "print(\"Das Abstandsquadrat ist: \", dist2)\n", "print(\"Die Koordinaten des Mittelpunkts sind: (\", mx, \",\", my, \")\")" ] } ], "metadata": { "celltoolbar": "Tags", "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.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }