{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to Python for the Image processing class" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Comments (not evaluated lines)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "# this is a comment" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Integers and floats" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i = 5\n", "type(i)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = 5.\n", "type(f)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n" ] }, { "data": { "text/plain": [ "int" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "res = i + 1\n", "print(res)\n", "type(res)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[6, 4, 10, 2.5]\n" ] } ], "source": [ "i=5\n", "res = [i+1,i-1,i*2,i/2]\n", "print(res)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Integer division and modulo operator:" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 1]\n" ] } ], "source": [ "i =5\n", "res = [i // 2,i %2]\n", "print(res)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Power - operator, square root: " ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[16, 2.0]\n" ] } ], "source": [ "i = 4\n", "res = [i ** 2,i**(1/2)]\n", "print(res)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set to-operators: +=, -=, * =, /=, ** =, //= " ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7\n" ] } ], "source": [ "i = 5\n", "i += 2\n", "print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Boolean operators: True, False, and, or, not, !=, ==, <, <=, >, >=" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True False\n" ] } ], "source": [ "b1 = True\n", "b2 = 3==4\n", "print(b1, b2)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print(b1 and b2)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print(b1 or b2)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print(b1 and not b2)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print(b1 != b2)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print(3<3)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print(3<=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Range: range(end), range(start,end), range(start, end, offset)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range(0, 5)" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "range(5)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4]" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(range(5)) # all numbers smaller than 5 starting at 0" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4]\n" ] } ], "source": [ "nums = list(range(1,5)) # all numbers smaller than 5 starting at 1\n", "print(nums)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-10, -40, -70]" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(range(-10,-100,-30))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## For-loops with the range operator" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "55\n" ] } ], "source": [ "j = 0\n", "N = 10\n", "for i in range(1,N+1):\n", " j += i \n", "print(j)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## For-loop over a list:" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for i in nums:\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 1, 4, 9, 16]\n" ] } ], "source": [ "nums = [0, 1, 2, 3, 4]\n", "squares = []\n", "for x in nums:\n", " squares.append(x ** 2)\n", "print(squares) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List-comprehension: more compact writing with \"list comprehension\" (in Matlab even simpler: squares = nums.^2)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 1, 4, 9, 16]\n" ] } ], "source": [ "nums = [0, 1, 2, 3, 4]\n", "squares = [x ** 2 for x in nums]\n", "print(squares) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Advanced list comprension with if-statement:" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 4, 16]\n" ] } ], "source": [ "even_squares = [x ** 2 for x in nums if x % 2 == 0]\n", "print(even_squares)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 4, 16]\n" ] } ], "source": [ "even_squares = [x ** 2 for x in range(5) if x % 2 == 0]\n", "print (even_squares)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Simplification: odd x odd = odd and even x even = even:" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 4, 16]\n" ] } ], "source": [ "even_squares = [x ** 2 for x in range(0,5,2)]\n", "print (even_squares)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example: Fibonacci-sequence" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946]\n" ] } ], "source": [ "a = 1\n", "b = 1\n", "res = [a,b]\n", "while b < 10**4:\n", " aa = a \n", " a = b \n", " b = aa+b\n", " res.append(b)\n", "print(res)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## as function:\n", "Defining functions with def (Procedures - functions without return statement)." ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "def fib(n):\n", " a = 1\n", " b = 1\n", " res = [a,b]\n", " for i in range(n):\n", " aa = a \n", " a = b \n", " b = aa+b\n", " res.append(b)\n", " return res " ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fib(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "functional can be treated as variable (Python is a functional programming language)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]\n" ] } ], "source": [ "f = fib\n", "print(f(10))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions with one return parameter:" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "def square(par=0):\n", " s = par*par\n", " return s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions with multiple return parameters as a pair:" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [], "source": [ "def squareAndCube(par=0):\n", " s = square(par)\n", " cube = s * par\n", " return (s,cube)" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4, 8)" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "squareAndCube(2)" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "8\n" ] } ], "source": [ "s,c = squareAndCube(2)\n", "print(s)\n", "print(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "take only second return value:" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "27" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "squareAndCube(3)[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "pass - parameter (Python does not know empty blocks)" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [], "source": [ "def to_implement():\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Input parameters of functions" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [], "source": [ "def calculator(arg1=0,arg2=0,op='+'):\n", " result = 0\n", " if (op =='+'):\n", " result = arg1+arg2\n", " elif (op=='*'):\n", " result = arg1*arg2\n", " return result" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "50\n", "0\n", "5\n", "3\n" ] } ], "source": [ "# provide all arguments \n", "print(calculator(10,5,'*'))\n", "\n", "# call function with default parameters:\n", "print(calculator())\n", "\n", "# jump second argument\n", "print(calculator(5,op='+'))\n", "\n", "# give arguments in arbitrary order\n", "print(calculator(arg1= 1, op='*',arg2=3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Python is a functional language, you can provide functions as parameters" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "-4\n" ] } ], "source": [ "def plus(arg1,arg2):\n", " return arg1+arg2\n", "\n", "def times(arg1,arg2):\n", " return arg1*arg2\n", "\n", "def doTheStuff(arg1,arg2,func):\n", " return func(arg1,arg2)\n", "\n", "print(doTheStuff(1,2,plus))\n", "print(doTheStuff(2,-2,times))" ] } ], "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.6.7" } }, "nbformat": 4, "nbformat_minor": 2 }