{ "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": 1, "id": "f6e3beda", "metadata": {}, "outputs": [], "source": [ "variablenname = \"Information\"\n", "zahl = 5\n", "text = \"Dies ist ein Text/String\"\n", "pi = 3.1415" ] }, { "cell_type": "markdown", "id": "eed741ec", "metadata": {}, "source": [ "*Merke:* Variablenname links, Wert rechts" ] }, { "cell_type": "code", "execution_count": null, "id": "e90c1763-dbbc-44b6-bbe9-01a42da179cc", "metadata": {}, "outputs": [], "source": [] }, { "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": 2, "id": "40335826", "metadata": {}, "outputs": [], "source": [ "zahl = str(zahl)" ] }, { "cell_type": "code", "execution_count": 3, "id": "d6c06d2d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(zahl)" ] }, { "cell_type": "code", "execution_count": 4, "id": "0e365738", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'5'" ] }, "execution_count": 4, "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": 8, "id": "5cec1e28", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "12" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 5\n", "b = 7\n", "c = a + b\n", "c" ] }, { "cell_type": "code", "execution_count": 9, "id": "63f29bb9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.4" ] }, "execution_count": 9, "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": null, "id": "ee8c9b2d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "90a3f4f6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "2df3d436", "metadata": {}, "source": [ "**Potenz**" ] }, { "cell_type": "code", "execution_count": 10, "id": "03dd0500", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**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": 11, "id": "eda0a3fa", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'HelloWorld'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ " \"Hello\" + \"World\" " ] }, { "cell_type": "markdown", "id": "c88e58ed-1c3b-4cb6-9c18-d904c0c5f1c5", "metadata": {}, "source": [ "Der \"\\*\"-Operator ist ebenso möglich" ] }, { "cell_type": "code", "execution_count": null, "id": "d68f5092-8457-4a82-b1cb-cd809665985a", "metadata": {}, "outputs": [], "source": [] }, { "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": 12, "id": "e8161b63", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Wert von x: 5\n" ] } ], "source": [ "x = 5\n", "print(\"Wert von x:\", x)" ] }, { "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": 13, "id": "3c4d7d44", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Gebe eine Zahl an: 8\n" ] } ], "source": [ "eingabe = input(\"Gebe eine Zahl an:\")" ] }, { "cell_type": "code", "execution_count": 14, "id": "9db4038a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deine Zahl lautet: 8\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": 15, "id": "c6e26f19", "metadata": {}, "outputs": [], "source": [ "# Dies ist ein Kommentar\n", "# Kommentare beginnen mit einem '#'\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": 16, "id": "59505661", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# i = 3\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.9.7" }, "toc-autonumbering": false, "toc-showcode": false, "toc-showmarkdowntxt": false }, "nbformat": 4, "nbformat_minor": 5 }