{ "cells": [ { "cell_type": "markdown", "id": "d32905fd-8062-49a1-a284-efbfbc48d00b", "metadata": {}, "source": [ "# 3. Vorlesung" ] }, { "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": [ "count = 1\n", "while count < 4:\n", " print(count)\n", " count += 1 " ] }, { "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": [ "for count in [1, 2, 3]:\n", " print(count)" ] }, { "cell_type": "markdown", "id": "daaa7cbe-0cb7-45c9-89a8-241561908db2", "metadata": {}, "source": [ "```C\n", "for (int i = 0; i < 4, i++) {}\n", "```" ] }, { "cell_type": "code", "execution_count": 7, "id": "3e461857-f366-46f8-ad51-9800348b4521", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "3\n" ] } ], "source": [ "for count in range(1,4,2):\n", " print(count)" ] }, { "cell_type": "code", "execution_count": 8, "id": "4f4d9b6c-c262-45a0-ab7a-ac8d3f13d110", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [0, 1, 2, 3, 4]\n", "l" ] }, { "cell_type": "code", "execution_count": 9, "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": 10, "id": "c1cb9b0a-170c-4b45-b329-e28b0f8ee818", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(l)" ] }, { "cell_type": "code", "execution_count": 13, "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": [ "for i in range(len(l)):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 14, "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": [ "for i in range(len(l)):\n", " print(l[i])" ] }, { "cell_type": "code", "execution_count": 15, "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": [ "for _ in range(6):\n", " print(\"Hello\")" ] }, { "cell_type": "code", "execution_count": 16, "id": "e1fbf047-ed8c-4a27-9729-6b05ed55140a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'a': 5, 'b': 8, 'c': 10}" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = {\"a\": 5, \"b\": 8, \"c\": 10}\n", "d" ] }, { "cell_type": "code", "execution_count": 17, "id": "faf3bea9-a308-4317-8a5d-ba4281a86671", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_values([5, 8, 10])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.values()" ] }, { "cell_type": "code", "execution_count": 18, "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": 20, "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": 21, "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": [ "for k, v in d.items():\n", " print(f\"Key: {k} mit Wert: {v}\")" ] }, { "cell_type": "code", "execution_count": 22, "id": "dc988e8a-135d-483f-9ae0-d20cc861c558", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 4, 9, 16, 25]" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "squared = []\n", "for i in range(6):\n", " squared.append(i*i)\n", "squared" ] }, { "cell_type": "code", "execution_count": 23, "id": "94f148fb-a1f3-4bd9-82b0-baa3ad0b9d35", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 4, 9, 16, 25]" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sq = [n**2 for n in range(6)]\n", "sq" ] }, { "cell_type": "code", "execution_count": 25, "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": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "di = {}\n", "for n in range(6):\n", " di[n] = n**2\n", "di" ] }, { "cell_type": "code", "execution_count": 26, "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": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dic = {n: n**2 for n in range(6)}\n", "dic" ] }, { "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": "code", "execution_count": 34, "id": "d47d956b-f131-4c4c-acad-4adc5ff1508e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "<_io.TextIOWrapper name='test.txt' mode='r' encoding='UTF-8'>" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = open('test.txt')\n", "f" ] }, { "cell_type": "code", "execution_count": 35, "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": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.readlines()" ] }, { "cell_type": "code", "execution_count": 39, "id": "1c0610b1-b6c2-430f-94c0-e50def936b16", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "<_io.TextIOWrapper name='data.txt' mode='w' encoding='UTF-8'>" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = open('data.txt', 'w')\n", "data" ] }, { "cell_type": "code", "execution_count": 40, "id": "1b74ffb0-487a-4ec7-9ed1-3e51b5c76450", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.write(\"Ich will nachhause\")" ] }, { "cell_type": "code", "execution_count": 41, "id": "f831efc1-b548-4a49-bbed-62c8018ecdfe", "metadata": {}, "outputs": [], "source": [ "f.close()\n", "data.close()" ] }, { "cell_type": "code", "execution_count": 42, "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": [ "f = open('test.txt')\n", "print(f.readlines())\n", "f.close()" ] }, { "cell_type": "code", "execution_count": 44, "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[44], 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\n", "\u001b[0;31mValueError\u001b[0m: I/O operation on closed file." ] } ], "source": [ "f.readlines()" ] }, { "cell_type": "code", "execution_count": 45, "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[45], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mopen\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtest.txt\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;28;01mas\u001b[39;00m f:\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(f\u001b[38;5;241m.\u001b[39mreadlines())\n\u001b[0;32m----> 3\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\n", "\u001b[0;31mValueError\u001b[0m: I/O operation on closed file." ] } ], "source": [ "with open('test.txt', 'r') as f:\n", " print(f.readlines())\n", "f.readlines()" ] }, { "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": [ "" ] }, "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": 51, "id": "c4c97328-95dd-4e6b-bc9c-857ee5d04e25", "metadata": {}, "outputs": [], "source": [ "from math import 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": null, "id": "b83a73fe-8f6e-4b22-97bc-c9016206a6bd", "metadata": {}, "outputs": [], "source": [ "from math import *" ] }, { "cell_type": "code", "execution_count": 53, "id": "d1568734-9077-4444-9c0e-5dbf385dc46a", "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 54, "id": "718bb6e9-1cda-438d-909a-b51064471d0a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "np.float64(94.86832980505137)" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sqrt(9000)" ] }, { "cell_type": "code", "execution_count": null, "id": "806082c4-61fc-4345-bd85-aa4deec1414a", "metadata": {}, "outputs": [], "source": [] } ], "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.5" } }, "nbformat": 4, "nbformat_minor": 5 }