fileio, lists & tuples started

main
romanamo 2023-06-24 11:59:29 +02:00
parent cfb3a35023
commit 9653cbe6d5
7 changed files with 336 additions and 0 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -0,0 +1 @@
100 Mali 24.98

View File

@ -0,0 +1,5 @@
100 Jones 24.98
200 Doe 345.67
300 White 0.0
400 Stone -42.16
500 Rich 224.62

View File

@ -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
}

View File

@ -0,0 +1,2 @@
Hello, PR3 Teil Skriptsprachen!
Genug der Tollerei

View File

@ -0,0 +1,2 @@
Hello World
Bye Jupiter