{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Jupyter Notebook: Python Advanced (2023-04-24 - 2023-04-26)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Iterator Protocol" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for i in range(3):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for i in [0,1,2]:\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "drei_millionen_ints = list(range(3_000_000))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(drei_millionen_ints)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "r = range(3)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(r))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "it = iter(r)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(it))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "try:\n", " next(it)\n", "except StopIteration:\n", " pass" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for i in range(3):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "numbers = range(10)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "def odd_numbers(iterable):\n", " for number in iterable:\n", " if number % 2 != 0:\n", " print('next element')\n", " yield number" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "next element\n", "1\n", "next element\n", "3\n", "next element\n", "5\n", "next element\n", "7\n", "next element\n", "9\n" ] } ], "source": [ "for i in odd_numbers(numbers):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "def odd_numbers_func(l):\n", " ret_nums = []\n", " for number in l:\n", " if number % 2 != 0:\n", " ret_nums.append(number)\n", " print('returning result')\n", " return ret_nums" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "returning result\n", "1\n", "3\n", "5\n", "7\n", "9\n" ] } ], "source": [ "for i in odd_numbers_func(numbers):\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dynamic Features" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "a = 42" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "globals()['a']" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range(0, 10)" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "globals()['numbers']" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "code = '''\n", "eine_variable = 7\n", "'''" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "context = {}" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "exec(code, context)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "context['eine_variable']" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "class Foo:\n", " def __init__(self, bar, blech):\n", " self.bar = bar\n", " self.blech = blech" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "f = Foo('Joerg', 'Faschingbauer')" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'bar': 'Joerg', 'blech': 'Faschingbauer'}" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.__dict__" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(odd_numbers))" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(a))" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'odd_numbers'" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "odd_numbers.__name__" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "blah = odd_numbers" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "blah" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'odd_numbers'" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "blah.__name__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variables" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "a = 42" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(a))" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "a = 1.234" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "a = [1,2,'drei']" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "a = odd_numbers" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "function" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "t = (1,2)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(t)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "x, y = t" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "x, y = 1, 2" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "x, y = (1, 2)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "x, y = 1, 2" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "tmp = x" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [], "source": [ "x = y" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "y = tmp" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "x, y = 1, 2" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [], "source": [ "x, y = y, x" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [], "source": [ "a = 42" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [], "source": [ "b = a" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0x7f32ccc4c610'" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hex(id(a))" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0x7f32ccc4c610'" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hex(id(b))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Datatypes" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [], "source": [ "i = 42" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [], "source": [ "i = 2**64-1" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b1111111111111111111111111111111111111111111111111111111111111111'" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(i)" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [], "source": [ "i += 1" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551616" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b10000000000000000000000000000000000000000000000000000000000000000'" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(i)" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1000000" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10**6" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10000000000" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10**10" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10**100" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10**1000" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.5" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3/2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ``is`` vs. ``==``" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [], "source": [ "l1 = [1,2,3]" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [], "source": [ "l2 = [3,4,5]" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139855258515840" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l1)" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139856078033344" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l2)" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1 is l2" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1 == l2" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [], "source": [ "l3 = [1,2,3]" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139856078226432" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l3)" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1 is l3" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1 == l3" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [], "source": [ "l1.append(4)" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l3" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [], "source": [ "i1 = 42" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [], "source": [ "i2 = i1" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139856160540176" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(i1)" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139856160540176" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(i2)" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i1 is i2" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i1 == i2" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [], "source": [ "i1 = 42" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [], "source": [ "i2 = 42" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i1 is i2" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [], "source": [ "i3 = 10**1000" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [], "source": [ "i4 = 10**1000" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i3 is i4" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [], "source": [ "b = False" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yay\n" ] } ], "source": [ "if b is False:\n", " print('yay')" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yay\n" ] } ], "source": [ "if b == False:\n", " print('yay')" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [], "source": [ "if b:\n", " print('nope')" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l3" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yay\n" ] } ], "source": [ "if l3:\n", " print('yay')" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [], "source": [ "if []:\n", " print('yay')" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [], "source": [ "s = 'abc'" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yay\n" ] } ], "source": [ "if s:\n", " print('yay')" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [], "source": [ "if '':\n", " print('yay')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Datatype Conversions" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [], "source": [ "s = 'abc'" ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(s))" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [], "source": [ "i = 42" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [], "source": [ "s = str(i)" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'42'" ] }, "execution_count": 114, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s == i" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "''" ] }, "execution_count": 116, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str()" ] }, { "cell_type": "code", "execution_count": 117, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 117, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int()" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [], "source": [ "i = int('42')" ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 119, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "66" ] }, "execution_count": 120, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('42', 16)" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2730" ] }, "execution_count": 121, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('0101010101010', 2)" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(l1))" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 123, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list()" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c']" ] }, "execution_count": 124, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list('abc')" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a\n", "b\n", "c\n" ] } ], "source": [ "for c in 'abc':\n", " print(c)" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [], "source": [ "l = []\n", "for c in 'abc':\n", " l.append(c)" ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c']" ] }, "execution_count": 127, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 128, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2]" ] }, "execution_count": 128, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(range(3))" ] }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 129, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(l)" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 130, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(42)" ] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(int)" ] }, { "cell_type": "code", "execution_count": 132, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(int))" ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [], "source": [ "int = 666" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'int' object is not callable \n" ] } ], "source": [ "try:\n", " int('42')\n", "except TypeError as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 135, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['.bash_logout',\n", " '.bash_profile',\n", " '.cache',\n", " '.config',\n", " '.local',\n", " 'Desktop',\n", " 'Downloads',\n", " 'Templates',\n", " 'Public',\n", " 'Documents',\n", " 'Music',\n", " 'Pictures',\n", " 'Videos',\n", " 'old-home',\n", " 'Homebrain',\n", " '.thunderbird',\n", " '.mozilla',\n", " 'work',\n", " 'venv',\n", " '.emacs.d',\n", " '.bashrc~',\n", " '.emacs',\n", " '.gnupg',\n", " 'tmp',\n", " '.wget-hsts',\n", " '.cups',\n", " '.gitconfig',\n", " '.pki',\n", " '.var',\n", " '.zoom',\n", " '.mplayer',\n", " '.ipython',\n", " '.jupyter',\n", " 'hello.~1~',\n", " 'x-tools',\n", " 'cross',\n", " 'build',\n", " '.dia',\n", " '.bashrc.~1~',\n", " '.bashrc.~2~',\n", " '.bashrc',\n", " 'raspi-home',\n", " 'daham',\n", " 'C++-Training-2023-03-20',\n", " '.ssh',\n", " '#*message*-20230403-222915#',\n", " 'blink-home',\n", " 'can.log',\n", " '.bash_history',\n", " '.python_history',\n", " '.lesshst']" ] }, "execution_count": 135, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import os\n", "os.listdir('/home/jfasch')" ] }, { "cell_type": "code", "execution_count": 136, "metadata": {}, "outputs": [], "source": [ "import os\n", "os.listdir = lambda d: []" ] }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 137, "metadata": {}, "output_type": "execute_result" } ], "source": [ "os.listdir('/home/jfasch')" ] }, { "cell_type": "code", "execution_count": 138, "metadata": {}, "outputs": [], "source": [ "os.listdir = None" ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'NoneType' object is not callable \n" ] } ], "source": [ "try:\n", " os.listdir('/home/jfasch')\n", "except TypeError as e:\n", " print(e, type(e))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Compound DataTypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### List, Tuple" ] }, { "cell_type": "code", "execution_count": 140, "metadata": {}, "outputs": [], "source": [ "l = [1,2,3]" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [], "source": [ "l.append(4)" ] }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 142, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "outputs": [], "source": [ "l.extend([5,6,7])" ] }, { "cell_type": "code", "execution_count": 144, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7]" ] }, "execution_count": 144, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 145, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 'a', 'b', 'c']" ] }, "execution_count": 145, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.extend('abc')\n", "l" ] }, { "cell_type": "code", "execution_count": 146, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'int' object is not iterable \n" ] } ], "source": [ "try:\n", " l.extend(666)\n", "except TypeError as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 147, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 'a', 'b', 'c', [8, 9, 10]]" ] }, "execution_count": 147, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.append([8,9,10])\n", "l" ] }, { "cell_type": "code", "execution_count": 148, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 148, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[0]" ] }, { "cell_type": "code", "execution_count": 149, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 149, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[1]" ] }, { "cell_type": "code", "execution_count": 150, "metadata": {}, "outputs": [], "source": [ "l1 = [1,2,3]" ] }, { "cell_type": "code", "execution_count": 151, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139855258795072" ] }, "execution_count": 151, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l1)" ] }, { "cell_type": "code", "execution_count": 152, "metadata": {}, "outputs": [], "source": [ "l2 = [4,5,6]" ] }, { "cell_type": "code", "execution_count": 153, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6]" ] }, "execution_count": 153, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1 + l2" ] }, { "cell_type": "code", "execution_count": 154, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139855258795072" ] }, "execution_count": 154, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l1)" ] }, { "cell_type": "code", "execution_count": 155, "metadata": {}, "outputs": [], "source": [ "l1 += l2" ] }, { "cell_type": "code", "execution_count": 156, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6]" ] }, "execution_count": 156, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139855258795072" ] }, "execution_count": 157, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l1) # <--- mutable" ] }, { "cell_type": "code", "execution_count": 158, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "t = (1,2,3)\n", "print(type(t))" ] }, { "cell_type": "code", "execution_count": 159, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, 3)" ] }, "execution_count": 159, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t" ] }, { "cell_type": "code", "execution_count": 160, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 160, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[0]" ] }, { "cell_type": "code", "execution_count": 161, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 161, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[1]" ] }, { "cell_type": "code", "execution_count": 162, "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, type(e))" ] }, { "cell_type": "code", "execution_count": 163, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139855260513728" ] }, "execution_count": 163, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(t)" ] }, { "cell_type": "code", "execution_count": 164, "metadata": {}, "outputs": [], "source": [ "t += (4,5,6)" ] }, { "cell_type": "code", "execution_count": 165, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139856085569920" ] }, "execution_count": 165, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(t)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Dictionary" ] }, { "cell_type": "code", "execution_count": 166, "metadata": {}, "outputs": [], "source": [ "d = {\n", " 'one': 1,\n", " 'two': 2,\n", "}" ] }, { "cell_type": "code", "execution_count": 167, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 167, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['one']" ] }, { "cell_type": "code", "execution_count": 168, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 168, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'one' in d" ] }, { "cell_type": "code", "execution_count": 169, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 169, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'four' in d" ] }, { "cell_type": "code", "execution_count": 170, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 170, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'four' not in d" ] }, { "cell_type": "code", "execution_count": 171, "metadata": {}, "outputs": [], "source": [ "d['four'] = 'vier'" ] }, { "cell_type": "code", "execution_count": 172, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2, 'four': 'vier'}" ] }, "execution_count": 172, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 173, "metadata": {}, "outputs": [], "source": [ "del d['four']" ] }, { "cell_type": "code", "execution_count": 174, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 174, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'four' in d" ] }, { "cell_type": "code", "execution_count": 196, "metadata": {}, "outputs": [], "source": [ "d['four'] = 'vier'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**pop**" ] }, { "cell_type": "code", "execution_count": 197, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'vier'" ] }, "execution_count": 197, "metadata": {}, "output_type": "execute_result" } ], "source": [ "elem = d['four']\n", "del d['four']\n", "elem" ] }, { "cell_type": "code", "execution_count": 198, "metadata": {}, "outputs": [], "source": [ "d['four'] = 'vier'" ] }, { "cell_type": "code", "execution_count": 199, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'vier'" ] }, "execution_count": 199, "metadata": {}, "output_type": "execute_result" } ], "source": [ "elem = d.pop('four')\n", "elem" ] }, { "cell_type": "code", "execution_count": 177, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'four' \n" ] } ], "source": [ "try:\n", " d.pop('four')\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**get**" ] }, { "cell_type": "code", "execution_count": 201, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nix\n" ] } ], "source": [ "try:\n", " elem = d['four']\n", "except KeyError:\n", " elem = None\n", "if elem is None:\n", " print('nix')\n", "else:\n", " print('yay')" ] }, { "cell_type": "code", "execution_count": 205, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nix\n" ] } ], "source": [ "elem = d.get('four')\n", "if elem is None:\n", " print('nix')\n", "else:\n", " print('yay')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Doppelt gemoppelt:" ] }, { "cell_type": "code", "execution_count": 217, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "if 'four' in d: # <--- constant time\n", " elem = d['four']\n", "else:\n", " elem = 4\n", "print(elem)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Einfach gemoppelt:" ] }, { "cell_type": "code", "execution_count": 218, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "elem = d.get('four', 4)\n", "print(elem)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "``keys()``? ``in``?" ] }, { "cell_type": "code", "execution_count": 219, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "if 'four' in d.keys(): # <--- sequential search!\n", " elem = d['four']\n", "else:\n", " elem = 4\n", "print(elem)" ] }, { "cell_type": "code", "execution_count": 211, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys" ] }, "execution_count": 211, "metadata": {}, "output_type": "execute_result" } ], "source": [ "keys = d.keys()\n", "type(keys)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Wenn ``'four'`` nicht in d, dann trag 4 ein (und gib 4 zurueck). Wenn doch drin, gib dessen Wert zurueck." ] }, { "cell_type": "code", "execution_count": 220, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "if 'four' not in d:\n", " d['four'] = 4\n", " value = 4\n", "else:\n", " value = d['four']\n", "print(value)" ] }, { "cell_type": "code", "execution_count": 221, "metadata": {}, "outputs": [], "source": [ "del d['four']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**setdefault**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Exakt das gleiche:" ] }, { "cell_type": "code", "execution_count": 222, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "value = d.setdefault('four', 4)\n", "print(value)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Dictionary constructor**" ] }, { "cell_type": "code", "execution_count": 226, "metadata": {}, "outputs": [], "source": [ "d = {\n", " 'one': 1,\n", " 'two': 2,\n", " 'three': 3,\n", "}" ] }, { "cell_type": "code", "execution_count": 227, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2, 'three': 3}" ] }, "execution_count": 227, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 229, "metadata": {}, "outputs": [], "source": [ "d = dict([('one', 1), ('two', 2), ('three', 3)])" ] }, { "cell_type": "code", "execution_count": 230, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2, 'three': 3}" ] }, "execution_count": 230, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Dictionary iteration**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Key iteration, implizit" ] }, { "cell_type": "code", "execution_count": 233, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n", "two\n", "three\n" ] } ], "source": [ "for elem in d:\n", " print(elem)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Key iteration, explizit" ] }, { "cell_type": "code", "execution_count": 234, "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": "markdown", "metadata": {}, "source": [ "Value iteration" ] }, { "cell_type": "code", "execution_count": 235, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "for v in d.values():\n", " print(v)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Paarweise iteration (keys, values)" ] }, { "cell_type": "code", "execution_count": 237, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('one', 1)\n", "('two', 2)\n", "('three', 3)\n" ] } ], "source": [ "for elem in d.items():\n", " print(elem)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Tuple unpacking*" ] }, { "cell_type": "code", "execution_count": 238, "metadata": {}, "outputs": [], "source": [ "x, y = (1,2)" ] }, { "cell_type": "code", "execution_count": 239, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 239, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 240, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 240, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y" ] }, { "cell_type": "code", "execution_count": 241, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "key: one, value: 1\n", "key: two, value: 2\n", "key: three, value: 3\n" ] } ], "source": [ "for elem in d.items():\n", " k = elem[0]\n", " v = elem[1]\n", " print(f'key: {k}, value: {v}')" ] }, { "cell_type": "code", "execution_count": 242, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "key: one, value: 1\n", "key: two, value: 2\n", "key: three, value: 3\n" ] } ], "source": [ "for k, v in d.items():\n", " print(f'key: {k}, value: {v}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Set" ] }, { "cell_type": "code", "execution_count": 178, "metadata": {}, "outputs": [], "source": [ "s = {1,2,3}" ] }, { "cell_type": "code", "execution_count": 179, "metadata": {}, "outputs": [], "source": [ "s = set()" ] }, { "cell_type": "code", "execution_count": 180, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'a', 'b', 'c'}" ] }, "execution_count": 180, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = set('abc')\n", "s" ] }, { "cell_type": "code", "execution_count": 181, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 181, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'a' in s" ] }, { "cell_type": "code", "execution_count": 182, "metadata": {}, "outputs": [], "source": [ "s.add('d')" ] }, { "cell_type": "code", "execution_count": 183, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 183, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'d' in s" ] }, { "cell_type": "code", "execution_count": 184, "metadata": {}, "outputs": [], "source": [ "s.remove('a')" ] }, { "cell_type": "code", "execution_count": 185, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 185, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'a' in s" ] }, { "cell_type": "code", "execution_count": 186, "metadata": {}, "outputs": [], "source": [ "s = frozenset('abc')" ] }, { "cell_type": "code", "execution_count": 187, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'frozenset' object has no attribute 'add' \n" ] } ], "source": [ "try:\n", " s.add('d')\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 188, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b\n", "a\n", "c\n" ] } ], "source": [ "for elem in s:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 189, "metadata": {}, "outputs": [], "source": [ "s1 = {1,2,3}\n", "s2 = {2,3,4,5}" ] }, { "cell_type": "code", "execution_count": 190, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4, 5}" ] }, "execution_count": 190, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 | s2" ] }, { "cell_type": "code", "execution_count": 191, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{2, 3}" ] }, "execution_count": 191, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 & s2" ] }, { "cell_type": "code", "execution_count": 192, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 4, 5}" ] }, "execution_count": 192, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1 ^ s2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Mutability" ] }, { "cell_type": "code", "execution_count": 243, "metadata": {}, "outputs": [], "source": [ "a = [1,2,3]\n", "b = a" ] }, { "cell_type": "code", "execution_count": 245, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 245, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a is b" ] }, { "cell_type": "code", "execution_count": 246, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 246, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a) == id(b)" ] }, { "cell_type": "code", "execution_count": 247, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 247, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.append(4)\n", "b" ] }, { "cell_type": "code", "execution_count": 248, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 248, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 250, "metadata": {}, "outputs": [], "source": [ "a = [1,2,3]\n", "b = a[:]" ] }, { "cell_type": "code", "execution_count": 251, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 251, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a is b" ] }, { "cell_type": "code", "execution_count": 252, "metadata": {}, "outputs": [], "source": [ "b.append(4)" ] }, { "cell_type": "code", "execution_count": 254, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 254, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 255, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 255, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Nested lists?" ] }, { "cell_type": "code", "execution_count": 256, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 256, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1, [2,3,4], 5]\n", "len(a)" ] }, { "cell_type": "code", "execution_count": 257, "metadata": {}, "outputs": [], "source": [ "b = a[:]" ] }, { "cell_type": "code", "execution_count": 258, "metadata": {}, "outputs": [], "source": [ "b[0] = 666" ] }, { "cell_type": "code", "execution_count": 259, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[666, [2, 3, 4], 5]" ] }, "execution_count": 259, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 260, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, [2, 3, 4], 5]" ] }, "execution_count": 260, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 261, "metadata": {}, "outputs": [], "source": [ "b[1].append(666)" ] }, { "cell_type": "code", "execution_count": 262, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[666, [2, 3, 4, 666], 5]" ] }, "execution_count": 262, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 263, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, [2, 3, 4, 666], 5]" ] }, "execution_count": 263, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## File I/O" ] }, { "cell_type": "code", "execution_count": 269, "metadata": {}, "outputs": [], "source": [ "f = open('/etc/passwd')" ] }, { "cell_type": "code", "execution_count": 270, "metadata": {}, "outputs": [], "source": [ "block = f.read(10)" ] }, { "cell_type": "code", "execution_count": 272, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 272, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(block)" ] }, { "cell_type": "code", "execution_count": 274, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'root:x:0:0'" ] }, "execution_count": 274, "metadata": {}, "output_type": "execute_result" } ], "source": [ "block" ] }, { "cell_type": "code", "execution_count": 275, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "':root:/root:/bin/bash\\n'" ] }, "execution_count": 275, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.readline()" ] }, { "cell_type": "code", "execution_count": 276, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'bin:x:1:1:bin:/bin:/sbin/nologin\\n'" ] }, "execution_count": 276, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.readline()" ] }, { "cell_type": "code", "execution_count": 277, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'daemon:x:2:2:daemon:/sbin:/sbin/nologin\\n'" ] }, "execution_count": 277, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.readline()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "... und so weiter ..." ] }, { "cell_type": "code", "execution_count": 291, "metadata": {}, "outputs": [], "source": [ "f = open('/etc/passwd')" ] }, { "cell_type": "code", "execution_count": 292, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "root:x:0:0:root:/root:/bin/bash\n", "bin:x:1:1:bin:/bin:/sbin/nologin\n", "daemon:x:2:2:daemon:/sbin:/sbin/nologin\n", "adm:x:3:4:adm:/var/adm:/sbin/nologin\n", "lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin\n", "sync:x:5:0:sync:/sbin:/bin/sync\n", "shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown\n", "halt:x:7:0:halt:/sbin:/sbin/halt\n", "mail:x:8:12:mail:/var/spool/mail:/sbin/nologin\n", "operator:x:11:0:operator:/root:/sbin/nologin\n", "games:x:12:100:games:/usr/games:/sbin/nologin\n", "ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin\n", "nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin\n", "dbus:x:81:81:System message bus:/:/sbin/nologin\n", "apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin\n", "tss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin\n", "systemd-network:x:192:192:systemd Network Management:/:/usr/sbin/nologin\n", "systemd-oom:x:999:999:systemd Userspace OOM Killer:/:/usr/sbin/nologin\n", "systemd-resolve:x:193:193:systemd Resolver:/:/usr/sbin/nologin\n", "qemu:x:107:107:qemu user:/:/sbin/nologin\n", "polkitd:x:998:997:User for polkitd:/:/sbin/nologin\n", "avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin\n", "unbound:x:997:995:Unbound DNS resolver:/etc/unbound:/sbin/nologin\n", "nm-openconnect:x:996:994:NetworkManager user for OpenConnect:/:/sbin/nologin\n", "geoclue:x:995:993:User for geoclue:/var/lib/geoclue:/sbin/nologin\n", "usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin\n", "gluster:x:994:992:GlusterFS daemons:/run/gluster:/sbin/nologin\n", "rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin\n", "chrony:x:993:990::/var/lib/chrony:/sbin/nologin\n", "saslauth:x:992:76:Saslauthd user:/run/saslauthd:/sbin/nologin\n", "dnsmasq:x:991:989:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin\n", "rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin\n", "colord:x:990:988:User for colord:/var/lib/colord:/sbin/nologin\n", "rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin\n", "openvpn:x:989:987:OpenVPN:/etc/openvpn:/sbin/nologin\n", "nm-openvpn:x:988:986:Default user for running openvpn spawned by NetworkManager:/:/sbin/nologin\n", "pipewire:x:987:985:PipeWire System Daemon:/var/run/pipewire:/sbin/nologin\n", "abrt:x:173:173::/etc/abrt:/sbin/nologin\n", "flatpak:x:986:983:User for flatpak system helper:/:/sbin/nologin\n", "gdm:x:42:42:GNOME Display Manager:/var/lib/gdm:/sbin/nologin\n", "gnome-initial-setup:x:985:982::/run/gnome-initial-setup/:/sbin/nologin\n", "vboxadd:x:984:1::/var/run/vboxadd:/sbin/nologin\n", "sshd:x:74:74:Privilege-separated SSH:/usr/share/empty.sshd:/sbin/nologin\n", "tcpdump:x:72:72::/:/sbin/nologin\n", "jfasch:x:1000:1000:Joerg Faschingbauer:/home/jfasch:/bin/bash\n", "systemd-coredump:x:978:978:systemd Core Dumper:/:/usr/sbin/nologin\n", "systemd-timesync:x:977:977:systemd Time Synchronization:/:/usr/sbin/nologin\n", "mosquitto:x:976:976:Mosquitto Broker:/etc/mosquitto:/sbin/nologin\n" ] } ], "source": [ "for line in f:\n", " print(line.rstrip()) # <--- strip trailing newline" ] }, { "cell_type": "code", "execution_count": 293, "metadata": {}, "outputs": [], "source": [ "f.close()" ] }, { "cell_type": "code", "execution_count": 294, "metadata": {}, "outputs": [], "source": [ "def read_lines_of_file(filename):\n", " f = open('/etc/passwd')\n", " lines = f.readlines()\n", " return lines" ] }, { "cell_type": "code", "execution_count": 295, "metadata": {}, "outputs": [], "source": [ "lines = read_lines_of_file('/etc/passwd')" ] }, { "cell_type": "code", "execution_count": 296, "metadata": {}, "outputs": [], "source": [ "with open('/etc/passwd') as f:\n", " # read file\n", " pass" ] }, { "cell_type": "code", "execution_count": 298, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[Errno 13] Permission denied: '/etc/passwd' \n" ] } ], "source": [ "try:\n", " f = open('/etc/passwd', 'w')\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``enumerate()``" ] }, { "cell_type": "code", "execution_count": 299, "metadata": {}, "outputs": [], "source": [ "l = ['Joerg', 'Faschingbauer', 'blah']" ] }, { "cell_type": "code", "execution_count": 301, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Joerg\n", "Faschingbauer\n", "blah\n" ] } ], "source": [ "for elem in l:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 305, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(0, 'Joerg')\n", "(1, 'Faschingbauer')\n", "(2, 'blah')\n" ] } ], "source": [ "for elem in enumerate(l):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 306, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0]: Joerg\n", "[1]: Faschingbauer\n", "[2]: blah\n" ] } ], "source": [ "for pos, elem in enumerate(l):\n", " print(f'[{pos}]: {elem}')" ] }, { "cell_type": "code", "execution_count": 307, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Joerg-Faschingbauer-blah'" ] }, "execution_count": 307, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'-'.join(['Joerg', 'Faschingbauer', 'blah'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``class``" ] }, { "cell_type": "code", "execution_count": 308, "metadata": {}, "outputs": [], "source": [ "class Foo:\n", " pass" ] }, { "cell_type": "code", "execution_count": 309, "metadata": {}, "outputs": [], "source": [ "f = Foo()" ] }, { "cell_type": "code", "execution_count": 312, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(f))" ] }, { "cell_type": "code", "execution_count": 314, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{}" ] }, "execution_count": 314, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.__dict__" ] }, { "cell_type": "code", "execution_count": 317, "metadata": {}, "outputs": [], "source": [ "f.bar = 'Blah'" ] }, { "cell_type": "code", "execution_count": 319, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'bar': 'Blah'}" ] }, "execution_count": 319, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.__dict__" ] }, { "cell_type": "code", "execution_count": 321, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "<__main__.Foo at 0x7f32c7d8d390>" ] }, "execution_count": 321, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f" ] }, { "cell_type": "code", "execution_count": 326, "metadata": {}, "outputs": [], "source": [ "class Foo:\n", " def __init__(self, bar):\n", " self.bar = bar" ] }, { "cell_type": "code", "execution_count": 327, "metadata": {}, "outputs": [], "source": [ "f = Foo('blah')" ] }, { "cell_type": "code", "execution_count": 324, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'blah'" ] }, "execution_count": 324, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.bar" ] }, { "cell_type": "code", "execution_count": 325, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'blah'" ] }, "execution_count": 325, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.__dict__['bar']" ] } ], "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 }