{ "cells": [ { "cell_type": "markdown", "id": "2f7dbb60-7056-46dc-b3e1-4b195c7c9c15", "metadata": {}, "source": [ "# Einfuehrung" ] }, { "cell_type": "markdown", "id": "3a2dedf3-21ee-4209-965c-5582fff6cfaa", "metadata": {}, "source": [ "Hallo Kathi. Das hier ist nur depperter Text. Zum Beispiel kann man hier eine Liste machen:\n", "* Eins\n", "* Zwei" ] }, { "cell_type": "code", "execution_count": 2, "id": "e18fb507-8082-40a2-b417-fbb2d1adc26c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hallo Kathi\n" ] } ], "source": [ "print(\"Hallo Kathi\")" ] }, { "cell_type": "code", "execution_count": 4, "id": "26856f3f-052b-43e0-bff6-99474640f7ba", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hallo Kathi\n" ] } ], "source": [ "text = \"Hallo Kathi\"\n", "print(text)" ] }, { "cell_type": "code", "execution_count": 5, "id": "0eaef5bb-db8d-4795-bd9a-5ce0abaecda0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "666\n" ] } ], "source": [ "num = 666\n", "print(num)" ] }, { "cell_type": "code", "execution_count": 7, "id": "fe1ec3e5-3c4c-4b3c-a35b-17d7043274c8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hallo Kathi 666\n" ] } ], "source": [ "print(text, num)" ] }, { "cell_type": "code", "execution_count": 10, "id": "79be2ab9-0bcf-48c3-92a7-4410d5037e95", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] } ], "source": [ "num1 = 1\n", "num2 = 2\n", "print(num1 + num2)" ] }, { "cell_type": "code", "execution_count": 12, "id": "6c38641f-81bd-41a8-a025-a5bee8facf45", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] } ], "source": [ "summe = num1 + num2\n", "print(summe)" ] }, { "cell_type": "markdown", "id": "b311a5a4-d2cf-4173-8e81-2dd7533d72e8", "metadata": {}, "source": [ "# Eingabe mit `input()`" ] }, { "cell_type": "code", "execution_count": 14, "id": "8cb2bae9-e37b-4550-bbb2-c9551bdf5fb9", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "hallo kathi, gib hier bitte einen text ein einen text\n" ] } ], "source": [ "eingabe = input('hallo kathi, gib hier bitte einen text ein')" ] }, { "cell_type": "code", "execution_count": 15, "id": "0f726fc0-b3fa-4be0-bc9c-a6eeba4ea4b2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "einen text\n" ] } ], "source": [ "print(eingabe)" ] }, { "cell_type": "markdown", "id": "ef0c4f6a-adc1-4816-aca2-71c3a1f32ad5", "metadata": {}, "source": [ "## Die `input()` Funktion liefert immer Text!" ] }, { "cell_type": "code", "execution_count": 16, "id": "54c6384e-03ed-43de-b117-8383e8f3ca1e", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "hier jetzt die x Koordinate: 42\n" ] } ], "source": [ "x = input('hier jetzt die x Koordinate: ')" ] }, { "cell_type": "code", "execution_count": 18, "id": "304f48dc-2b1d-4cbc-a018-a6856ba19266", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42\n" ] } ], "source": [ "print(x)" ] }, { "cell_type": "code", "execution_count": 19, "id": "3ed8c867-4209-482f-886c-861632339c42", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Der User hat 42 eingegeben\n" ] } ], "source": [ "print('Der User hat', x, 'eingegeben')" ] }, { "cell_type": "code", "execution_count": 21, "id": "f616b08f-9f47-402b-b110-308992315eb5", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "hier noch eine zahl, bitte liebe kathi: 666\n" ] } ], "source": [ "y = input('hier noch eine zahl, bitte liebe kathi: ')" ] }, { "cell_type": "code", "execution_count": 22, "id": "0d555315-86ae-44f0-a0b8-a391ae792e3b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "666\n" ] } ], "source": [ "print(y)" ] }, { "cell_type": "markdown", "id": "39594bf4-640b-48e1-8e07-4a0f16c6850b", "metadata": {}, "source": [ "x und y sind nicht das was du erwartest. input() liefert **immer** Text, egal ob der Text so aussieht, als ob es eine Zahl waere. Deswegen ist \"+\" zwischen x und y dasjenige \"+\", das TExte zusammenhaengt." ] }, { "cell_type": "code", "execution_count": 25, "id": "79a4b321-68e0-4e5a-a3d0-a8863829fd6d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42666\n" ] } ], "source": [ "summe = x + y\n", "print(summe)" ] }, { "cell_type": "code", "execution_count": 28, "id": "90d0343b-29ac-4069-9ded-14ed4307ad01", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "708\n" ] } ], "source": [ "summe = int(x) + int(y)\n", "print(summe)" ] }, { "cell_type": "code", "execution_count": 32, "id": "8adb13e5-220f-42ed-ab60-f10949ba7491", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "eine zahl: 42\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "typ von x: \n", "typ von x_zahl: \n" ] } ], "source": [ "x = input('eine zahl: ')\n", "x_zahl = int(x)\n", "print('typ von x:', type(x))\n", "print('typ von x_zahl:', type(x_zahl))" ] }, { "cell_type": "code", "execution_count": 33, "id": "7499f330-6179-40e9-97e9-0d0105bd00c8", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "eine zahl: 42\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "42 \n" ] } ], "source": [ "x = int(input('eine zahl: '))\n", "print(x, type(x))" ] }, { "cell_type": "markdown", "id": "95085536-b296-4936-b50e-c90e09ec56c6", "metadata": {}, "source": [ "# Typen" ] }, { "cell_type": "markdown", "id": "7ce30cb7-4ee5-48b8-ba83-e02131340e9e", "metadata": {}, "source": [ "## `int`: Ganze Zahl" ] }, { "cell_type": "code", "execution_count": 34, "id": "4fe8406e-e8d6-4480-919e-7e3ced1c2fd5", "metadata": {}, "outputs": [], "source": [ "x = 42 # das ist eine ganze zahl\n", "y = 666 # das auch" ] }, { "cell_type": "code", "execution_count": 35, "id": "0d97868c-0c16-4594-add6-872c905b033d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(x)" ] }, { "cell_type": "code", "execution_count": 36, "id": "2f20dac1-09d2-4722-8e99-d3b58d7e97c9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "708\n" ] } ], "source": [ "summe = x+y\n", "print(summe)" ] }, { "cell_type": "markdown", "id": "46002df1-662e-4adc-944a-c401f507f1c3", "metadata": {}, "source": [ "## Operatoren" ] }, { "cell_type": "code", "execution_count": 38, "id": "26da81e1-5c8f-4c2c-aa35-1e363b8ac198", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "708" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x+y" ] }, { "cell_type": "code", "execution_count": 39, "id": "4f1b83bb-445f-4470-8fcc-d557c78d64af", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-624" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x-y" ] }, { "cell_type": "code", "execution_count": 40, "id": "612e98d5-a9b6-403a-a776-a91d5a96cd51", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10%3" ] }, { "cell_type": "markdown", "id": "a68c9af4-1edd-4e32-89fd-efa7f80e63fc", "metadata": {}, "source": [ "## Strings (Text)" ] }, { "cell_type": "code", "execution_count": 42, "id": "f76744e0-70ed-43a7-aacb-b16050864972", "metadata": {}, "outputs": [], "source": [ "x = '42' # das ist ein string\n", "y = '666'" ] }, { "cell_type": "code", "execution_count": 43, "id": "b125cb74-bf4a-4061-8ee4-ce5734b66ed6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42666\n" ] } ], "source": [ "summe = x + y\n", "print(summe)" ] }, { "cell_type": "markdown", "id": "fddfd7d3-8ff5-4ba4-a5ad-11e8801b4f94", "metadata": {}, "source": [ "**Laenge eines Strings**" ] }, { "cell_type": "code", "execution_count": 50, "id": "8cce2868-4971-47ef-8867-f4cf87c870d6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "text = \"Kathi\"\n", "len(text)" ] }, { "cell_type": "markdown", "id": "5df443f7-cf2b-4d23-b8e4-5f6b81a460d1", "metadata": {}, "source": [ "**\"Methoden\" (Was kann so ein String)**" ] }, { "cell_type": "code", "execution_count": 44, "id": "622fe680-8540-4b7b-be3e-3b0927032aba", "metadata": {}, "outputs": [], "source": [ "text = 'kathi and a thistle with many thorns'" ] }, { "cell_type": "code", "execution_count": 45, "id": "9f4e5263-3118-4793-9dfe-960579f0c63a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "text.count('th')" ] }, { "cell_type": "code", "execution_count": 47, "id": "52f03f11-12ae-48a0-bb15-717b2c219a07", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "text.find('kathi')" ] }, { "cell_type": "code", "execution_count": 48, "id": "90248fb4-4398-4d1c-ad44-99c01408aa11", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "text.find('kathi', 5)" ] }, { "cell_type": "markdown", "id": "60b0abc5-dc01-448f-91b8-dc293b4b0c2c", "metadata": {}, "source": [ "## `float` (Gleitkommazahlen)" ] }, { "cell_type": "code", "execution_count": 52, "id": "376865a7-0de9-4996-8502-2580f6fc99d8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4.0" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 1.5\n", "y = 2.5\n", "summe = x + y\n", "summe" ] }, { "cell_type": "markdown", "id": "b59bd3ca-8ddf-4eec-8fb1-413268cc3ff7", "metadata": {}, "source": [ "## `bool` (Wahr oder falsch)" ] }, { "cell_type": "code", "execution_count": 54, "id": "7aa80944-c185-4cf6-9fae-c3c47b48dc73", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'x' == 'u'" ] }, { "cell_type": "code", "execution_count": 58, "id": "48474476-5021-41e0-ad84-2ad8262b4a86", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 == 1" ] }, { "cell_type": "markdown", "id": "d763d964-a666-477b-8992-0d1eb29408be", "metadata": {}, "source": [ "`True` und `False` wirst du in deinen Programmen wahrscheinlich nie direkt hinschreiben:" ] }, { "cell_type": "code", "execution_count": 60, "id": "0c50131d-1859-4b31-8dab-da332cc49ff8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ungerade\n" ] } ], "source": [ "zahl = 43\n", "if zahl % 2 == 0:\n", " print('gerade')\n", "else:\n", " print('ungerade')" ] }, { "cell_type": "markdown", "id": "c8c9d781-bf71-4bfe-9240-c060da44f125", "metadata": {}, "source": [ "## Konvertieren zwischen Datentypen" ] }, { "cell_type": "code", "execution_count": 61, "id": "a21ae380-6afa-4812-9e08-307aabff8e1a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "text = '7'\n", "zahl = int(text)\n", "zahl" ] }, { "cell_type": "code", "execution_count": 62, "id": "c1989bd0-cb31-43b9-9e1b-d4c5ed229729", "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "invalid literal for int() with base 10: 'sieben'", "output_type": "error", "traceback": [ "\u001b[31m---------------------------------------------------------------------------\u001b[39m", "\u001b[31mValueError\u001b[39m Traceback (most recent call last)", "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[62]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m int(\u001b[33m'sieben'\u001b[39m)\n", "\u001b[31mValueError\u001b[39m: invalid literal for int() with base 10: 'sieben'" ] } ], "source": [ "int('sieben')" ] }, { "cell_type": "code", "execution_count": 63, "id": "39e31a49-bd0d-44cb-ad34-36659c5a5649", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'7'" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zahl = 7\n", "text = str(zahl)\n", "text" ] }, { "cell_type": "code", "execution_count": 65, "id": "22386aa1-5a91-4e0f-8626-9fdf193b1922", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gleitzahl = 1.5\n", "zahl = int(gleitzahl) # schmeisst alles hinter dem komma weg\n", "zahl" ] }, { "cell_type": "code", "execution_count": 67, "id": "5dced076-866e-41fd-abba-ff55cf910016", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zahl = 2\n", "bool(zahl)" ] }, { "cell_type": "code", "execution_count": 69, "id": "641891ed-64d5-4db4-979b-75c18c82b19d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(1)" ] }, { "cell_type": "code", "execution_count": 70, "id": "82d0290a-4bc5-4421-b6db-a279595de480", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)" ] }, { "cell_type": "code", "execution_count": 71, "id": "7a9fbf05-bfd3-4acf-b44e-939c8625b5ad", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(0)" ] }, { "cell_type": "markdown", "id": "c9b42738-a123-4477-80eb-34da35cf8dc0", "metadata": {}, "source": [ "# `if`, `elif`, `else`" ] }, { "cell_type": "markdown", "id": "46c5181a-549b-4cec-8b90-6aa0b00f5fbe", "metadata": {}, "source": [ "In Lettland enden die Vornamen von Maennern immer auf \"s\", und die der Frauen auf \"a\". Das heist, der Algorithmus, um basierend auf einem Namen das Geschlecht herauszufinden, ist ganz leicht." ] }, { "cell_type": "code", "execution_count": 72, "id": "4fed5130-49f9-4626-9a2c-8d95f26c080a", "metadata": {}, "outputs": [], "source": [ "vorname = \"Katharina\"" ] }, { "cell_type": "code", "execution_count": 79, "id": "e9bd96e3-ad68-455d-b726-16e5a8877312", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Joerg ist keine Frau\n" ] } ], "source": [ "if vorname.endswith(\"a\"): # endswith ist auch was, was uns der string bietet (auch beginswith)\n", " print(vorname, 'ist eine Frau')\n", "else:\n", " print(vorname, 'ist keine Frau')" ] }, { "cell_type": "code", "execution_count": 76, "id": "1dfe0a56-54f9-4311-b3e4-68ad0d18225d", "metadata": {}, "outputs": [], "source": [ "vorname = \"Joerg\"" ] }, { "cell_type": "code", "execution_count": 80, "id": "588d2210-4c65-4cdd-8fe9-75b864ad136a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Joerg ist keine Frau\n" ] } ], "source": [ "if vorname.endswith(\"a\"): # endswith ist auch was, was uns der string bietet (auch beginswith)\n", " print(vorname, 'ist eine Frau')\n", "else:\n", " print(vorname, 'ist keine Frau')" ] }, { "cell_type": "markdown", "id": "df4dd40e-2e01-47fd-9c19-a648ec8e1cdb", "metadata": {}, "source": [ "Und `elif`?" ] }, { "cell_type": "code", "execution_count": 81, "id": "e769d383-1b44-4091-9d6c-bf5b566d8449", "metadata": {}, "outputs": [], "source": [ "vorname = \"Katharina\"" ] }, { "cell_type": "code", "execution_count": 82, "id": "da1e2fd9-f686-4bb4-bb22-f2a843698655", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Katharina ist eine Frau\n" ] } ], "source": [ "if vorname.endswith(\"a\"):\n", " print(vorname, 'ist eine Frau')\n", "elif vorname.endswith(\"s\"):\n", " print(vorname, 'ist ein Mann')\n", "else:\n", " print(vorname, 'ist weder Mann noch Frau')" ] }, { "cell_type": "markdown", "id": "80cd2d42-8e18-4127-b6ef-5bcb5e0460b2", "metadata": {}, "source": [ "Und nun das ganze parametrisiert:" ] }, { "cell_type": "code", "execution_count": 84, "id": "4291ec97-016c-4b0d-ac65-64e4f6218233", "metadata": {}, "outputs": [ { "name": "stdin", "output_type": "stream", "text": [ "Gib hier deinen Vornamen ein: x\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "x ist weder Mann noch Frau\n" ] } ], "source": [ "vorname = input('Gib hier deinen Vornamen ein: ')\n", "if vorname.endswith(\"a\"):\n", " print(vorname, 'ist eine Frau')\n", "elif vorname.endswith(\"s\"):\n", " print(vorname, 'ist ein Mann')\n", "else:\n", " print(vorname, 'ist weder Mann noch Frau')" ] } ], "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.14.4" } }, "nbformat": 4, "nbformat_minor": 5 }