{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "from IPython.display import Image\n", "from IPython.display import clear_output\n", "from IPython.display import FileLink, FileLinks\n", "import matplotlib.pylab as plt\n", "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Introduction to\n", "\n", "![title](img/python-logo-master-flat.png)\n", "\n", "### with Application to Bioinformatics\n", "\n", "#### - Day 1" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Who we are\n", "\n", "Nina | Malin\n", ":-------------------------:|:-------------------------:\n", "\"Drawing\" | \"Drawing\"\n", "\n", "\n", "Verena | Johan | Dimitrios\n", ":-------------------------:|:-------------------------:|:--------------------------:\n", "\"Drawing\" | \"Drawing\" | \"Drawing\"\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Who you are\n", "\n", "\"Drawing\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "data = pd.read_excel('C://Users/Nina/Documents/courses/Python_Beginner_Course/e - List of participants with invoice information.xlsx')\n", "\n", "labels = data['Programming/Scripting experience'].dropna().unique().tolist()\n", "labels2 = data['Position'].dropna().unique().tolist()\n", "slices = [count for count in data['Programming/Scripting experience'].value_counts()]\n", "slices2 = [count for count in data['Position'].value_counts()]\n", "explode = [0.05 for a in slices]\n", "explode2 = [0.05 for a in slices2]\n", "\n", "fig = plt.figure(figsize=(10,10))\n", "ax1 = fig.add_subplot(2,2,2)\n", "ax1.pie(slices, explode=explode, labels=labels, autopct='%1.0f%%',\n", " shadow=True, startangle=90)\n", "plt.title('Programming experience', size = 20)\n", "ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.\n", "\n", "ax2 = fig.add_subplot(2,2,3)\n", "ax2.pie(slices2, explode=explode2, labels=labels2, autopct='%1.0f%%',\n", " shadow=True, startangle=90)\n", "plt.title('Position', size = 20)\n", "ax2.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.\n", "\n", "#plt.show()\n", "#plt.close()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Schedule\n", "\n", "\"Drawing\"" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Check\n", "\n", "- Has everyone managed to install Python?\n", "- Have you managed to run the test script?\n", "- Have you installed notebooks? (optional)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### What is programming?\n", "\n", "Wikipedia: \n", "\n", "\"Computer programming is the process of building and designing an executable computer program for accomplishing a specific computing task\"" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "### What can we use it for?\n", "\n", "Endless possibilities! \n", "- reverse complement DNA\n", "- custom filtering of VCF files\n", "- plotting of results\n", "- all excel stuff!" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Why Python?\n", "\n", "### Typical workflow\n", "\n", "1. Get data\n", "2. Clean, transform data in spreadsheet\n", "3. Copy-paste, copy-paste, copy-paste\n", "4. Run analysis & export results\n", "7. Realise the columns were not sorted correctly\n", "8. Go back to step 2, Repeat\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "\"Drawing\"" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Python versions\n", "\n", " \n", "- Python 1.0 - January 1994 \n", "- Python 1.2 - April 10, 1995 \n", "- Python 1.3 - October 12, 1995 \n", "- Python 1.4 - October 25, 1996 \n", "- Python 1.5 - December 31, 1997 \n", "- Python 1.6 - September 5, 2000 \n", "- Python 2.0 - October 16, 2000 \n", "- Python 2.1 - April 17, 2001 \n", "- Python 2.2 - December 21, 2001 \n", "- Python 2.3 - July 29, 2003 \n", "- Python 2.4 - November 30, 2004 \n", "- Python 2.5 - September 19, 2006 \n", "- Python 2.6 - October 1, 2008 \n", "- Python 2.7 - July 3, 2010 \n", "\n", "\n", "- Python 3.0 - December 3, 2008 \n", "- Python 3.1 - June 27, 2009 \n", "- Python 3.2 - February 20, 2011 \n", "- Python 3.3 - September 29, 2012 \n", "- Python 3.4 - March 16, 2014 \n", "- Python 3.5 - September 13, 2015 \n", "- Python 3.6 - December 23, 2016 \n", "- Python 3.7 - June 27, 2018\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "\"Drawing\"" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Some good advice\n", "\n", "- 5 days to learn Python is not much\n", "- Amount of information will decrease over days\n", "- Complexity of tasks will increase over days\n", "- Read the error messages!\n", "- Save all your code\n", "\n", "How to seek help: \n", "- Google\n", "- Ask your neighbour\n", "- Ask an assistant\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "\"Drawing\"" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Example of a simple Python script" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "u is 2\n", "u is 3\n", "u is 4\n", "u is 5\n", "u is 6\n", "u is 7\n", "u is 8\n", "u is 9\n", "u is 10\n", "u is 11\n" ] } ], "source": [ "# A simple loop that adds 2 to a number\n", "i = 0\n", "while i < 10:\n", " u = i + 2\n", " print('u is',u)\n", " i += 1" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Example of a simple Python script\n", "\n", "\"Drawing\"\n", "\n", "### Comment\n", "\n", "All lines starting with # is interpreted by python as a comment and are not executed. Comments are important for documenting code and considered good practise when doing all types of programming" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Example of a simple Python script\n", "\n", "\"Drawing\"\n", "\n", "\n", "### Literals\n", "\n", "All literals have a type:\n", "\n", "- Strings (str)       ‘Hello’ “Hi”\n", "- Integers (int)\t    5\n", "- Floats (float)\t    3.14\n", "- Boolean (bool)     True or False" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Literals define values" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "'this is a string'\n", "\"this is also a string\"\n", "3 # here we can put a comment so we know that this is an integer\n", "3.14 # this is a float\n", "True # this is a boolean" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "### Collections" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "[3, 5, 7, 4, 99] # this is a list of integers\n", "\n", "('a', 'b', 'c', 'd') # this is a tuple of strings\n", "{'a', 'b', 'c'} # this is a set of strings\n", "{'a':3, 'b':5, 'c':7} # this is a dictionary with strings as keys and integers as values" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### What operations can we do with different values?\n", "\n", "That depends on their type:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "'a string another string'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'a string '+' another string'" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Type         Operations \n", "\n", "int           + - * / ** % // ... \n", "float           + - * / ** % // ... \n", "string           + *" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Example of a simple Python script\n", "\n", "\n", "\"Drawing\" \n", "### Identifiers\n", "\n", "Identifiers are used to identify a program element in the code. \n", "\n", "For example: \n", "- Variables\n", "- Functions\n", "- Modules\n", "- Classes " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Variables\n", "\n", "Used to store values and to assign them a name.\n", "\n", "Examples: \n", "- `i = 0`\n", "- `counter = 5`\n", "- `snpname = 'rs2315487'`\n", "- `snplist = ['rs21354', 'rs214569']` " ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "'rs56483rs56483'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "width = 23564\n", "height = 10\n", "\n", "snpname = 'rs56483'\n", "snplist = ['rs12345','rs458782']\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### How to correctly name a variable\n", "\n", "\n", "\n", "\"Drawing\" \n", "\n", "__Allowed:                       Not allowed:__ \n", "Var\\_name                       2save \n", "\\_total                           \\*important \n", "aReallyLongName                 Special% \n", "with\\_digit\\_2                       With   spaces \n", "dkfsjdsklut   _(well, allowed, but NOT recommended)_\n", "\n", "__NO special characters:__ \n", "\\+ - * $ % ; : , ? ! { } ( ) < > “ ‘ | \\ @" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Reserved keywords\n", "\n", "\"Drawing\" \n", "\n", "\n", "These words can not be used as variable names" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Summary\n", "\n", "- Comment your code!\n", "- Literals define values and can have different types (strings, integers, floats, boolean)\n", "- Values can be collected in lists, tuples, sets, and dictionaries\n", "- The operation that can be performed on a certain value depends on the type\n", "- Variables are identified by a name and are used to store a value or collections of values\n", "- Name your variables using descriptive words without special characters and reserved keywords\n", "\n", "__→ Notebook Day_1_Exercise_1 (~30 minutes)__" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## NOTE!\n", "\n", "### How to get help?\n", "\n", "- [Google](https://www.google.com/) and [Stack overflow](https://stackoverflow.com/) are your best friends!\n", "- Official [python documentation](https://docs.python.org/3/)\n", "- Ask your neighbour\n", "- Ask us" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Python standard library\n", "\n", "\"Drawing\" \n", "\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Example `print()` and `str()`\n", "\n", "\"Drawing\" \n", "\n", "__Note!__ \n", "Here we format everything to a string before printing it" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Python standard library\n", "\n", "\"Drawing\" \n", "\n" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for +: 'int' and 'str'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[0mnums\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m6\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m8\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0msum\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msnps\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" ] } ], "source": [ "width = 5\n", "height = 3.6\n", "snps = ['rs123', 'rs5487']\n", "snp = 'rs2546'\n", "active = True\n", "nums = [2,4,6,8,4,5,2]\n", "\n", "sum(snps)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## More on operations\n", "\n", "\"Drawing\" \n" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "23" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 4\n", "y = 3\n", "z = [2, 3, 6, 3, 9, 23]\n", "\n", "max(z)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Comparison operators\n", "\n", "\"Drawing\" \n", "\n", "Can be used on int, float, str, and bool. Outputs a boolean." ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 5.14\n", "y = 3.14\n", "\n", "y + 2 == x" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Logical operators\n", "\n", "\"Drawing\" \n", "\n", "\n", "\n", "## Membership operators\n", "\n", "\"Drawing\" \n" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [2,4,7,3,5,9]\n", "y = ['a','b','c']\n", "\n", "23 in x\n", "4 in x and 'd' in y" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "u is 2. Is this number even? True\n", "u is 3. Is this number even? False\n", "u is 4. Is this number even? True\n", "u is 5. Is this number even? False\n", "u is 6. Is this number even? True\n", "u is 7. Is this number even? False\n", "u is 8. Is this number even? True\n", "u is 9. Is this number even? False\n", "u is 10. Is this number even? True\n", "u is 11. Is this number even? False\n" ] } ], "source": [ "# A simple loop that adds 2 to a number\n", "i = 0\n", "even = [2,4,6,8,10]\n", "while i < 10:\n", " u = i + 2\n", " print('u is '+str(u)+'. Is this number even? '+str(u in even))\n", " i += 1" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "u is 2. Is this number even and below 5? True\n", "u is 3. Is this number even and below 5? False\n", "u is 4. Is this number even and below 5? True\n", "u is 5. Is this number even and below 5? False\n", "u is 6. Is this number even and below 5? False\n", "u is 7. Is this number even and below 5? False\n", "u is 8. Is this number even and below 5? False\n", "u is 9. Is this number even and below 5? False\n", "u is 10. Is this number even and below 5? False\n", "u is 11. Is this number even and below 5? False\n" ] } ], "source": [ "# A simple loop that adds 2 to a number\n", "i = 0\n", "even = [2,4,6,8,10]\n", "while i < 10:\n", " u = i + 2\n", " print('u is '+str(u)+'. Is this number even and below 5? '+\\\n", " str(u in even and u < 5))\n", " i += 1" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Order of precedence\n", "\n", "There is an order of precedence for all operators:\n", "\n", "\"Drawing\" \n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Word of caution when using operators" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 5\n", "y = 7\n", "z = 2\n", "(x > 6 and y == 7) or z > 1\n", "\n", "x > 6 and (y == 7 or z > 1)\n", "(x > 6 and y == 7) or z > 1\n", "\n", "#x > 4 or y == 6 and z > 3\n", "#x > 4 or (y == 6 and z > 3)\n", "#(x > 4 or y == 6) and z > 3" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# BEWARE!\n", "x = 5\n", "y = 8\n", "\n", "x > 2 or xx == 6 and xxx == 6\n", "x > 42 or (y < 0 and someRandomVariable > 1000)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "__Python does short-circuit evaluation of operators__" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## More on sequences (For example strings and lists)\n", "\n", "Lists (and strings) are an ORDERED collection of elements where every element can be access through an index.\n", "\n", "\"Drawing\" \n" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "ename": "TypeError", "evalue": "'str' object does not support item assignment", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0ms\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'somelongrandomstring'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'S'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: 'str' object does not support item assignment" ] } ], "source": [ "l = [2,3,4,5,3,7,5,9]\n", "s = 'somelongrandomstring'\n", "\n", "s[0] = 'S'" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Mutable vs Immutable objects\n", "\n", "

\n", "\n", "Mutable objects can be altered after creation, while immutable objects can't.\n", "\n", "\n", "__Immutable objects:       Mutable objects:__ \n", "- `int`               • `list`\n", "- `float`                • `set`\n", "- `bool`                • `dict`\n", "- `str`\n", "- `tuple`\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Operations on mutable sequences\n", "\n", "\n", "\n", "\"Drawing\" " ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 10, 5, 6, 7, 8, 9]" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = [0,1,2,3,4,5,6,7,8,9]\n", "s.insert(5,10)\n", "\n", "s" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Summary\n", "\n", "- The python standard library has many built-in functions regularly used\n", "- Operators are used to carry out computations on different values\n", "- Three types of operators; comparison, logical, and membership\n", "- Order of precedence crucial!\n", "- Mutable object can be changed after creation while immutable objects cannot be changed\n", "\n", "

\n", "

\n", "\n", "__→ Notebook Day_1_Exercise_2 (~30 minutes)__\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Loops in Python" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "scrolled": true, "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "apple\n", "pear\n", "banana\n", "orange\n" ] } ], "source": [ "fruits = ['apple','pear','banana','orange']\n", "\n", "print(fruits[0])\n", "print(fruits[1])\n", "print(fruits[2])\n", "print(fruits[3])" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "apple\n", "pear\n", "banana\n", "orange\n" ] } ], "source": [ "fruits = ['apple','pear','banana','orange']\n", "\n", "for fruit in fruits:\n", " print(fruit)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "__Always remember to INDENT your loops!__" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Different types of loops" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "### `For` loop" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "apple\n", "pear\n", "banana\n", "orange\n", "end\n" ] } ], "source": [ "fruits = ['apple','pear','banana','orange']\n", "\n", "for fruit in fruits:\n", " print(fruit)\n", "print('end')" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "### `While` loop" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "apple\n", "banana\n" ] } ], "source": [ "fruits = ['apple','pear','banana','orange']\n", "\n", "i = 0\n", "while i < len(fruits):\n", " print(fruits[i])\n", " i = i + 2" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Different types of loops\n", "\n", "__`For` loop__\n", "\n", "Is a control flow statement that performs a fixed operation over a known amount of steps.\n", "\n", "__`While` loop__\n", "\n", "Is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition.\n", "\n", "

\n", "\n", "__Which one to use?__\n", "\n", "`For` loops better for simple iterations over lists and other iterable objects\n", "\n", "`While` loops are more flexible and can iterate an unspecified number of times\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Example of a simple Python script\n", "\n", "

\n", "\n", "\"Drawing\" " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "__→ Notebook Day_1_Exercise_3 (~20 minutes)__" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Conditional `if/else`  statements\n", "\n", "\n", "\"Drawing\" " ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Go shopping!\n" ] } ], "source": [ "shopping_list = ['bread', 'egg', 'butter', 'milk']\n", "\n", "if len(shopping_list) > 2:\n", " print('Go shopping!')\n", "else:\n", " print('Nah! I\\'ll do it tomorrow!')" ] }, { "cell_type": "code", "execution_count": 68, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Nah! I'll do it tomorrow!\n" ] } ], "source": [ "shopping_list = ['bread', 'egg', 'butter', 'milk']\n", "tired = True\n", "\n", "if len(shopping_list) > 7:\n", " if not tired:\n", " print('Go shopping!')\n", " else:\n", " print('Too tired, I\\'ll do it later')\n", "else:\n", " if not tired:\n", " print('Better get it over with today anyway')\n", " else:\n", " print('Nah! I\\'ll do it tomorrow!')" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "### This is an example of a nested conditional" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Putting everything into a Python script\n", "\n", "Any longer pieces of code that have been used and will be re-used SHOULD be saved\n", "\n", "Two options:\n", "- Save it as a text file and make it executable\n", "- Save it as a notebook file\n", "\n", "

\n", "\n", "__Examples__" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Things to remember when working with scripts\n", "\n", "- Put _#!/usr/bin/env python3_ in the beginning of the file\n", "- Make the file executable to run with `./script.py`\n", "- Otherwise run script with `python script.py`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Working on files" ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "apple\n", "pear\n", "banana\n", "orange\n" ] } ], "source": [ "fruits = ['apple','pear','banana','orange']\n", "\n", "for fruit in fruits:\n", " print(fruit)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "\"Drawing\" " ] }, { "cell_type": "code", "execution_count": 70, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "apple\n", "pear\n", "banana\n", "orange\n" ] } ], "source": [ "fh = open('../files/fruits.txt', 'r', encoding = 'utf-8')\n", "\n", "for line in fh:\n", " print(line.strip())\n", "\n", "fh.close()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Pause for additional useful methods:\n", "

\n", "\n", "`'string'.strip()`       Removes whitespace \n", "`'string'.split()`       Splits on whitespace into list " ] }, { "cell_type": "code", "execution_count": 74, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "text/plain": [ "['an', 'example', 'string', 'to', 'split', 'with', 'whitespace', 'in', 'end']" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = 'an example string to split with whitespace in end '\n", "sw = s.strip()\n", "sw\n", "l = sw.split()\n", "l\n", "l = s.strip().split()\n", "l" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "\"Drawing\" " ] }, { "cell_type": "code", "execution_count": 75, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "apple\n", "pear\n", "banana\n", "orange\n" ] } ], "source": [ "fh = open('../files/fruits.txt', 'r', encoding = 'utf-8')\n", "\n", "for line in fh:\n", " print(line.strip())\n", "\n", "fh.close()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Another example" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "\"Drawing\" \n", "How much money is spent on ICA?" ] }, { "cell_type": "code", "execution_count": 76, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Total amount spent on ICA is: 1186.71\n" ] } ], "source": [ "fh = open(\"../files/bank_statement.txt\", \"r\", encoding = \"utf-8\")\n", "\n", "total = 0\n", "\n", "for line in fh:\n", " expenses = line.strip().split() # split line into list\n", " store = expenses[0] # save what store\n", " price = float(expenses[1]) # save the price\n", " if store == 'ICA': # only count the price if store is ICA\n", " total = total + price\n", "fh.close()\n", "\n", "print('Total amount spent on ICA is: '+str(total)) " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Slightly more complex..." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "\"Drawing\" \n", "\n", "How much money is spent on ICA in September?" ] }, { "cell_type": "code", "execution_count": 77, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "fh = open(\"../files/bank_statement_extended.txt\", \"r\", encoding = \"utf-8\")\n", "\n", "total = 0\n", "\n", "for line in fh:\n", " if not line.startswith('store'):\n", " expenses = line.strip().split()\n", " store = expenses[0]\n", " year = expenses[1]\n", " month = expenses[2]\n", " day = expenses[3]\n", " price = float(expenses[4])\n", " if store == 'ICA' and month == '09': # store has to be ICA and month september\n", " total = total + price\n", "fh.close()\n", "\n", "out = open(\"../files/bank_statement_result.txt\", \"w\", encoding = \"utf-8\") # open a file for writing the results to\n", "out.write('Total amount spent on ICA in september is: '+str(total))\n", "out.close()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Summary\n", "\n", "- Python has two types of loops, `For` loops and `While` loops\n", "- Loops can be used on any iterable types and objects\n", "- `If/Else` statement are used when deciding actions depending on a condition that evaluates to a boolean\n", "- Several `If/Else` statements can be nested\n", "- Save code as notebook or text file to be run using python\n", "- The function `open()` can be used to read in text files\n", "- A text file is iterable, meaning it is possible to loop over the lines\n", "\n", "__→ Notebook Day_1_Exercise_4__" ] } ], "metadata": { "celltoolbar": "Slideshow", "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.2" }, "rise": { "height": 1024, "width": 1024 } }, "nbformat": 4, "nbformat_minor": 2 }