{ "cells": [ { "cell_type": "markdown", "id": "883f329a", "metadata": {}, "source": [ "# Vorkurs Tag 1\n", "\n", "Author: Sebastian Krause" ] }, { "cell_type": "markdown", "id": "a87ba957", "metadata": {}, "source": [ "Übersicht heutiges Programm:\n", "\n", "1. Variablen\n", "2. Datentypen\n", "3. Arithmetik\n", "4. Konsolen Ein-/Ausgaben \n", "5. Kommentare" ] }, { "cell_type": "markdown", "id": "513ea5a0", "metadata": {}, "source": [ "## 1. Variablen 📦\n", "\n", "- Variablen werden verwendet um Informationen zu speichern\n", "- In Python dynamische Typenbindung\n", "### Benennung\n", " - nur alphanumerische Zeichen verwenden: (A-Z), (a-z), (0-9)\n", " - einziges erlaubtes Sonderzeichen: Unterstrich \"_\"\n", " - erstes Zeichen keine Zahl\n", " - Konvention: Variablennamen klein schreiben\n" ] }, { "cell_type": "markdown", "id": "4faadb0e", "metadata": {}, "source": [ " Beispiele Variableninitialisierung:" ] }, { "cell_type": "code", "execution_count": 7, "id": "f6e3beda", "metadata": {}, "outputs": [], "source": [ "variablenname = ' Information \"ahsuidh\" '\n", "variable = 'test'\n", "zahl = 5\n", "text = \"Dies ist ein Text/String\"\n", "pi = 3.1415\n", "wahrheitswert = True" ] }, { "cell_type": "markdown", "id": "eed741ec", "metadata": {}, "source": [ "*Merke:* Variablenname links, Wert rechts" ] }, { "cell_type": "code", "execution_count": 8, "id": "e90c1763-dbbc-44b6-bbe9-01a42da179cc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Information \"ahsuidh\" \n" ] } ], "source": [ "print(variablenname)" ] }, { "cell_type": "markdown", "id": "9b6811c1", "metadata": {}, "source": [ "## 2. Datentypen 🔢\n", "\n", "Es gibt unterschiedliche Arten von Variablen. In Python muss der Datentyp nicht explizit angegeben werden, da dieser anhand des Wertes, der in die Variable gespeichert wird, automatisch zugewiesen wird" ] }, { "cell_type": "markdown", "id": "0ffe5b44", "metadata": {}, "source": [ "|**Datentyp**|**Beispiel**|\n", "|:--- | :--- | \n", "|integer (Ganzzahl)|5, 0, -45|\n", "|float (Kommazahl) |2.5, 3.14, -2.05|\n", "|bool (Wahrheitswert)| True, False|\n", "|string (Zeichenkette) | \"Name\", 'Eins', \"Blau\"|\n" ] }, { "cell_type": "markdown", "id": "4d9b90f1", "metadata": {}, "source": [ "### Casten (Typ-Konvertierung) \n", "\n", "Das Ändern des Datentyps einer Variable nennt man \"Casten\" 🤓" ] }, { "cell_type": "code", "execution_count": 11, "id": "40335826", "metadata": {}, "outputs": [], "source": [ "zahl = 5\n", "\n", "zahl = str(zahl)" ] }, { "cell_type": "code", "execution_count": 12, "id": "d6c06d2d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(zahl)" ] }, { "cell_type": "code", "execution_count": 13, "id": "0e365738", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'5'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zahl" ] }, { "cell_type": "code", "execution_count": 5, "id": "d411ed25", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(pi)" ] }, { "cell_type": "markdown", "id": "256f0ea0", "metadata": {}, "source": [ "*Anmerkung:* Bei Konvertierungen von float in int wird immer der Nachkommateil abgeschnitten --> es wird abgerundet" ] }, { "cell_type": "code", "execution_count": 6, "id": "bb722d04", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fast_vier = 3.9999\n", "fast_vier = int(fast_vier)\n", "fast_vier" ] }, { "cell_type": "markdown", "id": "097b2148", "metadata": {}, "source": [ "### Konstanten " ] }, { "cell_type": "code", "execution_count": 7, "id": "746f7e98", "metadata": {}, "outputs": [], "source": [ "PI = 3.14159265\n", "SPEED_OF_LIGHT = 299792458\n" ] }, { "cell_type": "markdown", "id": "276d238f", "metadata": {}, "source": [ "## 3. Arithmetik 🧮" ] }, { "cell_type": "markdown", "id": "cb9d9d53", "metadata": {}, "source": [ "mathematische Operatoren wie: + , - , * , /\n", "\n", "können auf numerische Typen (int, float) angewendet werden\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 14, "id": "5cec1e28", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "12\n" ] } ], "source": [ "a = 5\n", "b = 7\n", "c = a + b\n", "print(c)" ] }, { "cell_type": "code", "execution_count": 15, "id": "63f29bb9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.4" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = b / a\n", "c" ] }, { "cell_type": "markdown", "id": "3dced979", "metadata": {}, "source": [ "Besondere Operatoren: \\\n", "**Ganzzahldivision:**... // \\\n", "**Modulo:**....................% --> Restwert einer Division\n", " " ] }, { "cell_type": "code", "execution_count": 21, "id": "ee8c9b2d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = b % a\n", "c\n", "\n", "c = b // a\n", "c" ] }, { "cell_type": "code", "execution_count": null, "id": "90a3f4f6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "2df3d436", "metadata": {}, "source": [ "**Potenz**" ] }, { "cell_type": "code", "execution_count": 26, "id": "03dd0500", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(2**3) + 3" ] }, { "cell_type": "markdown", "id": "c7dd97ed", "metadata": {}, "source": [ "**Zusammenfügen von Strings**\n", " " ] }, { "cell_type": "markdown", "id": "00bfa8fe", "metadata": {}, "source": [ "Auf String Variablen kann der \"+\"-Operator auch angewendet werden. Hierduch werden die beiden Zeichenketten zusammengefügt" ] }, { "cell_type": "code", "execution_count": 31, "id": "eda0a3fa", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello World 4334'" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ " \"Hello\" + \" \" + \"World\" + \" 4334\"" ] }, { "cell_type": "markdown", "id": "c88e58ed-1c3b-4cb6-9c18-d904c0c5f1c5", "metadata": {}, "source": [ "Der \"\\*\"-Operator ist ebenso möglich" ] }, { "cell_type": "code", "execution_count": 38, "id": "d68f5092-8457-4a82-b1cb-cd809665985a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello World 4334 Hello World 4334 Hello World 4334 Hello World 4334 Hello World 4334 Hello World 4334 Hello World 4334 Hello World 4334 Hello World 4334 Hello World 4334 Hello World 4334 Hello World 4334 Hello World 4334 Hello World 4334 Hello World 4334 Hello World 4334 Hello World 4334 Hello World 4334 Hello World 4334 Hello World 4334 Hello World 4334 Hello World 4334 Hello World 4334 Hello World 4334 Hello World 4334 Hello World 4334 Hello World 4334 '" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(\"Hello\" + \" \" + \"World\" + \" 4334 \") * 27" ] }, { "cell_type": "markdown", "id": "8169b704", "metadata": {}, "source": [ "## 4. Konsolen Ein-/Ausgaben 🖨️" ] }, { "cell_type": "markdown", "id": "ec4601e9", "metadata": {}, "source": [ "Um mit dem Anwender eines Programms zu kommunizieren, können Ein- und Ausgaben auf das Konsolenfenster gemacht werden\n", "Ausgaben auf die Konsole: print()\n", "Eingaben von der Konsole: input()" ] }, { "cell_type": "code", "execution_count": 52, "id": "e8161b63", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Wort: 5 iadoiad 10\n", "Test 5 Test2\n" ] } ], "source": [ "a = 5\n", "b = 10\n", "a+b\n", "print(f\"Wort: {a} iadoiad {b}\")\n", "print(\"Test\",a,\"Test2\")" ] }, { "cell_type": "markdown", "id": "439a68d3", "metadata": {}, "source": [ "In Jupiter Notebook ist diese Funktion überflüssig, da jede Zelle eine eigene Ausgabe hat. " ] }, { "cell_type": "markdown", "id": "f8b75511", "metadata": {}, "source": [ "Optional kann bei einer Eingabe noch eine Nachricht dem Anwender mitgegeben werden. " ] }, { "cell_type": "code", "execution_count": 60, "id": "3c4d7d44", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Gebe eine Zahl an: 1243\n" ] } ], "source": [ "eingabe = int(input(\"Gebe eine Zahl an:\"))" ] }, { "cell_type": "code", "execution_count": 61, "id": "9db4038a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deine Zahl lautet: 1243\n" ] } ], "source": [ "print(\"Deine Zahl lautet:\", eingabe)" ] }, { "cell_type": "markdown", "id": "92495442", "metadata": {}, "source": [ "## 5. Kommentare ✍️" ] }, { "cell_type": "markdown", "id": "40fe1bcd", "metadata": {}, "source": [ "Kommentare sind kurze Anmerkungen, die den Programmcode erklären. \\\n", "Sie beginnen mit '#' und alles was in der Zeile dahinter steht wird vom Python Interpreter nicht interpretiert" ] }, { "cell_type": "code", "execution_count": 62, "id": "c6e26f19", "metadata": {}, "outputs": [], "source": [ "# Dies ist ein Kommentar\n", "# Kommentare beginnen mit einem '#'\n", "\n", "# ahüd9ahdoih\n", "# \n", "\n", "i = 5 # Variable i wird mit dem Wert 5 initialisiert" ] }, { "cell_type": "markdown", "id": "81569621", "metadata": {}, "source": [ "Kommentare werden vom Programmierer auch häufig dafür verwedet bestimmte Zeilen kurzfristig auszublenden" ] }, { "cell_type": "code", "execution_count": 67, "id": "59505661", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i = 3\n", "i = 5\n", "i" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.12.2" }, "toc-autonumbering": false, "toc-showcode": false, "toc-showmarkdowntxt": false }, "nbformat": 4, "nbformat_minor": 5 }