{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python: Introduction\n", "\n", "This notebook provides basic information to get you started. You should read the [Python tutorial](https://docs.python.org/3/tutorial/) carefully if you want to master Python." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Comments" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# This is a python comment\n", "\n", "a = 3 # Another comment" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Numbers" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1234567891011121314151617181920" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1234567891011121314151617181920" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.14" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3.14" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2+3j)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 + 3j" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Booleans" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Strings" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello, world'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'hello, world'" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello, world'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"hello, world\"" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello,\\nworld'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"\"\"hello,\n", "world\"\"\"" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello,\\nworld'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'''hello,\n", "world'''" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "b'hello world'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b'hello world'" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello world'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "name = 'world'\n", "f'hello {name}'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### None" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "None" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic collection" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Lists" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1,2,3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tuples" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, 3)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(1,2,3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dictionaries" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'a': 1, 'b': 2, 'c': 3}" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{'a': 1, 'b': 2, 'c': 3}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Operators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Arithmetic operators" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 + 2" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 - 2" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 * 2" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.5" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 / 2" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 // 2" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "7 % 3" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "343" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "7**3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Relational operators" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(False, False)" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 < 2, 2 > 2" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 <= 2" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 == 2" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 != 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Logical operators" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not True" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True and False" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True or False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Bitwise" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "64" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "16 << 2" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "16 >> 2" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "16 | 4" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b10100'" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(16 | 4)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b0'" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(16 & 4)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b10100'" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(16 ^ 4)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b10000'" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(16)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Assignment and augmented assignment" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = 3" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": true }, "outputs": [], "source": [ "b = a" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 3)" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a, b" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = a + 3" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(6, 3)" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a, b" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = [1,2,3]" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": true }, "outputs": [], "source": [ "b = a" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "([1, 2, 3], [1, 2, 3])" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a, b" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a[1] = 12" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "([1, 12, 3], [1, 12, 3])" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a, b" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "collapsed": true }, "outputs": [], "source": [ "c = 1\n", "c += 2" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": true }, "outputs": [], "source": [ "c *= 3" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Identity and membership" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = 3\n", "b = 3" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a is b" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a == b" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4409367312, 4409367312)" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a), id(b)" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = [1,2,3]\n", "b = [1,2,3]" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a is b" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a == b" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4449129352, 4449130760)" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(a), id(b)" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 in [1,2,3]" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 not in [1,2,3]" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 in {'a': 1, 'b': 2}" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 in {1: 'a', 2: 'b'}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Naming conventions" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "collapsed": true }, "outputs": [], "source": [ "NAMES_FOR_CONSTANTS = None" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "collapsed": true }, "outputs": [], "source": [ "variable_and_function_names = None" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "collapsed": true }, "outputs": [], "source": [ "ClassNamesAreInCamelCase = None" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Control Flow" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### if-elif-else" ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "collapsed": true }, "outputs": [], "source": [ "day = 'sunny'\n", "\n", "if day == 'rainy':\n", " print('Bring an umbrella')" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Come as you are\n" ] } ], "source": [ "if day == 'rainy':\n", " print('Bring an umbrella')\n", "else:\n", " print('Come as you are')" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Bring sunglasses\n" ] } ], "source": [ "if day == 'rainy':\n", " print('Bring an umbrella')\n", "elif day == 'sunny':\n", " print('Bring sunglasses')\n", "elif day == 'snowing':\n", " print('Stay home')\n", "else:\n", " print('Come as you are')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The ternary operator" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "condition = 2 + 2 == 4\n", "True if condition else False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### for" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "4\n", "9\n" ] } ], "source": [ "for x in [1,2,3]:\n", " print(x**2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### while" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "2\n", "1\n" ] } ], "source": [ "n = 3\n", "while n > 0:\n", " print(n)\n", " n -= 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### break" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "9\n", "8\n" ] } ], "source": [ "n = 10\n", "while n > 0:\n", " print(n)\n", " n -= 1\n", " if n == 7:\n", " break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### continue" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "9\n", "25\n" ] } ], "source": [ "for i in [1,2,3,4,5]:\n", " if i % 2 == 0:\n", " continue\n", " print(i**2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Input and output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Reading text files" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing test_file.txt\n" ] } ], "source": [ "%%file test_file.txt\n", "This old man\n", "He played one\n", "He played knick-knack on my thumb" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The standard way is to treat the file handler as an iterator of lines" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This old man\n", "He played one\n", "He played knick-knack on my thumb" ] } ], "source": [ "with open('test_file.txt', 'r') as f:\n", " for line in f:\n", " print(line, end='')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### For small files, you may choose to read into memory as a single string" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'This old man\\nHe played one\\nHe played knick-knack on my thumb'" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with open('test_file.txt', 'r') as f:\n", " text = f.read()\n", "text" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Or as a list of lines" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['This old man\\n', 'He played one\\n', 'He played knick-knack on my thumb']" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with open('test_file.txt', 'r') as f:\n", " text = f.readlines()\n", "text" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Appending to text files" ] }, { "cell_type": "code", "execution_count": 79, "metadata": { "collapsed": true }, "outputs": [], "source": [ "with open('test_file.txt', 'a') as f:\n", " f.write('\\nThis old man\\n')\n", " f.write('He played two\\n')\n", " f.write('He played knick-knack on my shoe\\n')" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This old man\r\n", "He played one\r\n", "He played knick-knack on my thumb\r\n", "This old man\r\n", "He played two\r\n", "He played knick-knack on my shoe\r\n" ] } ], "source": [ "! cat test_file.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Writing to text files" ] }, { "cell_type": "code", "execution_count": 81, "metadata": { "collapsed": true }, "outputs": [], "source": [ "with open('test_file.txt', 'w') as f:\n", " f.write('This old man\\n')\n", " f.write('He played three\\n')\n", " f.write('He played knick-knack on my knee\\n')" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This old man\r\n", "He played three\r\n", "He played knick-knack on my knee\r\n" ] } ], "source": [ "! cat test_file.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Errors and exceptions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Examples of common errors and exceptions" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'prnt' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprnt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m123\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'prnt' is not defined" ] } ], "source": [ "prnt(123)" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m for (i in 1:5) {\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "for (i in 1:5) {\n", " print(i)\n", "}" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "ename": "IndentationError", "evalue": "expected an indented block (, line 2)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m print(i)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n" ] } ], "source": [ "for i in [1,23]:\n", "print(i)" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (, line 4)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m4\u001b[0m\n\u001b[0;31m else:\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "x = 1\n", "if x ==1:\n", " print(2)\n", " else:\n", " print(3)" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "list index out of range", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mxs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mxs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: list index out of range" ] } ], "source": [ "xs = [1,2,3]\n", "xs[3]" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "ename": "KeyError", "evalue": "'c'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0md\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m'a'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'b'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0md\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'c'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mKeyError\u001b[0m: 'c'" ] } ], "source": [ "d = {'a': 1, 'b': 2}\n", "d['c']" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "ename": "ZeroDivisionError", "evalue": "division by zero", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;36m1\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" ] } ], "source": [ "1/0" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "ename": "FileNotFoundError", "evalue": "[Errno 2] No such file or directory: 'foobar.txt'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'foobar.txt'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'r'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mline\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mline\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'foobar.txt'" ] } ], "source": [ "with open('foobar.txt', 'r'):\n", " for line in f:\n", " print(line)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### try-except-finally" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "division by zero\n" ] } ], "source": [ "n = 0\n", "try:\n", " x = 1/n\n", "except ZeroDivisionError as e:\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Can catch different types of exceptions" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "list index out of range\n" ] } ], "source": [ "try:\n", " xs = [1,2,3]\n", " xs[3]\n", "except ZeroDivisionError as e:\n", " print(e)\n", "except IndexError as e:\n", " print(e)\n", "except KeyError as e:\n", " print(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The `finally` clause is normally used for resource clean-up\n", "\n", "Code in `finally` block will be executed regardless of whetehr an exception occurred." ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "division by zero\n" ] } ], "source": [ "try:\n", " f = open('foobar.txt', 'w')\n", " f.write(str(1/0))\n", "except Exception as e:\n", " print(e)\n", "finally:\n", " f.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Clean-up" ] }, { "cell_type": "code", "execution_count": 94, "metadata": { "collapsed": true }, "outputs": [], "source": [ "! rm foobar.txt test_file.txt" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "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.1" } }, "nbformat": 4, "nbformat_minor": 2 }