{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# BlahBlah" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "i = 666" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "666\n" ] } ], "source": [ "print(i)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(i))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "j = 42" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def f():\n", " print('hallo')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hallo\n" ] } ], "source": [ "f()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(f))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "class Square:\n", " def __init__(self, kantenlaenge):\n", " self.kantenlaenge = kantenlaenge\n", " def area(self):\n", " return self.kantenlaenge ** 2" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "q = Square(10)\n", "q.area()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "s = 'mississippi'\n", "print(type(s))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.count('ss')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.find('ss')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Interactive" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4950\n" ] } ], "source": [ "i = 0\n", "summe = 0\n", "while i < 100:\n", " summe += i\n", " i += 1\n", "print(summe)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## For Beginners" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for element in range(0,5):\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Not For Beginners?" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "i = 666" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(i)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "i = ['eins', 1, 1.0]" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(i)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "a = 42\n", "b = '666'" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " '<' not supported between instances of 'int' and 'str'\n" ] } ], "source": [ "try:\n", " if a < b:\n", " print('jo eh')\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Strings, 1st Explanation" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'abc'\n", "s" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"abc\"\n", "s" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ab\"cd'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'ab\"cd'\n", "s" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"ab'cd\"" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"ab'cd\"\n", "s" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ab'cd\n" ] } ], "source": [ "print(s)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "EOL while scanning string literal (2437887948.py, line 2)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"/tmp/ipykernel_101004/2437887948.py\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m s = 'abc\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m EOL while scanning string literal\n" ] } ], "source": [ "try:\n", " s = 'abc\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Datatypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Integers" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "i = 666 # decimal" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "i" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "i = 0x10 # hexadecimal" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "i" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "i = 0b11" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "i = 1* 2**0 + 1* 2**1" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "i = 0b10100" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "i = 0*2**0 + 0*2**1 + 1*2**2 + 0*2**3 + 1*2**4" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "493" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i = 0o755\n", "i" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551615" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i = 0b1111111111111111111111111111111111111111111111111111111111111111\n", "i" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b1111111111111111111111111111111111111111111111111111111111111111'" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(i)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b10000000000000000000000000000000000000000000000000000000000000000'" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i += 1\n", "bin(i)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551615" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**64-1" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551616" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**64" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b10000000000000000000000000000000000000000000000000000000000000000'" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(2**64)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1267650600228229401496703205376" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**100" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1267650600228229401496703205381" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**100 + 5" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**1000" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Operators" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 < 2" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 <= 2" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 <= 2" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 > 2" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 >= 2" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "cannot assign to literal (735611673.py, line 2)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"/tmp/ipykernel_101004/735611673.py\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m 1 = 2\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m cannot assign to literal\n" ] } ], "source": [ "try:\n", " 1 = 2\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "1 == 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "1 != 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Integer Numbers: Arithmetic" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "1 + 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "1 - 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "3/2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "3//2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "5//2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "5//3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "round(1.5)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(1.3)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function round in module builtins:\n", "\n", "round(number, ndigits=None)\n", " Round a number to a given precision in decimal digits.\n", " \n", " The return value is an integer if ndigits is omitted or None. Otherwise\n", " the return value has the same type as the number. ndigits may be negative.\n", "\n" ] } ], "source": [ "help(round)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math\n", "\n", "math.floor(2.1)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.ceil(2.1)" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 % 2" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "7%4" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "7 % 2" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "def is_even(number):\n", " if number % 2 == 0:\n", " return True\n", " else:\n", " return False" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "is_even(7)" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "is_even(4)" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum([1,2,3])" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [], "source": [ "import statistics" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "statistics.mean([1,2,3])" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2^3 # exclusive or" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b100'" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(0b101 & 0b100)" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b101'" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(0b101 | 0b100)" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b1'" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(0b101 ^ 0b100)" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**3" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [], "source": [ "register_status = 0b1001000" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [], "source": [ "mask = 0b00001000" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b1000'" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(register_status & mask)" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hey, da liegt strom an!\n" ] } ], "source": [ "if register_status & mask:\n", " print('hey, da liegt strom an!')" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [], "source": [ "def liegt_strom_an(register_status):\n", " if register_status & 0b1000:\n", " return True\n", " return False" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liegt_strom_an(0b00111110)" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "43" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i = 42\n", "i = i+1\n", "i" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "44" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i += 1\n", "i" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [], "source": [ "i += 52" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "96" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Operator Precedence" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 + 2 * 3" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(1 + 2) * 3" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.5" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 * 3 / 4" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 * 3 % 2" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(2 * 3) % 2" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 * 3 ** 2" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "16" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 * (2 ** 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Floating Point Numbers" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.6666666666666666" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2/3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**DONT EVER DO THIS:**" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2/3 == 100/150" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Datatype Conversions" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [], "source": [ "s = '666'" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [], "source": [ "i = int(s)" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [], "source": [ "i = 42" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'42'" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(i)" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666.42" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float('666.42')" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(666.42)" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666.0" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(666)" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "16" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('10', 16)" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3735928559" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('deadbeef', 16)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Boolean" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not (1<2)" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not 1<2" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not 1" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not 0" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True or False" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True and False" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "False or True" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "False or False" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "False or (True and True)" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [], "source": [ "i = 666" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "eh im range\n" ] } ], "source": [ "if i > 0 and i < 1000:\n", " print('eh im range')" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "jawui ein akademiker\n" ] } ], "source": [ "s = 'grosses dokument mit doktor irgendwo drin'\n", "if s.find('doktor') != -1:\n", " print('jawui ein akademiker')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More About Strings" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "abc\n" ] } ], "source": [ "s = 'abc'\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "abc\n" ] } ], "source": [ "s = \"abc\"\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ab\"cd\n" ] } ], "source": [ "s = 'ab\"cd'\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ab'cd\n" ] } ], "source": [ "s = \"ab'cd\"\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "abc\n", "def\n" ] } ], "source": [ "s = 'abc\\ndef'\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "abc\tdef\n" ] } ], "source": [ "s = 'abc\\tdef'\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ab'cd\n" ] } ], "source": [ "s = 'ab\\'cd'\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "abc\"def'geh\n" ] } ], "source": [ "s = 'abc\"def\\'geh'\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\some\n", "ame\n" ] } ], "source": [ "s = 'C:\\some\\name'\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 117, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\some\\name\n" ] } ], "source": [ "s = 'C:\\some\\\\name'\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\some\\name\n" ] } ], "source": [ "s = r'C:\\some\\name'\n", "print(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Multiline strings**" ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "eine zeile\n", "noch eine zeile\n", "und noch eine\n", "und jetzt die letzte\n", "\n" ] } ], "source": [ "s = '''\n", "eine zeile\n", "noch eine zeile\n", "und noch eine\n", "und jetzt die letzte\n", "'''\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "abcdef\n" ] } ], "source": [ "s = 'abc' 'def'\n", "print(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Docstrings" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [], "source": [ "def summe(a, b):\n", " '''Diese hochkomplexe Algorithmus verendet Euler'sche woswasi\n", " was man hier noch fuer einen Bullshit dokumentiern koennte,\n", " und noch 200 zeilen hier\n", " '''\n", " return a+b" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] } ], "source": [ "s = summe(1,2)\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "function" ] }, "execution_count": 123, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(summe)" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"Diese hochkomplexe Algorithmus verendet Euler'sche woswasi\\n was man hier noch fuer einen Bullshit dokumentiern koennte,\\n und noch 200 zeilen hier\\n \"" ] }, "execution_count": 124, "metadata": {}, "output_type": "execute_result" } ], "source": [ "summe.__doc__" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function summe in module __main__:\n", "\n", "summe(a, b)\n", " Diese hochkomplexe Algorithmus verendet Euler'sche woswasi\n", " was man hier noch fuer einen Bullshit dokumentiern koennte,\n", " und noch 200 zeilen hier\n", "\n" ] } ], "source": [ "help(summe)" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [], "source": [ "import statistics" ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on module statistics:\n", "\n", "NAME\n", " statistics - Basic statistics module.\n", "\n", "MODULE REFERENCE\n", " https://docs.python.org/3.9/library/statistics\n", " \n", " The following documentation is automatically generated from the Python\n", " source files. It may be incomplete, incorrect or include features that\n", " are considered implementation detail and may vary between Python\n", " implementations. When in doubt, consult the module reference at the\n", " location listed above.\n", "\n", "DESCRIPTION\n", " This module provides functions for calculating statistics of data, including\n", " averages, variance, and standard deviation.\n", " \n", " Calculating averages\n", " --------------------\n", " \n", " ================== ==================================================\n", " Function Description\n", " ================== ==================================================\n", " mean Arithmetic mean (average) of data.\n", " fmean Fast, floating point arithmetic mean.\n", " geometric_mean Geometric mean of data.\n", " harmonic_mean Harmonic mean of data.\n", " median Median (middle value) of data.\n", " median_low Low median of data.\n", " median_high High median of data.\n", " median_grouped Median, or 50th percentile, of grouped data.\n", " mode Mode (most common value) of data.\n", " multimode List of modes (most common values of data).\n", " quantiles Divide data into intervals with equal probability.\n", " ================== ==================================================\n", " \n", " Calculate the arithmetic mean (\"the average\") of data:\n", " \n", " >>> mean([-1.0, 2.5, 3.25, 5.75])\n", " 2.625\n", " \n", " \n", " Calculate the standard median of discrete data:\n", " \n", " >>> median([2, 3, 4, 5])\n", " 3.5\n", " \n", " \n", " Calculate the median, or 50th percentile, of data grouped into class intervals\n", " centred on the data values provided. E.g. if your data points are rounded to\n", " the nearest whole number:\n", " \n", " >>> median_grouped([2, 2, 3, 3, 3, 4]) #doctest: +ELLIPSIS\n", " 2.8333333333...\n", " \n", " This should be interpreted in this way: you have two data points in the class\n", " interval 1.5-2.5, three data points in the class interval 2.5-3.5, and one in\n", " the class interval 3.5-4.5. The median of these data points is 2.8333...\n", " \n", " \n", " Calculating variability or spread\n", " ---------------------------------\n", " \n", " ================== =============================================\n", " Function Description\n", " ================== =============================================\n", " pvariance Population variance of data.\n", " variance Sample variance of data.\n", " pstdev Population standard deviation of data.\n", " stdev Sample standard deviation of data.\n", " ================== =============================================\n", " \n", " Calculate the standard deviation of sample data:\n", " \n", " >>> stdev([2.5, 3.25, 5.5, 11.25, 11.75]) #doctest: +ELLIPSIS\n", " 4.38961843444...\n", " \n", " If you have previously calculated the mean, you can pass it as the optional\n", " second argument to the four \"spread\" functions to avoid recalculating it:\n", " \n", " >>> data = [1, 2, 2, 4, 4, 4, 5, 6]\n", " >>> mu = mean(data)\n", " >>> pvariance(data, mu)\n", " 2.5\n", " \n", " \n", " Exceptions\n", " ----------\n", " \n", " A single exception is defined: StatisticsError is a subclass of ValueError.\n", "\n", "CLASSES\n", " builtins.ValueError(builtins.Exception)\n", " StatisticsError\n", " builtins.object\n", " NormalDist\n", " \n", " class NormalDist(builtins.object)\n", " | NormalDist(mu=0.0, sigma=1.0)\n", " | \n", " | Normal distribution of a random variable\n", " | \n", " | Methods defined here:\n", " | \n", " | __add__(x1, x2)\n", " | Add a constant or another NormalDist instance.\n", " | \n", " | If *other* is a constant, translate mu by the constant,\n", " | leaving sigma unchanged.\n", " | \n", " | If *other* is a NormalDist, add both the means and the variances.\n", " | Mathematically, this works only if the two distributions are\n", " | independent or if they are jointly normally distributed.\n", " | \n", " | __eq__(x1, x2)\n", " | Two NormalDist objects are equal if their mu and sigma are both equal.\n", " | \n", " | __hash__(self)\n", " | NormalDist objects hash equal if their mu and sigma are both equal.\n", " | \n", " | __init__(self, mu=0.0, sigma=1.0)\n", " | NormalDist where mu is the mean and sigma is the standard deviation.\n", " | \n", " | __mul__(x1, x2)\n", " | Multiply both mu and sigma by a constant.\n", " | \n", " | Used for rescaling, perhaps to change measurement units.\n", " | Sigma is scaled with the absolute value of the constant.\n", " | \n", " | __neg__(x1)\n", " | Negates mu while keeping sigma the same.\n", " | \n", " | __pos__(x1)\n", " | Return a copy of the instance.\n", " | \n", " | __radd__ = __add__(x1, x2)\n", " | \n", " | __repr__(self)\n", " | Return repr(self).\n", " | \n", " | __rmul__ = __mul__(x1, x2)\n", " | \n", " | __rsub__(x1, x2)\n", " | Subtract a NormalDist from a constant or another NormalDist.\n", " | \n", " | __sub__(x1, x2)\n", " | Subtract a constant or another NormalDist instance.\n", " | \n", " | If *other* is a constant, translate by the constant mu,\n", " | leaving sigma unchanged.\n", " | \n", " | If *other* is a NormalDist, subtract the means and add the variances.\n", " | Mathematically, this works only if the two distributions are\n", " | independent or if they are jointly normally distributed.\n", " | \n", " | __truediv__(x1, x2)\n", " | Divide both mu and sigma by a constant.\n", " | \n", " | Used for rescaling, perhaps to change measurement units.\n", " | Sigma is scaled with the absolute value of the constant.\n", " | \n", " | cdf(self, x)\n", " | Cumulative distribution function. P(X <= x)\n", " | \n", " | inv_cdf(self, p)\n", " | Inverse cumulative distribution function. x : P(X <= x) = p\n", " | \n", " | Finds the value of the random variable such that the probability of\n", " | the variable being less than or equal to that value equals the given\n", " | probability.\n", " | \n", " | This function is also called the percent point function or quantile\n", " | function.\n", " | \n", " | overlap(self, other)\n", " | Compute the overlapping coefficient (OVL) between two normal distributions.\n", " | \n", " | Measures the agreement between two normal probability distributions.\n", " | Returns a value between 0.0 and 1.0 giving the overlapping area in\n", " | the two underlying probability density functions.\n", " | \n", " | >>> N1 = NormalDist(2.4, 1.6)\n", " | >>> N2 = NormalDist(3.2, 2.0)\n", " | >>> N1.overlap(N2)\n", " | 0.8035050657330205\n", " | \n", " | pdf(self, x)\n", " | Probability density function. P(x <= X < x+dx) / dx\n", " | \n", " | quantiles(self, n=4)\n", " | Divide into *n* continuous intervals with equal probability.\n", " | \n", " | Returns a list of (n - 1) cut points separating the intervals.\n", " | \n", " | Set *n* to 4 for quartiles (the default). Set *n* to 10 for deciles.\n", " | Set *n* to 100 for percentiles which gives the 99 cuts points that\n", " | separate the normal distribution in to 100 equal sized groups.\n", " | \n", " | samples(self, n, *, seed=None)\n", " | Generate *n* samples for a given mean and standard deviation.\n", " | \n", " | zscore(self, x)\n", " | Compute the Standard Score. (x - mean) / stdev\n", " | \n", " | Describes *x* in terms of the number of standard deviations\n", " | above or below the mean of the normal distribution.\n", " | \n", " | ----------------------------------------------------------------------\n", " | Class methods defined here:\n", " | \n", " | from_samples(data) from builtins.type\n", " | Make a normal distribution instance from sample data.\n", " | \n", " | ----------------------------------------------------------------------\n", " | Readonly properties defined here:\n", " | \n", " | mean\n", " | Arithmetic mean of the normal distribution.\n", " | \n", " | median\n", " | Return the median of the normal distribution\n", " | \n", " | mode\n", " | Return the mode of the normal distribution\n", " | \n", " | The mode is the value x where which the probability density\n", " | function (pdf) takes its maximum value.\n", " | \n", " | stdev\n", " | Standard deviation of the normal distribution.\n", " | \n", " | variance\n", " | Square of the standard deviation.\n", " \n", " class StatisticsError(builtins.ValueError)\n", " | Method resolution order:\n", " | StatisticsError\n", " | builtins.ValueError\n", " | builtins.Exception\n", " | builtins.BaseException\n", " | builtins.object\n", " | \n", " | Data descriptors defined here:\n", " | \n", " | __weakref__\n", " | list of weak references to the object (if defined)\n", " | \n", " | ----------------------------------------------------------------------\n", " | Methods inherited from builtins.ValueError:\n", " | \n", " | __init__(self, /, *args, **kwargs)\n", " | Initialize self. See help(type(self)) for accurate signature.\n", " | \n", " | ----------------------------------------------------------------------\n", " | Static methods inherited from builtins.ValueError:\n", " | \n", " | __new__(*args, **kwargs) from builtins.type\n", " | Create and return a new object. See help(type) for accurate signature.\n", " | \n", " | ----------------------------------------------------------------------\n", " | Methods inherited from builtins.BaseException:\n", " | \n", " | __delattr__(self, name, /)\n", " | Implement delattr(self, name).\n", " | \n", " | __getattribute__(self, name, /)\n", " | Return getattr(self, name).\n", " | \n", " | __reduce__(...)\n", " | Helper for pickle.\n", " | \n", " | __repr__(self, /)\n", " | Return repr(self).\n", " | \n", " | __setattr__(self, name, value, /)\n", " | Implement setattr(self, name, value).\n", " | \n", " | __setstate__(...)\n", " | \n", " | __str__(self, /)\n", " | Return str(self).\n", " | \n", " | with_traceback(...)\n", " | Exception.with_traceback(tb) --\n", " | set self.__traceback__ to tb and return self.\n", " | \n", " | ----------------------------------------------------------------------\n", " | Data descriptors inherited from builtins.BaseException:\n", " | \n", " | __cause__\n", " | exception cause\n", " | \n", " | __context__\n", " | exception context\n", " | \n", " | __dict__\n", " | \n", " | __suppress_context__\n", " | \n", " | __traceback__\n", " | \n", " | args\n", "\n", "FUNCTIONS\n", " fmean(data)\n", " Convert data to floats and compute the arithmetic mean.\n", " \n", " This runs faster than the mean() function and it always returns a float.\n", " If the input dataset is empty, it raises a StatisticsError.\n", " \n", " >>> fmean([3.5, 4.0, 5.25])\n", " 4.25\n", " \n", " geometric_mean(data)\n", " Convert data to floats and compute the geometric mean.\n", " \n", " Raises a StatisticsError if the input dataset is empty,\n", " if it contains a zero, or if it contains a negative value.\n", " \n", " No special efforts are made to achieve exact results.\n", " (However, this may change in the future.)\n", " \n", " >>> round(geometric_mean([54, 24, 36]), 9)\n", " 36.0\n", " \n", " harmonic_mean(data)\n", " Return the harmonic mean of data.\n", " \n", " The harmonic mean, sometimes called the subcontrary mean, is the\n", " reciprocal of the arithmetic mean of the reciprocals of the data,\n", " and is often appropriate when averaging quantities which are rates\n", " or ratios, for example speeds. Example:\n", " \n", " Suppose an investor purchases an equal value of shares in each of\n", " three companies, with P/E (price/earning) ratios of 2.5, 3 and 10.\n", " What is the average P/E ratio for the investor's portfolio?\n", " \n", " >>> harmonic_mean([2.5, 3, 10]) # For an equal investment portfolio.\n", " 3.6\n", " \n", " Using the arithmetic mean would give an average of about 5.167, which\n", " is too high.\n", " \n", " If ``data`` is empty, or any element is less than zero,\n", " ``harmonic_mean`` will raise ``StatisticsError``.\n", " \n", " mean(data)\n", " Return the sample arithmetic mean of data.\n", " \n", " >>> mean([1, 2, 3, 4, 4])\n", " 2.8\n", " \n", " >>> from fractions import Fraction as F\n", " >>> mean([F(3, 7), F(1, 21), F(5, 3), F(1, 3)])\n", " Fraction(13, 21)\n", " \n", " >>> from decimal import Decimal as D\n", " >>> mean([D(\"0.5\"), D(\"0.75\"), D(\"0.625\"), D(\"0.375\")])\n", " Decimal('0.5625')\n", " \n", " If ``data`` is empty, StatisticsError will be raised.\n", " \n", " median(data)\n", " Return the median (middle value) of numeric data.\n", " \n", " When the number of data points is odd, return the middle data point.\n", " When the number of data points is even, the median is interpolated by\n", " taking the average of the two middle values:\n", " \n", " >>> median([1, 3, 5])\n", " 3\n", " >>> median([1, 3, 5, 7])\n", " 4.0\n", " \n", " median_grouped(data, interval=1)\n", " Return the 50th percentile (median) of grouped continuous data.\n", " \n", " >>> median_grouped([1, 2, 2, 3, 4, 4, 4, 4, 4, 5])\n", " 3.7\n", " >>> median_grouped([52, 52, 53, 54])\n", " 52.5\n", " \n", " This calculates the median as the 50th percentile, and should be\n", " used when your data is continuous and grouped. In the above example,\n", " the values 1, 2, 3, etc. actually represent the midpoint of classes\n", " 0.5-1.5, 1.5-2.5, 2.5-3.5, etc. The middle value falls somewhere in\n", " class 3.5-4.5, and interpolation is used to estimate it.\n", " \n", " Optional argument ``interval`` represents the class interval, and\n", " defaults to 1. Changing the class interval naturally will change the\n", " interpolated 50th percentile value:\n", " \n", " >>> median_grouped([1, 3, 3, 5, 7], interval=1)\n", " 3.25\n", " >>> median_grouped([1, 3, 3, 5, 7], interval=2)\n", " 3.5\n", " \n", " This function does not check whether the data points are at least\n", " ``interval`` apart.\n", " \n", " median_high(data)\n", " Return the high median of data.\n", " \n", " When the number of data points is odd, the middle value is returned.\n", " When it is even, the larger of the two middle values is returned.\n", " \n", " >>> median_high([1, 3, 5])\n", " 3\n", " >>> median_high([1, 3, 5, 7])\n", " 5\n", " \n", " median_low(data)\n", " Return the low median of numeric data.\n", " \n", " When the number of data points is odd, the middle value is returned.\n", " When it is even, the smaller of the two middle values is returned.\n", " \n", " >>> median_low([1, 3, 5])\n", " 3\n", " >>> median_low([1, 3, 5, 7])\n", " 3\n", " \n", " mode(data)\n", " Return the most common data point from discrete or nominal data.\n", " \n", " ``mode`` assumes discrete data, and returns a single value. This is the\n", " standard treatment of the mode as commonly taught in schools:\n", " \n", " >>> mode([1, 1, 2, 3, 3, 3, 3, 4])\n", " 3\n", " \n", " This also works with nominal (non-numeric) data:\n", " \n", " >>> mode([\"red\", \"blue\", \"blue\", \"red\", \"green\", \"red\", \"red\"])\n", " 'red'\n", " \n", " If there are multiple modes with same frequency, return the first one\n", " encountered:\n", " \n", " >>> mode(['red', 'red', 'green', 'blue', 'blue'])\n", " 'red'\n", " \n", " If *data* is empty, ``mode``, raises StatisticsError.\n", " \n", " multimode(data)\n", " Return a list of the most frequently occurring values.\n", " \n", " Will return more than one result if there are multiple modes\n", " or an empty list if *data* is empty.\n", " \n", " >>> multimode('aabbbbbbbbcc')\n", " ['b']\n", " >>> multimode('aabbbbccddddeeffffgg')\n", " ['b', 'd', 'f']\n", " >>> multimode('')\n", " []\n", " \n", " pstdev(data, mu=None)\n", " Return the square root of the population variance.\n", " \n", " See ``pvariance`` for arguments and other details.\n", " \n", " >>> pstdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])\n", " 0.986893273527251\n", " \n", " pvariance(data, mu=None)\n", " Return the population variance of ``data``.\n", " \n", " data should be a sequence or iterable of Real-valued numbers, with at least one\n", " value. The optional argument mu, if given, should be the mean of\n", " the data. If it is missing or None, the mean is automatically calculated.\n", " \n", " Use this function to calculate the variance from the entire population.\n", " To estimate the variance from a sample, the ``variance`` function is\n", " usually a better choice.\n", " \n", " Examples:\n", " \n", " >>> data = [0.0, 0.25, 0.25, 1.25, 1.5, 1.75, 2.75, 3.25]\n", " >>> pvariance(data)\n", " 1.25\n", " \n", " If you have already calculated the mean of the data, you can pass it as\n", " the optional second argument to avoid recalculating it:\n", " \n", " >>> mu = mean(data)\n", " >>> pvariance(data, mu)\n", " 1.25\n", " \n", " Decimals and Fractions are supported:\n", " \n", " >>> from decimal import Decimal as D\n", " >>> pvariance([D(\"27.5\"), D(\"30.25\"), D(\"30.25\"), D(\"34.5\"), D(\"41.75\")])\n", " Decimal('24.815')\n", " \n", " >>> from fractions import Fraction as F\n", " >>> pvariance([F(1, 4), F(5, 4), F(1, 2)])\n", " Fraction(13, 72)\n", " \n", " quantiles(data, *, n=4, method='exclusive')\n", " Divide *data* into *n* continuous intervals with equal probability.\n", " \n", " Returns a list of (n - 1) cut points separating the intervals.\n", " \n", " Set *n* to 4 for quartiles (the default). Set *n* to 10 for deciles.\n", " Set *n* to 100 for percentiles which gives the 99 cuts points that\n", " separate *data* in to 100 equal sized groups.\n", " \n", " The *data* can be any iterable containing sample.\n", " The cut points are linearly interpolated between data points.\n", " \n", " If *method* is set to *inclusive*, *data* is treated as population\n", " data. The minimum value is treated as the 0th percentile and the\n", " maximum value is treated as the 100th percentile.\n", " \n", " stdev(data, xbar=None)\n", " Return the square root of the sample variance.\n", " \n", " See ``variance`` for arguments and other details.\n", " \n", " >>> stdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])\n", " 1.0810874155219827\n", " \n", " variance(data, xbar=None)\n", " Return the sample variance of data.\n", " \n", " data should be an iterable of Real-valued numbers, with at least two\n", " values. The optional argument xbar, if given, should be the mean of\n", " the data. If it is missing or None, the mean is automatically calculated.\n", " \n", " Use this function when your data is a sample from a population. To\n", " calculate the variance from the entire population, see ``pvariance``.\n", " \n", " Examples:\n", " \n", " >>> data = [2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5]\n", " >>> variance(data)\n", " 1.3720238095238095\n", " \n", " If you have already calculated the mean of your data, you can pass it as\n", " the optional second argument ``xbar`` to avoid recalculating it:\n", " \n", " >>> m = mean(data)\n", " >>> variance(data, m)\n", " 1.3720238095238095\n", " \n", " This function does not check that ``xbar`` is actually the mean of\n", " ``data``. Giving arbitrary values for ``xbar`` may lead to invalid or\n", " impossible results.\n", " \n", " Decimals and Fractions are supported:\n", " \n", " >>> from decimal import Decimal as D\n", " >>> variance([D(\"27.5\"), D(\"30.25\"), D(\"30.25\"), D(\"34.5\"), D(\"41.75\")])\n", " Decimal('31.01875')\n", " \n", " >>> from fractions import Fraction as F\n", " >>> variance([F(1, 6), F(1, 2), F(5, 3)])\n", " Fraction(67, 108)\n", "\n", "DATA\n", " __all__ = ['NormalDist', 'StatisticsError', 'fmean', 'geometric_mean',...\n", "\n", "FILE\n", " /usr/lib64/python3.9/statistics.py\n", "\n", "\n" ] } ], "source": [ "help(statistics)" ] }, { "cell_type": "code", "execution_count": 128, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__annotations__',\n", " '__call__',\n", " '__class__',\n", " '__closure__',\n", " '__code__',\n", " '__defaults__',\n", " '__delattr__',\n", " '__dict__',\n", " '__dir__',\n", " '__doc__',\n", " '__eq__',\n", " '__format__',\n", " '__ge__',\n", " '__get__',\n", " '__getattribute__',\n", " '__globals__',\n", " '__gt__',\n", " '__hash__',\n", " '__init__',\n", " '__init_subclass__',\n", " '__kwdefaults__',\n", " '__le__',\n", " '__lt__',\n", " '__module__',\n", " '__name__',\n", " '__ne__',\n", " '__new__',\n", " '__qualname__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__setattr__',\n", " '__sizeof__',\n", " '__str__',\n", " '__subclasshook__']" ] }, "execution_count": 128, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(summe)" ] }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "300" ] }, "execution_count": 129, "metadata": {}, "output_type": "execute_result" } ], "source": [ "summe(100, 200)" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "300" ] }, "execution_count": 130, "metadata": {}, "output_type": "execute_result" } ], "source": [ "summe.__call__(100, 200)" ] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [], "source": [ "i = 666" ] }, { "cell_type": "code", "execution_count": 132, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__abs__',\n", " '__add__',\n", " '__and__',\n", " '__bool__',\n", " '__ceil__',\n", " '__class__',\n", " '__delattr__',\n", " '__dir__',\n", " '__divmod__',\n", " '__doc__',\n", " '__eq__',\n", " '__float__',\n", " '__floor__',\n", " '__floordiv__',\n", " '__format__',\n", " '__ge__',\n", " '__getattribute__',\n", " '__getnewargs__',\n", " '__gt__',\n", " '__hash__',\n", " '__index__',\n", " '__init__',\n", " '__init_subclass__',\n", " '__int__',\n", " '__invert__',\n", " '__le__',\n", " '__lshift__',\n", " '__lt__',\n", " '__mod__',\n", " '__mul__',\n", " '__ne__',\n", " '__neg__',\n", " '__new__',\n", " '__or__',\n", " '__pos__',\n", " '__pow__',\n", " '__radd__',\n", " '__rand__',\n", " '__rdivmod__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__rfloordiv__',\n", " '__rlshift__',\n", " '__rmod__',\n", " '__rmul__',\n", " '__ror__',\n", " '__round__',\n", " '__rpow__',\n", " '__rrshift__',\n", " '__rshift__',\n", " '__rsub__',\n", " '__rtruediv__',\n", " '__rxor__',\n", " '__setattr__',\n", " '__sizeof__',\n", " '__str__',\n", " '__sub__',\n", " '__subclasshook__',\n", " '__truediv__',\n", " '__trunc__',\n", " '__xor__',\n", " 'as_integer_ratio',\n", " 'bit_length',\n", " 'conjugate',\n", " 'denominator',\n", " 'from_bytes',\n", " 'imag',\n", " 'numerator',\n", " 'real',\n", " 'to_bytes']" ] }, "execution_count": 132, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(i)" ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "669" ] }, "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i + 3" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "669" ] }, "execution_count": 134, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i.__add__(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Einschub: String-Vergleich vs. Integer-Vergleich" ] }, { "cell_type": "code", "execution_count": 135, "metadata": {}, "outputs": [], "source": [ "s1 = 'abc'\n", "s2 = 'def'" ] }, { "cell_type": "code", "execution_count": 136, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 136, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 < s2" ] }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "outputs": [], "source": [ "num1 = '123'\n", "num2 = '4'" ] }, { "cell_type": "code", "execution_count": 138, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "49" ] }, "execution_count": 138, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ord('1')" ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "52" ] }, "execution_count": 139, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ord('4')" ] }, { "cell_type": "code", "execution_count": 140, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 140, "metadata": {}, "output_type": "execute_result" } ], "source": [ "num1 < num2" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 141, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(num1) < int(num2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Strings" ] }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [], "source": [ "s = \"Hallo\"" ] }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "outputs": [], "source": [ "s += ', Joerg'" ] }, { "cell_type": "code", "execution_count": 144, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hallo, Joerg'" ] }, "execution_count": 144, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 145, "metadata": {}, "outputs": [], "source": [ "s1 = 'Guten Morgen'\n", "s2 = 'Joerg'" ] }, { "cell_type": "code", "execution_count": 146, "metadata": {}, "outputs": [], "source": [ "gesamter_gruss = s1 + ', ' + s2" ] }, { "cell_type": "code", "execution_count": 147, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Guten Morgen, Joerg'" ] }, "execution_count": 147, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gesamter_gruss" ] }, { "cell_type": "code", "execution_count": 148, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hallo'" ] }, "execution_count": 148, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'HALLO'\n", "s = s.lower()\n", "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Random Numbers" ] }, { "cell_type": "code", "execution_count": 149, "metadata": {}, "outputs": [], "source": [ "import random" ] }, { "cell_type": "code", "execution_count": 150, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 150, "metadata": {}, "output_type": "execute_result" } ], "source": [ "random.randrange(1,7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Sequential Datatypes" ] }, { "cell_type": "code", "execution_count": 151, "metadata": {}, "outputs": [], "source": [ "s = 'abc'" ] }, { "cell_type": "code", "execution_count": 152, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'a'" ] }, "execution_count": 152, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[0]" ] }, { "cell_type": "code", "execution_count": 153, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " string index out of range\n" ] } ], "source": [ "try:\n", " s[3]\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 154, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 154, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = '12345'\n", "s.isalpha()" ] }, { "cell_type": "code", "execution_count": 155, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 155, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.isdigit()" ] }, { "cell_type": "code", "execution_count": 156, "metadata": {}, "outputs": [], "source": [ "l = [1, 'eins', 1.0]" ] }, { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [], "source": [ "l.append('one')" ] }, { "cell_type": "code", "execution_count": 158, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 'eins', 1.0, 'one']" ] }, "execution_count": 158, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 159, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 159, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(l)" ] }, { "cell_type": "code", "execution_count": 160, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 160, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[0]" ] }, { "cell_type": "code", "execution_count": 161, "metadata": {}, "outputs": [], "source": [ "try:\n", " l[3]\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 162, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 162, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sex = 'f'\n", "sex in ('f', 'F')" ] }, { "cell_type": "code", "execution_count": 163, "metadata": {}, "outputs": [], "source": [ "t = (1, 'eins', 1.0)" ] }, { "cell_type": "code", "execution_count": 164, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'tuple' object has no attribute 'append'\n" ] } ], "source": [ "try:\n", " t.append('one')\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 165, "metadata": {}, "outputs": [], "source": [ "l1 = [1,2,3]\n", "l2 = [4, 5, 6]" ] }, { "cell_type": "code", "execution_count": 166, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6]" ] }, "execution_count": 166, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = l1 + l2\n", "l" ] }, { "cell_type": "code", "execution_count": 167, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, 3, 4, 5, 6)" ] }, "execution_count": 167, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t1 = (1,2,3)\n", "t2 = (4,5,6)\n", "t = t1+t2\n", "t" ] }, { "cell_type": "code", "execution_count": 168, "metadata": {}, "outputs": [], "source": [ "s = 'Hello World'" ] }, { "cell_type": "code", "execution_count": 169, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 169, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(s)" ] }, { "cell_type": "code", "execution_count": 170, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'H'" ] }, "execution_count": 170, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[0]" ] }, { "cell_type": "code", "execution_count": 171, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'d'" ] }, "execution_count": 171, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[10]" ] }, { "cell_type": "code", "execution_count": 172, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " string index out of range\n" ] } ], "source": [ "try:\n", " s[len(s)]\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 173, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'d'" ] }, "execution_count": 173, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[len(s)-1]" ] }, { "cell_type": "code", "execution_count": 174, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'d'" ] }, "execution_count": 174, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[-1]" ] }, { "cell_type": "code", "execution_count": 175, "metadata": {}, "outputs": [], "source": [ "l = [4, 1, 'one', 1.0, [1,2,3], 7]" ] }, { "cell_type": "code", "execution_count": 176, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 176, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 in l" ] }, { "cell_type": "code", "execution_count": 177, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 177, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1100 in l" ] }, { "cell_type": "code", "execution_count": 178, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 178, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(l)" ] }, { "cell_type": "code", "execution_count": 179, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "jo eh\n" ] } ], "source": [ "if 1 in l:\n", " print('jo eh')" ] }, { "cell_type": "code", "execution_count": 180, "metadata": {}, "outputs": [], "source": [ "excelsheet = [\n", " [1, 'Hansi', 'Hinterseer'],\n", " [2, 'Andreas', 'Gabalier'],\n", " [3, 'Helene', 'Fischer'] \n", "]" ] }, { "cell_type": "code", "execution_count": 181, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 181, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(excelsheet)" ] }, { "cell_type": "code", "execution_count": 182, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 'Hansi', 'Hinterseer']" ] }, "execution_count": 182, "metadata": {}, "output_type": "execute_result" } ], "source": [ "excelsheet[0]" ] }, { "cell_type": "code", "execution_count": 183, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hansi'" ] }, "execution_count": 183, "metadata": {}, "output_type": "execute_result" } ], "source": [ "excelsheet[0][1]" ] }, { "cell_type": "code", "execution_count": 184, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Gabalier'" ] }, "execution_count": 184, "metadata": {}, "output_type": "execute_result" } ], "source": [ "excelsheet[1][2]" ] }, { "cell_type": "code", "execution_count": 185, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hinterseer\n", "Gabalier\n", "Fischer\n" ] } ], "source": [ "for row in excelsheet:\n", " print(row[2])" ] }, { "cell_type": "code", "execution_count": 186, "metadata": {}, "outputs": [], "source": [ "namen = ''" ] }, { "cell_type": "code", "execution_count": 187, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hinterseer,Gabalier,Fischer,\n" ] } ], "source": [ "for row in excelsheet:\n", " namen += row[2] + ','\n", "print(namen)" ] }, { "cell_type": "code", "execution_count": 188, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]" ] }, "execution_count": 188, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [1, 2, 3, 4]\n", "l * 4" ] }, { "cell_type": "code", "execution_count": 189, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 189, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'mississippi'\n", "'s' in s" ] }, { "cell_type": "code", "execution_count": 190, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 190, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'ss' in s" ] }, { "cell_type": "code", "execution_count": 191, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 191, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.count('ss')" ] }, { "cell_type": "code", "execution_count": 192, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 192, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.find('ss')" ] }, { "cell_type": "code", "execution_count": 193, "metadata": {}, "outputs": [], "source": [ "pos = s.find('ss')" ] }, { "cell_type": "code", "execution_count": 194, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 194, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pos" ] }, { "cell_type": "code", "execution_count": 195, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 195, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.find('ss', pos+1)" ] }, { "cell_type": "code", "execution_count": 196, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 196, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.count('s')" ] }, { "cell_type": "code", "execution_count": 197, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'sssklndkfgv fosvh s sd'" ] }, "execution_count": 197, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = ' sssklndkfgv fosvh s sd \\n\\t '\n", "s.strip()" ] }, { "cell_type": "code", "execution_count": 198, "metadata": {}, "outputs": [], "source": [ "s = 'HALLO'.center(100)" ] }, { "cell_type": "code", "execution_count": 199, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 199, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Indexing and Slicing" ] }, { "cell_type": "code", "execution_count": 200, "metadata": {}, "outputs": [], "source": [ "s = 'Hello World'" ] }, { "cell_type": "code", "execution_count": 201, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'o'" ] }, "execution_count": 201, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[4]" ] }, { "cell_type": "code", "execution_count": 202, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello'" ] }, "execution_count": 202, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[0:5]" ] }, { "cell_type": "code", "execution_count": 203, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello'" ] }, "execution_count": 203, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[:5]" ] }, { "cell_type": "code", "execution_count": 204, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'World'" ] }, "execution_count": 204, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[6:11]" ] }, { "cell_type": "code", "execution_count": 205, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'World'" ] }, "execution_count": 205, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[6:]" ] }, { "cell_type": "code", "execution_count": 206, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Worl'" ] }, "execution_count": 206, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[6:-1]" ] }, { "cell_type": "code", "execution_count": 207, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'loWr'" ] }, "execution_count": 207, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[2:9:2]" ] }, { "cell_type": "code", "execution_count": 208, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello World'" ] }, "execution_count": 208, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[:]" ] }, { "cell_type": "code", "execution_count": 209, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'dlroW olleH'" ] }, "execution_count": 209, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[::-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Slice Assignment" ] }, { "cell_type": "code", "execution_count": 210, "metadata": {}, "outputs": [], "source": [ "l = [1,2,3, 'a','b','c', 9,10,11]" ] }, { "cell_type": "code", "execution_count": 211, "metadata": {}, "outputs": [], "source": [ "l[3:6] = [4, 5, 6, 7, 8]" ] }, { "cell_type": "code", "execution_count": 212, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]" ] }, "execution_count": 212, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ``for`` Loops" ] }, { "cell_type": "code", "execution_count": 213, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Caro\n", "Joerg\n", "Johanna\n", "Philipp\n" ] } ], "source": [ "l = ['Caro', 'Joerg', 'Johanna', 'Philipp']\n", "for element in l:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 214, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a\n", "b\n", "c\n" ] } ], "source": [ "for element in 'abc':\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``range()``" ] }, { "cell_type": "code", "execution_count": 215, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "4\n", "5\n", "6\n" ] } ], "source": [ "for element in range(3, 7):\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 216, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "4\n", "5\n", "6\n" ] } ], "source": [ "for element in [3,4,5,6]:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 217, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n" ] } ], "source": [ "i = 0\n", "while i<4:\n", " print(i)\n", " i += 1" ] }, { "cell_type": "code", "execution_count": 218, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n" ] } ], "source": [ "for i in range(4):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 219, "metadata": {}, "outputs": [], "source": [ "r = range(4)" ] }, { "cell_type": "code", "execution_count": 220, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range" ] }, "execution_count": 220, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(r)" ] }, { "cell_type": "code", "execution_count": 221, "metadata": {}, "outputs": [], "source": [ "it = iter(r)" ] }, { "cell_type": "code", "execution_count": 222, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 222, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 223, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 223, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 224, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 224, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 225, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 225, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 226, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \n" ] } ], "source": [ "try:\n", " next(it)\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 227, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "2\n", "4\n", "6\n", "8\n", "10\n", "12\n", "14\n", "16\n", "18\n" ] } ], "source": [ "number = 0\n", "while number < 20:\n", " if number % 2 == 0:\n", " print(number)\n", " number += 1" ] }, { "cell_type": "code", "execution_count": 228, "metadata": {}, "outputs": [], "source": [ "def even_numbers_fun(begin, end):\n", " nums = []\n", " number = begin\n", " while number < end:\n", " if number % 2 == 0:\n", " nums.append(number)\n", " number += 1\n", " return nums" ] }, { "cell_type": "code", "execution_count": 229, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "6\n" ] } ], "source": [ "for number in even_numbers_fun(3, 8):\n", " print(number)" ] }, { "cell_type": "code", "execution_count": 230, "metadata": {}, "outputs": [], "source": [ "def even_numbers_gen(begin, end):\n", " number = begin\n", " while number < end:\n", " if number % 2 == 0:\n", " print('yielding', number)\n", " yield number\n", " print('continuing after yielding', number)\n", " number += 1" ] }, { "cell_type": "code", "execution_count": 231, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yielding 4\n", "4\n", "continuing after yielding 4\n", "yielding 6\n", "6\n", "continuing after yielding 6\n" ] } ], "source": [ "for number in even_numbers_gen(3, 8):\n", " print(number)" ] }, { "cell_type": "code", "execution_count": 232, "metadata": {}, "outputs": [], "source": [ "evennums = even_numbers_gen(3, 8)" ] }, { "cell_type": "code", "execution_count": 233, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "generator" ] }, "execution_count": 233, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(evennums)" ] }, { "cell_type": "code", "execution_count": 234, "metadata": {}, "outputs": [], "source": [ "it = iter(evennums)" ] }, { "cell_type": "code", "execution_count": 235, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yielding 4\n" ] }, { "data": { "text/plain": [ "4" ] }, "execution_count": 235, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 236, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "continuing after yielding 4\n", "yielding 6\n" ] }, { "data": { "text/plain": [ "6" ] }, "execution_count": 236, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 237, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "continuing after yielding 6\n", " \n" ] } ], "source": [ "try:\n", " next(it)\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Filterketten" ] }, { "cell_type": "code", "execution_count": 238, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "for element in range(10):\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 239, "metadata": {}, "outputs": [], "source": [ "def even_filter(iterable):\n", " for element in iterable:\n", " if element % 2 == 0:\n", " yield element" ] }, { "cell_type": "code", "execution_count": 240, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "6\n", "8\n", "10\n", "12\n", "14\n", "16\n", "18\n", "20\n", "22\n", "24\n", "26\n", "28\n", "30\n", "32\n", "34\n", "36\n", "38\n", "40\n", "42\n", "44\n", "46\n", "48\n" ] } ], "source": [ "for num in even_filter(range(3, 50)):\n", " print(num)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Zen" ] }, { "cell_type": "code", "execution_count": 257, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The Zen of Python, by Tim Peters\n", "\n", "Beautiful is better than ugly.\n", "Explicit is better than implicit.\n", "Simple is better than complex.\n", "Complex is better than complicated.\n", "Flat is better than nested.\n", "Sparse is better than dense.\n", "Readability counts.\n", "Special cases aren't special enough to break the rules.\n", "Although practicality beats purity.\n", "Errors should never pass silently.\n", "Unless explicitly silenced.\n", "In the face of ambiguity, refuse the temptation to guess.\n", "There should be one-- and preferably only one --obvious way to do it.\n", "Although that way may not be obvious at first unless you're Dutch.\n", "Now is better than never.\n", "Although never is often better than *right* now.\n", "If the implementation is hard to explain, it's a bad idea.\n", "If the implementation is easy to explain, it may be a good idea.\n", "Namespaces are one honking great idea -- let's do more of those!\n" ] } ], "source": [ "import this" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Compound Datatypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compound Datatypes By Example: List, Tuple" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "l = [1,2,3]" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(l)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.append(4)\n", "l" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "l.extend([5, 6, 7])" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7]" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, [8, 9]]" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.append([8, 9])\n", "l" ] }, { "cell_type": "code", "execution_count": 247, "metadata": {}, "outputs": [], "source": [ "del l[3]" ] }, { "cell_type": "code", "execution_count": 248, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 5, 6, 7, [8, 9]]" ] }, "execution_count": 248, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 249, "metadata": {}, "outputs": [], "source": [ "l.append('hundert')" ] }, { "cell_type": "code", "execution_count": 250, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 5, 6, 7, [8, 9], 'hundert']" ] }, "execution_count": 250, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 251, "metadata": {}, "outputs": [], "source": [ "a = 'tausend'\n", "l.append(a)" ] }, { "cell_type": "code", "execution_count": 252, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 5, 6, 7, [8, 9], 'hundert', 'tausend']" ] }, "execution_count": 252, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 254, "metadata": {}, "outputs": [], "source": [ "l.insert(3, 'aaa')" ] }, { "cell_type": "code", "execution_count": 255, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'aaa', 5, 6, 7, [8, 9], 'hundert', 'tausend']" ] }, "execution_count": 255, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 269, "metadata": {}, "outputs": [], "source": [ "t1 = (1,2,3)\n", "t2 = (4,5,6)" ] }, { "cell_type": "code", "execution_count": 270, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, 3, 4, 5, 6)" ] }, "execution_count": 270, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = t1 + t2\n", "t" ] }, { "cell_type": "code", "execution_count": 272, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 'tuple' object has no attribute 'append'\n" ] } ], "source": [ "try:\n", " t.append(7)\n", "except Exception as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 273, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Mrs.\n" ] } ], "source": [ "sex = 'f'\n", "if sex in ('f', 'F'):\n", " print('Mrs.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List Comprehensions" ] }, { "cell_type": "code", "execution_count": 258, "metadata": {}, "outputs": [], "source": [ "def squares(numbers):\n", " sqs = []\n", " for num in numbers:\n", " sqs.append(num**2)\n", " return sqs" ] }, { "cell_type": "code", "execution_count": 261, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4, 9, 1, 25, 64]" ] }, "execution_count": 261, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = squares([2,3,1,5,8])\n", "l" ] }, { "cell_type": "code", "execution_count": 263, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4, 9, 1, 25, 64]" ] }, "execution_count": 263, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [num**2 for num in [2,3,1,5,8]]\n", "l" ] }, { "cell_type": "code", "execution_count": 266, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4, 64]" ] }, "execution_count": 266, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [num**2 for num in [2,3,1,5,8] if num % 2 == 0]\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compound Datatypes By Example: Dictionary" ] }, { "cell_type": "code", "execution_count": 274, "metadata": {}, "outputs": [], "source": [ "translation_table = {\n", " 1: 'one',\n", " 2: 'two',\n", " 3: 'three'\n", "}" ] }, { "cell_type": "code", "execution_count": 275, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'one'" ] }, "execution_count": 275, "metadata": {}, "output_type": "execute_result" } ], "source": [ "translation_table[1]" ] }, { "cell_type": "code", "execution_count": 277, "metadata": {}, "outputs": [], "source": [ "translation_table[4] = 'four'" ] }, { "cell_type": "code", "execution_count": 279, "metadata": {}, "outputs": [], "source": [ "translation_table[4] = 'vier'" ] }, { "cell_type": "code", "execution_count": 280, "metadata": {}, "outputs": [], "source": [ "del translation_table[1]" ] }, { "cell_type": "code", "execution_count": 281, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{2: 'two', 3: 'three', 4: 'vier'}" ] }, "execution_count": 281, "metadata": {}, "output_type": "execute_result" } ], "source": [ "translation_table" ] }, { "cell_type": "code", "execution_count": 282, "metadata": {}, "outputs": [], "source": [ "users = {\n", " '1037190666': ('Joerg', 'Faschingbauer'),\n", " '1234250497': ('Caro', 'Faschingbauer')\n", "}" ] }, { "cell_type": "code", "execution_count": 283, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('Joerg', 'Faschingbauer')" ] }, "execution_count": 283, "metadata": {}, "output_type": "execute_result" } ], "source": [ "users['1037190666']" ] }, { "cell_type": "code", "execution_count": 284, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1037190666\n", "1234250497\n" ] } ], "source": [ "for element in users:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 286, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1037190666\n", "1234250497\n" ] } ], "source": [ "for key in users.keys():\n", " print(key)" ] }, { "cell_type": "code", "execution_count": 287, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('Joerg', 'Faschingbauer')\n", "('Caro', 'Faschingbauer')\n" ] } ], "source": [ "for value in users.values():\n", " print(value)" ] }, { "cell_type": "code", "execution_count": 288, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('1037190666', ('Joerg', 'Faschingbauer'))\n", "('1234250497', ('Caro', 'Faschingbauer'))\n" ] } ], "source": [ "for element in users.items():\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Datensatz als Dictionary?" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "joerg = {\n", " 'svnr': '1037190666',\n", " 'firstname': 'Joerg',\n", " 'lastname': 'Faschingbauer',\n", "}" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "caro = {\n", " 'svnr': '1234250597',\n", " 'firstname': 'Caro',\n", " 'lastname': 'Faschingbauer',\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tuple Unpacking" ] }, { "cell_type": "code", "execution_count": 292, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "svnr: 1037190666 , firstname: Joerg , lastname: Faschingbauer\n", "svnr: 1234250497 , firstname: Caro , lastname: Faschingbauer\n" ] } ], "source": [ "for element in users.items():\n", " svnr = element[0]\n", " value = element[1]\n", " firstname = value[0]\n", " lastname = value[1]\n", " \n", " print('svnr:', svnr, ', firstname:', firstname, ', lastname:', lastname)" ] }, { "cell_type": "code", "execution_count": 295, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "svnr: 1037190666 , firstname: Joerg , lastname: Faschingbauer\n", "svnr: 1234250497 , firstname: Caro , lastname: Faschingbauer\n" ] } ], "source": [ "for svnr, user in users.items():\n", " firstname = user[0]\n", " lastname = user[1]\n", "\n", " print('svnr:', svnr, ', firstname:', firstname, ', lastname:', lastname)" ] }, { "cell_type": "code", "execution_count": 298, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "svnr: 1037190666 , firstname: Joerg , lastname: Faschingbauer\n", "svnr: 1234250497 , firstname: Caro , lastname: Faschingbauer\n" ] } ], "source": [ "for svnr, user in users.items():\n", " firstname, lastname = user\n", "\n", " print('svnr:', svnr, ', firstname:', firstname, ', lastname:', lastname)" ] }, { "cell_type": "code", "execution_count": 297, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "svnr: 1037190666 , firstname: Joerg , lastname: Faschingbauer\n", "svnr: 1234250497 , firstname: Caro , lastname: Faschingbauer\n" ] } ], "source": [ "for svnr, (firstname, lastname) in users.items():\n", " print('svnr:', svnr, ', firstname:', firstname, ', lastname:', lastname)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## JSON (Javascript Object Notation)" ] }, { "cell_type": "code", "execution_count": 299, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'1037190666': ('Joerg', 'Faschingbauer'),\n", " '1234250497': ('Caro', 'Faschingbauer')}" ] }, "execution_count": 299, "metadata": {}, "output_type": "execute_result" } ], "source": [ "users" ] }, { "cell_type": "code", "execution_count": 300, "metadata": {}, "outputs": [], "source": [ "import json" ] }, { "cell_type": "code", "execution_count": 301, "metadata": {}, "outputs": [], "source": [ "users_json_string = json.dumps(users)" ] }, { "cell_type": "code", "execution_count": 302, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'{\"1037190666\": [\"Joerg\", \"Faschingbauer\"], \"1234250497\": [\"Caro\", \"Faschingbauer\"]}'" ] }, "execution_count": 302, "metadata": {}, "output_type": "execute_result" } ], "source": [ "users_json_string" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Jetzt liest der Kontrahent den String vom Kabel und verwandelt ihn zurueck in Python" ] }, { "cell_type": "code", "execution_count": 304, "metadata": {}, "outputs": [], "source": [ "gelesenes_ding_wieder_als_dict = json.loads(users_json_string)" ] }, { "cell_type": "code", "execution_count": 306, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/plain": [ "{'1037190666': ['Joerg', 'Faschingbauer'],\n", " '1234250497': ['Caro', 'Faschingbauer']}" ] }, "execution_count": 306, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gelesenes_ding_wieder_als_dict" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compound Datatypes By Example: Set" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = { 1, 'eins', 1.0, 2, 3 }\n", "len(s)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'eins' in s" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 in s" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1.0 in s" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "s.add('zwei')" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'zwei' in s" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 'eins', 'zwei'}" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 == 1.0" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "s.add('zwei')" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 'eins', 'zwei'}" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "s.remove('eins')" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'eins' in s" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 'zwei'}" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "s1 = {1,2,3,4}\n", "s2 = {3,4,5,6}" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4, 5, 6}" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 | s2" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{3, 4}" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 & s2" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "l = [4,7,1,3]\n", "s = set(l)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 3, 4, 7}" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "{'a', 'b', 'c', 'd'}" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = set('abcd')\n", "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Functions" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hallo schatz\n" ] } ], "source": [ "print('hallo schatz')" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "i = 1" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "jo eh\n" ] } ], "source": [ "if i == 1:\n", " print('jo eh')" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "import sys" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'linux'" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sys.platform" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hallo,schatz,1,1\n" ] } ], "source": [ "print('hallo', 'schatz', 1, i, sep=',')" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "l = [1,2,3]\n", "l.append(4)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "a = 100\n", "b = 200" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "200\n" ] } ], "source": [ "if a < b:\n", " print(b)\n", "else:\n", " print(a)" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [], "source": [ "def maximum(a, b):\n", " print('local a:', a)\n", " print('local b:', b)\n", " if a < b:\n", " return b\n", " else:\n", " return a" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "local a: 50\n", "local b: 60\n" ] }, { "data": { "text/plain": [ "60" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum(50, 60)" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "local a: 1\n", "local b: 2\n" ] }, { "data": { "text/plain": [ "2" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum(1, 2)" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [], "source": [ "a = 300\n", "b = 2000" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "local a: 3\n", "local b: 4\n" ] }, { "data": { "text/plain": [ "4" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum(3,4)" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "300" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2000" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [], "source": [ "erich = 59\n", "koxi = 53" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "local a: 59\n", "local b: 53\n" ] }, { "data": { "text/plain": [ "59" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum(erich, koxi)" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "local a: 35\n", "local b: 30\n" ] }, { "data": { "text/plain": [ "35" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import random\n", "maximum(random.randrange(40), 30)" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hallo schatz\n" ] } ], "source": [ "print('hallo schatz')" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hallo schatz\n" ] } ], "source": [ "wert = print('hallo schatz')" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "print(wert)" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [], "source": [ "def hatkeinenwert():\n", " pass" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "wert = hatkeinenwert()\n", "print(wert)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Das Letzte Beispiel am Letzten Tag" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [], "source": [ "joerg = {\n", " 'firstname': 'Joerg',\n", " 'lastname': 'Faschingbauer',\n", " 'svnr': '1037190666',\n", "}" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Joerg\n" ] } ], "source": [ "fn = joerg['firstname']\n", "print(fn)" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [], "source": [ "html = '''\n", "
    \n", "
  • Joerg
  • \n", "
  • Faschingbauer
  • \n", "
  • 1037190666
  • \n", "
\n", "\n", "'''" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [], "source": [ "fn = 'Joerg'\n", "ln = 'Faschingbauer'\n", "nr = '1037190666'" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "
    \n", "
  • Joerg
  • \n", "
  • Faschingbauer
  • \n", "
  • 1037190666
  • \n", "
\n" ] } ], "source": [ "html = '
    \\n
  • ' + fn + '
  • \\n
  • ' + ln + '
  • \\n
  • ' + nr + '
  • \\n
'\n", "print(html)" ] } ], "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.9.10" } }, "nbformat": 4, "nbformat_minor": 4 }