{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Basics (2023-05-15 - 2023-05-17)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "code = '[1,2,3]'" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(code)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "blah blah" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eval(code)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Syntax etc." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def add(a, b):\n", " 'Complex algorithm to add two numbers'\n", " return a+b" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(add))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Complex algorithm to add two numbers'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "add.__doc__" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function add in module __main__:\n", "\n", "add(a, b)\n", " Complex algorithm to add two numbers\n", "\n" ] } ], "source": [ "help(add)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "class Foo:\n", " 'pointless class'" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'pointless class'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Foo.__doc__" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on class Foo in module __main__:\n", "\n", "class Foo(builtins.object)\n", " | pointless class\n", " | \n", " | Data descriptors defined here:\n", " | \n", " | __dict__\n", " | dictionary for instance variables (if defined)\n", " | \n", " | __weakref__\n", " | list of weak references to the object (if defined)\n", "\n" ] } ], "source": [ "help(Foo)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Was ist das mit ``__``?" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "class Bar:\n", " def __init__(self, param):\n", " self.member = param" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "b = Bar(42)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "class Bar:\n", " def __init__(self, param):\n", " self.member = param\n", " def doSomething(self):\n", " return self.member**2" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1764" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = Bar(42)\n", "b.doSomething()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "<__main__.Bar object at 0x7fe7fbf80df0>\n" ] } ], "source": [ "print(b)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "class Bar:\n", " def __init__(self, param):\n", " self.member = param\n", " def doSomething(self):\n", " return self.member**2\n", " def __str__(self):\n", " return f'Ein nettes sinnloses Objekt mit dem Wert {self.member}'" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "b = Bar(42)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ein nettes sinnloses Objekt mit dem Wert 42\n" ] } ], "source": [ "print(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variables" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "a = 42" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(a))" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "a = 1.5" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(a))" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "a = 'blah'" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(a))" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "a = [1,2,3]\n", "print(type(a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Tuple unpacking**" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "a = 1\n", "b = 2" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "a, b = 1, 2" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "x = 1, 2" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(x))" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2)" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "not enough values to unpack (expected 3, got 2) \n" ] } ], "source": [ "try:\n", " a, b, c = 1, 2\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hier bin ich\n" ] } ], "source": [ "print('hier bin ich')" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2)" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a, b" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "tmp = a\n", "a = b\n", "b = tmp" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 1)" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a, b" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "a, b = b, a" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2)" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a, b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Object Identity**" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "a = 42" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140634424624656" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "b = a" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140634424624656" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(b)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "b = 7" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140634424623536" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(b)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140634424624656" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Datatypes" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [], "source": [ "i = 42" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "i = -42" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-42" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "i = 0o644" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "420" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [], "source": [ "i = 0xdeadbeef" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3735928559" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [], "source": [ "i = 0b01110101" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "117" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "i = 2**64-1" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b1111111111111111111111111111111111111111111111111111111111111111'" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(i)" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "66" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(bin(i))" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [], "source": [ "i += 1" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551616" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "67" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(bin(i))" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10**1000" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.5" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3/2" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3//2" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "can't multiply sequence by non-int of type 'list' \n" ] } ], "source": [ "l1 = [1,2,3]\n", "l2 = [2,3,4]\n", "try:\n", " l1*l2\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 2, 3, 4]" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1 + l2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Datatype Conversions" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [], "source": [ "i = 42" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [], "source": [ "s = str(i)" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'42'" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'42'" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i.__str__()" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42.0" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i.__float__()" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [], "source": [ "f = float(i)" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42.0" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(f)" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.__int__()" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [], "source": [ "s = '42'" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(s)" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "66" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(s, 16)" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [], "source": [ "s = '0xdeadbeef'" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "invalid literal for int() with base 10: '0xdeadbeef' \n" ] } ], "source": [ "try:\n", " int(s)\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3735928559" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(s, 16)" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.3" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 + 2.3" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "can only concatenate str (not \"int\") to str \n" ] } ], "source": [ "try:\n", " '1' + 2\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "can only concatenate str (not \"int\") to str \n" ] } ], "source": [ "try:\n", " '1'.__add__(2)\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Difference between ctor and ``eval()``?" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('42')" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eval('42')" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [], "source": [ "s = '[1,2,3]'" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['[', '1', ',', '2', ',', '3', ']']" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(s)" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eval(s)" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [], "source": [ "code = '''\n", "i = 0\n", "while i<42:\n", " i+=1\n", "print(i)\n", "'''" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(code)" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42\n" ] } ], "source": [ "exec(code)" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [], "source": [ "config = '''\n", "SETTING_BLAH = 42\n", "SETTING_FOO = 'jo oida'\n", "'''" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [], "source": [ "config_context = {}" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [], "source": [ "exec(config, config_context)" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "config_context['SETTING_BLAH']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Immutable vs. Mutable" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [], "source": [ "def foo(a):\n", " print(id(a))" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [], "source": [ "x = 666" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140634341074320" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(x)" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "140634341074320\n" ] } ], "source": [ "foo(x)" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [], "source": [ "x = 42" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140634424624656" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(x)" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [], "source": [ "x += 1" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140634424624688" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(x)" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [], "source": [ "x = 42" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140634424624656" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(x)" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [], "source": [ "x = 1234" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [], "source": [ "y = 1234" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 109, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x is y" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140634341076688" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(x)" ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140634341077776" ] }, "execution_count": 111, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Memory Management**" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [], "source": [ "abc = 666" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [], "source": [ "del abc" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name 'abc' is not defined \n" ] } ], "source": [ "try:\n", " abc\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [], "source": [ "f = open('/etc/passwd')" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [], "source": [ "f.close()" ] }, { "cell_type": "code", "execution_count": 117, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "48\n" ] } ], "source": [ "with open('/etc/passwd') as f:\n", " print(len(list(f)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compound Datatypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List, Tuple" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [], "source": [ "l = [1,2,'drei']" ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 119, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(l)" ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [], "source": [ "l.append(4)" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 121, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(l)" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'drei', 4]" ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [], "source": [ "l1 = [5, 'sechs']" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [], "source": [ "l.extend(l1)" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'drei', 4, 5, 'sechs']" ] }, "execution_count": 125, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [], "source": [ "l.append(l1)" ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'drei', 4, 5, 'sechs', [5, 'sechs']]" ] }, "execution_count": 127, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 128, "metadata": {}, "outputs": [], "source": [ "l.reverse()" ] }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[5, 'sechs'], 'sechs', 5, 4, 'drei', 2, 1]" ] }, "execution_count": 129, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [], "source": [ "l += l1" ] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[5, 'sechs'], 'sechs', 5, 4, 'drei', 2, 1, 5, 'sechs']" ] }, "execution_count": 131, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 132, "metadata": {}, "outputs": [], "source": [ "t = (1,2,'drei')" ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'tuple' object has no attribute 'append' \n" ] } ], "source": [ "try:\n", " t.append(4.0)\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [], "source": [ "t1 = (5, 'sechs')" ] }, { "cell_type": "code", "execution_count": 135, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140634343012864" ] }, "execution_count": 135, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(t)" ] }, { "cell_type": "code", "execution_count": 136, "metadata": {}, "outputs": [], "source": [ "t += t1" ] }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140634341099856" ] }, "execution_count": 137, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(t)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**The `in`` operator**" ] }, { "cell_type": "code", "execution_count": 138, "metadata": {}, "outputs": [], "source": [ "l = [3,4,1,'fuenf']" ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 139, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 in l" ] }, { "cell_type": "code", "execution_count": 140, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 140, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 not in l" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 141, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not 5 in l" ] }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 142, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'fuenf' in l" ] }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 143, "metadata": {}, "output_type": "execute_result" } ], "source": [ "6 in l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dictionary" ] }, { "cell_type": "code", "execution_count": 144, "metadata": {}, "outputs": [], "source": [ "d = {'one': 1, 'two': 2, 3: 'three'}" ] }, { "cell_type": "code", "execution_count": 145, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 145, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['one']" ] }, { "cell_type": "code", "execution_count": 146, "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": 147, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 147, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'three' in d" ] }, { "cell_type": "code", "execution_count": 148, "metadata": {}, "outputs": [], "source": [ "value = d.get('three')" ] }, { "cell_type": "code", "execution_count": 149, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "print(value)" ] }, { "cell_type": "code", "execution_count": 150, "metadata": {}, "outputs": [], "source": [ "d['three'] = 3" ] }, { "cell_type": "code", "execution_count": 151, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 151, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'three' in d" ] }, { "cell_type": "code", "execution_count": 152, "metadata": {}, "outputs": [], "source": [ "del d['three']" ] }, { "cell_type": "code", "execution_count": 153, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 153, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'three' in d" ] }, { "cell_type": "code", "execution_count": 154, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2, 3: 'three'}" ] }, "execution_count": 154, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Set" ] }, { "cell_type": "code", "execution_count": 155, "metadata": {}, "outputs": [], "source": [ "s = {1,2,'drei'}" ] }, { "cell_type": "code", "execution_count": 156, "metadata": {}, "outputs": [], "source": [ "s.add(4.0)" ] }, { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [], "source": [ "s.remove('drei')" ] }, { "cell_type": "code", "execution_count": 158, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 158, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 in s" ] }, { "cell_type": "code", "execution_count": 159, "metadata": {}, "outputs": [], "source": [ "l = [1,2,3]" ] }, { "cell_type": "code", "execution_count": 160, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unhashable type: 'list'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m/tmp/ipykernel_61373/1125713270.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ml\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'list'" ] } ], "source": [ "s.add(l)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "i = 42\n", "i.__hash__()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "i = 1234567876543234567654321234565432123456\n", "i.__hash__()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.add((1,2,3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Slicing" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l = [2,3,4,5,6]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l[2:4]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l[2:5]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l[2:len(l)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l[2:len(l)-1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l[2:-1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l[1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l[1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "try:\n", " l[len(l)]\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l[len(l)-1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l[-1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l[0:6]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l[0:]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l[:]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l[1:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``for``" ] }, { "cell_type": "code", "execution_count": 161, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Caro\n", "Johanna\n", "Eva\n", "Jörg\n" ] } ], "source": [ "for name in ['Caro', 'Johanna', 'Eva', 'Jörg']:\n", " print(name)" ] }, { "cell_type": "code", "execution_count": 162, "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": 163, "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": 164, "metadata": {}, "outputs": [], "source": [ "r = range(5)" ] }, { "cell_type": "code", "execution_count": 165, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(r))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Iterator Protocol" ] }, { "cell_type": "code", "execution_count": 166, "metadata": {}, "outputs": [], "source": [ "r = range(3)" ] }, { "cell_type": "code", "execution_count": 167, "metadata": {}, "outputs": [], "source": [ "it = iter(r)" ] }, { "cell_type": "code", "execution_count": 168, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 168, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 169, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 169, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 170, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 170, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 171, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \n" ] } ], "source": [ "try:\n", " next(it)\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 172, "metadata": {}, "outputs": [], "source": [ "l = [0,1,2]" ] }, { "cell_type": "code", "execution_count": 173, "metadata": {}, "outputs": [], "source": [ "it = iter(l)" ] }, { "cell_type": "code", "execution_count": 174, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 174, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 175, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 175, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 176, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 176, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 177, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \n" ] } ], "source": [ "try:\n", " next(it)\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 178, "metadata": {}, "outputs": [], "source": [ "def my_range(n):\n", " current = 0\n", " yield current\n", " while current < n:\n", " yield current\n", " current += 1" ] }, { "cell_type": "code", "execution_count": 179, "metadata": {}, "outputs": [], "source": [ "r = my_range(3)" ] }, { "cell_type": "code", "execution_count": 180, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(r))" ] }, { "cell_type": "code", "execution_count": 181, "metadata": {}, "outputs": [], "source": [ "it = iter(r)" ] }, { "cell_type": "code", "execution_count": 182, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 182, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 183, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 183, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 184, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 184, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 185, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 185, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 186, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \n" ] } ], "source": [ "try:\n", " next(it)\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The ``range`` Function" ] }, { "cell_type": "code", "execution_count": 187, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for elem in range(3):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 188, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for elem in range(0,3):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 189, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "for elem in range(1,4):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 190, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "3\n", "5\n" ] } ], "source": [ "for elem in range(1,7,2):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 191, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c']" ] }, "execution_count": 191, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list('abc')" ] }, { "cell_type": "code", "execution_count": 192, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a\n", "b\n", "c\n" ] } ], "source": [ "for elem in 'abc':\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 193, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for elem in range(3):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 194, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2]" ] }, "execution_count": 194, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(range(3))" ] }, { "cell_type": "code", "execution_count": 195, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Joerg\n", "Johanna\n", "Caro\n", "Philipp\n", "Eva\n" ] } ], "source": [ "for elem in ['Joerg', 'Johanna', 'Caro', 'Philipp', 'Eva']:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 196, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 Joerg\n", "1 Johanna\n", "2 Caro\n", "3 Philipp\n", "4 Eva\n" ] } ], "source": [ "i = 0\n", "for elem in ['Joerg', 'Johanna', 'Caro', 'Philipp', 'Eva']:\n", " print(i, elem)\n", " i += 1" ] }, { "cell_type": "code", "execution_count": 197, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(0, 'Joerg')\n", "(1, 'Johanna')\n", "(2, 'Caro')\n", "(3, 'Philipp')\n", "(4, 'Eva')\n" ] } ], "source": [ "for elem in enumerate(['Joerg', 'Johanna', 'Caro', 'Philipp', 'Eva']):\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 198, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 Joerg\n", "1 Johanna\n", "2 Caro\n", "3 Philipp\n", "4 Eva\n" ] } ], "source": [ "for elem in enumerate(['Joerg', 'Johanna', 'Caro', 'Philipp', 'Eva']):\n", " pos = elem[0]\n", " name = elem[1]\n", " print(pos, name)" ] }, { "cell_type": "code", "execution_count": 199, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 Joerg\n", "1 Johanna\n", "2 Caro\n", "3 Philipp\n", "4 Eva\n" ] } ], "source": [ "for pos, name in enumerate(['Joerg', 'Johanna', 'Caro', 'Philipp', 'Eva']):\n", " print(pos, name)" ] }, { "cell_type": "code", "execution_count": 200, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 3\n", "1 4\n", "2 5\n", "3 6\n" ] } ], "source": [ "for pos, elem in enumerate(range(3, 7)):\n", " print(pos, elem)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## References, (Im)mutability" ] }, { "cell_type": "code", "execution_count": 201, "metadata": {}, "outputs": [], "source": [ "a = 42" ] }, { "cell_type": "code", "execution_count": 202, "metadata": {}, "outputs": [], "source": [ "b = a" ] }, { "cell_type": "code", "execution_count": 203, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(140634424624656, 140634424624656)" ] }, "execution_count": 203, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a), id(b)" ] }, { "cell_type": "code", "execution_count": 204, "metadata": {}, "outputs": [], "source": [ "a = 666" ] }, { "cell_type": "code", "execution_count": 205, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 205, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 206, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140634281233520" ] }, "execution_count": 206, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a)" ] }, { "cell_type": "code", "execution_count": 207, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140634424624656" ] }, "execution_count": 207, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(b)" ] }, { "cell_type": "code", "execution_count": 208, "metadata": {}, "outputs": [], "source": [ "l1 = [1,2,3]" ] }, { "cell_type": "code", "execution_count": 209, "metadata": {}, "outputs": [], "source": [ "l2 = l1" ] }, { "cell_type": "code", "execution_count": 210, "metadata": {}, "outputs": [], "source": [ "l2.append(4)" ] }, { "cell_type": "code", "execution_count": 211, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 211, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 212, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 212, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 213, "metadata": {}, "outputs": [], "source": [ "l1 = [1,2,3]\n", "l2 = l1[:]" ] }, { "cell_type": "code", "execution_count": 214, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(140634341798400, 140634341995008)" ] }, "execution_count": 214, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l1), id(l2)" ] }, { "cell_type": "code", "execution_count": 215, "metadata": {}, "outputs": [], "source": [ "l1 = [1, ['a', 'b'], 2]" ] }, { "cell_type": "code", "execution_count": 216, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140634341576640" ] }, "execution_count": 216, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l1)" ] }, { "cell_type": "code", "execution_count": 217, "metadata": {}, "outputs": [], "source": [ "l2 = l1[:]" ] }, { "cell_type": "code", "execution_count": 218, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140634341773312" ] }, "execution_count": 218, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l2)" ] }, { "cell_type": "code", "execution_count": 219, "metadata": {}, "outputs": [], "source": [ "l1.append(3)" ] }, { "cell_type": "code", "execution_count": 220, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, ['a', 'b'], 2, 3]" ] }, "execution_count": 220, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 221, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, ['a', 'b'], 2]" ] }, "execution_count": 221, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 222, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b']" ] }, "execution_count": 222, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1[1]" ] }, { "cell_type": "code", "execution_count": 223, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b']" ] }, "execution_count": 223, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2[1]" ] }, { "cell_type": "code", "execution_count": 224, "metadata": {}, "outputs": [], "source": [ "l1[1].append('c')" ] }, { "cell_type": "code", "execution_count": 225, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, ['a', 'b', 'c'], 2, 3]" ] }, "execution_count": 225, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 226, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, ['a', 'b', 'c'], 2]" ] }, "execution_count": 226, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 227, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140634281802304" ] }, "execution_count": 227, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l1[1])" ] }, { "cell_type": "code", "execution_count": 228, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140634281802304" ] }, "execution_count": 228, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l2[1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions" ] }, { "cell_type": "code", "execution_count": 229, "metadata": {}, "outputs": [], "source": [ "def foo(a):\n", " return a**2" ] }, { "cell_type": "code", "execution_count": 230, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(foo))" ] }, { "cell_type": "code", "execution_count": 231, "metadata": {}, "outputs": [], "source": [ "x = foo" ] }, { "cell_type": "code", "execution_count": 232, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 232, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x(2)" ] }, { "cell_type": "code", "execution_count": 233, "metadata": {}, "outputs": [], "source": [ "foo = 666" ] }, { "cell_type": "code", "execution_count": 234, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'int' object is not callable \n" ] } ], "source": [ "try:\n", " foo(2)\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 235, "metadata": {}, "outputs": [], "source": [ "global_variable = 666" ] }, { "cell_type": "code", "execution_count": 236, "metadata": {}, "outputs": [], "source": [ "def bar():\n", " print(global_variable)" ] }, { "cell_type": "code", "execution_count": 237, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "666\n" ] } ], "source": [ "bar()" ] }, { "cell_type": "code", "execution_count": 238, "metadata": {}, "outputs": [], "source": [ "def bar2():\n", " global_variable = 42" ] }, { "cell_type": "code", "execution_count": 239, "metadata": {}, "outputs": [], "source": [ "bar2()" ] }, { "cell_type": "code", "execution_count": 240, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 240, "metadata": {}, "output_type": "execute_result" } ], "source": [ "global_variable" ] }, { "cell_type": "code", "execution_count": 241, "metadata": {}, "outputs": [], "source": [ "def bar3():\n", " global global_variable\n", " global_variable = 42" ] }, { "cell_type": "code", "execution_count": 242, "metadata": {}, "outputs": [], "source": [ "bar3()" ] }, { "cell_type": "code", "execution_count": 243, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 243, "metadata": {}, "output_type": "execute_result" } ], "source": [ "global_variable" ] }, { "cell_type": "code", "execution_count": 244, "metadata": {}, "outputs": [], "source": [ "def bar4(param=42):\n", " print(param)" ] }, { "cell_type": "code", "execution_count": 245, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "666\n" ] } ], "source": [ "bar4(666)" ] }, { "cell_type": "code", "execution_count": 246, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42\n" ] } ], "source": [ "bar4()" ] }, { "cell_type": "code", "execution_count": 247, "metadata": {}, "outputs": [], "source": [ "def bar5(param=[]):\n", " print(param)" ] }, { "cell_type": "code", "execution_count": 248, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42\n" ] } ], "source": [ "bar5(42)" ] }, { "cell_type": "code", "execution_count": 249, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[]\n" ] } ], "source": [ "bar5()" ] }, { "cell_type": "code", "execution_count": 250, "metadata": {}, "outputs": [], "source": [ "def bar6(param=[]):\n", " param.append(666)\n", " print(param)" ] }, { "cell_type": "code", "execution_count": 251, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[666]\n" ] } ], "source": [ "bar6()" ] }, { "cell_type": "code", "execution_count": 252, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[666, 666]\n" ] } ], "source": [ "bar6()" ] }, { "cell_type": "code", "execution_count": 253, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__annotations__',\n", " '__builtins__',\n", " '__call__',\n", " '__class__',\n", " '__closure__',\n", " '__code__',\n", " '__defaults__',\n", " '__delattr__',\n", " '__dict__',\n", " '__dir__',\n", " '__doc__',\n", " '__eq__',\n", " '__format__',\n", " '__ge__',\n", " '__get__',\n", " '__getattribute__',\n", " '__globals__',\n", " '__gt__',\n", " '__hash__',\n", " '__init__',\n", " '__init_subclass__',\n", " '__kwdefaults__',\n", " '__le__',\n", " '__lt__',\n", " '__module__',\n", " '__name__',\n", " '__ne__',\n", " '__new__',\n", " '__qualname__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__setattr__',\n", " '__sizeof__',\n", " '__str__',\n", " '__subclasshook__']" ] }, "execution_count": 253, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(bar6)" ] }, { "cell_type": "code", "execution_count": 254, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "([666, 666],)" ] }, "execution_count": 254, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bar6.__defaults__" ] }, { "cell_type": "code", "execution_count": 255, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[666, 666, 666]\n" ] } ], "source": [ "bar6()" ] }, { "cell_type": "code", "execution_count": 256, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[666, 666, 666, 666]\n" ] } ], "source": [ "bar6()" ] }, { "cell_type": "code", "execution_count": 257, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[42, 666]\n" ] } ], "source": [ "bar6([42])" ] }, { "cell_type": "code", "execution_count": 258, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[666, 666, 666, 666, 666]\n" ] } ], "source": [ "bar6()" ] }, { "cell_type": "code", "execution_count": 259, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[42, 666]\n" ] } ], "source": [ "l = [42]\n", "bar6(l)" ] }, { "cell_type": "code", "execution_count": 260, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[42, 666]" ] }, "execution_count": 260, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**And ``yield``?**" ] }, { "cell_type": "code", "execution_count": 261, "metadata": {}, "outputs": [], "source": [ "def f():\n", " return 42\n", " return 666" ] }, { "cell_type": "code", "execution_count": 262, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 262, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f()" ] }, { "cell_type": "code", "execution_count": 263, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "function" ] }, "execution_count": 263, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(f)" ] }, { "cell_type": "code", "execution_count": 264, "metadata": {}, "outputs": [], "source": [ "def f():\n", " yield 42\n", " yield 666" ] }, { "cell_type": "code", "execution_count": 265, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "function" ] }, "execution_count": 265, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(f)" ] }, { "cell_type": "code", "execution_count": 266, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 266, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f()" ] }, { "cell_type": "code", "execution_count": 267, "metadata": {}, "outputs": [], "source": [ "gen = f()" ] }, { "cell_type": "code", "execution_count": 268, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 268, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gen" ] }, { "cell_type": "code", "execution_count": 269, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "42\n", "666\n" ] } ], "source": [ "for i in gen:\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 270, "metadata": {}, "outputs": [], "source": [ "def f():\n", " print('start, yielding 42')\n", " yield 42\n", " print('after yield 42, yielding 666')\n", " yield 666\n", " print('after yield 666')" ] }, { "cell_type": "code", "execution_count": 271, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 271, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gen = f()\n", "gen" ] }, { "cell_type": "code", "execution_count": 272, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "start, yielding 42\n", "42\n", "after yield 42, yielding 666\n", "666\n", "after yield 666\n" ] } ], "source": [ "for i in gen:\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 273, "metadata": {}, "outputs": [], "source": [ "gen = f()" ] }, { "cell_type": "code", "execution_count": 274, "metadata": {}, "outputs": [], "source": [ "it = iter(gen)" ] }, { "cell_type": "code", "execution_count": 275, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "start, yielding 42\n" ] }, { "data": { "text/plain": [ "42" ] }, "execution_count": 275, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 276, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "after yield 42, yielding 666\n" ] }, { "data": { "text/plain": [ "666" ] }, "execution_count": 276, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": 277, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "after yield 666\n", " \n" ] } ], "source": [ "try:\n", " next(it)\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Miscellaneous String Methods" ] }, { "cell_type": "code", "execution_count": 278, "metadata": {}, "outputs": [], "source": [ "s = 'abc'" ] }, { "cell_type": "code", "execution_count": 279, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(type(s))" ] }, { "cell_type": "code", "execution_count": 280, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on class str in module builtins:\n", "\n", "class str(object)\n", " | str(object='') -> str\n", " | str(bytes_or_buffer[, encoding[, errors]]) -> str\n", " | \n", " | Create a new string object from the given object. If encoding or\n", " | errors is specified, then the object must expose a data buffer\n", " | that will be decoded using the given encoding and error handler.\n", " | Otherwise, returns the result of object.__str__() (if defined)\n", " | or repr(object).\n", " | encoding defaults to sys.getdefaultencoding().\n", " | errors defaults to 'strict'.\n", " | \n", " | Methods defined here:\n", " | \n", " | __add__(self, value, /)\n", " | Return self+value.\n", " | \n", " | __contains__(self, key, /)\n", " | Return key in self.\n", " | \n", " | __eq__(self, value, /)\n", " | Return self==value.\n", " | \n", " | __format__(self, format_spec, /)\n", " | Return a formatted version of the string as described by format_spec.\n", " | \n", " | __ge__(self, value, /)\n", " | Return self>=value.\n", " | \n", " | __getattribute__(self, name, /)\n", " | Return getattr(self, name).\n", " | \n", " | __getitem__(self, key, /)\n", " | Return self[key].\n", " | \n", " | __getnewargs__(...)\n", " | \n", " | __gt__(self, value, /)\n", " | Return self>value.\n", " | \n", " | __hash__(self, /)\n", " | Return hash(self).\n", " | \n", " | __iter__(self, /)\n", " | Implement iter(self).\n", " | \n", " | __le__(self, value, /)\n", " | Return self<=value.\n", " | \n", " | __len__(self, /)\n", " | Return len(self).\n", " | \n", " | __lt__(self, value, /)\n", " | Return self int\n", " | \n", " | Return the number of non-overlapping occurrences of substring sub in\n", " | string S[start:end]. Optional arguments start and end are\n", " | interpreted as in slice notation.\n", " | \n", " | encode(self, /, encoding='utf-8', errors='strict')\n", " | Encode the string using the codec registered for encoding.\n", " | \n", " | encoding\n", " | The encoding in which to encode the string.\n", " | errors\n", " | The error handling scheme to use for encoding errors.\n", " | The default is 'strict' meaning that encoding errors raise a\n", " | UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n", " | 'xmlcharrefreplace' as well as any other name registered with\n", " | codecs.register_error that can handle UnicodeEncodeErrors.\n", " | \n", " | endswith(...)\n", " | S.endswith(suffix[, start[, end]]) -> bool\n", " | \n", " | Return True if S ends with the specified suffix, False otherwise.\n", " | With optional start, test S beginning at that position.\n", " | With optional end, stop comparing S at that position.\n", " | suffix can also be a tuple of strings to try.\n", " | \n", " | expandtabs(self, /, tabsize=8)\n", " | Return a copy where all tab characters are expanded using spaces.\n", " | \n", " | If tabsize is not given, a tab size of 8 characters is assumed.\n", " | \n", " | find(...)\n", " | S.find(sub[, start[, end]]) -> int\n", " | \n", " | Return the lowest index in S where substring sub is found,\n", " | such that sub is contained within S[start:end]. Optional\n", " | arguments start and end are interpreted as in slice notation.\n", " | \n", " | Return -1 on failure.\n", " | \n", " | format(...)\n", " | S.format(*args, **kwargs) -> str\n", " | \n", " | Return a formatted version of S, using substitutions from args and kwargs.\n", " | The substitutions are identified by braces ('{' and '}').\n", " | \n", " | format_map(...)\n", " | S.format_map(mapping) -> str\n", " | \n", " | Return a formatted version of S, using substitutions from mapping.\n", " | The substitutions are identified by braces ('{' and '}').\n", " | \n", " | index(...)\n", " | S.index(sub[, start[, end]]) -> int\n", " | \n", " | Return the lowest index in S where substring sub is found,\n", " | such that sub is contained within S[start:end]. Optional\n", " | arguments start and end are interpreted as in slice notation.\n", " | \n", " | Raises ValueError when the substring is not found.\n", " | \n", " | isalnum(self, /)\n", " | Return True if the string is an alpha-numeric string, False otherwise.\n", " | \n", " | A string is alpha-numeric if all characters in the string are alpha-numeric and\n", " | there is at least one character in the string.\n", " | \n", " | isalpha(self, /)\n", " | Return True if the string is an alphabetic string, False otherwise.\n", " | \n", " | A string is alphabetic if all characters in the string are alphabetic and there\n", " | is at least one character in the string.\n", " | \n", " | isascii(self, /)\n", " | Return True if all characters in the string are ASCII, False otherwise.\n", " | \n", " | ASCII characters have code points in the range U+0000-U+007F.\n", " | Empty string is ASCII too.\n", " | \n", " | isdecimal(self, /)\n", " | Return True if the string is a decimal string, False otherwise.\n", " | \n", " | A string is a decimal string if all characters in the string are decimal and\n", " | there is at least one character in the string.\n", " | \n", " | isdigit(self, /)\n", " | Return True if the string is a digit string, False otherwise.\n", " | \n", " | A string is a digit string if all characters in the string are digits and there\n", " | is at least one character in the string.\n", " | \n", " | isidentifier(self, /)\n", " | Return True if the string is a valid Python identifier, False otherwise.\n", " | \n", " | Call keyword.iskeyword(s) to test whether string s is a reserved identifier,\n", " | such as \"def\" or \"class\".\n", " | \n", " | islower(self, /)\n", " | Return True if the string is a lowercase string, False otherwise.\n", " | \n", " | A string is lowercase if all cased characters in the string are lowercase and\n", " | there is at least one cased character in the string.\n", " | \n", " | isnumeric(self, /)\n", " | Return True if the string is a numeric string, False otherwise.\n", " | \n", " | A string is numeric if all characters in the string are numeric and there is at\n", " | least one character in the string.\n", " | \n", " | isprintable(self, /)\n", " | Return True if the string is printable, False otherwise.\n", " | \n", " | A string is printable if all of its characters are considered printable in\n", " | repr() or if it is empty.\n", " | \n", " | isspace(self, /)\n", " | Return True if the string is a whitespace string, False otherwise.\n", " | \n", " | A string is whitespace if all characters in the string are whitespace and there\n", " | is at least one character in the string.\n", " | \n", " | istitle(self, /)\n", " | Return True if the string is a title-cased string, False otherwise.\n", " | \n", " | In a title-cased string, upper- and title-case characters may only\n", " | follow uncased characters and lowercase characters only cased ones.\n", " | \n", " | isupper(self, /)\n", " | Return True if the string is an uppercase string, False otherwise.\n", " | \n", " | A string is uppercase if all cased characters in the string are uppercase and\n", " | there is at least one cased character in the string.\n", " | \n", " | join(self, iterable, /)\n", " | Concatenate any number of strings.\n", " | \n", " | The string whose method is called is inserted in between each given string.\n", " | The result is returned as a new string.\n", " | \n", " | Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'\n", " | \n", " | ljust(self, width, fillchar=' ', /)\n", " | Return a left-justified string of length width.\n", " | \n", " | Padding is done using the specified fill character (default is a space).\n", " | \n", " | lower(self, /)\n", " | Return a copy of the string converted to lowercase.\n", " | \n", " | lstrip(self, chars=None, /)\n", " | Return a copy of the string with leading whitespace removed.\n", " | \n", " | If chars is given and not None, remove characters in chars instead.\n", " | \n", " | partition(self, sep, /)\n", " | Partition the string into three parts using the given separator.\n", " | \n", " | This will search for the separator in the string. If the separator is found,\n", " | returns a 3-tuple containing the part before the separator, the separator\n", " | itself, and the part after it.\n", " | \n", " | If the separator is not found, returns a 3-tuple containing the original string\n", " | and two empty strings.\n", " | \n", " | removeprefix(self, prefix, /)\n", " | Return a str with the given prefix string removed if present.\n", " | \n", " | If the string starts with the prefix string, return string[len(prefix):].\n", " | Otherwise, return a copy of the original string.\n", " | \n", " | removesuffix(self, suffix, /)\n", " | Return a str with the given suffix string removed if present.\n", " | \n", " | If the string ends with the suffix string and that suffix is not empty,\n", " | return string[:-len(suffix)]. Otherwise, return a copy of the original\n", " | string.\n", " | \n", " | replace(self, old, new, count=-1, /)\n", " | Return a copy with all occurrences of substring old replaced by new.\n", " | \n", " | count\n", " | Maximum number of occurrences to replace.\n", " | -1 (the default value) means replace all occurrences.\n", " | \n", " | If the optional argument count is given, only the first count occurrences are\n", " | replaced.\n", " | \n", " | rfind(...)\n", " | S.rfind(sub[, start[, end]]) -> int\n", " | \n", " | Return the highest index in S where substring sub is found,\n", " | such that sub is contained within S[start:end]. Optional\n", " | arguments start and end are interpreted as in slice notation.\n", " | \n", " | Return -1 on failure.\n", " | \n", " | rindex(...)\n", " | S.rindex(sub[, start[, end]]) -> int\n", " | \n", " | Return the highest index in S where substring sub is found,\n", " | such that sub is contained within S[start:end]. Optional\n", " | arguments start and end are interpreted as in slice notation.\n", " | \n", " | Raises ValueError when the substring is not found.\n", " | \n", " | rjust(self, width, fillchar=' ', /)\n", " | Return a right-justified string of length width.\n", " | \n", " | Padding is done using the specified fill character (default is a space).\n", " | \n", " | rpartition(self, sep, /)\n", " | Partition the string into three parts using the given separator.\n", " | \n", " | This will search for the separator in the string, starting at the end. If\n", " | the separator is found, returns a 3-tuple containing the part before the\n", " | separator, the separator itself, and the part after it.\n", " | \n", " | If the separator is not found, returns a 3-tuple containing two empty strings\n", " | and the original string.\n", " | \n", " | rsplit(self, /, sep=None, maxsplit=-1)\n", " | Return a list of the substrings in the string, using sep as the separator string.\n", " | \n", " | sep\n", " | The separator used to split the string.\n", " | \n", " | When set to None (the default value), will split on any whitespace\n", " | character (including \\\\n \\\\r \\\\t \\\\f and spaces) and will discard\n", " | empty strings from the result.\n", " | maxsplit\n", " | Maximum number of splits (starting from the left).\n", " | -1 (the default value) means no limit.\n", " | \n", " | Splitting starts at the end of the string and works to the front.\n", " | \n", " | rstrip(self, chars=None, /)\n", " | Return a copy of the string with trailing whitespace removed.\n", " | \n", " | If chars is given and not None, remove characters in chars instead.\n", " | \n", " | split(self, /, sep=None, maxsplit=-1)\n", " | Return a list of the substrings in the string, using sep as the separator string.\n", " | \n", " | sep\n", " | The separator used to split the string.\n", " | \n", " | When set to None (the default value), will split on any whitespace\n", " | character (including \\\\n \\\\r \\\\t \\\\f and spaces) and will discard\n", " | empty strings from the result.\n", " | maxsplit\n", " | Maximum number of splits (starting from the left).\n", " | -1 (the default value) means no limit.\n", " | \n", " | Note, str.split() is mainly useful for data that has been intentionally\n", " | delimited. With natural text that includes punctuation, consider using\n", " | the regular expression module.\n", " | \n", " | splitlines(self, /, keepends=False)\n", " | Return a list of the lines in the string, breaking at line boundaries.\n", " | \n", " | Line breaks are not included in the resulting list unless keepends is given and\n", " | true.\n", " | \n", " | startswith(...)\n", " | S.startswith(prefix[, start[, end]]) -> bool\n", " | \n", " | Return True if S starts with the specified prefix, False otherwise.\n", " | With optional start, test S beginning at that position.\n", " | With optional end, stop comparing S at that position.\n", " | prefix can also be a tuple of strings to try.\n", " | \n", " | strip(self, chars=None, /)\n", " | Return a copy of the string with leading and trailing whitespace removed.\n", " | \n", " | If chars is given and not None, remove characters in chars instead.\n", " | \n", " | swapcase(self, /)\n", " | Convert uppercase characters to lowercase and lowercase characters to uppercase.\n", " | \n", " | title(self, /)\n", " | Return a version of the string where each word is titlecased.\n", " | \n", " | More specifically, words start with uppercased characters and all remaining\n", " | cased characters have lower case.\n", " | \n", " | translate(self, table, /)\n", " | Replace each character in the string using the given translation table.\n", " | \n", " | table\n", " | Translation table, which must be a mapping of Unicode ordinals to\n", " | Unicode ordinals, strings, or None.\n", " | \n", " | The table must implement lookup/indexing via __getitem__, for instance a\n", " | dictionary or list. If this operation raises LookupError, the character is\n", " | left untouched. Characters mapped to None are deleted.\n", " | \n", " | upper(self, /)\n", " | Return a copy of the string converted to uppercase.\n", " | \n", " | zfill(self, width, /)\n", " | Pad a numeric string with zeros on the left, to fill a field of the given width.\n", " | \n", " | The string is never truncated.\n", " | \n", " | ----------------------------------------------------------------------\n", " | Static methods defined here:\n", " | \n", " | __new__(*args, **kwargs) from builtins.type\n", " | Create and return a new object. See help(type) for accurate signature.\n", " | \n", " | maketrans(...)\n", " | Return a translation table usable for str.translate().\n", " | \n", " | If there is only one argument, it must be a dictionary mapping Unicode\n", " | ordinals (integers) or characters to Unicode ordinals, strings or None.\n", " | Character keys will be then converted to ordinals.\n", " | If there are two arguments, they must be strings of equal length, and\n", " | in the resulting dictionary, each character in x will be mapped to the\n", " | character at the same position in y. If there is a third argument, it\n", " | must be a string, whose characters will be mapped to None in the result.\n", "\n" ] } ], "source": [ "help(str)" ] }, { "cell_type": "code", "execution_count": 281, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abcabcabcabcabc'" ] }, "execution_count": 281, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s*5" ] }, { "cell_type": "code", "execution_count": 282, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "52" ] }, "execution_count": 282, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.__sizeof__()" ] }, { "cell_type": "code", "execution_count": 283, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "53" ] }, "execution_count": 283, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abcd'.__sizeof__()" ] }, { "cell_type": "code", "execution_count": 288, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 288, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.__len__()" ] }, { "cell_type": "code", "execution_count": 289, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 289, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(s)" ] }, { "cell_type": "code", "execution_count": 290, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 290, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.__gt__('aaa')" ] }, { "cell_type": "code", "execution_count": 292, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 292, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s > 'aaa'" ] }, { "cell_type": "code", "execution_count": 285, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Abc'" ] }, "execution_count": 285, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.capitalize()" ] }, { "cell_type": "code", "execution_count": 286, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 286, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 293, "metadata": {}, "outputs": [], "source": [ "s = 'mississippi'" ] }, { "cell_type": "code", "execution_count": 294, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 294, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.count('ss')" ] }, { "cell_type": "code", "execution_count": 295, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 295, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.count('i')" ] }, { "cell_type": "code", "execution_count": 296, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'MISSISSIPPI'" ] }, "execution_count": 296, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.upper()" ] }, { "cell_type": "code", "execution_count": 297, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'mississippi'" ] }, "execution_count": 297, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.upper().lower()" ] }, { "cell_type": "code", "execution_count": 298, "metadata": {}, "outputs": [], "source": [ "filename = 'blah.csv'" ] }, { "cell_type": "code", "execution_count": 299, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 299, "metadata": {}, "output_type": "execute_result" } ], "source": [ "filename.endswith('.csv')" ] }, { "cell_type": "code", "execution_count": 300, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 300, "metadata": {}, "output_type": "execute_result" } ], "source": [ "filename.startswith('xx')" ] }, { "cell_type": "code", "execution_count": 301, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'mississippi'" ] }, "execution_count": 301, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 302, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 302, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.find('ss')" ] }, { "cell_type": "code", "execution_count": 303, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 303, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.find('ss', 3)" ] }, { "cell_type": "code", "execution_count": 305, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 305, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.find('ss', s.find('ss')+1)" ] }, { "cell_type": "code", "execution_count": 306, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 306, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.find('xx')" ] }, { "cell_type": "code", "execution_count": 307, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 307, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.index('ss')" ] }, { "cell_type": "code", "execution_count": 309, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "substring not found \n" ] } ], "source": [ "try:\n", " s.index('xx')\n", "except ValueError as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 310, "metadata": {}, "outputs": [], "source": [ "line = '5;Joerg;Faschingbauer'" ] }, { "cell_type": "code", "execution_count": 311, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['5', 'Joerg', 'Faschingbauer']" ] }, "execution_count": 311, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line.split(';')" ] }, { "cell_type": "code", "execution_count": 313, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['5', 'Joerg;Faschingbauer']" ] }, "execution_count": 313, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line.split(';', 1)" ] }, { "cell_type": "code", "execution_count": 314, "metadata": {}, "outputs": [], "source": [ "fields = ['5', 'Joerg', 'Faschingbauer']" ] }, { "cell_type": "code", "execution_count": 315, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'5;Joerg;Faschingbauer'" ] }, "execution_count": 315, "metadata": {}, "output_type": "execute_result" } ], "source": [ "';'.join(fields)" ] }, { "cell_type": "code", "execution_count": 316, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' abc '" ] }, "execution_count": 316, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abc'.center(50)" ] }, { "cell_type": "code", "execution_count": 317, "metadata": {}, "outputs": [], "source": [ "line = ' \\n abc \\t '" ] }, { "cell_type": "code", "execution_count": 318, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 318, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line.strip()" ] }, { "cell_type": "code", "execution_count": 319, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'\\n abc \\t'" ] }, "execution_count": 319, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line.strip(' ')" ] }, { "cell_type": "code", "execution_count": 320, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' \\n abc'" ] }, "execution_count": 320, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line.rstrip()" ] }, { "cell_type": "code", "execution_count": 321, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc \\t '" ] }, "execution_count": 321, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line.lstrip()" ] }, { "cell_type": "code", "execution_count": 323, "metadata": {}, "outputs": [], "source": [ "import re" ] }, { "cell_type": "code", "execution_count": 324, "metadata": {}, "outputs": [], "source": [ "line = 'ss ss ss 1234'" ] }, { "cell_type": "code", "execution_count": 330, "metadata": {}, "outputs": [], "source": [ "rex = re.compile(r'^ss\\s+ss\\s+ss\\s+(\\d+)\\s*$')" ] }, { "cell_type": "code", "execution_count": 331, "metadata": {}, "outputs": [], "source": [ "match = rex.search(line)" ] }, { "cell_type": "code", "execution_count": 332, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(match)" ] }, { "cell_type": "code", "execution_count": 333, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'1234'" ] }, "execution_count": 333, "metadata": {}, "output_type": "execute_result" } ], "source": [ "match.group(1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lists" ] }, { "cell_type": "code", "execution_count": 335, "metadata": {}, "outputs": [], "source": [ "l = [3,2,4,5,1,]\n", "l.sort()" ] }, { "cell_type": "code", "execution_count": 336, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 336, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 337, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 337, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [3,2,4,5,1,]\n", "sorted(l)" ] }, { "cell_type": "code", "execution_count": 338, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 2, 4, 5, 1]" ] }, "execution_count": 338, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 339, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c']" ] }, "execution_count": 339, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted('cba')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dictionaries" ] }, { "cell_type": "code", "execution_count": 340, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2}" ] }, "execution_count": 340, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = {'one': 1, 'two': 2}\n", "d" ] }, { "cell_type": "code", "execution_count": 341, "metadata": {}, "outputs": [], "source": [ "d = {}" ] }, { "cell_type": "code", "execution_count": 343, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2}" ] }, "execution_count": 343, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [('one', 1), ('two', 2)]\n", "d = dict(l)\n", "d" ] }, { "cell_type": "code", "execution_count": 344, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 344, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['one']" ] }, { "cell_type": "code", "execution_count": 346, "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": 347, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 347, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.get('two')" ] }, { "cell_type": "code", "execution_count": 349, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "value = d.get('three')\n", "print(value)" ] }, { "cell_type": "code", "execution_count": 350, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] } ], "source": [ "value = d.get('three')\n", "if value is None:\n", " print(3)\n", "else:\n", " print(value)" ] }, { "cell_type": "code", "execution_count": 351, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 351, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.get('three', 3)" ] }, { "cell_type": "code", "execution_count": 352, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2}" ] }, "execution_count": 352, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 353, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] } ], "source": [ "if 'three' in d:\n", " value = d['three']\n", "else:\n", " d['three'] = 3\n", " value = d['three']\n", "print(value)" ] }, { "cell_type": "code", "execution_count": 354, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 354, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.setdefault('three', 3)" ] }, { "cell_type": "code", "execution_count": 355, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2, 'three': 3}" ] }, "execution_count": 355, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 356, "metadata": {}, "outputs": [], "source": [ "another_d = {'three': 3.0, 'four': 4.0, 'five': 5}" ] }, { "cell_type": "code", "execution_count": 357, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2, 'three': 3.0, 'four': 4.0, 'five': 5}" ] }, "execution_count": 357, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.update(another_d)\n", "d" ] }, { "cell_type": "code", "execution_count": 358, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 358, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 666\n", "x" ] }, { "cell_type": "code", "execution_count": 359, "metadata": {}, "outputs": [], "source": [ "del x" ] }, { "cell_type": "code", "execution_count": 361, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name 'x' is not defined \n" ] } ], "source": [ "try:\n", " x\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 362, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2, 'three': 3.0, 'four': 4.0, 'five': 5}" ] }, "execution_count": 362, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 363, "metadata": {}, "outputs": [], "source": [ "del d['three']" ] }, { "cell_type": "code", "execution_count": 364, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'one': 1, 'two': 2, 'four': 4.0, 'five': 5}" ] }, "execution_count": 364, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 366, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n", "two\n", "four\n", "five\n" ] } ], "source": [ "for elem in d:\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 367, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n", "two\n", "four\n", "five\n" ] } ], "source": [ "for elem in d.keys():\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 368, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "4.0\n", "5\n" ] } ], "source": [ "for elem in d.values():\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 369, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('one', 1)\n", "('two', 2)\n", "('four', 4.0)\n", "('five', 5)\n" ] } ], "source": [ "for elem in d.items():\n", " print(elem)" ] }, { "cell_type": "code", "execution_count": 370, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "key one, value 1\n", "key two, value 2\n", "key four, value 4.0\n", "key five, value 5\n" ] } ], "source": [ "for elem in d.items():\n", " key = elem[0]\n", " value = elem[1]\n", " print(f'key {key}, value {value}')" ] }, { "cell_type": "code", "execution_count": 376, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "key one, value 1\n", "key two, value 2\n", "key four, value 4.0\n", "key five, value 5\n" ] } ], "source": [ "for key, value in d.items():\n", " print(f'key {key}, value {value}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Good ol' format():" ] }, { "cell_type": "code", "execution_count": 374, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'key 3, value three'" ] }, "execution_count": 374, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'key {key}, value {value}'.format(key=3, value='three')" ] }, { "cell_type": "code", "execution_count": 375, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'key 3, value three'" ] }, "execution_count": 375, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'key {0}, value {1}'.format(3, 'three')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## File I/O" ] }, { "cell_type": "code", "execution_count": 377, "metadata": {}, "outputs": [], "source": [ "f = open('/etc/passwd')" ] }, { "cell_type": "code", "execution_count": 379, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "<_io.TextIOWrapper name='/etc/passwd' mode='r' encoding='UTF-8'>" ] }, "execution_count": 379, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f" ] }, { "cell_type": "code", "execution_count": 380, "metadata": {}, "outputs": [], "source": [ "f.close()" ] }, { "cell_type": "code", "execution_count": 381, "metadata": {}, "outputs": [], "source": [ "with open('/etc/passwd') as f:\n", " pass" ] }, { "cell_type": "code", "execution_count": 382, "metadata": {}, "outputs": [], "source": [ "f = open('/etc/passwd', encoding='ascii')" ] }, { "cell_type": "code", "execution_count": 384, "metadata": {}, "outputs": [], "source": [ "try:\n", " f.read()\n", "except Exception as e:\n", " print(e, type(e))" ] }, { "cell_type": "code", "execution_count": 385, "metadata": {}, "outputs": [], "source": [ "f = open('/etc/passwd', encoding='utf-8')" ] }, { "cell_type": "code", "execution_count": 386, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'root:x:0:0:root:/root:/bin/bash\\nbin:x:1:1:bin:/bin:/sbin/nologin\\ndaemon:x:2:2:daemon:/sbin:/sbin/nologin\\nadm:x:3:4:adm:/var/adm:/sbin/nologin\\nlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin\\nsync:x:5:0:sync:/sbin:/bin/sync\\nshutdown:x:6:0:shutdown:/sbin:/sbin/shutdown\\nhalt:x:7:0:halt:/sbin:/sbin/halt\\nmail:x:8:12:mail:/var/spool/mail:/sbin/nologin\\noperator:x:11:0:operator:/root:/sbin/nologin\\ngames:x:12:100:games:/usr/games:/sbin/nologin\\nftp:x:14:50:FTP User:/var/ftp:/sbin/nologin\\nnobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin\\ndbus:x:81:81:System message bus:/:/sbin/nologin\\napache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin\\ntss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin\\nsystemd-network:x:192:192:systemd Network Management:/:/usr/sbin/nologin\\nsystemd-oom:x:999:999:systemd Userspace OOM Killer:/:/usr/sbin/nologin\\nsystemd-resolve:x:193:193:systemd Resolver:/:/usr/sbin/nologin\\nqemu:x:107:107:qemu user:/:/sbin/nologin\\npolkitd:x:998:997:User for polkitd:/:/sbin/nologin\\navahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin\\nunbound:x:997:995:Unbound DNS resolver:/etc/unbound:/sbin/nologin\\nnm-openconnect:x:996:994:NetworkManager user for OpenConnect:/:/sbin/nologin\\ngeoclue:x:995:993:User for geoclue:/var/lib/geoclue:/sbin/nologin\\nusbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin\\ngluster:x:994:992:GlusterFS daemons:/run/gluster:/sbin/nologin\\nrtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin\\nchrony:x:993:990::/var/lib/chrony:/sbin/nologin\\nsaslauth:x:992:76:Saslauthd user:/run/saslauthd:/sbin/nologin\\ndnsmasq:x:991:989:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin\\nrpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin\\ncolord:x:990:988:User for colord:/var/lib/colord:/sbin/nologin\\nrpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin\\nopenvpn:x:989:987:OpenVPN:/etc/openvpn:/sbin/nologin\\nnm-openvpn:x:988:986:Default user for running openvpn spawned by NetworkManager:/:/sbin/nologin\\npipewire:x:987:985:PipeWire System Daemon:/var/run/pipewire:/sbin/nologin\\nabrt:x:173:173::/etc/abrt:/sbin/nologin\\nflatpak:x:986:983:User for flatpak system helper:/:/sbin/nologin\\ngdm:x:42:42:GNOME Display Manager:/var/lib/gdm:/sbin/nologin\\ngnome-initial-setup:x:985:982::/run/gnome-initial-setup/:/sbin/nologin\\nvboxadd:x:984:1::/var/run/vboxadd:/sbin/nologin\\nsshd:x:74:74:Privilege-separated SSH:/usr/share/empty.sshd:/sbin/nologin\\ntcpdump:x:72:72::/:/sbin/nologin\\njfasch:x:1000:1000:Jörg Faschingbauer:/home/jfasch:/bin/bash\\nsystemd-coredump:x:978:978:systemd Core Dumper:/:/usr/sbin/nologin\\nsystemd-timesync:x:977:977:systemd Time Synchronization:/:/usr/sbin/nologin\\nmosquitto:x:976:976:Mosquitto Broker:/etc/mosquitto:/sbin/nologin\\n'" ] }, "execution_count": 386, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.read()" ] }, { "cell_type": "code", "execution_count": 388, "metadata": {}, "outputs": [], "source": [ "f = open('/etc/passwd', 'rb')" ] }, { "cell_type": "code", "execution_count": 390, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "b'root:x:0:0:root:/root:/bin/bash\\nbin:x:1:1:bin:/bin:/sbin/nologin\\ndaemon:x:2:2:daemon:/sbin:/sbin/nologin\\nadm:x:3:4:adm:/var/adm:/sbin/nologin\\nlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin\\nsync:x:5:0:sync:/sbin:/bin/sync\\nshutdown:x:6:0:shutdown:/sbin:/sbin/shutdown\\nhalt:x:7:0:halt:/sbin:/sbin/halt\\nmail:x:8:12:mail:/var/spool/mail:/sbin/nologin\\noperator:x:11:0:operator:/root:/sbin/nologin\\ngames:x:12:100:games:/usr/games:/sbin/nologin\\nftp:x:14:50:FTP User:/var/ftp:/sbin/nologin\\nnobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin\\ndbus:x:81:81:System message bus:/:/sbin/nologin\\napache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin\\ntss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin\\nsystemd-network:x:192:192:systemd Network Management:/:/usr/sbin/nologin\\nsystemd-oom:x:999:999:systemd Userspace OOM Killer:/:/usr/sbin/nologin\\nsystemd-resolve:x:193:193:systemd Resolver:/:/usr/sbin/nologin\\nqemu:x:107:107:qemu user:/:/sbin/nologin\\npolkitd:x:998:997:User for polkitd:/:/sbin/nologin\\navahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin\\nunbound:x:997:995:Unbound DNS resolver:/etc/unbound:/sbin/nologin\\nnm-openconnect:x:996:994:NetworkManager user for OpenConnect:/:/sbin/nologin\\ngeoclue:x:995:993:User for geoclue:/var/lib/geoclue:/sbin/nologin\\nusbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin\\ngluster:x:994:992:GlusterFS daemons:/run/gluster:/sbin/nologin\\nrtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin\\nchrony:x:993:990::/var/lib/chrony:/sbin/nologin\\nsaslauth:x:992:76:Saslauthd user:/run/saslauthd:/sbin/nologin\\ndnsmasq:x:991:989:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin\\nrpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin\\ncolord:x:990:988:User for colord:/var/lib/colord:/sbin/nologin\\nrpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin\\nopenvpn:x:989:987:OpenVPN:/etc/openvpn:/sbin/nologin\\nnm-openvpn:x:988:986:Default user for running openvpn spawned by NetworkManager:/:/sbin/nologin\\npipewire:x:987:985:PipeWire System Daemon:/var/run/pipewire:/sbin/nologin\\nabrt:x:173:173::/etc/abrt:/sbin/nologin\\nflatpak:x:986:983:User for flatpak system helper:/:/sbin/nologin\\ngdm:x:42:42:GNOME Display Manager:/var/lib/gdm:/sbin/nologin\\ngnome-initial-setup:x:985:982::/run/gnome-initial-setup/:/sbin/nologin\\nvboxadd:x:984:1::/var/run/vboxadd:/sbin/nologin\\nsshd:x:74:74:Privilege-separated SSH:/usr/share/empty.sshd:/sbin/nologin\\ntcpdump:x:72:72::/:/sbin/nologin\\njfasch:x:1000:1000:J\\xc3\\xb6rg Faschingbauer:/home/jfasch:/bin/bash\\nsystemd-coredump:x:978:978:systemd Core Dumper:/:/usr/sbin/nologin\\nsystemd-timesync:x:977:977:systemd Time Synchronization:/:/usr/sbin/nologin\\nmosquitto:x:976:976:Mosquitto Broker:/etc/mosquitto:/sbin/nologin\\n'" ] }, "execution_count": 390, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.read()" ] }, { "cell_type": "code", "execution_count": 393, "metadata": {}, "outputs": [], "source": [ "f.close()" ] }, { "cell_type": "code", "execution_count": 394, "metadata": {}, "outputs": [], "source": [ "f = open('/etc/passwd', encoding='utf-8')" ] }, { "cell_type": "code", "execution_count": 395, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'root:x:0:0'" ] }, "execution_count": 395, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.read(10)" ] }, { "cell_type": "code", "execution_count": 397, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "':root:/roo'" ] }, "execution_count": 397, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.read(10)" ] }, { "cell_type": "code", "execution_count": 398, "metadata": {}, "outputs": [], "source": [ "_ = f.read(2640)" ] }, { "cell_type": "code", "execution_count": 399, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2640" ] }, "execution_count": 399, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(_)" ] }, { "cell_type": "code", "execution_count": 400, "metadata": {}, "outputs": [], "source": [ "s = f.read(30000)" ] }, { "cell_type": "code", "execution_count": 401, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "30" ] }, "execution_count": 401, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(s)" ] }, { "cell_type": "code", "execution_count": 403, "metadata": {}, "outputs": [], "source": [ "s = f.read(3000)" ] }, { "cell_type": "code", "execution_count": 404, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 404, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(s)" ] }, { "cell_type": "code", "execution_count": 405, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 405, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.seek(0)" ] }, { "cell_type": "code", "execution_count": 406, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'root:x:0:0'" ] }, "execution_count": 406, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.read(10)" ] }, { "cell_type": "code", "execution_count": 407, "metadata": {}, "outputs": [], "source": [ "f = open('/etc/passwd', encoding = 'utf-8')" ] }, { "cell_type": "code", "execution_count": 408, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "root:x:0:0:root:/root:/bin/bash\n", "\n", "bin:x:1:1:bin:/bin:/sbin/nologin\n", "\n", "daemon:x:2:2:daemon:/sbin:/sbin/nologin\n", "\n", "adm:x:3:4:adm:/var/adm:/sbin/nologin\n", "\n", "lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin\n", "\n", "sync:x:5:0:sync:/sbin:/bin/sync\n", "\n", "shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown\n", "\n", "halt:x:7:0:halt:/sbin:/sbin/halt\n", "\n", "mail:x:8:12:mail:/var/spool/mail:/sbin/nologin\n", "\n", "operator:x:11:0:operator:/root:/sbin/nologin\n", "\n", "games:x:12:100:games:/usr/games:/sbin/nologin\n", "\n", "ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin\n", "\n", "nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin\n", "\n", "dbus:x:81:81:System message bus:/:/sbin/nologin\n", "\n", "apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin\n", "\n", "tss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin\n", "\n", "systemd-network:x:192:192:systemd Network Management:/:/usr/sbin/nologin\n", "\n", "systemd-oom:x:999:999:systemd Userspace OOM Killer:/:/usr/sbin/nologin\n", "\n", "systemd-resolve:x:193:193:systemd Resolver:/:/usr/sbin/nologin\n", "\n", "qemu:x:107:107:qemu user:/:/sbin/nologin\n", "\n", "polkitd:x:998:997:User for polkitd:/:/sbin/nologin\n", "\n", "avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin\n", "\n", "unbound:x:997:995:Unbound DNS resolver:/etc/unbound:/sbin/nologin\n", "\n", "nm-openconnect:x:996:994:NetworkManager user for OpenConnect:/:/sbin/nologin\n", "\n", "geoclue:x:995:993:User for geoclue:/var/lib/geoclue:/sbin/nologin\n", "\n", "usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin\n", "\n", "gluster:x:994:992:GlusterFS daemons:/run/gluster:/sbin/nologin\n", "\n", "rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin\n", "\n", "chrony:x:993:990::/var/lib/chrony:/sbin/nologin\n", "\n", "saslauth:x:992:76:Saslauthd user:/run/saslauthd:/sbin/nologin\n", "\n", "dnsmasq:x:991:989:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin\n", "\n", "rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin\n", "\n", "colord:x:990:988:User for colord:/var/lib/colord:/sbin/nologin\n", "\n", "rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin\n", "\n", "openvpn:x:989:987:OpenVPN:/etc/openvpn:/sbin/nologin\n", "\n", "nm-openvpn:x:988:986:Default user for running openvpn spawned by NetworkManager:/:/sbin/nologin\n", "\n", "pipewire:x:987:985:PipeWire System Daemon:/var/run/pipewire:/sbin/nologin\n", "\n", "abrt:x:173:173::/etc/abrt:/sbin/nologin\n", "\n", "flatpak:x:986:983:User for flatpak system helper:/:/sbin/nologin\n", "\n", "gdm:x:42:42:GNOME Display Manager:/var/lib/gdm:/sbin/nologin\n", "\n", "gnome-initial-setup:x:985:982::/run/gnome-initial-setup/:/sbin/nologin\n", "\n", "vboxadd:x:984:1::/var/run/vboxadd:/sbin/nologin\n", "\n", "sshd:x:74:74:Privilege-separated SSH:/usr/share/empty.sshd:/sbin/nologin\n", "\n", "tcpdump:x:72:72::/:/sbin/nologin\n", "\n", "jfasch:x:1000:1000:Jörg Faschingbauer:/home/jfasch:/bin/bash\n", "\n", "systemd-coredump:x:978:978:systemd Core Dumper:/:/usr/sbin/nologin\n", "\n", "systemd-timesync:x:977:977:systemd Time Synchronization:/:/usr/sbin/nologin\n", "\n", "mosquitto:x:976:976:Mosquitto Broker:/etc/mosquitto:/sbin/nologin\n", "\n" ] } ], "source": [ "for line in f:\n", " print(line)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## CSV" ] }, { "cell_type": "code", "execution_count": 409, "metadata": {}, "outputs": [], "source": [ "d = {\n", " 'ID': '5',\n", " 'Firstname': 'Joerg',\n", " 'Lastname': 'Faschingbauer',\n", "}" ] }, { "cell_type": "code", "execution_count": 410, "metadata": {}, "outputs": [], "source": [ "id, firstname, lastname = d" ] }, { "cell_type": "code", "execution_count": 411, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('ID', 'Firstname', 'Lastname')" ] }, "execution_count": 411, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id, firstname, lastname" ] }, { "cell_type": "code", "execution_count": 412, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ID\n", "Firstname\n", "Lastname\n" ] } ], "source": [ "for elem in d:\n", " print(elem)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lambda, And Functional Programming Tools" ] }, { "cell_type": "code", "execution_count": 413, "metadata": {}, "outputs": [], "source": [ "def square(n): return n**2" ] }, { "cell_type": "code", "execution_count": 414, "metadata": {}, "outputs": [], "source": [ "l = [0,1,2,3,4,5]" ] }, { "cell_type": "code", "execution_count": 415, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n", "9\n", "16\n", "25\n" ] } ], "source": [ "for elem in l:\n", " print(square(elem))" ] }, { "cell_type": "code", "execution_count": 417, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n", "9\n", "16\n", "25\n" ] } ], "source": [ "for sq in map(square, l):\n", " print(sq)" ] }, { "cell_type": "code", "execution_count": 419, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n", "9\n", "16\n", "25\n" ] } ], "source": [ "for sq in map(lambda n: n**2, l):\n", " print(sq)" ] }, { "cell_type": "code", "execution_count": 420, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n", "9\n", "16\n", "25\n" ] } ], "source": [ "for sq in map(lambda n: n**2, range(6)):\n", " print(sq)" ] }, { "cell_type": "code", "execution_count": 421, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "2\n", "4\n" ] } ], "source": [ "for even in filter(lambda n: n%2==0, range(6)):\n", " print(even)" ] }, { "cell_type": "code", "execution_count": 423, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n", "9\n", "16\n", "25\n" ] } ], "source": [ "for sq in (n**2 for n in range(6)):\n", " print(sq)" ] }, { "cell_type": "code", "execution_count": 424, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "2\n", "4\n" ] } ], "source": [ "for even in (n for n in range(6) if n%2 == 0):\n", " print(even)" ] } ], "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 }