{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python (2022-10-17)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Modules" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import sys" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "module" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(sys)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "try:\n", " sys.exit()\n", "except SystemExit:\n", " pass" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "my_sys = sys\n", "del sys" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'little'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_sys.byteorder" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "import sys as my_sys" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "import sys" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'little'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sys.byteorder" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "from sys import byteorder" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'little'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "byteorder" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "from sys import * # all of dir(sys)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exit" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tuple Unpacking, Lists Iteration, And Such" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Tuple Unpacking**" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "a = 1\n", "b = 2" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 1\n" ] } ], "source": [ "tmp = a\n", "a = b\n", "b = tmp\n", "print(a, b)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "a = 1\n", "b = 2" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "a, b = b, a" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 1\n" ] } ], "source": [ "print(a, b)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "def function_returns_two_values():\n", " return 42, 666" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(42, 666)" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rv = function_returns_two_values()\n", "rv" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(42, 666)" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a, b = function_returns_two_values()\n", "a, b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**... On Lists of Pairs**" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "l = [(1, 'one'), (2, 'two')]" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 'one')\n", "(2, 'two')\n" ] } ], "source": [ "for elem in l:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 one\n", "2 two\n" ] } ], "source": [ "for elem in l:\n", " x = elem[0]\n", " y = elem[1]\n", " print(x, y)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 one\n", "2 two\n" ] } ], "source": [ "for x, y in l:\n", " print(x, y)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n", "two\n" ] } ], "source": [ "for _, y in l:\n", " print(y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Dictionaries**" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "l = [(1, 'one'), (2, 'two')]" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "d = {1: 'one',\n", " 2:'two'\n", " }" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "d = dict(l)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: 'one', 2: 'two'}" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'one'" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[1]" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "d[3] = 'three'" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(d)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "for elem in d:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "for elem in d.keys():\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n", "two\n", "three\n" ] } ], "source": [ "for v in d.values():\n", " print(v)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 one\n", "2 two\n", "3 three\n" ] } ], "source": [ "for k, v in d.items():\n", " print(k, v)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variables, Types" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "i = 42" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(i)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "i = 'abc'" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(i)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "i = [1,2,3, 'abc', {1:'one'}]" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(i)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140134238766608" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 42\n", "id(a)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140134238766608" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = a\n", "id(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**sizeof()**" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__abs__',\n", " '__add__',\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", " '__getformat__',\n", " '__getnewargs__',\n", " '__gt__',\n", " '__hash__',\n", " '__init__',\n", " '__init_subclass__',\n", " '__int__',\n", " '__le__',\n", " '__lt__',\n", " '__mod__',\n", " '__mul__',\n", " '__ne__',\n", " '__neg__',\n", " '__new__',\n", " '__pos__',\n", " '__pow__',\n", " '__radd__',\n", " '__rdivmod__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__rfloordiv__',\n", " '__rmod__',\n", " '__rmul__',\n", " '__round__',\n", " '__rpow__',\n", " '__rsub__',\n", " '__rtruediv__',\n", " '__setattr__',\n", " '__setformat__',\n", " '__sizeof__',\n", " '__str__',\n", " '__sub__',\n", " '__subclasshook__',\n", " '__truediv__',\n", " '__trunc__',\n", " 'as_integer_ratio',\n", " 'conjugate',\n", " 'fromhex',\n", " 'hex',\n", " 'imag',\n", " 'is_integer',\n", " 'real']" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(float)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "24" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import sys\n", "sys.getsizeof(3.14)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "24" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = 3.14\n", "f.__sizeof__()" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b1001'" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(0b0010^0b1011)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compound Datatypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "l = [1,2,3]" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "l.append(4)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [], "source": [ "l.append('fuenf')" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "l.extend([6,7,8])" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 'fuenf', 6, 7, 8]" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 4, 'fuenf', 6, 7, 8]" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "del l[2]\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tuple" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "t = (1,2,3)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'tuple' object has no attribute 'append'\n" ] } ], "source": [ "try:\n", " t.append(4)\n", "except AttributeError as e:\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dictionary" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [], "source": [ "d = {1:'one', 2:'two'}" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'one'" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[1]" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nix\n" ] } ], "source": [ "try:\n", " print(d[3])\n", "except KeyError as e:\n", " print('nix')" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nix\n" ] } ], "source": [ "value = d.get(3)\n", "if value is True:\n", " print(value)\n", "else:\n", " print('nix')" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [], "source": [ "d['vier'] = 4" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: 'one', 2: 'two', 'vier': 4}" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "unhashable type: 'list'\n" ] } ], "source": [ "key_list = [1,2,3]\n", "try:\n", " d[key_list] = 666\n", "except TypeError as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [], "source": [ "key_tuple = (1,2,3)\n", "d[key_tuple] = 42" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Set" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [], "source": [ "s = {1,2,3}" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 in s" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 in s" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [], "source": [ "s.add(5)" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 in s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Immutable? References?" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [], "source": [ "l1 = [1,2,3,4,5]\n", "l2 = l1" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1 == l2 # same content" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1 is l2" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l1) == id(l2)" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6]" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1.append(6)\n", "l2" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [], "source": [ "l3 = l1[:] # allocate new list, and copy toplevel" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l3 == l1" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l3 is l1" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [], "source": [ "l3.append(7)" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6]" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7]" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l3" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [], "source": [ "evil_list = [1,2,3,[4,5,6],7,8,9]" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(evil_list)" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [], "source": [ "evil_list_2 = evil_list[:]" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [], "source": [ "evil_list_2.append(10)" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, [4, 5, 6], 7, 8, 9]" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "evil_list " ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [], "source": [ "evil_list_2[3].append(100)" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, [4, 5, 6, 100], 7, 8, 9]" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "evil_list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Strings" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'abc'\n", "s" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"abc\"\n", "s" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ab\"c'" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'ab\"c'\n", "s" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ab\"c'" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"ab\\\"c\"\n", "s" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [], "source": [ "doc = '''\n", "das ist eine doku von \n", "einem modul, das blah \n", "viel zu kompliziert ist,\n", "um in eine zeile zu passen.\n", "'''" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'\\ndas ist eine doku von \\neinem modul, das blah \\nviel zu kompliziert ist,\\num in eine zeile zu passen.\\n'" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "doc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Tuple Unpacking" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [], "source": [ "l = [{'id': 1, 'firstname': 'Joerg'}, {'id': 2, 'firstname': 'Caro'}]" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'id': 1, 'firstname': 'Joerg'}\n", "{'id': 2, 'firstname': 'Caro'}\n" ] } ], "source": [ "for elem in l:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "id firstname\n", "id firstname\n" ] } ], "source": [ "for id, firstname in l:\n", " print(id, firstname)" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [], "source": [ "d = {'id': 1, 'firstname': 'Joerg'}" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "id firstname\n" ] } ], "source": [ "x, y = d\n", "print(x, y)" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [], "source": [ "x, y = d.keys()" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('id', 'firstname')" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x, y" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 'Joerg')" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x, y = d.values()\n", "x, y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Raw Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "``\\U`` is a unicode escape sequence" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [], "source": [ "# Syntax error: doze_path = 'D:\\Users\\blah'" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'D:\\\\Users\\\\blah'" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "doze_path = 'D:\\\\Users\\\\blah'\n", "doze_path" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'D:\\\\Users\\\\blah'" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "doze_path = r'D:\\Users\\blah'\n", "doze_path" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [], "source": [ "regex = r'^[0-9]*\\.txt'\n", "import re" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [], "source": [ "compiled_regex = re.compile(regex)" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "compiled_regex.search('123.txt')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## JSON" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [], "source": [ "l = [{'id': 1, 'firstname': 'Joerg'}, {'id': 2, 'firstname': 'Caro'}]" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[{'firstname': 'Joerg',\n", " 'id': 1},\n", " {'firstname': 'Caro',\n", " 'id': 2}]\n" ] } ], "source": [ "import pprint\n", "pprint.pprint(l, width=10)" ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [], "source": [ "import json" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'[{\"id\": 1, \"firstname\": \"Joerg\"}, {\"id\": 2, \"firstname\": \"Caro\"}]'" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l_json = json.dumps(l)\n", "l_json" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [], "source": [ "l_json_sent = l_json" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'id': 1, 'firstname': 'Joerg'}, {'id': 2, 'firstname': 'Caro'}]" ] }, "execution_count": 114, "metadata": {}, "output_type": "execute_result" } ], "source": [ "received_data = json.loads(l_json_sent)\n", "received_data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Iteration" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [], "source": [ "l = [666, 42.5,3, 'abc']" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "666\n", "42.5\n", "3\n", "abc\n" ] } ], "source": [ "for elem in l:\n", " print(elem)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Index based iteration**" ] }, { "cell_type": "code", "execution_count": 117, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 666\n", "1 42.5\n", "2 3\n", "3 abc\n" ] } ], "source": [ "for i in range(len(l)):\n", " print(i, l[i])" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 666\n", "1 42.5\n", "2 3\n", "3 abc\n" ] } ], "source": [ "for i, elem in enumerate(l):\n", " print(i, elem)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Iterable?**" ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "666\n", "42.5\n", "3\n", "abc\n" ] } ], "source": [ "for elem in l:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n" ] } ], "source": [ "d = {1:'one', 2:'two'}\n", "for elem in d:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.5\n", "abc\n", "666\n" ] } ], "source": [ "s = {666, 1.5, 'abc'}\n", "for elem in s:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "J\n", "o\n", "e\n", "r\n", "g\n" ] } ], "source": [ "s = 'Joerg'\n", "for elem in s:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'int' object is not iterable\n" ] } ], "source": [ "i = 666\n", "try:\n", " for elem in i:\n", " print(elem)\n", "except TypeError as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['J', 'o', 'e', 'r', 'g']" ] }, "execution_count": 124, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = list('Joerg')\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "lists from dictionaries" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: 'one', 2: 'two'}" ] }, "execution_count": 125, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2]" ] }, "execution_count": 126, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(d)" ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['one', 'two']" ] }, "execution_count": 127, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(d.values())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### And ``range()``?" ] }, { "cell_type": "code", "execution_count": 128, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for i in range(5):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range(0, 5)" ] }, "execution_count": 129, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r = range(5)\n", "r" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [], "source": [ "it = iter(r)" ] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 131, "metadata": {}, "output_type": "execute_result" } ], "source": [ "it" ] }, { "cell_type": "code", "execution_count": 132, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 132, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 133, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 134, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 135, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 135, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 136, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 136, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "outputs": [], "source": [ "try:\n", " next(it)\n", "except StopIteration:\n", " pass" ] }, { "cell_type": "code", "execution_count": 138, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for i in range(5):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 139, "metadata": {}, "output_type": "execute_result" } ], "source": [ "it = iter([1,2,3])\n", "next(it)" ] }, { "cell_type": "code", "execution_count": 140, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 140, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 141, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [], "source": [ "try:\n", " next(it)\n", "except StopIteration:\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More About Dictionaries" ] }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "outputs": [], "source": [ "d = {\n", " 'one': 1,\n", " 'two': 2,\n", "}" ] }, { "cell_type": "code", "execution_count": 144, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2}" ] }, "execution_count": 144, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 145, "metadata": {}, "outputs": [], "source": [ "d['three'] = 3" ] }, { "cell_type": "code", "execution_count": 146, "metadata": {}, "outputs": [], "source": [ "d['two'] = 200" ] }, { "cell_type": "code", "execution_count": 147, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 200, 'three': 3}" ] }, "execution_count": 147, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 148, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 148, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 in d" ] }, { "cell_type": "code", "execution_count": 149, "metadata": {}, "outputs": [], "source": [ "if 2 in d:\n", " d[2] += 100" ] }, { "cell_type": "code", "execution_count": 150, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 200, 'three': 3}" ] }, "execution_count": 150, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 151, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100\n" ] } ], "source": [ "try:\n", " d[100]\n", "except KeyError as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 152, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "value = d.get(100)\n", "print(value)" ] }, { "cell_type": "code", "execution_count": 153, "metadata": {}, "outputs": [], "source": [ "value = d.get(100)\n", "if value is None:\n", " d[100] = 'hundred'" ] }, { "cell_type": "code", "execution_count": 154, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 200, 'three': 3, 100: 'hundred'}" ] }, "execution_count": 154, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ``collections.defaultdict``" ] }, { "cell_type": "code", "execution_count": 155, "metadata": {}, "outputs": [], "source": [ "from collections import defaultdict" ] }, { "cell_type": "code", "execution_count": 156, "metadata": {}, "outputs": [], "source": [ "d = defaultdict(int)" ] }, { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 157, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[100]" ] }, { "cell_type": "code", "execution_count": 158, "metadata": {}, "outputs": [], "source": [ "def create_avg_items():\n", " return [0,0]" ] }, { "cell_type": "code", "execution_count": 159, "metadata": {}, "outputs": [], "source": [ "d = defaultdict(create_avg_items)" ] }, { "cell_type": "code", "execution_count": 160, "metadata": {}, "outputs": [], "source": [ "d = defaultdict(lambda: [0,0])" ] }, { "cell_type": "code", "execution_count": 161, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 0]" ] }, "execution_count": 161, "metadata": {}, "output_type": "execute_result" } ], "source": [ "elem = d['sensor1']\n", "elem" ] }, { "cell_type": "code", "execution_count": 162, "metadata": {}, "outputs": [], "source": [ "elem[0] += 3.14\n", "elem[1] += 1" ] }, { "cell_type": "code", "execution_count": 163, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "[3.14, 1]" ] }, "execution_count": 163, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['sensor1']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# More About Lists" ] }, { "cell_type": "code", "execution_count": 164, "metadata": {}, "outputs": [], "source": [ "l = [3,2,5,6,7]" ] }, { "cell_type": "code", "execution_count": 165, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 2, 5, 6, 7, 42]" ] }, "execution_count": 165, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.append(42)\n", "l" ] }, { "cell_type": "code", "execution_count": 166, "metadata": {}, "outputs": [], "source": [ "l1 = ['a', 'b']\n", "l.append(l1)" ] }, { "cell_type": "code", "execution_count": 167, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 2, 5, 6, 7, 42, ['a', 'b']]" ] }, "execution_count": 167, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 168, "metadata": {}, "outputs": [], "source": [ "del l[len(l)-1]" ] }, { "cell_type": "code", "execution_count": 169, "metadata": {}, "outputs": [], "source": [ "l1 = ['a', 'b']\n", "l.append(l1)" ] }, { "cell_type": "code", "execution_count": 170, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 2, 5, 6, 7, 42, ['a', 'b']]" ] }, "execution_count": 170, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 171, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 2, 5, 6, 7, 42]" ] }, "execution_count": 171, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[-1:] = []\n", "l" ] }, { "cell_type": "code", "execution_count": 172, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 2, 5, 6, 7, 42, ['a', 'b']]" ] }, "execution_count": 172, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1 = ['a', 'b']\n", "l.append(l1)\n", "l" ] }, { "cell_type": "code", "execution_count": 173, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b']" ] }, "execution_count": 173, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.pop(len(l)-1)" ] }, { "cell_type": "code", "execution_count": 174, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 2, 5, 6, 7, 42]" ] }, "execution_count": 174, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 175, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b']" ] }, "execution_count": 175, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 176, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 2, 5, 6, 7, 42, 'a', 'b']" ] }, "execution_count": 176, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.extend(l1)\n", "l" ] }, { "cell_type": "code", "execution_count": 177, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'<' not supported between instances of 'str' and 'int'\n" ] } ], "source": [ "try:\n", " l.sort()\n", "except TypeError as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 178, "metadata": {}, "outputs": [], "source": [ "l = [3,2,5,6,7]" ] }, { "cell_type": "code", "execution_count": 179, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 5, 6, 7]" ] }, "execution_count": 179, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.sort()\n", "l" ] }, { "cell_type": "code", "execution_count": 180, "metadata": {}, "outputs": [], "source": [ "l = [3,2,5,6,7]" ] }, { "cell_type": "code", "execution_count": 181, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 5, 6, 7]" ] }, "execution_count": 181, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(l)" ] }, { "cell_type": "code", "execution_count": 182, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 2, 5, 6, 7]" ] }, "execution_count": 182, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More About Dictionaries" ] }, { "cell_type": "code", "execution_count": 183, "metadata": {}, "outputs": [], "source": [ "d = {}" ] }, { "cell_type": "code", "execution_count": 184, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 184, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(d)" ] }, { "cell_type": "code", "execution_count": 185, "metadata": {}, "outputs": [], "source": [ "d = dict()" ] }, { "cell_type": "code", "execution_count": 186, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1: 'one', 2: 'two'}" ] }, "execution_count": 186, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [(1, 'one'), [2, 'two']]\n", "d = dict(l)\n", "d" ] }, { "cell_type": "code", "execution_count": 187, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 zero\n", "1 one\n", "2 two\n" ] } ], "source": [ "l = ('zero', 'one', 'two')\n", "for i, name in enumerate(l):\n", " print(i, name)" ] }, { "cell_type": "code", "execution_count": 188, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0: 'zero', 1: 'one', 2: 'two'}" ] }, "execution_count": 188, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = ('zero', 'one', 'two')\n", "d = dict(enumerate(l))\n", "d" ] }, { "cell_type": "code", "execution_count": 189, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0: 'zero', 1: 'one', 2: 'two', 100: 'hundred'}" ] }, "execution_count": 189, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[100] = 'hundred'\n", "d" ] }, { "cell_type": "code", "execution_count": 190, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hundred'" ] }, "execution_count": 190, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[100]" ] }, { "cell_type": "code", "execution_count": 191, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "99\n" ] } ], "source": [ "try:\n", " d[99]\n", "except KeyError as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 192, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "leider nein\n" ] } ], "source": [ "value = d.get(99)\n", "if value is not None:\n", " print(value)\n", "else:\n", " print('leider nein')" ] }, { "cell_type": "code", "execution_count": 193, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "leider nein\n" ] } ], "source": [ "value = d.get(99, 'leider nein')\n", "print(value)" ] }, { "cell_type": "code", "execution_count": 194, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0: 'zero', 1: 'one', 2: 'two'}" ] }, "execution_count": 194, "metadata": {}, "output_type": "execute_result" } ], "source": [ "del d[100]\n", "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Iteration**" ] }, { "cell_type": "code", "execution_count": 196, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0: 'zero', 1: 'one', 2: 'two'}" ] }, "execution_count": 196, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 202, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for elem in d:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 203, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2]" ] }, "execution_count": 203, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(d)" ] }, { "cell_type": "code", "execution_count": 204, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for k in d.keys():\n", " print(k)" ] }, { "cell_type": "code", "execution_count": 205, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "zero\n", "one\n", "two\n" ] } ], "source": [ "for v in d.values():\n", " print(v)" ] }, { "cell_type": "code", "execution_count": 206, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['zero', 'one', 'two']" ] }, "execution_count": 206, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(d.values())" ] }, { "cell_type": "code", "execution_count": 209, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0: 'zero', 1: 'one', 2: 'two'}" ] }, "execution_count": 209, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dict(enumerate(d.values()))" ] }, { "cell_type": "code", "execution_count": 211, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(0, 'zero')\n", "(1, 'one')\n", "(2, 'two')\n" ] } ], "source": [ "for elem in d.items():\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 212, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(0, 'zero'), (1, 'one'), (2, 'two')]" ] }, "execution_count": 212, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(d.items())" ] }, { "cell_type": "code", "execution_count": 213, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ninetynine'" ] }, "execution_count": 213, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.setdefault(99, 'ninetynine')" ] }, { "cell_type": "code", "execution_count": 214, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ninetynine'" ] }, "execution_count": 214, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[99]" ] }, { "cell_type": "code", "execution_count": 215, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'one'" ] }, "execution_count": 215, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[1]" ] }, { "cell_type": "code", "execution_count": 216, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'one'" ] }, "execution_count": 216, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.setdefault(1, 'eins')" ] }, { "cell_type": "code", "execution_count": 218, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hundred\n" ] } ], "source": [ "if 100 in d:\n", " result = d[100]\n", "else:\n", " d[100] = 'hundred'\n", " result = 'hundred'\n", "print(result)" ] }, { "cell_type": "code", "execution_count": 219, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0: 'zero', 1: 'one', 2: 'two', 99: 'ninetynine', 100: 'hundred'}" ] }, "execution_count": 219, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 220, "metadata": {}, "outputs": [], "source": [ "del d[100]" ] }, { "cell_type": "code", "execution_count": 221, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hundred'" ] }, "execution_count": 221, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = d.setdefault(100, 'hundred')\n", "result" ] }, { "cell_type": "code", "execution_count": 222, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0: 'zero', 1: 'one', 2: 'two', 99: 'ninetynine', 100: 'hundred'}" ] }, "execution_count": 222, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] } ], "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.10.7" } }, "nbformat": 4, "nbformat_minor": 4 }