From 9653cbe6d56f0cc85492a381036b8eaec484fa7d Mon Sep 17 00:00:00 2001 From: romanamo Date: Sat, 24 Jun 2023 11:59:29 +0200 Subject: [PATCH] fileio, lists & tuples started --- python/datentypen/lists.ipynb | 109 +++++++++++++++++++ python/datentypen/tuples.ipynb | 27 +++++ python/fileio/accounts.txt | 1 + python/fileio/buchungen.txt | 5 + python/fileio/fileio.ipynb | 190 +++++++++++++++++++++++++++++++++ python/fileio/hello.txt | 2 + python/fileio/myfile.txt | 2 + 7 files changed, 336 insertions(+) create mode 100644 python/datentypen/lists.ipynb create mode 100644 python/datentypen/tuples.ipynb create mode 100644 python/fileio/accounts.txt create mode 100644 python/fileio/buchungen.txt create mode 100644 python/fileio/fileio.ipynb create mode 100644 python/fileio/hello.txt create mode 100644 python/fileio/myfile.txt diff --git a/python/datentypen/lists.ipynb b/python/datentypen/lists.ipynb new file mode 100644 index 0000000..0e02bec --- /dev/null +++ b/python/datentypen/lists.ipynb @@ -0,0 +1,109 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Die Anzahl der Wagen bei dem ICE beträgt: 7\n", + "Die Anzahl der Wagen bei dem ICE beträgt: 12\n", + "Die Anzahl der Wagen bei dem ICE beträgt: 13\n" + ] + } + ], + "source": [ + "k1s = [1,2,3,4,5,6,7]\n", + "k3s = [1,2,3,4,5,6,7,9,10,11,12,14]\n", + "k4s = [1,2,3,4,5,6,7,8,9,10,11,12,14]\n", + "\n", + "for train in [k1s, k3s, k4s]:\n", + " print(f\"Die Anzahl der Wagen bei dem ICE beträgt: {len(train)}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Wagenreihung bei Abfahrt: [1, 2, 3, 4, 5, 6, 7]\n", + "Wagenreihung bei Ankunft: [1, 2, 3, 4, 5, 6, 7]\n" + ] + } + ], + "source": [ + "k1s_verlassen_berlin = k1s[:]\n", + "\n", + "k1s_ankunft_frankfurt = k1s_verlassen_berlin[:]\n", + "k1s_verlassen_frankfurt = k1s_ankunft_frankfurt[::-1]\n", + "\n", + "k1s_ankunft_stuttgart = k1s_verlassen_frankfurt[:]\n", + "k1s_verlassen_stuttgart = k1s_ankunft_stuttgart[::-1]\n", + "\n", + "k1s_ankunft_münchen = k1s_verlassen_stuttgart[:]\n", + "\n", + "print(f\"Wagenreihung bei Abfahrt in Berlin: {k1s_verlassen_berlin}\")\n", + "print(f\"Wagenreihung bei Ankunft in : {k1s_ankunft_münchen}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 4, 9, 16]\n", + "[2, 6, 20]\n", + "[[1, 3], [1, 10], [3, 1], [3, 10], [10, 1], [10, 3]]\n" + ] + } + ], + "source": [ + "# List Comprehensions\n", + "\n", + "my_list = [ i**2 for i in range(5)]\n", + "print(my_list)\n", + "\n", + "ints = [1,3,10]\n", + "\n", + "my_list2 = [i*2 for i in ints]\n", + "print(my_list2)\n", + "\n", + "my_list3 = [[i, j] for i in ints for j in ints if i != j]\n", + "print(my_list3)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.10.9" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/python/datentypen/tuples.ipynb b/python/datentypen/tuples.ipynb new file mode 100644 index 0000000..76faedc --- /dev/null +++ b/python/datentypen/tuples.ipynb @@ -0,0 +1,27 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "my_tuple = (1,2,3,4)\n", + "\n", + "print(my_tuple[0])\n", + "\n", + "print(my_tuple[1:3])\n", + "print(my_tuple[:3])\n", + "print(my_tuple[2:])" + ] + } + ], + "metadata": { + "language_info": { + "name": "python" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/python/fileio/accounts.txt b/python/fileio/accounts.txt new file mode 100644 index 0000000..1daed64 --- /dev/null +++ b/python/fileio/accounts.txt @@ -0,0 +1 @@ +100 Mali 24.98 diff --git a/python/fileio/buchungen.txt b/python/fileio/buchungen.txt new file mode 100644 index 0000000..cdde710 --- /dev/null +++ b/python/fileio/buchungen.txt @@ -0,0 +1,5 @@ +100 Jones 24.98 +200 Doe 345.67 +300 White 0.0 +400 Stone -42.16 +500 Rich 224.62 diff --git a/python/fileio/fileio.ipynb b/python/fileio/fileio.ipynb new file mode 100644 index 0000000..90f5a42 --- /dev/null +++ b/python/fileio/fileio.ipynb @@ -0,0 +1,190 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n", + "\n" + ] + } + ], + "source": [ + "file_object = open(\"myfile.txt\", \"r\")\n", + "\n", + "line = file_object.readline()\n", + "\n", + "print(line)\n", + "file_object.close()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World\n", + "Bye Jupiter\n" + ] + } + ], + "source": [ + "# Öffnen mit Kontextmanager\n", + "\n", + "with open(\"myfile.txt\", \"r\") as file_object:\n", + " # entfernen von newlines\n", + " lines = [line.strip() for line in file_object.readlines()]\n", + " for line in lines:\n", + " print(line)\n", + " # automatisches schließen" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "62696e616572646174656e\n", + "b'binaerdaten'\n", + "62696e616572646174656e\n", + "bytearray(b'binaerdaten')\n" + ] + } + ], + "source": [ + "# Binärdaten in Python\n", + "\n", + "\"\"\"\n", + "Bytes:\n", + " - Literale enthalten nur ASCII-Zeichen\n", + " - unveränderlich\n", + "\"\"\"\n", + "\n", + "b = b\"binaerdaten\"\n", + "print(b.hex())\n", + "print(b)\n", + "\n", + "\"\"\"\n", + "Bytearray:\n", + " - veränderlich\n", + "\"\"\"\n", + "\n", + "ba = bytearray(b)\n", + "print(ba.hex())\n", + "print(ba)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "50\n" + ] + } + ], + "source": [ + "name = \"PR3 Teil Skriptsprachen\"\n", + "\n", + "with open(\"hello.txt\", \"w\") as f:\n", + " f.truncate()\n", + " print(f.tell())\n", + " f.write(\"Hello, {}!\\n\".format(name))\n", + " f.write(\"Genug der Tollerei\")\n", + " print(f.tell())" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "with open(\"accounts.txt\", mode=\"w\") as f:\n", + " print(\"100 Mali 24.98\", file=f)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Account Name Balance \n", + "100 Jones 24.98 \n", + "200 Doe 345.67 \n", + "300 White 0.0 \n", + "400 Stone -42.16 \n", + "500 Rich 224.62 \n" + ] + } + ], + "source": [ + "# Aufgabe\n", + "\n", + "accounts = list(range(100,600,100))\n", + "names = [\"Jones\", \"Doe\", \"White\", \"Stone\", \"Rich\"]\n", + "balances = [24.98, 345.67, 0.00, -42.16, 224.62]\n", + "\n", + "# Erzeugen von buchungen.txt\n", + "with open(\"buchungen.txt\", \"w\") as file:\n", + " for i in range(5):\n", + " file.write(f\"{accounts[i]} {names[i]} {balances[i]}\\n\")\n", + "\n", + "\n", + "# Formatierte Ausgabe von Buchungen.txt\n", + "with open(\"buchungen.txt\", \"r\") as file:\n", + " lines = [line.strip() for line in file.readlines()]\n", + "\n", + " print(f\"{'Account':<8} {'Name':<8} {'Balance':<8}\")\n", + " for line in lines:\n", + " entry = line.split(\" \")\n", + " entry = (int(entry[0]), entry[1], float(entry[2]))\n", + " print(f\"{entry[0]:<8} {entry[1]:<8} {entry[2]:<8}\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.10.9" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/python/fileio/hello.txt b/python/fileio/hello.txt new file mode 100644 index 0000000..870fbe5 --- /dev/null +++ b/python/fileio/hello.txt @@ -0,0 +1,2 @@ +Hello, PR3 Teil Skriptsprachen! +Genug der Tollerei \ No newline at end of file diff --git a/python/fileio/myfile.txt b/python/fileio/myfile.txt new file mode 100644 index 0000000..a396492 --- /dev/null +++ b/python/fileio/myfile.txt @@ -0,0 +1,2 @@ +Hello World +Bye Jupiter \ No newline at end of file