{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# An Example" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "def maximum(a, b):\n", " if a < b:\n", " return b\n", " else:\n", " return a" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum(42, 666)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "333.0" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum(42, 666) / 2" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "m = maximum(42, 666) / 2" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "333.0" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Sidenote: Pure Beauty" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "function" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(maximum)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum(42, 666)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = maximum\n", "a(42, 666)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Parameters and Types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Aufruf mit ``int`` ist ok:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum(42, 666)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Aufruf mit ``int`` und ``float`` auch:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum(42, 6.666)" ] }, { "cell_type": "code", "execution_count": 24, "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 Exception as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "666" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "maximum(42, int('666'))" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "invalid literal for int() with base 10: 'xxx'\n" ] } ], "source": [ "try:\n", " maximum(42, int('xxx'))\n", "except Exception as e:\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Default Parameters" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "def say_hello(name, greeting):\n", " print(f'{greeting}, {name}') # f-String, since Python 3.6\n", " # print(greeting + ', ' + name)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Seas, Jörg\n" ] } ], "source": [ "say_hello('Jörg', 'Seas')" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "def say_hello(name, greeting='Hello'):\n", " print(f'{greeting}, {name}') # f-String, since Python 3.6" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, Jörg\n" ] } ], "source": [ "say_hello('Jörg')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Pitfalls" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "def f(i, x=[]):\n", " x.append(i)\n", " return x" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1]" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(1)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2]" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Keyword Arguments" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "def velocity(length_m, time_s):\n", " return length_m / time_s" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "33.333333333333336" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "velocity(100, 3)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.03" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "velocity(3, 100)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "33.333333333333336" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "velocity(time_s = 3, length_m = 100)" ] } ], "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 }