{ "cells": [ { "cell_type": "markdown", "id": "53ef5460-e38f-4d51-92e3-7d680f23d710", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "# 2. Einführung in die Programmierung für Nicht Informatiker*innen" ] }, { "cell_type": "markdown", "id": "391a0811-2a99-4cde-8b88-bd7e0c1d6cb8", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "source": [ "Bitte ihre Jupyter Kennung + Vor- & Nachnamen unter folgendem link eintragen [survey](https://survey.cyperpunk.de/form/GVkRjB)" ] }, { "cell_type": "markdown", "id": "c39f4116-a533-4d21-87e5-6d24268b995c", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## Beispiele Print" ] }, { "cell_type": "code", "execution_count": 1, "id": "48342195-b5a8-4298-9f83-2908c8a52821", "metadata": { "editable": true, "slideshow": { "slide_type": "subslide" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hallo Python\n", "Freitag 15h\n" ] } ], "source": [ "# Beispiel Print\n", "print(\"Hallo\", \"Python\", end='\\n') # Zeigen wie end Funktioniert\n", "print(\"Freitag 15h\") # Ausgabe mittels print" ] }, { "cell_type": "code", "execution_count": 2, "id": "d08902d2-e454-4f81-bd63-30ef9a33deb2", "metadata": { "editable": true, "slideshow": { "slide_type": "subslide" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'Hallo Python'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Hallo Python\" # direkte ausgabe einer Variablen oder Wert" ] }, { "cell_type": "code", "execution_count": 3, "id": "63ac2349-5a3d-49f5-b379-ec92ff502694", "metadata": { "editable": true, "slideshow": { "slide_type": "subslide" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "432423654645" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "432423654645 " ] }, { "cell_type": "markdown", "id": "0b230834-0e94-48ff-9462-5b627726db70", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## Variablen zuweisungen" ] }, { "cell_type": "code", "execution_count": 4, "id": "aa39464e-eaa1-4c8c-bd29-2ca20bc35336", "metadata": { "editable": true, "slideshow": { "slide_type": "subslide" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zahl1 = 42 # 'zahl1' speichert den Wert '42'\n", "zahl1" ] }, { "cell_type": "code", "execution_count": 5, "id": "45281ff8-2c07-4774-9f96-e95a5b8f31ae", "metadata": { "editable": true, "slideshow": { "slide_type": "subslide" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42\n" ] } ], "source": [ "print(zahl1) # Ausgabe der Variabeln mittels print" ] }, { "cell_type": "code", "execution_count": 6, "id": "c9e3bf85-5b75-4efc-bbcc-9c5de28be421", "metadata": { "editable": true, "slideshow": { "slide_type": "subslide" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "420" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zahl2 = 420\n", "zahl2" ] }, { "cell_type": "markdown", "id": "750a84cf-6528-4d1c-9e53-85da156d47a3", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## Operationen mit Integern (gilt auch für Floats)" ] }, { "cell_type": "code", "execution_count": 7, "id": "599a7dec-0e03-43a2-92df-a60777a33f03", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "462" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zahl1 + zahl2 # Addition" ] }, { "cell_type": "code", "execution_count": 8, "id": "db143bf7-4d07-4a2b-b30a-b2f9054efeb6", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "-378" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zahl1 - zahl2 # Subtraction" ] }, { "cell_type": "code", "execution_count": 9, "id": "3b96f637-a5a2-4188-9632-a99faf881f76", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "378" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zahl2 - zahl1 # Order matters" ] }, { "cell_type": "code", "execution_count": 10, "id": "7dc56688-4dd5-441d-b9f1-2c17028338e6", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "17640" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zahl2 * zahl1 # Multiplikation" ] }, { "cell_type": "code", "execution_count": 11, "id": "4cb21e7e-f77d-4c5d-afb4-4aa0e7e974e5", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "10.0" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zahl2 / zahl1 # Division" ] }, { "cell_type": "code", "execution_count": 12, "id": "7d941f53-9fa4-494a-9871-bf02ecad97b7", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "16" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**4 # Exponentiation" ] }, { "cell_type": "code", "execution_count": 13, "id": "0e6a2cae-c360-454c-a622-37e6fed45879", "metadata": { "editable": true, "slideshow": { "slide_type": "subslide" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "42.42" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float1 = 42.42 # Fließkommzahl speichern\n", "float1" ] }, { "cell_type": "code", "execution_count": 14, "id": "d360fc12-dfff-498e-95b8-664648fb8442", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zahl2 // zahl1 # Ganzzahldivision" ] }, { "cell_type": "code", "execution_count": 15, "id": "1813dd22-f4f0-499b-92aa-c5f5f95b8a6c", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "42 % 2 # Modulo (Rest einer Division)" ] }, { "cell_type": "code", "execution_count": 16, "id": "87f97c26-4fa2-4192-ae5b-21a6adcb8aa4", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "(21, 0)" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "divmod(42, 2) # Kombinierte Built-in funktion, welche den Wert der Ganzzahldivision und den Rest berechnet" ] }, { "cell_type": "code", "execution_count": 17, "id": "5a8948ca-8662-43f1-9a1e-cb9bec803380", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "21.0" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "42 / 2" ] }, { "cell_type": "code", "execution_count": 18, "id": "f17cc46a-906e-4fe9-9c0a-5cb81e09bcc5", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "42 % 2" ] }, { "cell_type": "code", "execution_count": 19, "id": "a6d36974-b750-41e8-a0b3-a40fdba9f02c", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "0.30000000000000004" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0.1 + 0.2 # Addition von Floats sind nicht immer akkurat" ] }, { "cell_type": "markdown", "id": "73b246a0-f6d9-4e19-9dc3-a5e25efd0c8c", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## Booleans" ] }, { "cell_type": "code", "execution_count": 20, "id": "99e978bc-b40e-432a-81a0-801391760fb3", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0.1 + 0.2 == 0.3 # Nicht richtig aufgrund der unkorrekten binären darstellung von floats" ] }, { "cell_type": "code", "execution_count": 21, "id": "fc761a8c-8988-4a20-81a1-28e33f11f138", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True " ] }, { "cell_type": "code", "execution_count": 22, "id": "668ad1b3-741e-46fb-a3af-375861ecbd13", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "False" ] }, { "cell_type": "code", "execution_count": 23, "id": "0b23648c-1a8a-44e9-9941-a5e0e8b80ffb", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True + True # Bools sind auch nur Zahlen (True = 1, False = 0)" ] }, { "cell_type": "markdown", "id": "da342349-6c33-4fc6-864f-7171ac6dd029", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## Listen" ] }, { "cell_type": "code", "execution_count": 24, "id": "1b91f589-8469-49cf-90ba-af14d638d8eb", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "['Hallo Python', 42, 10.1]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [\"Hallo Python\", 42, 10.10] # Erstellen einer Liste mit 3 elementen\n", "l" ] }, { "cell_type": "code", "execution_count": 25, "id": "678973b3-81ad-48e2-8ab2-c1784440a91c", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "['Hallo Python', 42, 10.1, 'Text']" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.append(\"Text\") # Weiteres Element an die liste anfügen\n", "l" ] }, { "cell_type": "code", "execution_count": 26, "id": "0c68eb51-c5f1-4650-9032-31f77a72006f", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "['Hallo Python', 42, 10.1, 'Text']" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 27, "id": "e41e1c08-b570-4423-a220-c428512ab686", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "10.1" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g = l[2] # Zugriff auf das dritte Element der Liste\n", "g" ] }, { "cell_type": "code", "execution_count": 28, "id": "6a4dc400-c788-4105-a57d-c42a64bf8928", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'Hallo Python'" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[0] # Zugriff erstes element der Liste" ] }, { "cell_type": "code", "execution_count": 29, "id": "174211ad-dda8-4d43-87a1-eee4d582c47c", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[-3] # Auff listen lässt sich auch 'rückwärts' zugreifen" ] }, { "cell_type": "code", "execution_count": 30, "id": "8ed1b344-94d0-49d6-8662-ea81d33f9d8f", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "['Hallo Python', 42, 10.1]" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.remove('Text') # Einen Wert aus der Liste entfernen\n", "l" ] }, { "cell_type": "markdown", "id": "5e1f1661-cb9d-464f-be4a-51d838bde23d", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## Strings" ] }, { "cell_type": "code", "execution_count": 31, "id": "a10e3b76-8b0c-4979-880c-cb5a86c1c42a", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'Tex\"t'" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str1 = 'Tex\"t' # String mit ''\n", "str1" ] }, { "cell_type": "code", "execution_count": 32, "id": "783b1c13-fe63-452c-8f0e-8dab0c1173ee", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "\"Text'2\"" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str2 = \"Text'2\" # String mit \"\"\n", "str2" ] }, { "cell_type": "code", "execution_count": 33, "id": "91a8fb1e-c9ba-4e37-81d4-fae62e30fc8c", "metadata": { "editable": true, "slideshow": { "slide_type": "subslide" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'\\nHallo dies ist ein \\necht langer \\nnicht formattierter \\ntext!!!! \\' \"\\n\\n'" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Documentation Strings oder mehrzeillige Strings\n", "str3 = '''\n", "Hallo dies ist ein \n", "echt langer \n", "nicht formattierter \n", "text!!!! ' \"\n", "\n", "'''\n", "str3 # Ausgabe mit escape Characters" ] }, { "cell_type": "code", "execution_count": 34, "id": "7fa2bc58-3cf0-4ecb-ba6e-ab0af08c2806", "metadata": { "editable": true, "slideshow": { "slide_type": "subslide" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Hallo dies ist ein \n", "echt langer \n", "nicht formattierter \n", "text!!!! ' \"\n", "\n", "\n" ] } ], "source": [ "print(str3) # \"Hübsche\" Ausgabe" ] }, { "cell_type": "code", "execution_count": 35, "id": "430c663e-6320-45bb-85ac-3781394a4a37", "metadata": { "editable": true, "slideshow": { "slide_type": "subslide" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'x'" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str1[2] # Strings sind auch nur Listen" ] }, { "cell_type": "code", "execution_count": 36, "id": "e2f20468-3ea7-411e-9c72-e29d5a344fa2", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'Tex\"t ist ein text'" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f\"{str1} ist ein text\" # F-String formatierung" ] }, { "cell_type": "code", "execution_count": 37, "id": "58dc5f40-26df-45e8-9c36-319dce3a11cd", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'Tex\"t ist ein text'" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"{} ist ein text\".format(str1) # Formatierung mittels format funktion" ] }, { "cell_type": "markdown", "id": "8dc440b6-a8c3-43a5-937f-1748d6a23c41", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## Tuples" ] }, { "cell_type": "code", "execution_count": 38, "id": "cf0bd9a6-db63-4795-847f-b6dbd0cd4d1e", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "(42, 10.1)" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tuple1 = (42, 10.10) # Tuple erstellen\n", "tuple1 " ] }, { "cell_type": "code", "execution_count": 39, "id": "6a6c1c1e-ea17-4609-82c8-2fa02b05b208", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tuple1[0] # auf element des Tupels zugreifen" ] }, { "cell_type": "code", "execution_count": 40, "id": "a46ec0e4-0cb3-419b-ac9e-9f2a669812b0", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "ename": "AttributeError", "evalue": "'tuple' object has no attribute 'append'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[40], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mtuple1\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mappend\u001b[49m(\u001b[38;5;241m1\u001b[39m) \u001b[38;5;66;03m# Tuples sind unveränderlich (immutable) daher können nicht einfach elemente angefügt werden\u001b[39;00m\n", "\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'" ] } ], "source": [ "tuple1.append(1) # Tuples sind unveränderlich (immutable) daher können nicht einfach elemente angefügt werden" ] }, { "cell_type": "markdown", "id": "448e1ab1-b51e-4577-a51d-da985079bb11", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## Dictionarys" ] }, { "cell_type": "code", "execution_count": 41, "id": "eb6815fd-755f-49e0-a9c3-71e28b1096c3", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "{'Name': 'Phil', 'Key': 42}" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dict1 = {\"Name\": \"Phil\", \"Key\": 42} # Dictionary erstellen\n", "dict1" ] }, { "cell_type": "code", "execution_count": 42, "id": "3a00b7f8-910b-4261-9b49-cee2fefab2ec", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dict1[\"Key\"] # Zugriff auf element des Dicts mittels Schlüssel" ] }, { "cell_type": "code", "execution_count": 43, "id": "9128089a-d000-4cb3-87e3-524a53a93b84", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "{'Name': 'Phil', 'Key': 42, 'Key2': 10}" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dict1[\"Key2\"] = 10 # neues Element in das Dict einfügen\n", "dict1" ] }, { "cell_type": "markdown", "id": "ea00b1d5-b0fa-4949-8d51-09b045f4d25b", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## Sets" ] }, { "cell_type": "code", "execution_count": 44, "id": "07a6f887-1831-42db-a81f-69ec41b6489b", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4}" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "set1 = {1, 1, 2, 2, 3, 3, 4} # Set erstellen\n", "set1 # Sets haben ausschließlich einzigartige Elemente " ] }, { "cell_type": "markdown", "id": "729cb47d-ae3c-4317-b19e-c423b1f1fc9a", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## Conditionals" ] }, { "cell_type": "code", "execution_count": 45, "id": "23dc654e-7434-4e41-8286-c9fa86aa2fae", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "42 % 2 == 0 # Prüfen auf equality" ] }, { "cell_type": "code", "execution_count": 46, "id": "50757a7f-d3a2-43ba-9c59-f2b122661717", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ist Gerade\n" ] } ], "source": [ "# Unterschiedlichen Code ausführen je nach Bedingung\n", "zahl = 32\n", "if zahl % 2 == 1: # ist die zahl Gerade?\n", " print(\"Ist Ungerade\") # Wenn Ja\n", "else:\n", " print(\"Ist Gerade\") # Wenn Nein" ] }, { "cell_type": "code", "execution_count": 47, "id": "645f5df9-8ade-4cf3-aa7f-6bf642d53674", "metadata": { "editable": true, "slideshow": { "slide_type": "subslide" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ist Gerade\n" ] } ], "source": [ "# Argument \"verneinen\"\n", "zahl = 32\n", "if not zahl % 2 == 1: # Ist die zahl nicht gerade?\n", " print(\"Ist Gerade\") # Wenn Ja (Zahl nicht gerade -> Zahl gerade)\n", "else:\n", " print(\"Ist Ungerade\") # Wenn Nein " ] }, { "cell_type": "code", "execution_count": 48, "id": "40d8928e-365b-4c73-a852-b13b507d7b2f", "metadata": { "editable": true, "slideshow": { "slide_type": "subslide" }, "tags": [] }, "outputs": [], "source": [ "if 42 % 2 == 0 and 36 % 2 == 1: # Und verknüpfung alle bedingungen müssen wahr sein\n", " print(\"Sind beide Gerade\")" ] }, { "cell_type": "code", "execution_count": 49, "id": "082f72fe-8ced-4acd-bd51-0b7bec7195a7", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True and False" ] }, { "cell_type": "code", "execution_count": 50, "id": "0f911f39-056d-42bb-abfb-521fb926a1b1", "metadata": { "editable": true, "slideshow": { "slide_type": "subslide" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Eine Zahl ist Gerade\n" ] } ], "source": [ "if 42 % 2 == 0 or 36 % 2 == 1: # Oder Verknüpfung eine oder mehrere Bedingungen müssen wahr sein\n", " print(\"Eine Zahl ist Gerade\")" ] }, { "cell_type": "code", "execution_count": 51, "id": "e8b94c36-e921-455a-80ae-a9f6e38a6d57", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True or False" ] }, { "cell_type": "code", "execution_count": 52, "id": "e828d799-89af-4991-bdc7-39d70dd30614", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "False or False" ] }, { "cell_type": "code", "execution_count": 53, "id": "db94bbb7-c422-4566-beea-d5d606825563", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True or True" ] }, { "cell_type": "code", "execution_count": 54, "id": "1a7093bc-a4ce-4fbd-97c0-f93570a185c7", "metadata": { "editable": true, "slideshow": { "slide_type": "subslide" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Zahl ist durch 5 glatt teilbar\n" ] } ], "source": [ "# Verschiedene Bedingungen mit verschiedenen Ausgaben\n", "zahl = 10\n", "\n", "if zahl % 3 == 0: # zahl durch 3 teilbar?\n", " print(\"Zahl ist durch 3 glatt teilbar\")\n", "elif zahl % 5 == 0: # Zahl durch 5 teilbar?\n", " print(\"Zahl ist durch 5 glatt teilbar\")\n", "else: # Weder noch (Base Case)\n", " print(\"Zahl ist nicht durch 3 oder 5 teilbar\")" ] }, { "cell_type": "code", "execution_count": 55, "id": "eaa7e654-59b0-4ce4-80bf-f3ec83382663", "metadata": { "editable": true, "slideshow": { "slide_type": "subslide" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zahl = 90.0\n", "isinstance(zahl, float) # Prüfen ob eine variable einem der Datentypen entspricht (hier float)" ] }, { "cell_type": "markdown", "id": "dfb03801-ad4f-4006-837e-e6d62f36a02a", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## Funktionen" ] }, { "cell_type": "code", "execution_count": 56, "id": "e031a4f3-b42b-4a18-872c-1241a920b842", "metadata": { "editable": true, "slideshow": { "slide_type": "subslide" }, "tags": [] }, "outputs": [], "source": [ "def f(x): # Einleiten einer Funktion\n", " ''' Berechnet das quadrat einer Zahl''' # Docstring zum beschreiben der Funktion\n", " # .... irgendeine Berechnung\n", " rückgabewert = x*x # ist der rückgabewert\n", " return rückgabewert #" ] }, { "cell_type": "code", "execution_count": 57, "id": "0f92364f-c666-485b-be38-d293cc167f17", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "49" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(7)" ] }, { "cell_type": "markdown", "id": "173dd25d-f83c-4dc9-847b-8d4a6f9ba4b7", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## While Loops" ] }, { "cell_type": "code", "execution_count": 58, "id": "17722b1d-0491-451f-b130-01b89ee960cf", "metadata": { "editable": true, "slideshow": { "slide_type": "fragment" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 Hallo Python\n", "1 Hallo Python\n", "2 Hallo Python\n", "3 Hallo Python\n", "4 Hallo Python\n", "5 Hallo Python\n", "6 Hallo Python\n", "7 Hallo Python\n", "8 Hallo Python\n", "9 Hallo Python\n" ] } ], "source": [ "i = 0 # Condition\n", "while i < 10: # Prüfung\n", " print(i, \"Hallo Python\")\n", " i = i + 1 # Anpassung" ] }, { "cell_type": "markdown", "id": "974862d5-8279-4277-9008-726c45e392a3", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## 99 Bootles of Beer" ] }, { "cell_type": "code", "execution_count": null, "id": "89075c38-1487-4175-8841-3546c8ac304f", "metadata": { "editable": true, "slideshow": { "slide_type": "subslide" }, "tags": [] }, "outputs": [], "source": [ "# Vereinigung aller heute gelernten Dinge außer der Funktions Definition\n", "\n", "# 99 Bootles of Beer \n", "beer = 99\n", "while beer > -1:\t\n", " print()\n", " if beer == 0:\n", " print('''\n", " No more bottles of beer on the wall, no more bottles of beer.\n", " We've taken them down and passed them around;\n", " now we're drunk and passed out!\n", " ''')\n", " elif beer == 1:\n", " print(beer, \"bottle of beer on the wall,\", beer, \"bottle of beer.\")\n", " print(\"Take one down and pass it around, no more bottles of beer on the wall.\", end='')\n", " else:\n", " print(beer, \"bottles of beer on the wall,\", beer, \"bottles of beer.\")\n", " if beer - 1 == 1:\n", " print(f\"Take one down and pass it around, {beer - 1} bottle of beer on the wall.\") \n", " else:\n", " print(f\"Take one down and pass it around, {beer - 1} bottles of beer on the wall.\")\n", " \n", " beer -= 1\n", "\n", "# Ausgabe ist zu groß für Slides" ] } ], "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.7" } }, "nbformat": 4, "nbformat_minor": 5 }