{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Basics (2023-06-20 - 2023-06-22)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "a = 1\n", "b = 2" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "print(a)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "print(b)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "c, d = 3, 4" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = (3, 4)\n", "type(t)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "t = (3, 4, 5)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(t)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "tmp = c\n", "c = d\n", "d = tmp" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4, 3)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c, d" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "c, d = d, c" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "a = 42" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140640823526928" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0x7fe97e53c610'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hex(id(a))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "b = a" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140640823526928" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(b)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "a += 1" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140640823526960" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "del b" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "NameError(specific)\n" ] } ], "source": [ "try:\n", " b\n", "except NameError as e:\n", " print('NameError(specific)')\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "b = None" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "NoneType" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(b)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "print(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## DataTypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Integers" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "i = 2**64-1" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0xffffffffffffffff'" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hex(i)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "i += 1" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551616" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0x10000000000000000'" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hex(i)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**1000" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Datatype Conversions" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = '666'\n", "int(s)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3735928559" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('0xdeadbeef', 16)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "342" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('666', 7)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'666'" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(666)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666.0" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(666)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42.666" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float('42.666')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## (Im)mutable?" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "l = [1, 2, 3]" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140640741310912" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "l.append(4)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140640741310912" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "t = (1,2,3)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140640741034048" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(t)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'tuple' object has no attribute 'append' \n" ] } ], "source": [ "try:\n", " t.append(4)\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compound Datatypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "l = [1, 2, 3, 'vier']" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140640741341184" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier']" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "l.append(5.0)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier', 5.0]" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(l)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "l.extend([6, 7, 'acht'])" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier', 5.0, 6, 7, 'acht']" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "l.append([9, 10])" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier', 5.0, 6, 7, 'acht', [9, 10]]" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "del l[8]" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier', 5.0, 6, 7, 'acht']" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n" ] } ], "source": [ "for elem in l:\n", " print(type(elem))" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[int, int, int, str, float, int, int, str]" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[type(elem) for elem in l]" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier', 5.0, 6, 7, 'acht']" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[elem for elem in l if type(elem) is not list]" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "vier\n", "5.0\n", "6\n", "7\n", "acht\n" ] } ], "source": [ "for e in l:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [], "source": [ "l += [9,10]" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140640741341184" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l)" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier', 5.0, 6, 7, 'acht', 9, 10]" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 in l" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 in l" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 in l" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "666 in l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dictionary" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [], "source": [ "d = {'one': 1, 'two': 2, 3: 'three'}" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['one']" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'three'" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[3]" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(d)" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [], "source": [ "del d[3]" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 \n" ] } ], "source": [ "try:\n", " del d[2]\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [], "source": [ "d['three'] = 3" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'three' in d" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'four' in d" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [], "source": [ "d['three'] = 3.0" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2, 'three': 3.0}" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [], "source": [ "d['three'] = [3, 3.0, 'drei']" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2, 'three': [3, 3.0, 'drei']}" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(d)" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'four' in d" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'four' \n" ] } ], "source": [ "try:\n", " d['four']\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "elem = d.get('four')\n", "if elem is None:\n", " elem = 4\n", "print(elem)" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "elem = d.get('four', 4)\n", "print(elem)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise: Mixed List, Interactive Interpreter" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [], "source": [ "l = []" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [], "source": [ "l.append(42)" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[42]" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [], "source": [ "l.append(42.666)\n", "l.append(True)\n", "l.append('abc')" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[42, 42.666, True, 'abc']" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [], "source": [ "l.append([1,2,'drei'])" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[42, 42.666, True, 'abc', [1, 2, 'drei']]" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [], "source": [ "l.append((1, 2, 'drei'))" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[42, 42.666, True, 'abc', [1, 2, 'drei'], (1, 2, 'drei')]" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [], "source": [ "l.append({1,2,3})" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[42, 42.666, True, 'abc', [1, 2, 'drei'], (1, 2, 'drei'), {1, 2, 3}]" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [], "source": [ "l.append({1:'one', 2:'two'})" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[42,\n", " 42.666,\n", " True,\n", " 'abc',\n", " [1, 2, 'drei'],\n", " (1, 2, 'drei'),\n", " {1, 2, 3},\n", " {1: 'one', 2: 'two'}]" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``len()``, ``range()``, ``for``" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(l)" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42\n", "42.666\n", "True\n", "abc\n", "[1, 2, 'drei']\n", "(1, 2, 'drei')\n", "{1, 2, 3}\n", "{1: 'one', 2: 'two'}\n" ] } ], "source": [ "for elem in l:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "l = [0,1,2,3,4]\n", "for elem in l:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for elem in range(5):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "elem = 0\n", "while elem < 5:\n", " print(elem)\n", " elem += 1" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2, 'three': [3, 3.0, 'drei']}" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n", "two\n", "three\n" ] } ], "source": [ "for k in d:\n", " print(k)" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n", "two\n", "three\n" ] } ], "source": [ "for k in d.keys():\n", " print(k)" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "[3, 3.0, 'drei']\n" ] } ], "source": [ "for v in d.values():\n", " print(v)" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('one', 1)\n", "('two', 2)\n", "('three', [3, 3.0, 'drei'])\n" ] } ], "source": [ "for wasjetzt in d.items():\n", " print(wasjetzt)" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "key: one , value: 1\n", "key: two , value: 2\n", "key: three , value: [3, 3.0, 'drei']\n" ] } ], "source": [ "for elem in d.items():\n", " k = elem[0]\n", " v = elem[1]\n", " print('key: ', k, ', value: ', v)" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "key: one , value: 1\n", "key: two , value: 2\n", "key: three , value: [3, 3.0, 'drei']\n" ] } ], "source": [ "for k, v in d.items():\n", " print('key: ', k, ', value: ', v)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## References, (Im)mutability" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [], "source": [ "a = 42" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140640823526928" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [], "source": [ "b = a" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140640823526928" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(b)" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 113, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a) == id(b)" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 114, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a is b" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a == b" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [], "source": [ "l1 = [1, 2, 3]" ] }, { "cell_type": "code", "execution_count": 117, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140640722624768" ] }, "execution_count": 117, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l1)" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [], "source": [ "l2 = [1, 2, 3]" ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140640722715136" ] }, "execution_count": 119, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l2)" ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [], "source": [ "l1.append(4)" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 121, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 123, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1 is l2" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 124, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [], "source": [ "l1_copy = l1" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 126, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1_copy is l1" ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 127, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l1_copy) == id(l1)" ] }, { "cell_type": "code", "execution_count": 128, "metadata": {}, "outputs": [], "source": [ "l1.append(5)" ] }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 129, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 130, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1_copy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generators, ``yield``" ] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [], "source": [ "l = [1, 2, 3]" ] }, { "cell_type": "code", "execution_count": 132, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "for elem in l:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [], "source": [ "it = iter(l)" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 134, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 135, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 135, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 136, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 136, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \n" ] } ], "source": [ "try:\n", " next(it)\n", "except StopIteration as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 138, "metadata": {}, "outputs": [], "source": [ "s = 'abc'" ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a\n", "b\n", "c\n" ] } ], "source": [ "for elem in s:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 140, "metadata": {}, "outputs": [], "source": [ "l = list()" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 141, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [], "source": [ "l = list([1, 2, 3])" ] }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 143, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 144, "metadata": {}, "outputs": [], "source": [ "l = [1, 2, 3]" ] }, { "cell_type": "code", "execution_count": 145, "metadata": {}, "outputs": [], "source": [ "l = list(range(1, 4))" ] }, { "cell_type": "code", "execution_count": 146, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 146, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 147, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n" ] } ], "source": [ "for elem in range(4):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 148, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "3\n" ] } ], "source": [ "for elem in range(2, 4):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 149, "metadata": {}, "outputs": [], "source": [ "def my_funky_generator():\n", " print('bis zum ersten')\n", " yield 1\n", " print('bis zum zweiten')\n", " yield 2\n", " print('bis zum dritten')\n", " yield 3\n", " print('und aus')" ] }, { "cell_type": "code", "execution_count": 150, "metadata": {}, "outputs": [], "source": [ "meine_dumme_sequenz = my_funky_generator()" ] }, { "cell_type": "code", "execution_count": 151, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "generator" ] }, "execution_count": 151, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(meine_dumme_sequenz)" ] }, { "cell_type": "code", "execution_count": 152, "metadata": {}, "outputs": [], "source": [ "it = iter(meine_dumme_sequenz)" ] }, { "cell_type": "code", "execution_count": 153, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bis zum ersten\n" ] }, { "data": { "text/plain": [ "1" ] }, "execution_count": 153, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 154, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bis zum zweiten\n" ] }, { "data": { "text/plain": [ "2" ] }, "execution_count": 154, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 155, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bis zum dritten\n" ] }, { "data": { "text/plain": [ "3" ] }, "execution_count": 155, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 156, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "und aus\n", " \n" ] } ], "source": [ "try:\n", " next(it)\n", "except StopIteration as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bis zum ersten\n", "1\n", "bis zum zweiten\n", "2\n", "bis zum dritten\n", "3\n", "und aus\n" ] } ], "source": [ "for elem in my_funky_generator():\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 158, "metadata": {}, "outputs": [], "source": [ "def my_range(end):\n", " cur = 0\n", " while cur < end:\n", " yield cur\n", " cur += 1" ] }, { "cell_type": "code", "execution_count": 159, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n" ] } ], "source": [ "for elem in my_range(4):\n", " print(elem)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Other -ables: *Callable*" ] }, { "cell_type": "code", "execution_count": 160, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hallo\n" ] } ], "source": [ "print('hallo')" ] }, { "cell_type": "code", "execution_count": 161, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "builtin_function_or_method" ] }, "execution_count": 161, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(print)" ] }, { "cell_type": "code", "execution_count": 162, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "type" ] }, "execution_count": 162, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(list)" ] }, { "cell_type": "code", "execution_count": 163, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 163, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list()" ] }, { "cell_type": "code", "execution_count": 164, "metadata": {}, "outputs": [], "source": [ "def f():\n", " print('hallo f()')" ] }, { "cell_type": "code", "execution_count": 165, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hallo f()\n" ] } ], "source": [ "f()" ] }, { "cell_type": "code", "execution_count": 166, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "function" ] }, "execution_count": 166, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(f)" ] }, { "cell_type": "code", "execution_count": 167, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 167, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.__call__" ] }, { "cell_type": "code", "execution_count": 168, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hallo f()\n" ] } ], "source": [ "f.__call__()" ] }, { "cell_type": "code", "execution_count": 169, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 169, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list.__call__" ] }, { "cell_type": "code", "execution_count": 170, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c']" ] }, "execution_count": 170, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list.__call__('abc')" ] }, { "cell_type": "code", "execution_count": 171, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c']" ] }, "execution_count": 171, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list('abc')" ] }, { "cell_type": "code", "execution_count": 172, "metadata": {}, "outputs": [], "source": [ "class Greeter:\n", " def __call__(self, s):\n", " print('hallo', s)" ] }, { "cell_type": "code", "execution_count": 173, "metadata": {}, "outputs": [], "source": [ "g = Greeter()" ] }, { "cell_type": "code", "execution_count": 174, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "__main__.Greeter" ] }, "execution_count": 174, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(g)" ] }, { "cell_type": "code", "execution_count": 175, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hallo Joerg\n" ] } ], "source": [ "g('Joerg')" ] }, { "cell_type": "code", "execution_count": 176, "metadata": {}, "outputs": [ { "data": { "text/plain": [ ">" ] }, "execution_count": 176, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g.__call__" ] }, { "cell_type": "code", "execution_count": 177, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hallo Joerg\n" ] } ], "source": [ "g.__call__('Joerg')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Indexing and Slicing" ] }, { "cell_type": "code", "execution_count": 178, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 4, 5]" ] }, "execution_count": 178, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [1, 2, [3, 4, 5], 6, 7]\n", "l[2]" ] }, { "cell_type": "code", "execution_count": 179, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 179, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[2][1]" ] }, { "cell_type": "code", "execution_count": 180, "metadata": {}, "outputs": [], "source": [ "l[2:3] = l[2]" ] }, { "cell_type": "code", "execution_count": 181, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7]" ] }, "execution_count": 181, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## References, (Im)mutability" ] }, { "cell_type": "code", "execution_count": 182, "metadata": {}, "outputs": [], "source": [ "a = 42" ] }, { "cell_type": "code", "execution_count": 183, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140640823526928" ] }, "execution_count": 183, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "code", "execution_count": 184, "metadata": {}, "outputs": [], "source": [ "a += 1" ] }, { "cell_type": "code", "execution_count": 185, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140640823526960" ] }, "execution_count": 185, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "code", "execution_count": 186, "metadata": {}, "outputs": [], "source": [ "t = (1, 'eins')" ] }, { "cell_type": "code", "execution_count": 187, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140640740502720" ] }, "execution_count": 187, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(t)" ] }, { "cell_type": "code", "execution_count": 188, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'tuple' object has no attribute 'append' \n" ] } ], "source": [ "try:\n", " t.append(3)\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 189, "metadata": {}, "outputs": [], "source": [ "t += (2, 'zwei')" ] }, { "cell_type": "code", "execution_count": 190, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140640722823952" ] }, "execution_count": 190, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(t)" ] }, { "cell_type": "code", "execution_count": 191, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 'eins', 2, 'zwei')" ] }, "execution_count": 191, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t" ] }, { "cell_type": "code", "execution_count": 192, "metadata": {}, "outputs": [], "source": [ "l = [1, 2, 3]" ] }, { "cell_type": "code", "execution_count": 193, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140640741119552" ] }, "execution_count": 193, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l)" ] }, { "cell_type": "code", "execution_count": 194, "metadata": {}, "outputs": [], "source": [ "l += [4, 5, 6]" ] }, { "cell_type": "code", "execution_count": 195, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140640741119552" ] }, "execution_count": 195, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l)" ] }, { "cell_type": "code", "execution_count": 196, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6]" ] }, "execution_count": 196, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 197, "metadata": {}, "outputs": [], "source": [ "l_copy = l[:]" ] }, { "cell_type": "code", "execution_count": 198, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6]" ] }, "execution_count": 198, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l_copy" ] }, { "cell_type": "code", "execution_count": 199, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140640741312256" ] }, "execution_count": 199, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l_copy)" ] }, { "cell_type": "code", "execution_count": 200, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 200, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l_copy is l" ] }, { "cell_type": "code", "execution_count": 201, "metadata": {}, "outputs": [], "source": [ "l1 = [1, [2, 3, 4], 5]" ] }, { "cell_type": "code", "execution_count": 202, "metadata": {}, "outputs": [], "source": [ "l2 = l1[:]" ] }, { "cell_type": "code", "execution_count": 203, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, [2, 3, 4], 5]" ] }, "execution_count": 203, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 204, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 204, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2 is not l1" ] }, { "cell_type": "code", "execution_count": 205, "metadata": {}, "outputs": [], "source": [ "l2[1].append(666)" ] }, { "cell_type": "code", "execution_count": 206, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, [2, 3, 4, 666], 5]" ] }, "execution_count": 206, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 207, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, [2, 3, 4, 666], 5]" ] }, "execution_count": 207, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 208, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 208, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1[1] is l2[1]" ] }, { "cell_type": "code", "execution_count": 209, "metadata": {}, "outputs": [], "source": [ "import copy" ] }, { "cell_type": "code", "execution_count": 210, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, [2, 3, 4, 666], 5]" ] }, "execution_count": 210, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 211, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140640722734080" ] }, "execution_count": 211, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l1)" ] }, { "cell_type": "code", "execution_count": 212, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140640722760768" ] }, "execution_count": 212, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l1[1])" ] }, { "cell_type": "code", "execution_count": 213, "metadata": {}, "outputs": [], "source": [ "l3 = copy.deepcopy(l1)" ] }, { "cell_type": "code", "execution_count": 214, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140640722739712" ] }, "execution_count": 214, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l3)" ] }, { "cell_type": "code", "execution_count": 215, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140640741402496" ] }, "execution_count": 215, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l3[1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``exec()`` (and ``eval()``)" ] }, { "cell_type": "code", "execution_count": 216, "metadata": {}, "outputs": [], "source": [ "code = '''\n", "b = 666\n", "print('hallo')\n", "'''" ] }, { "cell_type": "code", "execution_count": 217, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hallo\n" ] } ], "source": [ "exec(code)" ] }, { "cell_type": "code", "execution_count": 218, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 218, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 219, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hallo\n" ] } ], "source": [ "context = {}\n", "exec(code, context)" ] }, { "cell_type": "code", "execution_count": 220, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 220, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 221, "metadata": {}, "outputs": [], "source": [ "del b" ] }, { "cell_type": "code", "execution_count": 222, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 222, "metadata": {}, "output_type": "execute_result" } ], "source": [ "context['b']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More About Strings" ] }, { "cell_type": "code", "execution_count": 223, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 223, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"abc\"" ] }, { "cell_type": "code", "execution_count": 224, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 224, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abc'" ] }, { "cell_type": "code", "execution_count": 225, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"don't\"" ] }, "execution_count": 225, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'don\\'t'" ] }, { "cell_type": "code", "execution_count": 226, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"don't\"" ] }, "execution_count": 226, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"don't\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Raw Strings" ] }, { "cell_type": "code", "execution_count": 227, "metadata": {}, "outputs": [], "source": [ "s = \"C:\\path\\to\\nowhere\"" ] }, { "cell_type": "code", "execution_count": 228, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\path\to\n", "owhere\n" ] } ], "source": [ "print(s)" ] }, { "cell_type": "code", "execution_count": 229, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\path\\to\\nowhere\n" ] } ], "source": [ "s = r\"C:\\path\\to\\nowhere\"\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 230, "metadata": {}, "outputs": [], "source": [ "import re" ] }, { "cell_type": "code", "execution_count": 231, "metadata": {}, "outputs": [], "source": [ "line = \" 666 | Joerg |Faschingbauer \"" ] }, { "cell_type": "code", "execution_count": 232, "metadata": {}, "outputs": [], "source": [ "regex = re.compile(r'^\\s*(\\d+)\\s*|\\s*([A-Za-z]+)\\s*|\\s*([A-Za-z]+)\\s*$')" ] }, { "cell_type": "code", "execution_count": 233, "metadata": {}, "outputs": [], "source": [ "match = regex.search(line)" ] }, { "cell_type": "code", "execution_count": 234, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'666'" ] }, "execution_count": 234, "metadata": {}, "output_type": "execute_result" } ], "source": [ "match.group(1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Formatting (f-Strings)" ] }, { "cell_type": "code", "execution_count": 235, "metadata": {}, "outputs": [], "source": [ "a = 42\n", "b = 666" ] }, { "cell_type": "code", "execution_count": 236, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'a hat den Wert 42, und b hat den Wert 666'" ] }, "execution_count": 236, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'a hat den Wert '+str(a)+', und b hat den Wert '+str(b)" ] }, { "cell_type": "code", "execution_count": 237, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'a hat den Wert 0000042, und b hat den Wert 666'" ] }, "execution_count": 237, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f'a hat den Wert {a:07}, und b hat den Wert {b}'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Miscellaneous String Methods" ] }, { "cell_type": "code", "execution_count": 238, "metadata": {}, "outputs": [], "source": [ "s = '666'" ] }, { "cell_type": "code", "execution_count": 239, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 239, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.isdigit()" ] }, { "cell_type": "code", "execution_count": 240, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 240, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abc'.isdigit()" ] }, { "cell_type": "code", "execution_count": 241, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 241, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abc'.isalpha()" ] }, { "cell_type": "code", "execution_count": 242, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 242, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abc'.isidentifier()" ] }, { "cell_type": "code", "execution_count": 243, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 243, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'666'.isidentifier()" ] }, { "cell_type": "code", "execution_count": 244, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 244, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'mississippi'.count('ss')" ] }, { "cell_type": "code", "execution_count": 245, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 245, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'file.csv'.endswith('.csv') " ] }, { "cell_type": "code", "execution_count": 246, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 246, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'mississippi'.find('ss')" ] }, { "cell_type": "code", "execution_count": 247, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 247, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'mississippi'.find('xx')" ] }, { "cell_type": "code", "execution_count": 248, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 248, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'mississippi'.rfind('ss')" ] }, { "cell_type": "code", "execution_count": 249, "metadata": {}, "outputs": [], "source": [ "line = \" 666 | Joerg |Faschingbauer \"" ] }, { "cell_type": "code", "execution_count": 250, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[' 666 ', ' Joerg ', 'Faschingbauer ']" ] }, "execution_count": 250, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line.split('|')" ] }, { "cell_type": "code", "execution_count": 251, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'666'" ] }, "execution_count": 251, "metadata": {}, "output_type": "execute_result" } ], "source": [ "' 666 '.strip()" ] }, { "cell_type": "code", "execution_count": 252, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'666 '" ] }, "execution_count": 252, "metadata": {}, "output_type": "execute_result" } ], "source": [ "' 666 '.lstrip()" ] }, { "cell_type": "code", "execution_count": 253, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' 666'" ] }, "execution_count": 253, "metadata": {}, "output_type": "execute_result" } ], "source": [ "' 666 '.rstrip()" ] }, { "cell_type": "code", "execution_count": 254, "metadata": {}, "outputs": [], "source": [ "line = \" 666 | Joerg |Faschingbauer \"" ] }, { "cell_type": "code", "execution_count": 255, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['666', 'Joerg', 'Faschingbauer']" ] }, "execution_count": 255, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[elem.strip() for elem in line.split('|')]" ] }, { "cell_type": "code", "execution_count": 256, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'666|Joerg|Faschingbauer'" ] }, "execution_count": 256, "metadata": {}, "output_type": "execute_result" } ], "source": [ "elems = ['666', 'Joerg', 'Faschingbauer']\n", "'|'.join(elems)" ] }, { "cell_type": "code", "execution_count": 257, "metadata": {}, "outputs": [], "source": [ "line = \" 666 | Joerg |Faschingbauer \"" ] }, { "cell_type": "code", "execution_count": 258, "metadata": {}, "outputs": [], "source": [ "svnr, firstname, lastname = [elem.strip() for elem in line.split('|')]" ] }, { "cell_type": "code", "execution_count": 259, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('666', 'Joerg', 'Faschingbauer')" ] }, "execution_count": 259, "metadata": {}, "output_type": "execute_result" } ], "source": [ "svnr, firstname, lastname" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### More About Lists" ] }, { "cell_type": "code", "execution_count": 260, "metadata": {}, "outputs": [], "source": [ "l = [1, 2, 'drei']" ] }, { "cell_type": "code", "execution_count": 261, "metadata": {}, "outputs": [], "source": [ "l.append(4.0)" ] }, { "cell_type": "code", "execution_count": 262, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'drei', 4.0]" ] }, "execution_count": 262, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 263, "metadata": {}, "outputs": [], "source": [ "l.extend([3, 4, 5])" ] }, { "cell_type": "code", "execution_count": 264, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'drei', 4.0, 3, 4, 5]" ] }, "execution_count": 264, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 265, "metadata": {}, "outputs": [], "source": [ "l.extend(range(10, 15))" ] }, { "cell_type": "code", "execution_count": 266, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'drei', 4.0, 3, 4, 5, 10, 11, 12, 13, 14]" ] }, "execution_count": 266, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 267, "metadata": {}, "outputs": [], "source": [ "l.extend('abc')" ] }, { "cell_type": "code", "execution_count": 268, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'drei', 4.0, 3, 4, 5, 10, 11, 12, 13, 14, 'a', 'b', 'c']" ] }, "execution_count": 268, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 269, "metadata": {}, "outputs": [], "source": [ "l.insert(3, 3.5)" ] }, { "cell_type": "code", "execution_count": 270, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'drei', 3.5, 4.0, 3, 4, 5, 10, 11, 12, 13, 14, 'a', 'b', 'c']" ] }, "execution_count": 270, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 271, "metadata": {}, "outputs": [], "source": [ "l.insert(0, -1)" ] }, { "cell_type": "code", "execution_count": 272, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-1, 1, 2, 'drei', 3.5, 4.0, 3, 4, 5, 10, 11, 12, 13, 14, 'a', 'b', 'c']" ] }, "execution_count": 272, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 273, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'drei'" ] }, "execution_count": 273, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.pop(3)" ] }, { "cell_type": "code", "execution_count": 274, "metadata": {}, "outputs": [], "source": [ "del l[1]" ] }, { "cell_type": "code", "execution_count": 275, "metadata": {}, "outputs": [], "source": [ "l = [45, 2, 3, 9, 1]" ] }, { "cell_type": "code", "execution_count": 276, "metadata": {}, "outputs": [], "source": [ "l.sort()" ] }, { "cell_type": "code", "execution_count": 277, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 9, 45]" ] }, "execution_count": 277, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 278, "metadata": {}, "outputs": [], "source": [ "l = [45, 2, 3, 9, 1]" ] }, { "cell_type": "code", "execution_count": 279, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 9, 45]" ] }, "execution_count": 279, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(l)" ] }, { "cell_type": "code", "execution_count": 280, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[45, 2, 3, 9, 1]" ] }, "execution_count": 280, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 281, "metadata": {}, "outputs": [], "source": [ "l.reverse()" ] }, { "cell_type": "code", "execution_count": 282, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 9, 3, 2, 45]" ] }, "execution_count": 282, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### More About Dictionaries" ] }, { "cell_type": "code", "execution_count": 283, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2}" ] }, "execution_count": 283, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = {'one':1, 'two': 2}\n", "d" ] }, { "cell_type": "code", "execution_count": 284, "metadata": {}, "outputs": [], "source": [ "l = [('one',1), ('two', 2)]\n", "d = dict(l)" ] }, { "cell_type": "code", "execution_count": 285, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2}" ] }, "execution_count": 285, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 286, "metadata": {}, "outputs": [], "source": [ "d['three'] = 3" ] }, { "cell_type": "code", "execution_count": 287, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2, 'three': 3}" ] }, "execution_count": 287, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 288, "metadata": {}, "outputs": [], "source": [ "d['three'] = 3.0" ] }, { "cell_type": "code", "execution_count": 289, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2, 'three': 3.0}" ] }, "execution_count": 289, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 290, "metadata": {}, "outputs": [], "source": [ "del d['three']" ] }, { "cell_type": "code", "execution_count": 291, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 291, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'three' in d" ] }, { "cell_type": "code", "execution_count": 293, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'three' \n" ] } ], "source": [ "try:\n", " d['three']\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 294, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bled, aber, waere doch wohl 3\n" ] } ], "source": [ "value = d.get('three')\n", "if value is None:\n", " print('bled, aber, waere doch wohl', 3)\n", "else:\n", " print('jo eh,', value)" ] }, { "cell_type": "code", "execution_count": 296, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] } ], "source": [ "value = d.get('three', 3)\n", "print(value)" ] }, { "cell_type": "code", "execution_count": 298, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] } ], "source": [ "if 'three' in d:\n", " value = d['three']\n", "else:\n", " value = 3\n", " d['three'] = value\n", "print(value)" ] }, { "cell_type": "code", "execution_count": 299, "metadata": {}, "outputs": [], "source": [ "del d['three']" ] }, { "cell_type": "code", "execution_count": 300, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] } ], "source": [ "value = d.setdefault('three', 3)\n", "print(value)" ] }, { "cell_type": "code", "execution_count": 301, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2, 'three': 3}" ] }, "execution_count": 301, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 302, "metadata": {}, "outputs": [], "source": [ "other = {'three': 3.0, 'four': 4.0}" ] }, { "cell_type": "code", "execution_count": 303, "metadata": {}, "outputs": [], "source": [ "d.update(other)" ] }, { "cell_type": "code", "execution_count": 305, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2, 'three': 3.0, 'four': 4.0}" ] }, "execution_count": 305, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### More About Sets" ] }, { "cell_type": "code", "execution_count": 306, "metadata": {}, "outputs": [], "source": [ "s = {1,2,3}" ] }, { "cell_type": "code", "execution_count": 307, "metadata": {}, "outputs": [], "source": [ "l = [1, 2, 3]" ] }, { "cell_type": "code", "execution_count": 308, "metadata": {}, "outputs": [], "source": [ "s = set(l)" ] }, { "cell_type": "code", "execution_count": 309, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3}" ] }, "execution_count": 309, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 310, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'a', 'b', 'c'}" ] }, "execution_count": 310, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = set('abc')\n", "s" ] }, { "cell_type": "code", "execution_count": 311, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0, 1, 2, 3, 4}" ] }, "execution_count": 311, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = set(range(5))\n", "s" ] }, { "cell_type": "code", "execution_count": 312, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 312, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 in s" ] }, { "cell_type": "code", "execution_count": 313, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0, 1, 3, 4}" ] }, "execution_count": 313, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.remove(2)\n", "s" ] }, { "cell_type": "code", "execution_count": 314, "metadata": {}, "outputs": [], "source": [ "s.add(2)" ] }, { "cell_type": "code", "execution_count": 315, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0, 1, 2, 3, 4}" ] }, "execution_count": 315, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 320, "metadata": {}, "outputs": [], "source": [ "s1 = set(range(4))\n", "s2 = set(range(3, 8))" ] }, { "cell_type": "code", "execution_count": 321, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "({0, 1, 2, 3}, {3, 4, 5, 6, 7})" ] }, "execution_count": 321, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1, s2" ] }, { "cell_type": "code", "execution_count": 322, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0, 1, 2, 3, 4, 5, 6, 7}" ] }, "execution_count": 322, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 | s2" ] }, { "cell_type": "code", "execution_count": 323, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{3}" ] }, "execution_count": 323, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 & s2" ] }, { "cell_type": "code", "execution_count": 324, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0, 1, 2, 4, 5, 6, 7}" ] }, "execution_count": 324, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 ^ s2" ] }, { "cell_type": "code", "execution_count": 325, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 325, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 < s2" ] }, { "cell_type": "code", "execution_count": 326, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 326, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 <= s2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Encoding" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "s = 'Liebe Grüße, Jörg'" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(s)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'ascii' codec can't encode characters in position 8-9: ordinal not in range(128) \n" ] } ], "source": [ "try:\n", " s.encode(encoding='ascii')\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "b = s.encode(encoding='iso-8859-1')" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "b'Liebe Gr\\xfc\\xdfe, J\\xf6rg'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bytes" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(b)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Liebe Grќпe, Jіrg'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.decode(encoding='iso-8859-5')" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b'Liebe Gr\\xfc\\xdfe, J\\xf6rg'\n" ] } ], "source": [ "print(b)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "lg = '祝好'" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(lg)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(lg)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "b'\\xaf\\xac\\xa6n'" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lg.encode(encoding='big5')" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(lg.encode(encoding='big5'))" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "unterschrift = lg + ' Jörg'" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'祝好 Jörg'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "unterschrift" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'latin-1' codec can't encode characters in position 0-1: ordinal not in range(256) \n" ] } ], "source": [ "try:\n", " unterschrift.encode('iso-8859-1')\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'big5' codec can't encode character '\\xf6' in position 4: illegal multibyte sequence \n" ] } ], "source": [ "try:\n", " unterschrift.encode('big5')\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "b'\\xe7\\xa5\\x9d\\xe5\\xa5\\xbd J\\xc3\\xb6rg'" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "unterschrift.encode('utf-8')" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "b'\\xff\\xfe]y}Y \\x00J\\x00\\xf6\\x00r\\x00g\\x00'" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "unterschrift.encode('utf-16')" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "b'\\xff\\xfe\\x00\\x00]y\\x00\\x00}Y\\x00\\x00 \\x00\\x00\\x00J\\x00\\x00\\x00\\xf6\\x00\\x00\\x00r\\x00\\x00\\x00g\\x00\\x00\\x00'" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "unterschrift.encode('utf-32')" ] } ], "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.11" } }, "nbformat": 4, "nbformat_minor": 4 }