{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Initial Terminology" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sequence" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sequence is an entire class of data types which have a few things in commmon: elements have positions, elements are ordered." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "l = [1, 2, 3, 'vier'] # list\n", "l[0]" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[1]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'vier'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[3]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'H'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'Hello World' # str\n", "s[0]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'W'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s[6]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Variables" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 42\n", "type(a)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 'hello'\n", "type(a)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 42.345\n", "type(a)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1, 2, 3, 'vier']\n", "type(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Datatypes" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "i = 666" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Highest natively representable number:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551615" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**64 - 1" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18446744073709551616" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**64" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1267650600228229401496703205376" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**100" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**1000" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Modulo Operator: %" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5%2" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "6%4" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "6%3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Negation vs. Subtraction" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "a, b = 2, 3" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b-a" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-3" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "-b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Strings" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Good Morning, Mr.\n" ] } ], "source": [ "hour = 9\n", "sex = 'm'\n", "if hour >= 9 and hour < 11:\n", " greeting = 'Good Morning'\n", "# ...\n", "if sex == 'm':\n", " greeting += ', Mr.'\n", "# ...\n", "print(greeting)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Boolean" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "jo\n" ] } ], "source": [ "if True:\n", " print('jo')" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "string is not empty\n" ] } ], "source": [ "s = 'x'\n", "if len(s) == 0:\n", " print('string is empty')\n", "else:\n", " print('string is not empty')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Strings evaluate to boolean ..." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "s = 'x'" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "string is not empty\n" ] } ], "source": [ "if s:\n", " print('string is not empty')\n", "else:\n", " print('string is empty')" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "string is not empty\n" ] } ], "source": [ "if not s:\n", " print('string is empty')\n", "else:\n", " print('string is not empty')" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ja\n" ] } ], "source": [ "sex = 'x'\n", "if sex == 'm' or 'M':\n", " print('ja')" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "jo\n" ] } ], "source": [ "if 'M':\n", " print('jo')\n", "else:\n", " print('na')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Compound Datatypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## List (mutable)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier', ['noch', 1, 'liste']]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [1,2,3,'vier', ['noch', 1, 'liste']]\n", "l" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'vier'" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[3]" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "l.append(6.0)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier', ['noch', 1, 'liste'], 6.0]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "l.extend([7, 8, 9])" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier', ['noch', 1, 'liste'], 6.0, 7, 8, 9]" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, ['noch', 1, 'liste'], 6.0, 7, 8, 9]" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "del l[3]\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "'+' leaves operands unmodified:" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, ['noch', 1, 'liste'], 6.0, 7, 8, 9, 10, 'elf']" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l + [10, 'elf']" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, ['noch', 1, 'liste'], 6.0, 7, 8, 9]" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tuple (immutable)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, 3, 'vier')" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = (1, 2, 3, 'vier')\n", "t" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Item deletion not possible (for example)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "try:\n", " del t[1]\n", "except TypeError:\n", " pass" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "try:\n", " t.append(666)\n", "except AttributeError:\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dictionary" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "table = {\n", " 'zero': 0,\n", " 'one': 1,\n", " # ...\n", " 'nine': 9,\n", "}" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "table['nine']" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'six'\n" ] } ], "source": [ "try:\n", " table['six']\n", "except KeyError as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "table['six'] = 6" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "table['six']" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "del table['six']" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'six' in table" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'one' in table" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Set" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 'vier'}" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = { 1, 2, 3, 'vier' }\n", "s" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "s.add(5.0)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 5.0, 'vier'}" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5.0 in s" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "6 not in s" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not 6 in s" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4}" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{ 1, 2, 3} | { 1, 2, 4}" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2}" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{ 1, 2, 3} & { 1, 2, 4}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# References, (Im)mutability" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [], "source": [ "l1 = [1, 2, 3, 'vier']\n", "l2 = l1" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier']" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier']" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140576769461632" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l1)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "140576769461632" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(l2)" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier', 5.0]" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l1.append(5.0)\n", "l1" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'vier', 5.0]" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Iteration, ``for``, and ``range()``" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "eins\n", "zwei\n", "drei\n", "vier\n" ] } ], "source": [ "l = ['eins', 'zwei', 'drei', 'vier']\n", "for element in l:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n" ] } ], "source": [ "l = [0, 1, 2, 3]\n", "for element in l:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n" ] } ], "source": [ "for element in range(0, 4):\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Iterator Protocol (not so important for beginners)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r = range(0,4)\n", "type(r)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "iterator = iter(r)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(iterator)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(iterator)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(iterator)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(iterator)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "try:\n", " next(iterator)\n", "except StopIteration:\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Iteration, and Compound Datatypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List and Tuple" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "vier\n" ] } ], "source": [ "l = [1, 2, 3, 'vier']\n", "for element in l:\n", " print(element)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "vier\n" ] } ], "source": [ "t = (1, 2, 3, 'vier')\n", "for element in t:\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Set" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "vier\n" ] } ], "source": [ "s = {1, 2, 3, 'vier'}\n", "for element in s:\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Order ist **not** guaranteed:" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "5.0\n", "vier\n" ] } ], "source": [ "s.add(5.0)\n", "for element in s:\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dictionary" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "table = {\n", " 'one': 1,\n", " 'two': 2,\n", " 'three': 3,\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dictionary iteration is simplicit *key* iteration " ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n", "two\n", "three\n" ] } ], "source": [ "for element in table:\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Explicit is better than implicit:" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n", "two\n", "three\n" ] } ], "source": [ "for key in table.keys():\n", " print(key)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How do I iterate over values?" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "for value in table.values():\n", " print(value)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('one', 1)\n", "('two', 2)\n", "('three', 3)\n" ] } ], "source": [ "for item in table.items():\n", " print(item)" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "key: one , value: 1\n", "key: two , value: 2\n", "key: three , value: 3\n" ] } ], "source": [ "for item in table.items():\n", " key = item[0]\n", " value = item[1]\n", " print('key:', key, ', value:', value)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tuple unpacking:" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "key: one , value: 1\n", "key: two , value: 2\n", "key: three , value: 3\n" ] } ], "source": [ "for key, value in table.items():\n", " print('key:', key, ', value:', value)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate maximum of two variables ..." ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "a = 100\n", "b = 200" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "200\n" ] } ], "source": [ "if a < b:\n", " print(b)\n", "else:\n", " print(a)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [], "source": [ "def maximum(lhs, rhs):\n", " if lhs < rhs:\n", " return rhs\n", " else:\n", " return lhs" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "200\n" ] } ], "source": [ "m = maximum(a, b)\n", "print(m)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Only integers? And strings?" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abc' < 'def'" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'def'" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum('abc', 'def')" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'<' not supported between instances of 'int' and 'str'\n" ] } ], "source": [ "try:\n", " maximum(42, '666')\n", "except TypeError as e:\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ``matplotlib``" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 129, "metadata": {}, "output_type": "execute_result" }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD4CAYAAAD1jb0+AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAhIUlEQVR4nO3dd3zV1eH/8dfJ3oskEAgQlkxZSUCrbR21blHrqFVkCXbYavvtsNpabe2w/andVasM90apWi21Yqu1moS9ZSRsErL3uPf8/shVAyYQwr353E/u+/l45JF7by73vi8h7xzO53zONdZaRETEfcKcDiAiIj2jAhcRcSkVuIiIS6nARURcSgUuIuJSEb35ZOnp6TYnJ6c3n1JExPWKiooOWWszjry9Vws8JyeHwsLC3nxKERHXM8aUdHa7plBERFxKBS4i4lIqcBERl1KBi4i4lApcRMSlVOAiIi6lAhcRcSkVuIhIAJXXNfPTv22kqdXj98dWgYuIBEhTq4cbHi3kifdL2F5W5/fH79UzMUVEQoXXa/m/Z9ewencVf7l2KuMHJvv9OTQCFxEJgF+/sYVX1+3ntvPHct6ErIA8hwpcRMTPnvpgFw+8vZ1rpw/hhs8OC9jzqMBFRPzo7a1l/Oil9ZwxOoO7LhmPMSZgz6UCFxHxk80HavjGEys5qX8if/zKVCLCA1uxKnARET84WNPE3EUFxEeHs3B2HgnRgV8jolUoIiInqL65jXlLCqhqbOXZG08lKzm2V55XI3ARkRPg8VpufnoVG/fV8KevTGXCIP8vF+yKRuAiIifgZ69s5J+bSvnZjPGcOSazV59bI3ARkR5a9O5OFv+3mBtOH8bMU3N6/flV4CIiPbB840F++spGzh3fnx9eMNaRDCpwEZHjtG5PNd96ahUTByXz26unEB4WuLXeR6MCFxE5DnurGpm7pIC0+CgenpVPbFS4Y1l0EFNEpJtqmlqZu6iAplYPT94wnYzE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ma+3/HI4WcMaY7wB5QJK19iKn8wSaMaYYyLPW+v3EJTeMwKcB26y1O6y1LcDTwAyHMwWUtfbfQIXTOXqLtXa/tXal73ItsAkY5GyqwLLt6nxXI30fwT2a8gNjTDZwIfCw01n6AjcU+CBgd4fre+jjP9yhzBiTA0wB3nc4SsD5phJWA6XAcmttn3/NwG+B7wNeh3P0Jgv8wxhTZIxZ4M8HdkOBS4gwxiQALwC3WGtrnM4TaNZaj7V2MpANTDPG9OnpMmPMRUCptbbI6Sy97HRr7VTgfOAbvilSv3BDge8FBne4nu27TfoQ3zzwC8AT1toXnc7Tm6y1VcBbwHkORwm004BLfHPCTwNnGWMedzZS4Flr9/o+lwJLaZ8W9gs3FHgBMMoYM8wYEwV8GVjmcCbxI98BvUeATdba+5zO0xuMMRnGmBTf5VjaD9JvdjRUgFlrf2itzbbW5tD+c/wva+11DscKKGNMvO/APMaYeOCLgN9WlwV9gVtr24CbgDdoP7j1rLV2g7OpAssY8xTwHjDaGLPHGDPP6UwBdhowk/YR2WrfxwVOhwqwLOAtY8xa2gcpy621IbGsLsT0B94xxqwBPgBetda+7q8HD/plhCIi0rmgH4GLiEjnVOAiIi6lAhcRcSkVuIiIS6nARURcSgUuIuJSKnAREZf6/ySlixQalJqTAAAAAElFTkSuQmCC\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "plt.plot([0, 1, 2, 3, 4, 5], [3, 4, 7, 15, 30, 45])\n", "plt.plot([0, 1, 2, 3, 4, 5], [3, 4, 5, 6, 7, 8])\n", "plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# File I/O, and Strings" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0;200;0.5\n", "\n", "1;202;10\n", "\n", "2;220;15\n", "\n", "3;300;40\n", "\n", "4;450;80\n", "\n", "5;600;150\n", "\n", "6;1100;3000\n", "\n" ] } ], "source": [ "f = open('inputfile.csv')\n", "for line in f:\n", " print(line)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hm. We want to split each line into three elements, and store those columns in three lists: ``time``, ``pressure``, and ``temperature``. See the extra program on Github." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Quoting?" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello world'" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'hello world'\n", "s" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello world'" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"hello world\"\n", "s" ] }, { "cell_type": "code", "execution_count": 83, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/plain": [ "\"das ist ein single-quote: '\"" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'das ist ein single-quote: \\''\n", "s" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"das ist ein single-quote: '\"" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"das ist ein single-quote: '\"\n", "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Raw strings: Windows path names" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\some\\name.txt\n" ] } ], "source": [ "filename = 'C:\\some\\\\name.txt' # WTF?\n", "print(filename)" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C:\\some\\name.txt\n" ] } ], "source": [ "filename = r'C:\\some\\name.txt'\n", "print(filename)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Multiline Strings" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Eine Zeile\n", "Noch eine Zeile\n", "Und noch viel mehr\n", "\n" ] } ], "source": [ "paragraph = 'Eine Zeile\\nNoch eine Zeile\\nUnd noch viel mehr\\n'\n", "print(paragraph)" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Eine Zeile\n", "Noch eine Zeile\n", "Und noch viel mehr\n", "\n" ] } ], "source": [ "paragraph = '''Eine Zeile\n", "Noch eine Zeile\n", "Und noch viel mehr\n", "'''\n", "print(paragraph)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Commonly used to write docstrings:" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [], "source": [ "# this highly sophisticated algorithm determines \n", "# the maximum blah blah\n", "def maximum(lhs, rhs):\n", " if lhs < rhs:\n", " return rhs\n", " else:\n", " return lhs" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [], "source": [ "def maximum(lhs, rhs):\n", " '''this highly sophisticated algorithm determines \n", " the maximum blah blah\n", " '''\n", " if lhs < rhs:\n", " return rhs\n", " else:\n", " return lhs" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'this highly sophisticated algorithm determines \\n the maximum blah blah\\n '" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum.__doc__" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function maximum in module __main__:\n", "\n", "maximum(lhs, rhs)\n", " this highly sophisticated algorithm determines \n", " the maximum blah blah\n", "\n" ] } ], "source": [ "help(maximum)" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [], "source": [ "import sys" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in module sys:\n", "\n", "NAME\n", " sys\n", "\n", "MODULE REFERENCE\n", " https://docs.python.org/3.9/library/sys\n", " \n", " The following documentation is automatically generated from the Python\n", " source files. It may be incomplete, incorrect or include features that\n", " are considered implementation detail and may vary between Python\n", " implementations. When in doubt, consult the module reference at the\n", " location listed above.\n", "\n", "DESCRIPTION\n", " This module provides access to some objects used or maintained by the\n", " interpreter and to functions that interact strongly with the interpreter.\n", " \n", " Dynamic objects:\n", " \n", " argv -- command line arguments; argv[0] is the script pathname if known\n", " path -- module search path; path[0] is the script directory, else ''\n", " modules -- dictionary of loaded modules\n", " \n", " displayhook -- called to show results in an interactive session\n", " excepthook -- called to handle any uncaught exception other than SystemExit\n", " To customize printing in an interactive session or to install a custom\n", " top-level exception handler, assign other functions to replace these.\n", " \n", " stdin -- standard input file object; used by input()\n", " stdout -- standard output file object; used by print()\n", " stderr -- standard error object; used for error messages\n", " By assigning other file objects (or objects that behave like files)\n", " to these, it is possible to redirect all of the interpreter's I/O.\n", " \n", " last_type -- type of last uncaught exception\n", " last_value -- value of last uncaught exception\n", " last_traceback -- traceback of last uncaught exception\n", " These three are only available in an interactive session after a\n", " traceback has been printed.\n", " \n", " Static objects:\n", " \n", " builtin_module_names -- tuple of module names built into this interpreter\n", " copyright -- copyright notice pertaining to this interpreter\n", " exec_prefix -- prefix used to find the machine-specific Python library\n", " executable -- absolute path of the executable binary of the Python interpreter\n", " float_info -- a named tuple with information about the float implementation.\n", " float_repr_style -- string indicating the style of repr() output for floats\n", " hash_info -- a named tuple with information about the hash algorithm.\n", " hexversion -- version information encoded as a single integer\n", " implementation -- Python implementation information.\n", " int_info -- a named tuple with information about the int implementation.\n", " maxsize -- the largest supported length of containers.\n", " maxunicode -- the value of the largest Unicode code point\n", " platform -- platform identifier\n", " prefix -- prefix used to find the Python library\n", " thread_info -- a named tuple with information about the thread implementation.\n", " version -- the version of this interpreter as a string\n", " version_info -- version information as a named tuple\n", " __stdin__ -- the original stdin; don't touch!\n", " __stdout__ -- the original stdout; don't touch!\n", " __stderr__ -- the original stderr; don't touch!\n", " __displayhook__ -- the original displayhook; don't touch!\n", " __excepthook__ -- the original excepthook; don't touch!\n", " \n", " Functions:\n", " \n", " displayhook() -- print an object to the screen, and save it in builtins._\n", " excepthook() -- print an exception and its traceback to sys.stderr\n", " exc_info() -- return thread-safe information about the current exception\n", " exit() -- exit the interpreter by raising SystemExit\n", " getdlopenflags() -- returns flags to be used for dlopen() calls\n", " getprofile() -- get the global profiling function\n", " getrefcount() -- return the reference count for an object (plus one :-)\n", " getrecursionlimit() -- return the max recursion depth for the interpreter\n", " getsizeof() -- return the size of an object in bytes\n", " gettrace() -- get the global debug tracing function\n", " setdlopenflags() -- set the flags to be used for dlopen() calls\n", " setprofile() -- set the global profiling function\n", " setrecursionlimit() -- set the max recursion depth for the interpreter\n", " settrace() -- set the global debug tracing function\n", "\n", "FUNCTIONS\n", " __breakpointhook__ = breakpointhook(...)\n", " breakpointhook(*args, **kws)\n", " \n", " This hook function is called by built-in breakpoint().\n", " \n", " __displayhook__ = displayhook(object, /)\n", " Print an object to sys.stdout and also save it in builtins._\n", " \n", " __excepthook__ = excepthook(exctype, value, traceback, /)\n", " Handle an exception by displaying it with a traceback on sys.stderr.\n", " \n", " __unraisablehook__ = unraisablehook(unraisable, /)\n", " Handle an unraisable exception.\n", " \n", " The unraisable argument has the following attributes:\n", " \n", " * exc_type: Exception type.\n", " * exc_value: Exception value, can be None.\n", " * exc_traceback: Exception traceback, can be None.\n", " * err_msg: Error message, can be None.\n", " * object: Object causing the exception, can be None.\n", " \n", " addaudithook(hook)\n", " Adds a new audit hook callback.\n", " \n", " audit(...)\n", " audit(event, *args)\n", " \n", " Passes the event to any audit hooks that are attached.\n", " \n", " breakpointhook(...)\n", " breakpointhook(*args, **kws)\n", " \n", " This hook function is called by built-in breakpoint().\n", " \n", " call_tracing(func, args, /)\n", " Call func(*args), while tracing is enabled.\n", " \n", " The tracing state is saved, and restored afterwards. This is intended\n", " to be called from a debugger from a checkpoint, to recursively debug\n", " some other code.\n", " \n", " exc_info()\n", " Return current exception information: (type, value, traceback).\n", " \n", " Return information about the most recent exception caught by an except\n", " clause in the current stack frame or in an older stack frame.\n", " \n", " exit(status=None, /)\n", " Exit the interpreter by raising SystemExit(status).\n", " \n", " If the status is omitted or None, it defaults to zero (i.e., success).\n", " If the status is an integer, it will be used as the system exit status.\n", " If it is another kind of object, it will be printed and the system\n", " exit status will be one (i.e., failure).\n", " \n", " get_asyncgen_hooks()\n", " Return the installed asynchronous generators hooks.\n", " \n", " This returns a namedtuple of the form (firstiter, finalizer).\n", " \n", " get_coroutine_origin_tracking_depth()\n", " Check status of origin tracking for coroutine objects in this thread.\n", " \n", " getallocatedblocks()\n", " Return the number of memory blocks currently allocated.\n", " \n", " getdefaultencoding()\n", " Return the current default encoding used by the Unicode implementation.\n", " \n", " getdlopenflags()\n", " Return the current value of the flags that are used for dlopen calls.\n", " \n", " The flag constants are defined in the os module.\n", " \n", " getfilesystemencodeerrors()\n", " Return the error mode used Unicode to OS filename conversion.\n", " \n", " getfilesystemencoding()\n", " Return the encoding used to convert Unicode filenames to OS filenames.\n", " \n", " getprofile()\n", " Return the profiling function set with sys.setprofile.\n", " \n", " See the profiler chapter in the library manual.\n", " \n", " getrecursionlimit()\n", " Return the current value of the recursion limit.\n", " \n", " The recursion limit is the maximum depth of the Python interpreter\n", " stack. This limit prevents infinite recursion from causing an overflow\n", " of the C stack and crashing Python.\n", " \n", " getrefcount(object, /)\n", " Return the reference count of object.\n", " \n", " The count returned is generally one higher than you might expect,\n", " because it includes the (temporary) reference as an argument to\n", " getrefcount().\n", " \n", " getsizeof(...)\n", " getsizeof(object [, default]) -> int\n", " \n", " Return the size of object in bytes.\n", " \n", " getswitchinterval()\n", " Return the current thread switch interval; see sys.setswitchinterval().\n", " \n", " gettrace()\n", " Return the global debug tracing function set with sys.settrace.\n", " \n", " See the debugger chapter in the library manual.\n", " \n", " intern(string, /)\n", " ``Intern'' the given string.\n", " \n", " This enters the string in the (global) table of interned strings whose\n", " purpose is to speed up dictionary lookups. Return the string itself or\n", " the previously interned string object with the same value.\n", " \n", " is_finalizing()\n", " Return True if Python is exiting.\n", " \n", " set_asyncgen_hooks(...)\n", " set_asyncgen_hooks(* [, firstiter] [, finalizer])\n", " \n", " Set a finalizer for async generators objects.\n", " \n", " set_coroutine_origin_tracking_depth(depth)\n", " Enable or disable origin tracking for coroutine objects in this thread.\n", " \n", " Coroutine objects will track 'depth' frames of traceback information\n", " about where they came from, available in their cr_origin attribute.\n", " \n", " Set a depth of 0 to disable.\n", " \n", " setdlopenflags(flags, /)\n", " Set the flags used by the interpreter for dlopen calls.\n", " \n", " This is used, for example, when the interpreter loads extension\n", " modules. Among other things, this will enable a lazy resolving of\n", " symbols when importing a module, if called as sys.setdlopenflags(0).\n", " To share symbols across extension modules, call as\n", " sys.setdlopenflags(os.RTLD_GLOBAL). Symbolic names for the flag\n", " modules can be found in the os module (RTLD_xxx constants, e.g.\n", " os.RTLD_LAZY).\n", " \n", " setprofile(...)\n", " setprofile(function)\n", " \n", " Set the profiling function. It will be called on each function call\n", " and return. See the profiler chapter in the library manual.\n", " \n", " setrecursionlimit(limit, /)\n", " Set the maximum depth of the Python interpreter stack to n.\n", " \n", " This limit prevents infinite recursion from causing an overflow of the C\n", " stack and crashing Python. The highest possible limit is platform-\n", " dependent.\n", " \n", " setswitchinterval(interval, /)\n", " Set the ideal thread switching delay inside the Python interpreter.\n", " \n", " The actual frequency of switching threads can be lower if the\n", " interpreter executes long sequences of uninterruptible code\n", " (this is implementation-specific and workload-dependent).\n", " \n", " The parameter must represent the desired switching delay in seconds\n", " A typical value is 0.005 (5 milliseconds).\n", " \n", " settrace(...)\n", " settrace(function)\n", " \n", " Set the global debug tracing function. It will be called on each\n", " function call. See the debugger chapter in the library manual.\n", " \n", " unraisablehook(unraisable, /)\n", " Handle an unraisable exception.\n", " \n", " The unraisable argument has the following attributes:\n", " \n", " * exc_type: Exception type.\n", " * exc_value: Exception value, can be None.\n", " * exc_traceback: Exception traceback, can be None.\n", " * err_msg: Error message, can be None.\n", " * object: Object causing the exception, can be None.\n", "\n", "DATA\n", " __stderr__ = <_io.TextIOWrapper name='' mode='w' encoding='utf...\n", " __stdin__ = <_io.TextIOWrapper name='' mode='r' encoding='utf-8...\n", " __stdout__ = <_io.TextIOWrapper name='' mode='w' encoding='utf...\n", " abiflags = ''\n", " api_version = 1013\n", " argv = ['/home/jfasch/venv/homepage/lib64/python3.9/site-packages/ipyk...\n", " base_exec_prefix = '/usr'\n", " base_prefix = '/usr'\n", " builtin_module_names = ('_abc', '_ast', '_codecs', '_collections', '_f...\n", " byteorder = 'little'\n", " copyright = 'Copyright (c) 2001-2021 Python Software Foundati...ematis...\n", " displayhook = \n", " dont_write_bytecode = False\n", " exec_prefix = '/home/jfasch/venv/homepage'\n", " executable = '/home/jfasch/venv/homepage/bin/python'\n", " flags = sys.flags(debug=0, inspect=0, interactive=0, opt...ation=1, is...\n", " float_info = sys.float_info(max=1.7976931348623157e+308, max_...epsilo...\n", " float_repr_style = 'short'\n", " hash_info = sys.hash_info(width=64, modulus=2305843009213693...iphash2...\n", " hexversion = 50922736\n", " implementation = namespace(name='cpython', cache_tag='cpython-39'...xv...\n", " int_info = sys.int_info(bits_per_digit=30, sizeof_digit=4)\n", " last_value = SyntaxError('EOL while scanning string literal',...', 1, ...\n", " maxsize = 9223372036854775807\n", " maxunicode = 1114111\n", " meta_path = [, , \n", " stdin = <_io.TextIOWrapper name='' mode='r' encoding='utf-8'>\n", " stdout = \n", " thread_info = sys.thread_info(name='pthread', lock='semaphore', versio...\n", " version = '3.9.4 (default, Apr 6 2021, 00:00:00) \\n[GCC 11.0.1 202103...\n", " version_info = sys.version_info(major=3, minor=9, micro=4, releaseleve...\n", " warnoptions = []\n", "\n", "FILE\n", " (built-in)\n", "\n", "\n" ] } ], "source": [ "help(sys)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Miscellaneous String Methods" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 109, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abc'.isalpha()" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abc'.isdigit()" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'123'.isdigit()" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 113, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abc'.isidentifier()" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 114, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'123'.isidentifier()" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abc'" ] }, "execution_count": 115, "metadata": {}, "output_type": "execute_result" } ], "source": [ "' \\r abc \\n\\t'.strip()" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' \\r abc'" ] }, "execution_count": 116, "metadata": {}, "output_type": "execute_result" } ], "source": [ "' \\r abc \\n\\t'.rstrip()" ] }, { "cell_type": "code", "execution_count": 117, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 117, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'mississippi'.find('ss')" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 118, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'mississippi'.find('xx')" ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 120, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'mississippi'.count('i')" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'mississippi'.count('pp')" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'eins-zwei-drei'" ] }, "execution_count": 123, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list_of_strings = ['eins', 'zwei', 'drei']\n", "'-'.join(list_of_strings)" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['0.5', '500', '5000']" ] }, "execution_count": 124, "metadata": {}, "output_type": "execute_result" } ], "source": [ "line = '0.5;500;5000'\n", "line.split(';')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tuple unpacking" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [], "source": [ "time, pressure, temperature = line.split(';')" ] }, { "cell_type": "code", "execution_count": 128, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Time: 0.5 , Pressure: 500 , Temperature: 5000\n" ] } ], "source": [ "print('Time:', time, ', Pressure:', pressure, ', Temperature:', temperature)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.9.4" } }, "nbformat": 4, "nbformat_minor": 4 }