{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Programmablauf \n", "## Wahrheitswerte und Bedingungen\n", "Der Interpreter arbeitet die Anweisungen im Quellcode eines Programmes Schritt für Schritt, d.h. Zeile für Zeile ab. Oft ist es jedoch nützlich, bestimmte Anweiseung nur unter bestimmten _Bedingungen_ auszuführen. \n", "In solchen _Bedingungen_ prüfen wir, ob etwa _wahr_ oder _falsch_ ist. Die Wahrheitswerte in Python heissen `True` und `False` ." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wahr = True\n", "wahr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Vergleiche\n", "Ob eine Bedingung zutrifft könnnen wir mit dem Operator `==` prüfen. Im Gegensatz zum Zuweisungsoperator `=` gibt stehen zwei Gleichheitszeichen direkt, d.h. ohne Leerzeichen hintereinader. Ein solcher Vergleich liefert immer einen der beiden Wahrheitswerte. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 + 1 == 2" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 + 1 == 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Die Ergebnisse einer solchen Prüfung können wir auch einer Variablen zuweisen. Diese Variable nimmt den den Datentypen `bool` an." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wahr = 1 + 1 == 2\n", "type(wahr)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "verrechnet = 1 * 2 == 5\n", "verrechnet" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Neben dem Vergleichsoperator `==` gibt es weiter Möglichkeiten Bedingungen zu prüfen:\n", "- `!=` prüft ob etwas *nicht* wahr ist.\n", "- `<` und `>` prüfen, ob zahlen größer oder kleiner sind\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bedingungen: Wenn-Dann Entscheidungen mit dem `if` Operator" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Entscheidungen können in der Form \"Wenn-Dann\" mit dem `if` Operator getroffen werden." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "richtig\n" ] } ], "source": [ "if 1 + 1 == 2 : print(\"richtig\")" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "falsch\n" ] } ], "source": [ "if 1 - 1 != 3 : print (\"falsch\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Verschiedene Optionen können in der Form \"Wenn-Dann-Andernfalls\" geprüft und entsprechend behandelt werden." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.6" } }, "nbformat": 4, "nbformat_minor": 4 }