Skip to content

Instantly share code, notes, and snippets.

@mvoelk
Last active May 8, 2023 15:19
Show Gist options
  • Save mvoelk/7ff52dbc2723f3a50752e00d2bb629df to your computer and use it in GitHub Desktop.
Save mvoelk/7ff52dbc2723f3a50752e00d2bb629df to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Gist\n",
"https://gist.github.com/mvoelk/7ff52dbc2723f3a50752e00d2bb629df\n",
"\n",
"Google Colab\n",
"https://colab.research.google.com/gist/mvoelk/7ff52dbc2723f3a50752e00d2bb629df"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Python 3.8.10\r\n"
]
}
],
"source": [
"!python --version"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Help\n",
"\n",
"Official Python documentation \n",
"https://docs.python.org/3.11\n",
"\n",
"- Python comes with a build in help system and documentation is typically done as part of the code"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on _Helper in module _sitebuiltins object:\n",
"\n",
"class _Helper(builtins.object)\n",
" | Define the builtin 'help'.\n",
" | \n",
" | This is a wrapper around pydoc.help that provides a helpful message\n",
" | when 'help' is typed at the Python interactive prompt.\n",
" | \n",
" | Calling help() at the Python prompt starts an interactive help session.\n",
" | Calling help(thing) prints help for the python object 'thing'.\n",
" | \n",
" | Methods defined here:\n",
" | \n",
" | __call__(self, *args, **kwds)\n",
" | Call self as a function.\n",
" | \n",
" | __repr__(self)\n",
" | Return repr(self).\n",
" | \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(help)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"View documentation and source code of objects and functions in Jupyter Notebook"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"help?"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"help??"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Syntax\n",
"\n",
"- Indentation is used to indicate code blocks and enforce readability"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello, World!\n"
]
}
],
"source": [
"if 5 > 2:\n",
" print(\"Hello, World!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Comments"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello, World!\n",
"Hello, World!\n"
]
}
],
"source": [
"# this is a comment\n",
"print(\"Hello, World!\") # this is also a comment \n",
"\n",
"'''\n",
"This is a comment\n",
"over multiple lines\n",
"'''\n",
"print(\"Hello, World!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Variables and Data Types"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"int"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 5\n",
"\n",
"type(x)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"isinstance(x, int)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"isinstance(x, str)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Strings\n",
"\n",
"| Escape Characters | |\n",
"| :--- | :--- |\n",
"| \\\\\" | Double Quote |\n",
"| \\\\' | Single Quote |\n",
"| \\\\\\\\ | Backslash |\n",
"| \\n | New Line |\n",
"| \\r | Carriage Return |\n",
"| \\t | Tab |\n",
"| \\b | Backspace |\n",
"| \\f | Form Feed |\n",
"| \\ooo | Octal value |\n",
"| \\xhh | Hex value |"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"hello 'world' !\""
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s1 = 'hello'\n",
"s2 = \"'world' !\"\n",
"\n",
"s3 = s1 + ' ' + s2\n",
"\n",
"s3"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'hello hello hello !!!'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s4 = (s1 + ' ') * 3 + '!' * 3\n",
"s4"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(str, 15)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(s3), len(s3)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'orl'"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s2[2:5]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"orld'\""
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s2[2:-2]"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'world' in s3"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'long\\nlong\\nstring'"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s = \"\"\"long\n",
"long\n",
"string\"\"\"\n",
"\n",
"s"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"long\n",
"long\n",
"string\n"
]
}
],
"source": [
"print(s)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Numbers"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(100000, 50159747054, 29973)"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Numeric Literals\n",
"100000, 0xbadc0ffee, 0b0111010100010101"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(100000, 50159747054, 29973)"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Synthactic tweak for readability, since Python 3.6\n",
"100_000, 0xbad_c0ffee, 0b_0111_0101_0001_0101"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(-1, int)"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v = -1\n",
"v, type(v)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1.0, float)"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v = 1.\n",
"v, type(v)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"((2+1j), complex)"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v = 1.0j + 2\n",
"v, type(v)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(-8.77e+101, float)"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v = -87.7e100\n",
"v, type(v)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Number Functions"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(5, 25)"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"abs(-5), pow(-5, 2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Lists\n",
"\n",
"Lists are ordered and changeable collections, that allow duplicates"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(list, [1, 2, 3, 'foobar'])"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l = [1,2,3,'foobar']\n",
"\n",
"type(l), l"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l[2]"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 'foobar', 4, 5, 9]"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l = [1,2,3,'foobar']\n",
"l.remove(3)\n",
"l.append(4)\n",
"l.extend([5,9])\n",
"l"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('foobar', [1, 2, 99, 3])"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l = [1,2,3,'foobar']\n",
"e = l.pop(3)\n",
"l.insert(2, 99)\n",
"e, l"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[]"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l = [1,2,3,'foobar']\n",
"l.clear()\n",
"l"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l = [1,2,3,'foobar']\n",
"l.index(2)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l = [1,2,3,2]\n",
"l.count(2)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['foobar', 3, 2, 1]"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l = [1,2,3,'foobar']\n",
"l.reverse()\n",
"l"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 6]"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l = [1,6,3,2]\n",
"l.sort()\n",
"l"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[3, 2, 1, 6]"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l = [1,6,3,2]\n",
"l.sort(key=lambda x: abs(3-x))\n",
"l"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"([1, 2, 3, 4], [1, 2, 3, 'foobar'])"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l1 = [1,2,3,'foobar']\n",
"l2 = l1.copy()\n",
"l1[3] = 4\n",
"l1, l2"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5, 6]"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l1 = [1,2,3]\n",
"l2 = [4,5,6]\n",
"l3 = l1 + l2\n",
"l3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"List Functions"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(4, 10, 1, 4)"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l1 = [1,2,3,4]\n",
"len(l1), sum(l1), min(l1), max(l1)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2.2, 5, 8, 9]"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted([1,8,9,2.2,5])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"List Comprehensios"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 4, 9, 16, 25]"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l1 = [1,2,3,4,5]\n",
"l2 = [ i**2 for i in l1 ]\n",
"l2"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 4]"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l1 = [1,2,3,4,5]\n",
"l2 = [ i for i in l1 if i%2 == 0 ]\n",
"l2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Tuples"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tuples are ordered and unchangeable collections, that allow duplicates"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(tuple, (1, 2, 3, 'foobar'))"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = (1,2,3,'foobar')\n",
"\n",
"type(x), x"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2, 3)"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[1:3]"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3, 'foobar')"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[-2:]"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(x)"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3 in x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Sets\n",
"\n",
"Sets are unordered and unindexed collections without duplicates"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(set, {'abc', 'cde', 'efg'})"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s = {'abc', 'cde', 'efg'}\n",
"type(s), s"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(True, False)"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'cde' in s, 'xyz' in s"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(4, {'cde', 'efg', 'hij', 'klm'})"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.add('hij')\n",
"s.update({'cde', 'klm'})\n",
"#s.remove('abc')\n",
"s.discard('abc') # will not throw an error if element does not exist\n",
"\n",
"len(s), s"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Set Operations"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'abc', 'cde', 'efg'}"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s1 = {'abc', 'cde', 'efg'}\n",
"s2 = {'efg', 'cde', 'efg'}\n",
"s1.union(s2)"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'cde', 'efg'}"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s1.intersection(s2)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'cde', 'efg'}"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"set.intersection(s1,s2)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'abc', 'cde', 'efg'}"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"set.union(s1, s2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dictionaries\n",
"\n",
"Dictionaries are unordered, changeable collections, which map keys to values\n",
"- A key can be everything that can be hashed"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'first': 'Max', 'name': 'Muster', 'age': 24}"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = {\n",
" 'first': 'Max',\n",
" 'name': 'Muster',\n",
" 'age': 24,\n",
"}\n",
"\n",
"d"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(d)"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Muster'"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d['name']"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'first': 'Max', 'name': 'Muster', 'age': 24, 3: 5}\n",
"{'first': 'Max', 'name': 'Muster', 'age': 24}\n"
]
},
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d[3] = 5\n",
"print(d)\n",
"v = d.pop(3)\n",
"print(d)\n",
"v"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['first', 'name', 'age'])"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d.keys()"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_values(['Max', 'Muster', 24])"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d.values()"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_items([('first', 'Max'), ('name', 'Muster'), ('age', 24)])"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d.items()"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"first Max\n",
"name Muster\n",
"age 24\n"
]
}
],
"source": [
"for k, v in d.items():\n",
" print(k, v)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Dict Comprehension"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'k1': 1, 'k2': 4, 'k3': 9, 'k4': 16, 'k5': 25}"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d1 = {'k'+str(i): i**2 for i in [1,2,3,4,5]}\n",
"d1"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'k1': 2, 'k2': 8, 'k3': 18, 'k4': 32, 'k5': 50}"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d2 = { k: v*2 for k,v in d1.items() }\n",
"d2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Union and update"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'foo': 3, 'bar': 7, 'key': 8}"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d1 = {'foo': 3, 'bar': 2}\n",
"d2 = {'bar': 7, 'key': 8}\n",
"\n",
"{**d1, **d2}"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'foo': 3, 'bar': 7, 'key': 8}"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d1.update(d2)\n",
"d1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Since Python 3.9"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"d1 = {'foo': 3, 'bar': 2}\n",
"d2 = {'bar': 7, 'key': 8}\n",
"\n",
"d1 | d2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"d1 |= d2\n",
"d1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Booleas"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"bool"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b1 = True\n",
"b2 = False\n",
"\n",
"type(b1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Boolean Operations"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b2 and b1"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b2 or b1"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"not b1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Bytes\n",
"\n",
"The <code>bytes</code> type represents binary data\n"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(bytes, 6)"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = b'foobar'\n",
"type(d), len(d)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Casting"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1, int)"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v = int(1)\n",
"v, type(v)"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2, int)"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v = int(2.3)\n",
"v, type(v)"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(33, int)"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v = int('33')\n",
"v, type(v)"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1.0, float)"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v = float(1)\n",
"v, type(v)"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2.3, float)"
]
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v = float(2.3)\n",
"v, type(v)"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2.3, float)"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v = float('2.3')\n",
"v, type(v)"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('1', str)"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v = str(1)\n",
"v, type(v)"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('2.3', str)"
]
},
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v = str(2.3)\n",
"v, type(v)"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('2.3', str)"
]
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v = str('2.3')\n",
"v, type(v)"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(True, True, True)"
]
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool('abc'), bool(123), bool(['abc','xyz'])"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(False, False, False, False, False, False)"
]
},
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool(None), bool(0), bool(\"\"), bool(()), bool([]), bool({})"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('True', 1, 1.0)"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"str(True), int(True), float(True)"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"([1, 2, 3], (1, 2, 3))"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list((1,2,3)), tuple([1,2,3])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Operators"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Arithmetic\n",
"\n",
"| Operator | Name | Example |\n",
"| :--- | :--- | :--- |\n",
"| + | Addition | x + y |\n",
"| - | Subtraction | x - y |\n",
"| * | Multiplication | x * y |\n",
"| / | Division | x / y |\n",
"| % | Modulus | x % y |\n",
"| ** | Exponentiation | x ** y |\n",
"| // | Floor division | x // y |\n",
"\n",
"\n",
"### Assignment\n",
"\n",
"| Operator | Example | Same As |\n",
"| :--- | :--- | :--- |\n",
"| = | x = 5 | x = 5 |\n",
"| += | x += 3 | x = x + 3 |\n",
"| -= | x -= 3 | x = x - 3 |\n",
"| *= | x *= 3 | x = x * 3 |\n",
"| /= | x /= 3 | x = x / 3 |\n",
"| %= | x %= 3 | x = x % 3 |\n",
"| //= | x //= 3 | x = x // 3 |\n",
"| **= | x **= 3 | x = x ** 3 |\n",
"| &amp;= | x &amp;= 3 | x = x &amp; 3 |\n",
"| &#124;= | x &#124;= 3 | x = x &#124; 3 |\n",
"| ^= | x ^= 3 | x = x ^ 3 |\n",
"| >>= | x >>= 3 | x = x >> 3 |\n",
"| <<= | x <<= 3 | x = x << 3 |\n",
"\n",
"### Coparison\n",
"\n",
"| Operator | Name | Example |\n",
"| :--- | :--- | :--- |\n",
"| == | Equal | x == y |\n",
"| != | Not equal | x != y |\n",
"| > | Greater than | x > y |\n",
"| < | Less than | x < y |\n",
"| >= | Greater than or equal to | x >= y |\n",
"| <= | Less than or equal to | x <= y |\n",
"\n",
"### Logical\n",
"\n",
"| Operator | Description | Example |\n",
"| :--- | :--- | :--- |\n",
"| and | Returns True if both statements are true | x < 5 and x < 10 |\n",
"| or | Returns True if one of the statements is true | x < 5 or x < 4 |\n",
"| not | Reverse the result, returns False if the result is true | not (x < 5 and x < 10) |\n",
"\n",
"### Identity\n",
"\n",
"| Operator | Description | Example |\n",
"| :--- | :--- | :--- |\n",
"| is | Returns True if both variables are the same object | x is y |\n",
"| is not | Returns True if both variables are not the same object | x is not y |\n",
"\n",
"### Membership\n",
"\n",
"| Operator | Description | Example |\n",
"| :--- | :--- | :--- |\n",
"| in | Returns True if a sequence with the specified value is present in the object | x in y |\n",
"| not in | Returns True if a sequence with the specified value is not present in the object | x not in y |\n",
"\n",
"### Bitwise\n",
"\n",
"| Operator | Name | Description |\n",
"| :--- | :--- | :--- |\n",
"| &amp; | AND | Sets each bit to 1 if both bits are 1 |\n",
"| &#124; | OR | Sets each bit to 1 if one of two bits is 1 |\n",
"| ^ | XOR | Sets each bit to 1 if only one of two bits is 1 |\n",
"| ~ | NOT | Inverts all the bits |\n",
"| << | Zero fill left shift | Shift left by pushing zeros in from the right and let the leftmost bits fall off |\n",
"| >> | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Control Flow"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### If ... Else Conditions"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a equal b\n"
]
}
],
"source": [
"a, b = 30, 30\n",
"\n",
"if a > b:\n",
" print('a greater b')\n",
"elif a < b:\n",
" print('a less b')\n",
"else:\n",
" print('a equal b')"
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {},
"outputs": [],
"source": [
"if a > b: print('a greater b')"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c = 4 if b > 3 else 5\n",
"\n",
"c"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Comparison Examples"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(True, True, False, True, False, True)"
]
},
"execution_count": 86,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"5 > 3, 3 <= 4, 3 == 5, 3 != 5, 3 > 2 and 4 < 2, 5 >= 5 and not 3 > 4"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<code>==</code> vs. <code>is</code> or equal vs. identical\n",
"\n",
"- <code>==</code> compares values\n",
"- <code>is</code> compares references"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(True, True, True, False)"
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = [1,2,3]\n",
"b = a\n",
"c = [1,2,3]\n",
"\n",
"a == b, a == c, a is b, a is c"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<code>a</code> and <code>b</code> point to the sampe object, <code>c</code> does not"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### While Loops\n",
"\n",
"Executes a set of statements as long as a condition is <code>True</code>"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 2 3 4 "
]
}
],
"source": [
"i = 1\n",
"while i < 5:\n",
" print(i, end=' ')\n",
" i += 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### For Loops\n",
"\n",
"Iterates over iterables\n",
"\n",
"- Due to the Python interpreter, loops are slow in general\n",
"- For many data elements, <code>map</code>, list comprehensions, generator expressions or NumPy operations are much faster then loops"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a\n",
"b\n",
"c\n"
]
}
],
"source": [
"for c in 'abc':\n",
" print(c)"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"abc\n",
"def\n",
"ghi\n"
]
}
],
"source": [
"for s in ['abc', 'def', 'ghi']:\n",
" print(s)"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 1 2 3 4 5 6 7 8 9 "
]
}
],
"source": [
"for i in range(10):\n",
" print(i, end=' ')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Break and Continue\n",
"\n",
"The <code>break</code> statement stops the loop before it has looped through all iterations\n",
"\n",
"The <code>continue</code> statement stops the current iteration of the loop and continue with the next iteration"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8 5\n"
]
}
],
"source": [
"k = 0\n",
"i = 0\n",
"\n",
"while True:\n",
" i += 1\n",
" if i == 8:\n",
" break\n",
" if i % 3 == 0:\n",
" continue\n",
" k += 1\n",
"\n",
"print(i, k)"
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8 5\n"
]
}
],
"source": [
"k = 0\n",
"\n",
"for i in range(10):\n",
" if i == 8:\n",
" break\n",
" if i % 3 == 0:\n",
" continue\n",
" k += 1\n",
"\n",
"print(i, k)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exceptions and Error Handling\n",
"\n",
"Throwing an Exception"
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [
{
"ename": "AssertionError",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn [94], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;241m3\u001b[39m \u001b[38;5;241m!=\u001b[39m \u001b[38;5;241m3\u001b[39m\n",
"\u001b[0;31mAssertionError\u001b[0m: "
]
}
],
"source": [
"assert 3 != 3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"raise Exception(\"Sorry for being an exception!\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = 3.0\n",
"if not isinstance(x, int):\n",
" raise TypeError(\"Sorry, wrong type!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Handle Exceptions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" print(some_variable_that_does_not_exist)\n",
"except:\n",
" print(\"An exception occurred\")\n",
"print('continue code execution')"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'NameError'>\n",
"<built-in method with_traceback of NameError object at 0x7f1aac6e9c70>\n",
"(\"name 'some_variable_that_does_not_exist' is not defined\",)\n",
"3\n"
]
}
],
"source": [
"try:\n",
" i = 3\n",
" i = some_variable_that_does_not_exist\n",
"except (TypeError, ValueError):\n",
" pass\n",
"except Exception as e:\n",
" print(type(e))\n",
" print(e.with_traceback)\n",
" print(e.args)\n",
"finally:\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"List of built-in exceptions \n",
"https://docs.python.org/3/library/exceptions.html"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Functions"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"some string len 6\n",
"some new string len 10\n"
]
},
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 96,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def some_function(s):\n",
" l = len(s)\n",
" print('some ' + s + ' len ' + str(l))\n",
" return l\n",
"\n",
"l = some_function('string')\n",
"some_function('new string')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 2 3\n",
"2 2 6\n",
"3 2 c\n"
]
},
{
"data": {
"text/plain": [
"tuple"
]
},
"execution_count": 97,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# multiple return values\n",
"\n",
"def some_function(a, b, c):\n",
" print(a, b, c)\n",
" return a+1, b, c*2\n",
"\n",
"l = [1,2,3]\n",
"\n",
"a, b, c = some_function(*l)\n",
"print(a, b, c)\n",
"\n",
"l = some_function(3, 2, 'c')\n",
"type(l)"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x: 33\n",
"y: 5\n",
"args: (6, 'abc')\n",
"kwargs: {'foo': 24, 'bar': 'xyz'}\n"
]
}
],
"source": [
"# variable number of arguments\n",
"\n",
"def some_function(x, y=3, *args, **kwargs):\n",
" print('x:', x)\n",
" print('y:', y)\n",
" print('args:', args)\n",
" print('kwargs:', kwargs)\n",
"\n",
"some_function(33, 5, 6, 'abc', foo=24, bar='xyz')"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x: 33\n",
"y: 3\n",
"args: ()\n",
"kwargs: {'foo': 5}\n"
]
}
],
"source": [
"some_function(33, 3, foo=5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lambda Functions"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 100,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f = lambda x,y: x+y\n",
"f(1,2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Iterators"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Range\n",
"\n",
"Returns an iterator over a sequence of numbers from start to stop with a certain step\n",
"\n",
"- If the start is ommited, the iterator starts with 0\n",
"- If the step is ommited, the step is 1"
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
]
},
"execution_count": 101,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list( range(10) )"
]
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[10, 8, 6, 4, 2]"
]
},
"execution_count": 102,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list( range(10, 0, -2) )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Generators\n",
"\n",
"Generates one item at a time\n",
"\n",
"- Generators can be used to load only the data that is needed at a certain time instead of keeping large lists of objects in memory\n",
"- The <code>yield</code> keyword is used to return the values inside a function"
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [],
"source": [
"def generator():\n",
" for i in range(5):\n",
" yield i*i\n",
"\n",
"g = generator()"
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"generator"
]
},
"execution_count": 104,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(g)"
]
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 105,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 106,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 1 4 9 16 "
]
}
],
"source": [
"g = generator()\n",
"\n",
"for i in g:\n",
" print(i, end=' ')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Generator Expressions"
]
},
{
"cell_type": "code",
"execution_count": 107,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"generator"
]
},
"execution_count": 107,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l = ['aa', 'bb', 'cc']\n",
"\n",
"g = ( s.upper() for s in l )\n",
"\n",
"type(g)"
]
},
{
"cell_type": "code",
"execution_count": 108,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"AA BB CC "
]
}
],
"source": [
"for i in g:\n",
" print(i, end=' ')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Enumerate"
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(0, 'abc'), (1, 'def'), (2, 'ghi')]"
]
},
"execution_count": 109,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list( enumerate(['abc', 'def', 'ghi']) )"
]
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 abc\n",
"1 def\n",
"2 ghi\n"
]
}
],
"source": [
"l = ['abc', 'def', 'ghi']\n",
"for i, e in enumerate(l):\n",
" print(i, e)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Zip\n",
"\n",
"Takes iterables (can be zero or more), aggregates them in a tuple"
]
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(1, 4), (2, 5), (3, 6)]"
]
},
"execution_count": 111,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list( zip([1,2,3], [4,5,6]) )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Reversed\n",
"\n",
"Returns the reversed iterator of the given sequence"
]
},
{
"cell_type": "code",
"execution_count": 112,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[16, 8, 3]"
]
},
"execution_count": 112,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list( reversed([3,8,16]) )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Map\n",
"\n",
"Applies a given function to each item of an iterable (list, tuple etc.)"
]
},
{
"cell_type": "code",
"execution_count": 113,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 4, 9]"
]
},
"execution_count": 113,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list( map(lambda x: x**2, [1,2,3]) )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Filter\n",
"\n",
"Constructs an iterator from elements of an iterable for which a function returns True"
]
},
{
"cell_type": "code",
"execution_count": 114,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 4]"
]
},
"execution_count": 114,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list( filter(lambda x: x%2==0, [1,2,3,4]) )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Classes and Objects\n",
"\n",
"Practically everything in python is an object"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Instances"
]
},
{
"cell_type": "code",
"execution_count": 115,
"metadata": {},
"outputs": [],
"source": [
"class SomeClass:\n",
" pass\n",
"\n",
"some_instance = SomeClass()"
]
},
{
"cell_type": "code",
"execution_count": 116,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"__main__.SomeClass"
]
},
"execution_count": 116,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(some_instance)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Properties"
]
},
{
"cell_type": "code",
"execution_count": 117,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"3\n",
"{'some_other_property': 3}\n",
"{'some_other_property': 3, 'some_property': 1}\n"
]
},
{
"data": {
"text/plain": [
"['__class__',\n",
" '__delattr__',\n",
" '__dict__',\n",
" '__dir__',\n",
" '__doc__',\n",
" '__eq__',\n",
" '__format__',\n",
" '__ge__',\n",
" '__getattribute__',\n",
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__init_subclass__',\n",
" '__le__',\n",
" '__lt__',\n",
" '__module__',\n",
" '__ne__',\n",
" '__new__',\n",
" '__reduce__',\n",
" '__reduce_ex__',\n",
" '__repr__',\n",
" '__setattr__',\n",
" '__sizeof__',\n",
" '__str__',\n",
" '__subclasshook__',\n",
" '__weakref__',\n",
" 'some_other_property',\n",
" 'some_property']"
]
},
"execution_count": 117,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class SomeClass:\n",
" some_property = 1\n",
" \n",
"some_instance = SomeClass()\n",
"\n",
"some_instance.some_other_property = 3\n",
"\n",
"print(some_instance.some_property)\n",
"print(some_instance.some_other_property)\n",
"\n",
"print(some_instance.__dict__)\n",
"some_instance.some_property = 1\n",
"print(some_instance.__dict__)\n",
"\n",
"dir(some_instance)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Methods\n",
"\n",
"- The first parameter <code>self</code> passed to a method is the reference to the current instance\n",
"- The <code>self</code> parameter can be used to access the properties and methods of the instance"
]
},
{
"cell_type": "code",
"execution_count": 118,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"value: text\n",
"value: more text\n",
"2\n"
]
}
],
"source": [
"class SomeClass:\n",
" def __init__(self, x):\n",
" self.x = x\n",
" \n",
" def some_other_method(self, v):\n",
" self.x += 1\n",
" print('value: ' + v)\n",
" \n",
" def some_method(self, v):\n",
" self.v = v\n",
" self.some_other_method(v)\n",
" \n",
"some_instance = SomeClass(0)\n",
"\n",
"print(some_instance.x)\n",
"some_instance.some_method('text')\n",
"some_instance.some_method('more text')\n",
"print(some_instance.x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Inheritance\n",
"\n",
"- The parent or base class is the class being inherited from\n",
"- The child or derived class is the class that inherits form another class\n",
"- Child classes inherit the properties and methods from the parent\n",
"- The <code>super</code> function can be used to call methods from the parent class that get overwritten by a child class"
]
},
{
"cell_type": "code",
"execution_count": 119,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"parent value: text1\n",
"child value: text2\n",
"2\n",
"10\n"
]
}
],
"source": [
"class ParentClass:\n",
" def __init__(self, y):\n",
" self.y = y\n",
" \n",
" def some_parent_method(self, v):\n",
" print('parent value: ' + v)\n",
"\n",
"class ChildClass(ParentClass):\n",
" def __init__(self, x):\n",
" self.x = x\n",
" super().__init__(x+1) \n",
" \n",
" def some_child_method(self, v):\n",
" print('child value: ' + v)\n",
" self.y = 10\n",
" \n",
"some_instance = ChildClass(2)\n",
"\n",
"some_instance.some_parent_method('text1')\n",
"some_instance.some_child_method('text2')\n",
"print(some_instance.x)\n",
"print(some_instance.y)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"some_instance."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Jupyter Notebook has autocompletion to show the properties and methods of an object\n",
"- <code>some_instance.&lt;tab&gt;</code> in this example"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Scopes\n",
"\n",
"- A local scope is created inside functions or methods\n",
"- Local variables are created inside functions and methods\n",
"- Global variables are created outsid of functions"
]
},
{
"cell_type": "code",
"execution_count": 121,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"global x 4\n",
"local x functioon1 3\n",
"local x functioon2 2\n",
"local x functioon1 3\n",
"global x 4\n"
]
}
],
"source": [
"x = 4\n",
"\n",
"def function1():\n",
" x = 3\n",
" \n",
" def function2():\n",
" x = 2\n",
" print('local x functioon2 %i' % x)\n",
" \n",
" print('local x functioon1 %i' % x)\n",
" function2()\n",
" print('local x functioon1 %i' % x)\n",
"\n",
"\n",
"print('global x %i' % x)\n",
"function1()\n",
"print('global x %i' % x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Global variables can be accessed via the <code>global</code> keyword\n",
"- Global and local variables can also be accessed via the <code>globals</code> and <code>locals</code> function\n",
"- The <code>vars</code> function can be used for objects"
]
},
{
"cell_type": "code",
"execution_count": 122,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"local x 4\n",
"local y 3\n",
"global x 6\n",
"global y 6\n"
]
}
],
"source": [
"x = 4\n",
"y = 5\n",
"\n",
"def function():\n",
" x = 3\n",
" global y\n",
" y = 6\n",
" print('local x %i' % globals()['x'] )\n",
" print('local y %i' % locals()['x'] )\n",
" \n",
"function()\n",
"print('global x %i' % y)\n",
"print('global y %i' % y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## File Handling\n",
"\n",
"Files can be opend in different modes\n",
"\n",
"| Mode | Name | Comment |\n",
"| :--- | :--- | :--- |\n",
"| r | Read | Returns an error if file does not exist |\n",
"| a | Append | Creates the file if it does not exist |\n",
"| w | Write | Creates the file if it does not exist |\n",
"| x | Create | Returns an error if file exist |\n",
"\n",
"Files can be handled in binary or text mode\n",
"\n",
"| Mode | Name |\n",
"| :--- | :--- |\n",
"| t | Text |\n",
"| b | Binary |"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Write File"
]
},
{
"cell_type": "code",
"execution_count": 123,
"metadata": {},
"outputs": [],
"source": [
"f = open('textfile.txt', 'w')\n",
"f.write('file has some \\n')\n",
"f.write('content!')\n",
"f.close()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Read File"
]
},
{
"cell_type": "code",
"execution_count": 124,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"file has some \n",
"content!\n"
]
}
],
"source": [
"# entire file\n",
"f = open('textfile.txt', 'rt')\n",
"print( f.read() )\n",
"f.close()"
]
},
{
"cell_type": "code",
"execution_count": 125,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"file has some \n",
"\n"
]
}
],
"source": [
"# by line\n",
"f = open('textfile.txt', 'rt')\n",
"print(f.readline())\n",
"f.close()"
]
},
{
"cell_type": "code",
"execution_count": 126,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"file has some \n",
"\n",
"content!\n"
]
}
],
"source": [
"f = open('textfile.txt', 'rt')\n",
"for x in f:\n",
" print(x)\n",
"f.close()"
]
},
{
"cell_type": "code",
"execution_count": 127,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"file \n"
]
}
],
"source": [
"# by number of characters\n",
"f = open('textfile.txt', 'rt')\n",
"print( f.read(5) )\n",
"f.close()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The <code>open</code> function can also be used with the <code>with</code> statement as a context manager "
]
},
{
"cell_type": "code",
"execution_count": 128,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"file has some \n",
"content!\n"
]
}
],
"source": [
"with open('textfile.txt', 'rt', encoding='utf-8') as f:\n",
" content = f.read()\n",
"\n",
"print(content)"
]
},
{
"cell_type": "code",
"execution_count": 129,
"metadata": {},
"outputs": [],
"source": [
"!rm textfile*.txt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## String Manipulation\n",
"\n",
"### String Methods"
]
},
{
"cell_type": "code",
"execution_count": 130,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hello World!'"
]
},
"execution_count": 130,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'Yello World!'.replace('Y', 'H')"
]
},
{
"cell_type": "code",
"execution_count": 131,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Hello', 'World!']"
]
},
"execution_count": 131,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'Hello World!'.split(' ')"
]
},
{
"cell_type": "code",
"execution_count": 132,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hello World!'"
]
},
"execution_count": 132,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"' '.join(['Hello', 'World!'])"
]
},
{
"cell_type": "code",
"execution_count": 133,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'hello world!'"
]
},
"execution_count": 133,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'Hello World!'.lower()"
]
},
{
"cell_type": "code",
"execution_count": 134,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'HELLO WORLD!'"
]
},
"execution_count": 134,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'Hello World!'.upper()"
]
},
{
"cell_type": "code",
"execution_count": 135,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hello World!'"
]
},
"execution_count": 135,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"' Hello World! '.strip()"
]
},
{
"cell_type": "code",
"execution_count": 136,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hello World! '"
]
},
"execution_count": 136,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'Hello World!'.ljust(16)"
]
},
{
"cell_type": "code",
"execution_count": 137,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"' Hello World!'"
]
},
"execution_count": 137,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'Hello World!'.rjust(16)"
]
},
{
"cell_type": "code",
"execution_count": 138,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'00015'"
]
},
"execution_count": 138,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'15'.zfill(5)"
]
},
{
"cell_type": "code",
"execution_count": 139,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 139,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'Hello World!'.startswith('Hell')"
]
},
{
"cell_type": "code",
"execution_count": 140,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 140,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'Hello World!'.endswith('Hell')"
]
},
{
"cell_type": "code",
"execution_count": 141,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7"
]
},
"execution_count": 141,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'Hello World!'.find('orld')"
]
},
{
"cell_type": "code",
"execution_count": 142,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(False, True, True)"
]
},
"execution_count": 142,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s = '33'\n",
"s.isalpha(), s.isnumeric(), s.isalnum()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Format Strings\n",
"\n",
"Old style"
]
},
{
"cell_type": "code",
"execution_count": 143,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'some text 08.33'"
]
},
"execution_count": 143,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'some %-8s %05.2f' % ('text', 8.3333333)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"New style, since Python 2.6, 2.7"
]
},
{
"cell_type": "code",
"execution_count": 144,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'some text 08.33'"
]
},
"execution_count": 144,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'some {:8s} {:05.2f}'.format('text', 8.3333333)"
]
},
{
"cell_type": "code",
"execution_count": 145,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'|left | center | right|'"
]
},
"execution_count": 145,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'|{:<10s}|{:^10s}|{:>10s}|'.format('left', 'center', 'right')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Formatted String Literals, since Python 3.6"
]
},
{
"cell_type": "code",
"execution_count": 146,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'text 16.6666666'"
]
},
"execution_count": 146,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s1, f1 = 'text', 8.3333333\n",
"f'{s1} {f1*2}'"
]
},
{
"cell_type": "code",
"execution_count": 147,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'some text 08.33'"
]
},
"execution_count": 147,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f'some {s1:8s} {f1:05.2f}'"
]
},
{
"cell_type": "code",
"execution_count": 148,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'error 1e5b8dcc54'"
]
},
"execution_count": 148,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"error = 130385038420\n",
"f'error {error:x}'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Debugging Specifier, since Python 3.8"
]
},
{
"cell_type": "code",
"execution_count": 149,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"s1='text'\n",
"f1=8.3333333\n"
]
}
],
"source": [
"print(f'{s1=}\\n{f1=}') # instead of print(f's1={s1}\\nf1={f1}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"https://docs.python.org/3/library/string.html#string-formatting"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Modules"
]
},
{
"cell_type": "code",
"execution_count": 150,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import os, sys\n",
"from os import path\n",
"import os.path as p\n",
"from os import path as p"
]
},
{
"cell_type": "code",
"execution_count": 151,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'3.8.10 (default, Mar 13 2023, 10:26:41) \\n[GCC 9.4.0]'"
]
},
"execution_count": 151,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sys.version"
]
},
{
"cell_type": "code",
"execution_count": 152,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['/home/mvoe/ml_schulung/python_introduction',\n",
" '',\n",
" '/opt/ros/noetic/lib/python3/dist-packages',\n",
" '/usr/lib/python38.zip',\n",
" '/usr/lib/python3.8',\n",
" '/usr/lib/python3.8/lib-dynload',\n",
" '/home/mvoe/.local/lib/python3.8/site-packages',\n",
" '/usr/local/lib/python3.8/dist-packages',\n",
" '/usr/lib/python3/dist-packages',\n",
" '/usr/lib/python3.8/dist-packages']"
]
},
"execution_count": 152,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sys.path"
]
},
{
"cell_type": "code",
"execution_count": 153,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'/bin/bash'"
]
},
"execution_count": 153,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"os.getenv('SHELL')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Standard Library\n",
"\n",
"A complete list and documentation of the Python Standard Library can be found under\n",
"\n",
"https://docs.python.org/3/library/"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dates"
]
},
{
"cell_type": "code",
"execution_count": 154,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.0004055500030518"
]
},
"execution_count": 154,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import time\n",
"\n",
"t1 = time.time()\n",
"time.sleep(1)\n",
"t2 = time.time()\n",
"\n",
"t2 - t1"
]
},
{
"cell_type": "code",
"execution_count": 155,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2023-05-08 17:16:53.563268\n",
"2023 16\n",
"Monday 08 05 2023\n",
"2020-05-17 00:00:00\n"
]
}
],
"source": [
"import datetime\n",
"\n",
"d = datetime.datetime.now()\n",
"\n",
"print(d)\n",
"print(d.year, d.minute)\n",
"print(d.strftime('%A %d %m %Y'))\n",
"\n",
"d = datetime.datetime(2020, 5, 17)\n",
"\n",
"print(d)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"https://docs.python.org/3/library/datetime.html"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Math"
]
},
{
"cell_type": "code",
"execution_count": 156,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4.0\n",
"2\n",
"1\n",
"3.141592653589793\n"
]
}
],
"source": [
"import math\n",
"\n",
"print( math.sqrt(16) )\n",
"print( math.ceil(1.6) )\n",
"print( math.floor(1.6) )\n",
"print( math.pi )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Itertools"
]
},
{
"cell_type": "code",
"execution_count": 157,
"metadata": {},
"outputs": [],
"source": [
"import itertools\n",
"\n",
"l = [1,2,3]"
]
},
{
"cell_type": "code",
"execution_count": 158,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(1, 2), (1, 3), (2, 3)]"
]
},
"execution_count": 158,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list( itertools.combinations(l, 2) )"
]
},
{
"cell_type": "code",
"execution_count": 159,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]"
]
},
"execution_count": 159,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list( itertools.permutations(l, 2) )"
]
},
{
"cell_type": "code",
"execution_count": 160,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]"
]
},
"execution_count": 160,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list( itertools.product(l, repeat=2))"
]
},
{
"cell_type": "code",
"execution_count": 161,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 1, 2, 3, 1, 2, 3, 1]"
]
},
"execution_count": 161,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g = itertools.cycle(l)\n",
"[ next(g) for i in range(10) ]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### JSON"
]
},
{
"cell_type": "code",
"execution_count": 162,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'{\"name\": \"John\", \"age\": 32, \"city\": \"New York\"}'"
]
},
"execution_count": 162,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import json\n",
"\n",
"s = '{ \"name\":\"John\", \"age\":30, \"city\":\"New York\"}'\n",
"\n",
"d = json.loads(s)\n",
"\n",
"d['age'] = 32\n",
"\n",
"json.dumps(d)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 163,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"name\": \"John\",\n",
" \"age\": 30,\n",
" \"married\": true,\n",
" \"divorced\": false,\n",
" \"children\": [\n",
" \"Ann\",\n",
" \"Billy\"\n",
" ],\n",
" \"pets\": null,\n",
" \"cars\": [\n",
" {\n",
" \"model\": \"BMW 230\",\n",
" \"mpg\": 27.5\n",
" },\n",
" {\n",
" \"model\": \"Ford Edge\",\n",
" \"mpg\": 24.1\n",
" }\n",
" ]\n",
"}\n"
]
}
],
"source": [
"d = {\n",
" \"name\": \"John\",\n",
" \"age\": 30,\n",
" \"married\": True,\n",
" \"divorced\": False,\n",
" \"children\": (\"Ann\",\"Billy\"),\n",
" \"pets\": None,\n",
" \"cars\": [\n",
" {\"model\": \"BMW 230\", \"mpg\": 27.5},\n",
" {\"model\": \"Ford Edge\", \"mpg\": 24.1}\n",
" ]\n",
"}\n",
"\n",
"print(json.dumps(d, indent=4))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### RegEx"
]
},
{
"cell_type": "code",
"execution_count": 164,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['rai', 'pai']"
]
},
"execution_count": 164,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import re\n",
"\n",
"txt = \"The rain in Spain\"\n",
"re.findall(\".ai\", txt)"
]
},
{
"cell_type": "code",
"execution_count": 165,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The_rain_in_Spain\n"
]
}
],
"source": [
"txt = \"The rain in Spain\"\n",
"x = re.sub(\"\\s\", \"_\", txt)\n",
"print(x) "
]
},
{
"cell_type": "code",
"execution_count": 166,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<re.Match object; span=(0, 17), match='The rain in Spain'>"
]
},
"execution_count": 166,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"txt = \"The rain in Spain\"\n",
"res = re.search(\"^The.*Spain$\", txt)\n",
"res"
]
},
{
"cell_type": "code",
"execution_count": 167,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The rain in Spain\n"
]
}
],
"source": [
"txt = \"The rain in Spain\"\n",
"res = re.search(r\"\\bS\\w+\", txt)\n",
"print(res.string)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"https://docs.python.org/3/library/re.html"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Threading"
]
},
{
"cell_type": "code",
"execution_count": 168,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1683559019.532858 start service\n",
"1683559019.534409 start worker\n",
"1683559020.535930 worker alive <bound method Thread.is_alive of <Thread(t1, started 139752165590784)>>\n",
"1683559021.536339 end worker\n",
"1683559022.536315 end service\n",
"1683559022.536753 worker alive <bound method Thread.is_alive of <Thread(t1, stopped 139752165590784)>>\n"
]
}
],
"source": [
"import threading\n",
"import time\n",
" \n",
"def service():\n",
" print('%f start service' % time.time())\n",
" time.sleep(3)\n",
" print('%f end service' % time.time())\n",
"\n",
"def worker():\n",
" print('%f start worker' % time.time())\n",
" time.sleep(2)\n",
" print('%f end worker' % time.time())\n",
"\n",
"t1 = threading.Thread(name='t1', target=service)\n",
"t2 = threading.Thread(name='t2', target=worker)\n",
"\n",
"t1.start()\n",
"t2.start()\n",
"\n",
"\n",
"# check if thread is running\n",
"time.sleep(1)\n",
"print('%f worker alive %s' % (time.time(), str(t1.is_alive)))\n",
"\n",
"# wait for threads to complete\n",
"t2.join()\n",
"t1.join()\n",
"\n",
"print('%f worker alive %s' % (time.time(), str(t1.is_alive)))\n",
"\n",
"# in a script, without join, the process will wait until the threads exit, this can be avoided with t1.setDaemon(True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Basic Synchronization Using Lock\n",
"\n",
"- Avoides or solves race conditions\n",
"- Does what is also know as Mutex (MUTual EXclusion)\n",
"- Allows only one thread at a time in the read-modify-write section of the code"
]
},
{
"cell_type": "code",
"execution_count": 169,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"1683559022.550760 start worker\n",
"1683559022.551356 lock acquired via aquire and releas\n",
"1683559022.551427 star worker_with\n",
"1683559023.552321 increment x in worker\n",
"1683559023.552675 end worker1683559023.552708 lock acquired via context manager\n",
"\n",
"1683559023.552952 increment x in worker_with\n",
"1683559023.552994 end worker_with\n",
"4\n"
]
}
],
"source": [
"class SomeClass:\n",
" def __init__(self):\n",
" self.x = 1\n",
" self.lock = threading.Lock()\n",
"\n",
" def worker(self, v):\n",
" print('%f start worker' % (time.time()))\n",
" self.lock.acquire()\n",
" try:\n",
" print('%f lock acquired via aquire and releas' % (time.time()))\n",
" time.sleep(1)\n",
" print('%f increment x in worker' % (time.time()))\n",
" self.x += v\n",
" finally:\n",
" self.lock.release()\n",
" print('%f end worker' % (time.time()))\n",
"\n",
" def worker_with(self, v):\n",
" print('%f star worker_with' % (time.time()))\n",
" with self.lock:\n",
" print('%f lock acquired via context manager' % (time.time()))\n",
" print('%f increment x in worker_with' % (time.time()))\n",
" self.x += v\n",
" print('%f end worker_with' % (time.time()))\n",
" \n",
" def run(self):\n",
" print(self.x)\n",
" \n",
" t1 = threading.Thread(target=self.worker, args=(2,))\n",
" t2 = threading.Thread(target=self.worker_with, args=(1,))\n",
"\n",
" t1.start()\n",
" t2.start()\n",
"\n",
" t1.join()\n",
" t2.join()\n",
" \n",
" print(self.x)\n",
"\n",
"some_instance = SomeClass()\n",
"some_instance.run()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Deadlocks are situation in which a thread waits forever because a lock is never released"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"l = threading.Lock()\n",
"l.acquire()\n",
"l.acquire() # deadlock, the same thread will never call l.release()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Multiprocessing\n",
"\n",
"- Tasks run on different CPUs (in comparison to threading)\n",
"- The code that is executed gets pickled (serialized)"
]
},
{
"cell_type": "code",
"execution_count": 170,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1683559023.589827 process ['b', '4']\twaiting 2 seconds1683559023.589724 process ['a', '2']\twaiting 2 seconds\n",
"\n",
"1683559025.599079 process ['a', '2']\tDONE\n",
"1683559025.600315 process ['b', '4']\tDONE\n",
"1683559025.610953 process ['d', '8']\twaiting 2 seconds\n",
"1683559025.602715 process ['c', '6']\twaiting 2 seconds\n",
"1683559027.623771 process ['c', '6']\tDONE\n",
"1683559027.619619 process ['d', '8']\tDONE1683559027.634437 process ['e', '1']\twaiting 2 seconds\n",
"\n",
"1683559027.641892 process ['f', '3']\twaiting 2 seconds\n",
"1683559029.642821 process ['e', '1']\tDONE\n",
"1683559029.649174 process ['g', '5']\twaiting 2 seconds\n",
"1683559029.649390 process ['f', '3']\tDONE\n",
"1683559029.681002 process ['h', '7']\twaiting 2 seconds\n",
"1683559031.654783 process ['g', '5']\tDONE\n",
"1683559031.696316 process ['h', '7']\tDONE\n"
]
},
{
"data": {
"text/plain": [
"16"
]
},
"execution_count": 170,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import multiprocessing\n",
"import time\n",
"\n",
"data = (\n",
" ['a', '2'], ['b', '4'], ['c', '6'], ['d', '8'],\n",
" ['e', '1'], ['f', '3'], ['g', '5'], ['h', '7']\n",
")\n",
"\n",
"def mp_worker(inputs):\n",
" t = 2\n",
" print(\"%f process %s\\twaiting %s seconds\" % (time.time(), inputs, t))\n",
" time.sleep(int(t))\n",
" print(\"%f process %s\\tDONE\" % (time.time(), inputs))\n",
"\n",
"def mp_handler():\n",
" p = multiprocessing.Pool(2)\n",
" p.map(mp_worker, data)\n",
"\n",
"mp_handler()\n",
"\n",
"multiprocessing.cpu_count()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Profiling\n",
"\n",
"A profile is a set of statistics that describes how long and how often certain parts of the code are executed."
]
},
{
"cell_type": "code",
"execution_count": 171,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 45 function calls in 0.501 seconds\n",
"\n",
" Ordered by: cumulative time\n",
"\n",
" ncalls tottime percall cumtime percall filename:lineno(function)\n",
" 2 0.000 0.000 0.501 0.250 interactiveshell.py:3397(run_code)\n",
" 2 0.000 0.000 0.501 0.250 {built-in method builtins.exec}\n",
" 1 0.000 0.000 0.501 0.501 876735232.py:12(<module>)\n",
" 1 0.000 0.000 0.501 0.501 876735232.py:4(do_everything_you_want)\n",
" 5 0.501 0.100 0.501 0.100 {built-in method time.sleep}\n",
" 2 0.000 0.000 0.000 0.000 codeop.py:142(__call__)\n",
" 2 0.000 0.000 0.000 0.000 {built-in method builtins.compile}\n",
" 2 0.000 0.000 0.000 0.000 contextlib.py:238(helper)\n",
" 4 0.000 0.000 0.000 0.000 {built-in method builtins.next}\n",
" 2 0.000 0.000 0.000 0.000 contextlib.py:108(__enter__)\n",
" 2 0.000 0.000 0.000 0.000 contextlib.py:117(__exit__)\n",
" 1 0.000 0.000 0.000 0.000 876735232.py:14(<module>)\n",
" 2 0.000 0.000 0.000 0.000 contextlib.py:82(__init__)\n",
" 4 0.000 0.000 0.000 0.000 compilerop.py:165(extra_flags)\n",
" 2 0.000 0.000 0.000 0.000 traitlets.py:566(__get__)\n",
" 4 0.000 0.000 0.000 0.000 {built-in method builtins.getattr}\n",
" 2 0.000 0.000 0.000 0.000 traitlets.py:535(get)\n",
" 2 0.000 0.000 0.000 0.000 interactiveshell.py:3349(compare)\n",
" 2 0.000 0.000 0.000 0.000 interactiveshell.py:1222(user_global_ns)\n",
" 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}\n",
"\n",
"\n"
]
}
],
"source": [
"import cProfile\n",
"import time\n",
"\n",
"def do_everything_you_want():\n",
" for i in range(5):\n",
" k = i*3\n",
" time.sleep(0.1)\n",
"\n",
"p = cProfile.Profile()\n",
"p.enable()\n",
"\n",
"do_everything_you_want()\n",
"\n",
"p.disable()\n",
"\n",
"#p.print_stats(sort='time')\n",
"p.print_stats(sort='cumulative')\n",
"#p.print_stats(sort='name')\n",
"\n",
"# from command line\n",
"# python -m cProfile script.py"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Measuring execution time"
]
},
{
"cell_type": "code",
"execution_count": 172,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.0014683809131383896"
]
},
"execution_count": 172,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import timeit\n",
"\n",
"timeit.timeit('for i in range(1000): a = i*2', number=20)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Measuring execution time in Jupyter Notebook"
]
},
{
"cell_type": "code",
"execution_count": 173,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 3 µs, sys: 1 µs, total: 4 µs\n",
"Wall time: 7.63 µs\n"
]
}
],
"source": [
"%time\n",
"for i in range(1000): a = i*2"
]
},
{
"cell_type": "code",
"execution_count": 174,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"91.2 µs ± 3.59 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"for i in range(1000): a = i*2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# PIP\n",
"\n",
"PIP is a package manager for Python modules"
]
},
{
"cell_type": "code",
"execution_count": 175,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\r\n",
"Usage: \r\n",
" pip <command> [options]\r\n",
"\r\n",
"Commands:\r\n",
" install Install packages.\r\n",
" download Download packages.\r\n",
" uninstall Uninstall packages.\r\n",
" freeze Output installed packages in requirements format.\r\n",
" inspect Inspect the python environment.\r\n",
" list List installed packages.\r\n",
" show Show information about installed packages.\r\n",
" check Verify installed packages have compatible dependencies.\r\n",
" config Manage local and global configuration.\r\n",
" search Search PyPI for packages.\r\n",
" cache Inspect and manage pip's wheel cache.\r\n",
" index Inspect information available from package indexes.\r\n",
" wheel Build wheels from your requirements.\r\n",
" hash Compute hashes of package archives.\r\n",
" completion A helper command used for command completion.\r\n",
" debug Show information useful for debugging.\r\n",
" help Show help for commands.\r\n",
"\r\n",
"General Options:\r\n",
" -h, --help Show help.\r\n",
" --debug Let unhandled exceptions propagate outside the\r\n",
" main subroutine, instead of logging them to\r\n",
" stderr.\r\n",
" --isolated Run pip in an isolated mode, ignoring\r\n",
" environment variables and user configuration.\r\n",
" --require-virtualenv Allow pip to only run in a virtual environment;\r\n",
" exit with an error otherwise.\r\n",
" --python <python> Run pip with the specified Python interpreter.\r\n",
" -v, --verbose Give more output. Option is additive, and can be\r\n",
" used up to 3 times.\r\n",
" -V, --version Show version and exit.\r\n",
" -q, --quiet Give less output. Option is additive, and can be\r\n",
" used up to 3 times (corresponding to WARNING,\r\n",
" ERROR, and CRITICAL logging levels).\r\n",
" --log <path> Path to a verbose appending log.\r\n",
" --no-input Disable prompting for input.\r\n",
" --keyring-provider <keyring_provider>\r\n",
" Enable the credential lookup via the keyring\r\n",
" library if user input is allowed. Specify which\r\n",
" mechanism to use [disabled, import, subprocess].\r\n",
" (default: disabled)\r\n",
" --proxy <proxy> Specify a proxy in the form\r\n",
" scheme://[user:passwd@]proxy.server:port.\r\n",
" --retries <retries> Maximum number of retries each connection should\r\n",
" attempt (default 5 times).\r\n",
" --timeout <sec> Set the socket timeout (default 15 seconds).\r\n",
" --exists-action <action> Default action when a path already exists:\r\n",
" (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.\r\n",
" --trusted-host <hostname> Mark this host or host:port pair as trusted,\r\n",
" even though it does not have valid or any HTTPS.\r\n",
" --cert <path> Path to PEM-encoded CA certificate bundle. If\r\n",
" provided, overrides the default. See 'SSL\r\n",
" Certificate Verification' in pip documentation\r\n",
" for more information.\r\n",
" --client-cert <path> Path to SSL client certificate, a single file\r\n",
" containing the private key and the certificate\r\n",
" in PEM format.\r\n",
" --cache-dir <dir> Store the cache data in <dir>.\r\n",
" --no-cache-dir Disable the cache.\r\n",
" --disable-pip-version-check\r\n",
" Don't periodically check PyPI to determine\r\n",
" whether a new version of pip is available for\r\n",
" download. Implied with --no-index.\r\n",
" --no-color Suppress colored output.\r\n",
" --no-python-version-warning\r\n",
" Silence deprecation warnings for upcoming\r\n",
" unsupported Pythons.\r\n",
" --use-feature <feature> Enable new functionality, that may be backward\r\n",
" incompatible.\r\n",
" --use-deprecated <feature> Enable deprecated functionality, that will be\r\n",
" removed in the future.\r\n"
]
}
],
"source": [
"!pip --help"
]
},
{
"cell_type": "code",
"execution_count": 176,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Defaulting to user installation because normal site-packages is not writeable\n",
"Requirement already satisfied: numpy in /home/mvoe/.local/lib/python3.8/site-packages (1.24.2)\n"
]
}
],
"source": [
"!pip install numpy"
]
},
{
"cell_type": "code",
"execution_count": 177,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'1.24.2'"
]
},
"execution_count": 177,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"\n",
"np.__version__"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.8.10"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {
"height": "calc(100% - 180px)",
"left": "10px",
"top": "150px",
"width": "264.2px"
},
"toc_section_display": true,
"toc_window_display": true
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"oldHeight": 353.85,
"position": {
"height": "633.85px",
"left": "937px",
"right": "20px",
"top": "173px",
"width": "546px"
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"varInspector_section_display": "block",
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment