{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Kontrollstrukturen und Vergleichsoperatoren" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Verzweigung (Branching)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# if / else / elif\n", "\n", "alter = -1\n", "\n", "if alter >= 16:\n", " print(\"Nicht vergessen: am 9.6.2024 ist Europa-Wahl!\")\n", "elif alter > 0:\n", " print(\"Schön, dass du geboren bist!\")\n", "else:\n", " print(\"Hmm, ob das so stimmt?\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "richtung = \"s\"\n", "\n", "if richtung == \"n\":\n", " print(\"Auf in den hohen Norden!\")\n", "elif richtung == \"s\":\n", " print(\"Nach Süden, nach Süden!\")\n", "elif richtung == \"w\":\n", " print(\"Go west!\")\n", "elif richtung == \"o\":\n", " print(\"Bin schon da.\")\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# match\n", "\n", "# richtung = input(\"Wo soll's denn lang gehen? \")\n", "richtung = \"n\"\n", "\n", "match richtung:\n", " case \"n\" | \"norden\":\n", " print(\"Auf in den hohen Norden!\")\n", " case \"s\" | \"süden\":\n", " print(\"Nach Süden, nach Süden!\")\n", " case \"w\" | \"westen\":\n", " print(\"Go west!\")\n", " case \"o\" | \"osten\":\n", " print(\"Bin schon da.\")\n", " case _:\n", " print(\"Das Glück liegt manchmal abseits des direkten Wegs!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Schleifen" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# for\n", "\n", "for i in range(5):\n", " print(\"Schleifendurchlauf Nr. \", i)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "result = 0\n", "for i in range(1, 11):\n", " result += i\n", "print(\"Summe der Zahlen 1-10:\", result)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in (2, 4, 6, 8, 9, 10):\n", " if i % 2:\n", " print(i, \"passt nicht in die Reihe\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# while\n", "\n", "i = 10\n", "while i > 0: # or: while True:\n", " print(i, \"... \", end=\"\")\n", " i -= 1\n", "print(\"Lift off!\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# continue\n", "\n", "for i in range(10, 20):\n", " if i == 13:\n", " continue\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# break\n", "\n", "i = 0\n", "result = 0\n", "maximum = 10_000\n", "\n", "while True:\n", " result += i\n", "\n", " if result >= maximum:\n", " print(\"Iteration: \", i+1)\n", " break\n", "\n", " i += 1\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "alphabet = \"abcdefghifklmnopqrstuvwxyz\"\n", "buchstabe = \"q\"\n", "i = 0\n", "for char in alphabet:\n", " i += 1\n", " if char == buchstabe:\n", " print(buchstabe, \"ist Buchstabe Nr.\", i, \"des dt. Alphabets.\")\n", " break\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# einfacher:\n", "\n", "for i, char in enumerate(alphabet):\n", " if char == buchstabe:\n", " print(buchstabe, \"ist Buchstabe Nr.\", i + 1, \"des dt. Alphabets.\")\n", " break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Funktionen" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Aufruf eingebauter Funktionen\n", "\n", "x = pow(2, 8)\n", "y = max(x, 100)\n", "z = min(max(0, x), 100)\n", "print(x, y, z)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# optionale Parameter\n", "\n", "print()\n", "print(\"\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# eigene Funktionen\n", "\n", "def add(a, b):\n", " return a + b\n", "\n", "print(add(2, 3))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Keyword arguments\n", "\n", "print(add(a=5, b=10))\n", "print(add(4, b=20))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Das geht nicht:\n", "print(add(a=6, 10))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Vergleichsausdrücke" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = 10\n", "b = 50\n", "c = 2\n", "d = 5 * a\n", "\n", "print(a == b)\n", "print(a != d)\n", "print(a > b)\n", "print(a > c)\n", "print(a >= c)\n", "print(c > a)\n", "print(c < b)\n", "print(c >= 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Negation\n", "\n", "print(not c > a)\n", "print(not a == b) # klarer: a != b\n", "print(not b != d) # klarer: b == d\n", "print(not a > c)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# logische Kombination\n", "\n", "print(b > a and a > c)\n", "print(a > b or a > c)\n", "print(not a < c and b >= d)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Bool'scher (True/False) Wert von Integers, Floats und Strings \n", "print(a > c)\n", "print(bool(a > c)) ## redundant, da Vergleichsausdrücke immer einen bool'schen Wert haben\n", "# Aber:\n", "print(bool(a))\n", "print(not a) # auch ein bool'scher Ausdruck\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(bool(0))\n", "print(bool(1))\n", "print(bool(-1))\n", "print(bool(0.0))\n", "print(bool(0.1))\n", "print(bool(-0.1))\n", "print(bool(\"True\"))\n", "print(bool(\"False\")) # !!!\n", "print(bool(\"\"))\n", "print(bool(\"\\0\"))\n" ] } ], "metadata": { "kernelspec": { "display_name": "softed", "language": "python", "name": "softed" }, "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.11.8" } }, "nbformat": 4, "nbformat_minor": 2 }