diff --git a/notebooks/recap.ipynb b/notebooks/recap.ipynb index f215a73c424120267f317c764276037cf082c8ba..84959d78d8f5199b7def5dd1ff0868f2c829e840 100644 --- a/notebooks/recap.ipynb +++ b/notebooks/recap.ipynb @@ -11,6 +11,32 @@ "- Kommentare beginnen mit Raute #" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Krasse neue Bibliothek\n", + "\n", + "Formatiert euren Code nach jeder Ausführung\n", + "\n", + "- für \"normale\" python scripte: **black**\n", + "\n", + "- für jupyter notebooks: **nb_black**\n", + "\n", + "- Installation: *pip install nb_black*\n", + "\n", + "- Ausführung: Einfach **%load_ext nb_black** in einem Codeblock einmalig ausführen" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%load_ext nb_black" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -25,10 +51,11 @@ "metadata": {}, "outputs": [], "source": [ - "a = 2*2.0\n", + "a = 2 * 2.0\n", "b = 5\n", "\n", - "print(a + b)" + "print(a + b)\n", + "print(type(a))" ] }, { @@ -38,6 +65,8 @@ "outputs": [], "source": [ "a += 1\n", + "# ist kurz für\n", + "a = a + 1\n", "# a++ existiert nicht\n", "\n", "print(a)" @@ -49,7 +78,7 @@ "metadata": {}, "outputs": [], "source": [ - "2**4" + "2 ** 4 # Potenzen" ] }, { @@ -58,7 +87,7 @@ "metadata": {}, "outputs": [], "source": [ - "2/4" + "2 / 4 # Brüche" ] }, { @@ -67,14 +96,16 @@ "metadata": {}, "outputs": [], "source": [ - "text1 = 'Hallo '\n", + "text1 = \"Hallo \"\n", "text2 = \"Welt\"\n", "\n", - "print (text1 + text2)\n", + "print(text1 + text2)\n", "\n", "# Andere Datentypen müssen explizit in Strings konvertiert werden,\n", "# wenn sie an einen String angehängt werden sollen.\n", - "print(text1 + str(1))" + "print(text1 + str(1))\n", + "# ansonsten muss per Komma getrennt werden\n", + "print(text1, 1)" ] }, { @@ -83,7 +114,7 @@ "metadata": {}, "outputs": [], "source": [ - "float('1')" + "float(\"1\")" ] }, { @@ -127,7 +158,7 @@ "outputs": [], "source": [ "if myvar is None:\n", - " print('x ist nicht definiert')" + " print(\"x ist nicht definiert\")" ] }, { @@ -144,9 +175,11 @@ "metadata": {}, "outputs": [], "source": [ - "l = [1,2,3,3]\n", + "l1 = []\n", + "l = [1, 2, 3, 3]\n", "l.append(4)\n", "\n", + "print(l1)\n", "print(l)" ] }, @@ -181,7 +214,7 @@ "metadata": {}, "outputs": [], "source": [ - "l2 = l + [5,6]\n", + "l2 = l + [5, 6]\n", "\n", "print(l2)" ] @@ -200,8 +233,8 @@ "metadata": {}, "outputs": [], "source": [ - "s = {1,2,3,3}\n", - "s2 = set([2,3,4,5])\n", + "s = {1, 2, 3, 3}\n", + "s2 = set([2, 3, 4, 5])\n", "\n", "print(s)\n", "print(s.intersection(s2))" @@ -240,7 +273,7 @@ "metadata": {}, "outputs": [], "source": [ - "set([1,2,3])" + "set([1, 2, 3])" ] }, { @@ -275,7 +308,7 @@ "metadata": {}, "outputs": [], "source": [ - "essen = 'Chili'\n", + "essen = \"Chili\"\n", "preis = 2.51\n", "boneintrag = (essen, preis)\n", "\n", @@ -331,7 +364,7 @@ "metadata": {}, "outputs": [], "source": [ - "d = {'Chili': 1.90, 'Penne': 2.50}" + "d = {\"Chili\": 1.90, \"Penne\": 2.50}" ] }, { @@ -359,7 +392,7 @@ "outputs": [], "source": [ "for key, val in d.items():\n", - " print('{}: {}'.format(key, val))" + " print(\"{}: {}\".format(key, val))" ] }, { @@ -368,7 +401,7 @@ "metadata": {}, "outputs": [], "source": [ - "d['Chili']" + "d[\"Chili\"]" ] }, { @@ -377,7 +410,7 @@ "metadata": {}, "outputs": [], "source": [ - "d['Burger'] = 4.90\n", + "d[\"Burger\"] = 4.90\n", "d" ] }, @@ -387,7 +420,7 @@ "metadata": {}, "outputs": [], "source": [ - "del d['Burger']\n", + "del d[\"Burger\"]\n", "d" ] }, @@ -404,8 +437,8 @@ "metadata": {}, "outputs": [], "source": [ - "if 2>1 or 1>2:\n", - " print ('Bedingung erfüllt.')" + "if 2 > 1 or 1 > 2:\n", + " print(\"Bedingung erfüllt.\")" ] }, { @@ -445,7 +478,7 @@ "metadata": {}, "outputs": [], "source": [ - "for x in ['a', 'b', 'c']:\n", + "for x in [\"a\", \"b\", \"c\"]:\n", " print(x)" ] }, @@ -463,7 +496,8 @@ "outputs": [], "source": [ "def sum_upto(n):\n", - " return n*(n+1)/2\n", + " return n * (n + 1) / 2\n", + "\n", "\n", "sum_upto(4)" ] @@ -498,7 +532,7 @@ "outputs": [], "source": [ "def fun_with_default(x=3):\n", - " print('Parameter is {}'.format(x))" + " print(\"Parameter is {}\".format(x))" ] }, { @@ -560,7 +594,7 @@ "metadata": {}, "outputs": [], "source": [ - "calc_square = lambda x: x**2\n", + "calc_square = lambda x: x ** 2\n", "calc_square(3)" ] }, @@ -579,7 +613,7 @@ "metadata": {}, "outputs": [], "source": [ - "calc_and_print(lambda x: x**3, 4)" + "calc_and_print(lambda x: x ** 3, 4)" ] }, { @@ -595,7 +629,7 @@ "metadata": {}, "outputs": [], "source": [ - "y = [x**2 for x in range(10)]\n", + "y = [x ** 2 for x in range(10)]\n", "y" ] }, @@ -605,7 +639,7 @@ "metadata": {}, "outputs": [], "source": [ - "xy = [(x, x**2) for x in range(10) if x%2 == 0]\n", + "xy = [(x, x ** 2) for x in range(10) if x % 2 == 0]\n", "xy" ] }, @@ -623,12 +657,12 @@ "metadata": {}, "outputs": [], "source": [ - "class Vehicle():\n", + "class Vehicle:\n", " # Constructor\n", - " def __init__(self, n_wheels=4, noise='beep'):\n", + " def __init__(self, n_wheels=4, noise=\"beep\"):\n", " self.n_wheels = n_wheels\n", " self.noise = noise\n", - " \n", + "\n", " def make_noise(self):\n", " print(self.noise)" ] @@ -674,7 +708,7 @@ "metadata": {}, "outputs": [], "source": [ - "bike = Vehicle(n_wheels=2, noise='ring')\n", + "bike = Vehicle(n_wheels=2, noise=\"ring\")\n", "bike.make_noise()" ] }, @@ -687,7 +721,7 @@ "class Bike(Vehicle):\n", " def __init__(self):\n", " self.n_wheels = 2\n", - " self.noise = 'ring'" + " self.noise = \"ring\"" ] }, { @@ -731,13 +765,6 @@ "- *M*: Zelltyp Markdown (für formatierte beschreibende Texte)\n", "- *Y*: Zelltyp Code (Default)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": {