programmieren_wise_24_25/Material/V3.ipynb
2024-11-14 19:06:50 +01:00

1064 lines
21 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "d32905fd-8062-49a1-a284-efbfbc48d00b",
"metadata": {},
"source": [
"# 3. Vorlesung"
]
},
{
"cell_type": "markdown",
"id": "a21df6bb-f501-474a-9e1a-7dd2a90cd92d",
"metadata": {},
"source": [
"### Einfache Zählschleife"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "52207bf8-c854-4249-a011-741cc6f57283",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n"
]
}
],
"source": [
"# Als While Loop\n",
"count = 1 # Zählvariable\n",
"while count < 4: # Bedingung\n",
" print(count)\n",
" count += 1 # Hochzählen"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "cfc37791-5b75-4c51-ae76-a21f306821e0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n"
]
}
],
"source": [
"# Als For Loop\n",
"for count in [1, 2, 3]:\n",
" print(count)"
]
},
{
"cell_type": "markdown",
"id": "daaa7cbe-0cb7-45c9-89a8-241561908db2",
"metadata": {},
"source": [
"Beispiel einer Zählschleife in C:\n",
"\n",
"```C\n",
"for (int i = 0; i < 4, i++) {}\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "3e461857-f366-46f8-ad51-9800348b4521",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n"
]
}
],
"source": [
"# Zählschleife mittels range Funktion\n",
"for count in range(1,4):\n",
" print(count)"
]
},
{
"cell_type": "markdown",
"id": "b572967d-7488-4be7-b8b7-8b0237eddc86",
"metadata": {},
"source": [
"`range` kann bis zu 3 Parameter nehmen.\n",
"\n",
"- 1 Parameter `range(4)` -> Zählt in 1er Schritten bis exklusive der eingegebenen Zahl *0,1,2,3*\n",
"\n",
"Der folgend genutzte Stern `*` sagt Python er soll den `iterator` entpacken."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "30d52051-cee6-4bcd-a622-c70bdd0cae1e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 1 2 3\n"
]
}
],
"source": [
"print(*range(4))"
]
},
{
"cell_type": "markdown",
"id": "8e2dbb80-5bfd-43ee-83b6-8ef299c70391",
"metadata": {},
"source": [
"- 2 Parameter `range(1,4)` -> Zählt in 1er Schritten von dem ersten Parameter bis exklusiv zum zweiten Parameter *1,2,3*"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "fe434e93-729b-466c-a530-125c668f2329",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 2 3\n"
]
}
],
"source": [
"print(*range(1,4))"
]
},
{
"cell_type": "markdown",
"id": "7d5d28a6-b873-4a2b-8e45-b02e75982c10",
"metadata": {},
"source": [
"- 3 Parameter `range(1,11,2)` -> Zählt in `2`er Schritten von dem ersten Parameter bis exklusiv zum zweiten Parameter "
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "03e36a0d-9d0f-4dcd-8e02-3d234da9fb52",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 3 5 7 9\n"
]
}
],
"source": [
"print(*range(1,11,2))"
]
},
{
"cell_type": "markdown",
"id": "698e2a24-d96e-4f39-b76a-bfa2b6d20297",
"metadata": {},
"source": [
"`For-Loops` itertieren über Iteratoren. Listen sind z.b. Iteratoren."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "4f4d9b6c-c262-45a0-ab7a-ac8d3f13d110",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 2, 3, 4]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l = [0, 1, 2, 3, 4]\n",
"l"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "fbcb9b7d-2850-41fe-82a5-09ad75191329",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n"
]
}
],
"source": [
"for el in l:\n",
" print(el)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "c1cb9b0a-170c-4b45-b329-e28b0f8ee818",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(l) # Anzahl 'Länge' der Liste l"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "f595c1f5-4945-4ee4-89e7-cde25d2a7e41",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n"
]
}
],
"source": [
"# range zählt bis 'exklusive' seines Eingabeparameters um folgendes verhalten zu emulieren\n",
"for i in range(len(l)):\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "6902e5e5-0a49-4bce-a03a-f4c4d812ffa7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n"
]
}
],
"source": [
"# Iteration über die Indexe der Liste \n",
"for i in range(len(l)):\n",
" print(l[i]) # Zugriff über Index auf die Elemente der Liste"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "4e2f0c81-894d-424d-848f-3e7cc36bd70b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello\n",
"Hello\n",
"Hello\n",
"Hello\n",
"Hello\n",
"Hello\n"
]
}
],
"source": [
"# _ wird verwendet für Loops die einfach etwas immer und immer wiederholen sollen\n",
"for _ in range(6):\n",
" print(\"Hello\")"
]
},
{
"cell_type": "markdown",
"id": "c555a1d3-dc65-43e1-b19a-070653a34645",
"metadata": {},
"source": [
"Folgende Dict beispiele Eklären sich dementsprechend selber"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "e1fbf047-ed8c-4a27-9729-6b05ed55140a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'a': 5, 'b': 8, 'c': 10}"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = {\"a\": 5, \"b\": 8, \"c\": 10}\n",
"d"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "faf3bea9-a308-4317-8a5d-ba4281a86671",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_values([5, 8, 10])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d.values()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "52262d79-76d2-4bf4-8f06-8ed55dcff7cc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Wert: 5\n",
"Wert: 8\n",
"Wert: 10\n"
]
}
],
"source": [
"for el in d.values():\n",
" print(f\"Wert: {el}\")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "280eb1d9-bfe8-4715-a54a-4b40ef542618",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Key: a\n",
"Key: b\n",
"Key: c\n"
]
}
],
"source": [
"for key in d.keys():\n",
" print(f\"Key: {key}\")"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "7a0fde62-9fa8-4089-b257-d2a2263b2b0d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Key: a mit Wert: 5\n",
"Key: b mit Wert: 8\n",
"Key: c mit Wert: 10\n"
]
}
],
"source": [
"# Items gibt eine Liste mit tupeln zurück, jedes tuple wird in seine Elemente zerlegt und den Variablen k & v zugewiesen\n",
"for k, v in d.items():\n",
" print(f\"Key: {k} mit Wert: {v}\")"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "dc988e8a-135d-483f-9ae0-d20cc861c558",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 4, 9, 16, 25]"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Liste füllen\n",
"squared = []\n",
"for i in range(6):\n",
" squared.append(i*i)\n",
"squared"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "94f148fb-a1f3-4bd9-82b0-baa3ad0b9d35",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 4, 9, 16, 25]"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# List Comprehension \n",
"sq = [n**2 for n in range(6)]\n",
"sq"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "3e6d5db7-3cc1-4b21-9ad0-4d3402b4765b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Dict füllen\n",
"di = {}\n",
"for n in range(6):\n",
" di[n] = n**2\n",
"di"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "6bd693d3-8e27-48c2-9fe4-8ecafb98b181",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Dictionary Comprehension\n",
"dic = {n: n**2 for n in range(6)}\n",
"dic"
]
},
{
"cell_type": "markdown",
"id": "bf09cbc2-c2c5-4f59-8ad7-c7c5e9e50f63",
"metadata": {},
"source": [
"## System Interaction"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "af2dfb75-e7b5-40c9-804f-d1c8fca9d6e0",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
" 4\n"
]
},
{
"data": {
"text/plain": [
"'4'"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"input()"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "43939109-c7e5-4583-8a8d-bc12c03163e6",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
" 6\n"
]
}
],
"source": [
"text = input()"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "d023c299-89a4-4c13-8c6e-aae4feae9004",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'6'"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"text"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "c6e009bc-954d-4bc6-81eb-98efbc82024a",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Gebe bitte eine Zahl ein: 7\n"
]
},
{
"data": {
"text/plain": [
"'7'"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"input(\"Gebe bitte eine Zahl ein:\")"
]
},
{
"cell_type": "markdown",
"id": "60afb4f7-e8c5-431e-a592-b9b719f9b68c",
"metadata": {},
"source": [
"## File Handling"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "d47d956b-f131-4c4c-acad-4adc5ff1508e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<_io.TextIOWrapper name='test.txt' mode='r' encoding='UTF-8'>"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f = open('test.txt') # Öffne File und gebe den Handler an f, Standard im Lesemodus\n",
"f"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "4d38875a-18f9-4ad6-991b-fc61ea1dd08a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Super Secret Message\\n', 'Hallo Welt\\n', 'Geiler Kurs\\n', 'Freitag 15h yeah']"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f.readlines() # Lese den Inhalt aus f"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "1c0610b1-b6c2-430f-94c0-e50def936b16",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<_io.TextIOWrapper name='data.txt' mode='w' encoding='UTF-8'>"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = open('data.txt', 'w') # Öffne eine beschreibare File\n",
"data"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "1b74ffb0-487a-4ec7-9ed1-3e51b5c76450",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"18"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.write(\"Ich will nachhause\") # Schreibe in die File "
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "f831efc1-b548-4a49-bbed-62c8018ecdfe",
"metadata": {},
"outputs": [],
"source": [
"# Schliese die Files\n",
"f.close()\n",
"data.close()"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "4580acb8-cc79-440c-a463-140547883ded",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Super Secret Message\\n', 'Hallo Welt\\n', 'Geiler Kurs\\n', 'Freitag 15h yeah']\n"
]
}
],
"source": [
"# Standard File handling\n",
"f = open('test.txt')\n",
"print(f.readlines())\n",
"f.close()"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "50f35e0c-5138-478c-abe9-dae163c467a4",
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "I/O operation on closed file.",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[33], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mf\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreadlines\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# File ist geschlossen also ist lesen nicht möglich\u001b[39;00m\n",
"\u001b[0;31mValueError\u001b[0m: I/O operation on closed file."
]
}
],
"source": [
"f.readlines() # File ist geschlossen also ist lesen nicht möglich"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "999e5179-4d96-4b8b-bec6-3f8b0a857291",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Super Secret Message\\n', 'Hallo Welt\\n', 'Geiler Kurs\\n', 'Freitag 15h yeah']\n"
]
},
{
"ename": "ValueError",
"evalue": "I/O operation on closed file.",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[35], line 6\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(f\u001b[38;5;241m.\u001b[39mreadlines())\n\u001b[1;32m 5\u001b[0m \u001b[38;5;66;03m# File ist bereits geschlossen \u001b[39;00m\n\u001b[0;32m----> 6\u001b[0m \u001b[43mf\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreadlines\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# Wirft Fehler\u001b[39;00m\n",
"\u001b[0;31mValueError\u001b[0m: I/O operation on closed file."
]
}
],
"source": [
"# Contexte nehmen einem die Arbeit ab\n",
"with open('test.txt', 'r') as f:\n",
" print(f.readlines())\n",
"\n",
"# File ist bereits geschlossen \n",
"f.readlines() # Wirft Fehler"
]
},
{
"cell_type": "markdown",
"id": "d63dce40-9d51-4ab6-92e6-65fedb982dd8",
"metadata": {},
"source": [
"# Importing"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "f92c73ed-c02b-4142-ac27-25f22caaa199",
"metadata": {},
"outputs": [],
"source": [
"import math"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "a911de4f-f9c9-4098-8369-d4f97efab684",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<module 'math' from '/usr/local/Cellar/python@3.12/3.12.5/Frameworks/Python.framework/Versions/3.12/lib/python3.12/lib-dynload/math.cpython-312-darwin.so'>"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"math"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "7513811c-18e9-4cab-9edf-8f383be21027",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.141592653589793"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"math.pi"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "254a6fab-a259-4379-a3e3-8f11560b87d7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.1622776601683795"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"math.sqrt(10)"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "15c63d11-6da3-4c44-a579-16a2f6cbe499",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2.0"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"math.sqrt(4)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "c4c97328-95dd-4e6b-bc9c-857ee5d04e25",
"metadata": {},
"outputs": [],
"source": [
"from math import sqrt"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "1f29d236-0368-4fd4-97c1-a33a6adc7bf3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<function math.sqrt(x, /)>"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sqrt"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "5d5a0a0d-bb66-49ad-bcee-2656afc7af47",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"9.486832980505138"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sqrt(90)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "b83a73fe-8f6e-4b22-97bc-c9016206a6bd",
"metadata": {},
"outputs": [],
"source": [
"from math import * # Böse nicht mache führt nur zu unerklärbaren Fehlern"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "d1568734-9077-4444-9c0e-5dbf385dc46a",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "718bb6e9-1cda-438d-909a-b51064471d0a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.float64(94.86832980505137)"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.sqrt(9000)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "806082c4-61fc-4345-bd85-aa4deec1414a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<module 'numpy' from '/home/phil/Desktop/programmieren_wise_24_25/Material/env/lib64/python3.12/site-packages/numpy/__init__.py'>"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np"
]
}
],
"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
}