{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Informatik 1 - Biomedical Engineering\n", "## Tutor Session 1 - Syntax/Variables and Datatypes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Overview\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Newsgroups\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Datatypes and Operators\n", "https://docs.python.org/3/library/stdtypes.html \n", "https://learnxinyminutes.com/docs/python3/" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Numbers\n", "Numbers can either be integers or floats:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = 5\n", "type(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b = 1.45\n", "type(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Math works as expected:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a + 3 # addition" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "7 - a # subtraction" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "4 * b # multiplication" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "10 / 4 # division" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(10 / 2)\n", "print(type(10 / 2)) # division result is always a float" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "7 % 3 # modulo (-> check for divisibility)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "3**4 # exponentiation (3^4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "2**0.5 # works with floats too -> useful for roots" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Integers and floats can be cast to each other:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "int(10.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "float(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Booleans\n", "have capital first letters!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = True\n", "b = False\n", "print(type(a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Booleans can be combined/manipulated with \"and\", \"or\" and \"not\", and compared with \"==\":" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a and b" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a or b" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "not a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "not False" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a == True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Comparisons of numbers result in booleans:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "1 != 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "3 > 7" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "2 <= 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Strings\n", "Are used to store text and can be created with double quotes (\") or single quotes ('):\n", "\n", "## String Operators\n", "" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "string_1 = \"Hello\"\n", "string_2 = 'World'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Strings can be concatenated using '+':" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(string_1 + \" \" + string_2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Individual letters are accessed with square brackets:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "string_1[1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "string_2[1:4]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Their length can be found with 'len':" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "len(string_1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are different ways to put variables into a string:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "name = \"Tim\"\n", "age = 25\n", "print(\"Hi, my name is {} and I'm {} years old\".format(name, age))\n", "print(\"Hi, my name is {n} and I'm {a} years old\".format(a=age, n=name))\n", "print(f\"Hi, my name is {name} and I'm {age} years old\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "string = \"I im a string, I im a string\"\n", "print(\"length of string: \", len(string))\n", "print(\"Char on specific position: \", string[4])\n", "print(\"Part of string: \", string[3:7])\n", "number_string = \"23\"\n", "print(\"Is digit: \", number_string.isdigit(), type(number_string))\n", "#split examples\n", "print(string.split( ))\n", "print(string.split('i',2))\n", "print(string.split('a'))\n", "#find\n", "print(string.find(\"string\"))#returns the index\n", "#replace\n", "print(string.replace(\"string\",\"character list\"))\n", "#format\n", "print(\"I can count from {} to the incredible number {}\".format(1, 100))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Input\n", "The built-in function 'input' can be used to ask a user for input data:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "name = input(\"What's your name? \")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The result is always a string!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "age = input(\"Please enter your age: \")\n", "print(type(age))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So, if you need to calculate something with an entered number, use typecasts:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "age = int(input(\"Please enter your age: \"))\n", "print(type(age))\n", "new_age = age + 2\n", "print(f\"In two years you'll be {new_age}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## I/O\n", "Python build-in function open() opens a file and returns a File Object\n", "" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#reading whole file\n", "tf = open('textfile.txt', 'r')\n", "content = tf.read()\n", "tf.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#reading line by line\n", "tf = open('textfile.txt', 'r')\n", "for line in tf:\n", " print(line)\n", "tf.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#writing to a file\n", "tf = open('textfile.txt', 'w')\n", "for i in range(10):\n", " line = 'Line number ' + str(i+1) + '\\n'\n", " tf.write(line)\n", "tf.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lists\n", "Lists store sequences of arbitrary items:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "list_1 = [1, 2, 3.5, 4, \"hallo\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Empty lists are created with empty square brackets:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "empty_list = []" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "List items can be accessed with indices in square brackets (starting at 0!):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(list_1[0])\n", "print(list_1[3])\n", "print(list_1[-1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can add elements with 'append':" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "empty_list.append(1)\n", "empty_list.append(2)\n", "print(empty_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The length can be found like with strings:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "len(empty_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The leftmost position of an element in a lists is found with 'index':" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "list_2 = [1, 2, 3, 4, 3, 2, 1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(list_2.index(3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Control flow\n", "\n", "## if / else\n", "Kewords while programming: \"wenn/falls\"\n", "\n", "General form:\n", "```python\n", "if condition:\n", " # do something\n", "elif some_other_condition:\n", " # do something\n", "else:\n", " # do something\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "some_var = 5\n", "if some_var > 10:\n", " print(\"some_var is totally bigger than 10.\")\n", "elif some_var < 10: # This elif clause is optional.\n", " print(\"some_var is smaller than 10.\")\n", "else: # This is optional too.\n", " print(\"some_var is indeed 10.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## while\n", "\n", "Keywords while programming: \"solange\"\n", "\n", "General form:\n", "```python\n", "while condition:\n", " # do something\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "while condition:\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Example:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = 0\n", "while x < 3:\n", " print(x)\n", " x +=1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Controlling while" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### continue\n", "continue starts with the next iteration of the innermost loop" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = 0\n", "while x < 10:\n", " x += 1\n", " if x % 2 == 0:\n", " continue\n", " print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### break\n", "break stops the execution of the innermost loop altogether" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = 0\n", "while x < 10:\n", " x += 1\n", " if x % 4 == 0:\n", " break\n", " print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## for\n", "For-loops iterate over sequences. Most of the time they are simpler and cleaner than while-loops:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "i = 0\n", "while i < 5:\n", " print(i)\n", " i += 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in range(5):\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists and strings are sequences too!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for item in list_2:\n", " print(item)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "string_3 = \"Hello World\"\n", "for letter in string_3:\n", " print(letter)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "_break_ and _continue_ work here too:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for letter in string_3:\n", " if letter == \"l\":\n", " continue\n", " print(letter)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for letter in string_3:\n", " if letter == \"l\":\n", " break\n", " print(letter)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Ranges" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "_range_ can be used to easily create a sequence of numbers:\n", "```python\n", "range(start, end[, step])\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in range(5):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in range(10, 15):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in range (1, 10, 2):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in range(5, 0, -1):\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Programming examples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ask the user to enter ten numbers. Save them into a list and find the most frequent value." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compute the factorial of a numer read from ``stdin``." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Read words from ``stdin`` and only store them if they haven't been stored already. Do so until 5 words are stored." ] }, { "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.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }