{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Basics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Jupyter Notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Das Notebook tut aus Markdown HTML machen, und aus Python Code auch" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "print('hello world')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Syntax" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "for c in 'hello':\n", " print(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Batteries" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import random\n", "random.randrange(3, 7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tuple Unpacking" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "a, b = 1, 2" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "a, b = b, a" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ist das gleiche wie ..." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "(a, b) = (b, a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``for``" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "eins\n", "1.0\n" ] } ], "source": [ "liste = [1, 'eins', 1.0]\n", "for element in liste:\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In C: ``for (i=0; i 'drei'\n" ] } ], "source": [ "try:\n", " d['drei'] # KeyError: no such key\n", "except KeyError as e:\n", " print(type(e), e)" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "value = d.get('drei')\n", "print(value)" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "nix drin\n" ] } ], "source": [ "if d.get('drei') is None:\n", " print('nix drin')" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['drei'] = 3\n", "d['drei']" ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 111, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'drei' in d" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [], "source": [ "del d['drei'] # delete key 'drei'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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 }