{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python - Kurzwiederholung\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Elementare Datentypen" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# eine Variable mit einem int-Wert\n", "i = 10\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Python kann sher grosse Zahlen verarbeiten:\n", "j = 123456395823842193412376429384\n", "j+1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# int-Zahlen sind richtige Objekte - sie brauchen mehr Speicher, \n", "# haben aber auch mehr Fähigkeiten als in anderen Sprachen\n", "import sys\n", "\n", "print(sys.getsizeof(10)) # Specherverbrauch einer einzelnen Zahl\n", "print(i.bit_length()) # Methoden, hier Bitlänge der Zahl" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# ein Float \n", "f = 5.0\n", "\n", "# auch Floats haben Methoden\n", "print(f.is_integer())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Floats haben so ihre Probleme...\n", "0.1 + 0.2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Strings, Konkatenation\n", "s = \"Hello\" + \" \" + \"World\"\n", "s" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Boolsche Werte\n", "a = True\n", "b = False\n", "\n", "print(a and b)\n", "print(a or b)\n", "type(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Umwandlung von Datentypen" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# zahl in string umwandeln\n", "i = 5\n", "str(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# string in zahl umwandeln\n", "s = \"55\"\n", "float(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Kontrollstrukturen: if, while, for, range-Funktion" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "i = 10\n", "\n", "if i > 5:\n", " print(\"i ist grösser 5\")\n", " \n", "while i > 0:\n", " print(i)\n", " i = i - 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in range(5):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# rückwärts zählen\n", "for i in range(10, 4, -1):\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Beispiele für Funktionen" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def add(i, j):\n", " return i+j" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(add(2, 3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Listen, Indizierung, Slicing" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Platznummern\n", "# 0 1 2 3 4 5 6\n", "l = [1, 2, 3, 4, 5, 6, 7]\n", "\n", "# slicing\n", "l[1::2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# enumerate: Aufzählen einer Liste (Tupelbildung mit Index)\n", "list(enumerate([5,9,42]))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l = [\"test\", 1, 5.5, True] # Listen können gemischte Werte enthalten" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tupel" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t = (4, 5) # Tupel einpacken\n", "a, b = t # Tupel auspacken\n", "\n", "print(a)\n", "print(b)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "builtin-Funktionen, Standardbibliothek" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# builtin-Funktion\n", "# range, list, int, float, len, sum, bool, tuple " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# ein Import - hier math\n", "import math" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "math.exp(2.0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dictionaries" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "d = { 'eins': 'one', 'zwei': 'two' }\n", "\n", "d.keys(), d.values()\n", "\n", "for key, value in d.items():\n", " print(key, '->', value)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "List-Comprehensions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Quadratzahlen mit gerader Basis\n", "[ x*x for x in range(0,21) if x % 2 == 0 ]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Statistics" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = [3,4,5,6,7]\n", "mean(a), variance(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b = [0, 2, 4, 6, 8, 10]\n", "mean(b), variance(b)" ] } ], "metadata": { "interpreter": { "hash": "a6b707a736c5fbba452b904aff207ddd250a7524df1f8c74db5bc52ff4a2560b" }, "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.8" } }, "nbformat": 4, "nbformat_minor": 4 }