{ "metadata": { "name": "", "signature": "sha256:1462b52e33d18a1bbe7b2b50ccffc5d463c2e7bda4c6e33efd51b064a6c40e19" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Introduction to Python (A crash course)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For those of you that know python, this aims to refresh your memory. For those of you that don't know python -- but do know programming -- this class aims to give you an idea how python is similar/different with your favorite programming language. " ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Printing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "From the interactive python environment:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Hello World\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello World\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "From a file:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#!/usr/bin/env python\n", "\n", "print \"Hello World!\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello World!\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Standard I/O" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Writing to standard out:\n", "\n", "print \"Python is awesome!\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Python is awesome!\n" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "# Reading from standard input and output to standard output\n", "\n", "name = raw_input(\"What is your name?\")\n", "print name" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "What is your name?evimaria\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "evimaria\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Data types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Basic data types:\n", "\n", "1. Strings\n", "2. Integers\n", "3. Floats\n", "4. Booleans\n", "\n", "These are all objects in Python. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "#String\n", "a = \"apple\"\n", "type(a)\n", "#print type(a)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "str" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "#Integer \n", "b = 3\n", "type(b)\n", "#print type(b)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "int" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "#Float \n", "c = 3.2\n", "type(c)\n", "#print type(c)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "float" ] } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "#Boolean\n", "d = True\n", "type(d)\n", "#print type(d)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "bool" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python **doesn't require explicitly declared variable types** like C and other languages. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pay special attention to assigning floating point values to variables or you may get values you do not expect in your programs." ] }, { "cell_type": "code", "collapsed": false, "input": [ "14/b" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "4" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "14/c" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "4.375" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you divide an integer by an integer, it will return an answer rounded to the nearest integer. If you want a floating point answer, one of the numbers must be a float. Simply appending a decimal point will do the trick:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "14./b" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "4.666666666666667" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "String manipulation will be very important for many of the tasks we will do. Therefore let us play around a bit with strings." ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Concatenating strings\n", "\n", "a = \"Hello\" # String\n", "b = \" World\" # Another string\n", "print a + b # Concatenation" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello World\n" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "# Slicing strings\n", "\n", "a = \"World\"\n", "\n", "print a[0]\n", "print a[-1]\n", "print \"World\"[0:4]\n", "print a[::-1]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "W\n", "d\n", "Worl\n", "dlroW\n" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "# Popular string functions\n", "a = \"Hello World\"\n", "print \"-\".join(a)\n", "print a.startswith(\"Wo\")\n", "print a.endswith(\"rld\")\n", "print a.replace(\"o\",\"0\").replace(\"d\",\"[)\").replace(\"l\",\"1\")\n", "print a.split()\n", "print a.split('o')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "H-e-l-l-o- -W-o-r-l-d\n", "False\n", "True\n", "He110 W0r1[)\n", "['Hello', 'World']\n", "['Hell', ' W', 'rld']\n" ] } ], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Strings are an example of an **imutable** data type. Once you instantiate a string you cannot change any characters in it's set. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "string = \"string\"\n", "string[-1] = \"y\" #Here we attempt to assign the last character in the string to \"y\"" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'str' object does not support item assignment", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\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[0mstring\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"string\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mstring\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"y\"\u001b[0m \u001b[0;31m#Here we attempt to assign the last character in the string to \"y\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment" ] } ], "prompt_number": 15 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Whitespace in Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python uses indents and whitespace to group statements together. To write a short loop in C, you might use:\n", "\n", " ```c\n", " for (i = 0, i < 5, i++){\n", " printf(\"Hi! \\n\");\n", " }\n", " ```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python does not use curly braces like C, so the same program as above is written in Python as follows:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for i in range(5):\n", " print \"Hi \\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hi \n", "\n", "Hi \n", "\n", "Hi \n", "\n", "Hi \n", "\n", "Hi \n", "\n" ] } ], "prompt_number": 16 }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you have nested for-loops, there is a further indent for the inner loop." ] }, { "cell_type": "code", "collapsed": false, "input": [ "for i in range(3):\n", " for j in range(3):\n", " print i, j\n", " \n", " print \"This statement is within the i-loop, but not the j-loop\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 0\n", "0 1\n", "0 2\n", "This statement is within the i-loop, but not the j-loop\n", "1 0\n", "1 1\n", "1 2\n", "This statement is within the i-loop, but not the j-loop\n", "2 0\n", "2 1\n", "2 2\n", "This statement is within the i-loop, but not the j-loop\n" ] } ], "prompt_number": 17 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "File I/O" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Writing to a file\n", "with open(\"example.txt\", \"w\") as f:\n", " f.write(\"Hello World! \\n\")\n", " f.write(\"How are you? \\n\")\n", " f.write(\"I'm fine.\")" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "# Reading from a file\n", "with open(\"example.txt\", \"r\") as f:\n", " data = f.readlines()\n", " for line in data:\n", " words = line.split()\n", " print words" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['Hello', 'World!']\n", "['How', 'are', 'you?']\n", "[\"I'm\", 'fine.']\n" ] } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "# Count lines and words in a file\n", "lines = 0\n", "words = 0\n", "the_file = \"example.txt\"\n", "\n", "with open(the_file, 'r') as f:\n", " for line in f:\n", " lines += 1\n", " words += len(line.split())\n", "print \"There are %i lines and %i words in the %s file.\" % (lines, words, the_file)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "There are 3 lines and 7 words in the example.txt file.\n" ] } ], "prompt_number": 20 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Lists, Tuples, Sets and Dictionaries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Number and strings alone are not enough! we need data types that can hold multiple values." ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Lists: " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists are **mutable** or able to be altered. Lists are a collection of data and that data can be of differing types." ] }, { "cell_type": "code", "collapsed": false, "input": [ "groceries = []\n", "\n", "# Add to list\n", "groceries.append(\"oranges\") \n", "groceries.append(\"meat\")\n", "groceries.append(\"asparangus\")\n", "\n", "# Access by index\n", "print groceries[2]\n", "print groceries[0]\n", "\n", "# Find number of things in list\n", "print len(groceries)\n", "\n", "# Sort the items in the list\n", "groceries.sort()\n", "print groceries\n", "\n", "# List Comprehension\n", "veggie = [x for x in groceries if x is not \"meat\"]\n", "print veggie\n", "\n", "# Remove from list\n", "groceries.remove(\"asparangus\")\n", "print groceries\n", "\n", "#The list is mutable\n", "groceries[0] = 2\n", "print groceries" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "asparangus\n", "oranges\n", "3\n", "['asparangus', 'meat', 'oranges']\n", "['asparangus', 'oranges']\n", "['meat', 'oranges']\n", "[2, 'oranges']\n" ] } ], "prompt_number": 21 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "List Comprehension" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Recall the mathematical notation:\n", "\n", "$$L_1 = \\left\\{x^2 : x \\in \\{0\\ldots 9\\}\\right\\}$$\n", "\n", "$$L_2 = \\left(1, 2, 4, 8,\\ldots, 2^{12}\\right)$$\n", "\n", "$$M = \\left\\{x \\mid x \\in L_1 \\text{ and } x \\text{ is even}\\right\\}$$\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "L1 = [x**2 for x in range(10)]\n", "L2 = [2**i for i in range(13)]\n", "L3 = [x for x in L1 if x % 2 == 0]\n", "print L1\n", "print L2 \n", "print L3" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n", "[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096]\n", "[0, 4, 16, 36, 64]\n" ] } ], "prompt_number": 22 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Prime numbers with list comprehension" ] }, { "cell_type": "code", "collapsed": false, "input": [ "noprimes = [j for i in range(2, 8) for j in range(i*2, 50, i)]\n", "print noprimes\n", "primes = [x for x in range(2, 50) if x not in noprimes]\n", "print primes" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 10, 15, 20, 25, 30, 35, 40, 45, 12, 18, 24, 30, 36, 42, 48, 14, 21, 28, 35, 42, 49]\n", "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]\n" ] } ], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": [ "primes = [x for x in range(2, 50) if x not in [j for i in range(2, 8) for j in range(i*2, 50, i) ]]\n", "print primes" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]\n" ] } ], "prompt_number": 24 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Tuples: " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tuples are an **immutable** type. Like strings, once you create them, you cannot change them. It is their immutability that allows you to use them as keys in dictionaries. However, they are similar to lists in that they are a collection of data and that data can be of differing types. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Tuple grocery list\n", "\n", "groceries = ('orange', 'meat', 'asparangus', 2.5, True)\n", "\n", "print groceries\n", "\n", "#print groceries[2]\n", "\n", "#groceries[2] = 'milk'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "('orange', 'meat', 'asparangus', 2.5, True)\n" ] } ], "prompt_number": 25 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Sets: " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A set is a sequence of items that cannot contain duplicates. They handle operations like sets in mathematics." ] }, { "cell_type": "code", "collapsed": false, "input": [ "numbers = range(10)\n", "evens = [2, 4, 6, 8]\n", "\n", "evens = set(evens)\n", "numbers = set(numbers)\n", "\n", "# Use difference to find the odds\n", "odds = numbers - evens\n", "\n", "print odds\n", "\n", "# Note: Set also allows for use of union (|), and intersection (&)\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "set([0, 1, 3, 5, 7, 9])\n" ] } ], "prompt_number": 26 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Dictionaries: " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A dictionary is a map of keys to values. **Keys must be unique**." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# A simple dictionary\n", "\n", "simple_dic = {'cs591': 'data-mining tools'}\n", "\n", "# Access by key\n", "print simple_dic['cs591']" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "data-mining tools\n" ] } ], "prompt_number": 27 }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "# A longer dictionary\n", "classes = {\n", " 'cs591': 'data-mining tools',\n", " 'cs565': 'data-mining algorithms'\n", "}\n", "\n", "# Check if item is in dictionary\n", "print 'cs530' in classes\n", "\n", "# Add new item\n", "classes['cs530'] = 'algorithms'\n", "print classes['cs530']\n", "\n", "# Print just the keys\n", "print classes.keys()\n", "\n", "# Print just the values\n", "print classes.values()\n", "\n", "# Print the items in the dictionary\n", "print classes.items()\n", "\n", "# Print dictionary pairs another way\n", "for key, value in classes.items():\n", " print key, value" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "False\n", "algorithms\n", "['cs530', 'cs591', 'cs565']\n", "['algorithms', 'data-mining tools', 'data-mining algorithms']\n", "[('cs530', 'algorithms'), ('cs591', 'data-mining tools'), ('cs565', 'data-mining algorithms')]\n", "cs530 algorithms\n", "cs591 data-mining tools\n", "cs565 data-mining algorithms\n" ] } ], "prompt_number": 28 }, { "cell_type": "code", "collapsed": false, "input": [ "# Complex Data structures\n", "# Dictionaries inside a dictionary!\n", "\n", "professors = {\n", " \"prof1\": {\n", " \"name\": \"Evimaria Terzi\",\n", " \"department\": \"Computer Science\",\n", " \"research interests\": [\"algorithms\", \"data mining\", \"machine learning\",]\n", " },\n", " \"prof2\": {\n", " \"name\": \"Chris Dellarocas\",\n", " \"department\": \"Management\",\n", " \"interests\": [\"market analysis\", \"data mining\", \"computational education\",],\n", " }\n", "}\n", "\n", "for prof in professors:\n", " print professors[prof][\"name\"]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Chris Dellarocas\n", "Evimaria Terzi\n" ] } ], "prompt_number": 29 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Iterators and Generators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can loop over the elements of a list using **for**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for i in [1,2,3,4]:\n", " print i" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n", "2\n", "3\n", "4\n" ] } ], "prompt_number": 30 }, { "cell_type": "markdown", "metadata": {}, "source": [ "When we use **for** for dictionaries it loops over the keys of the dictionary" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for k in {'evimaria': 'terzi', 'aris': 'anagnostopoulos'}:\n", " print k" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "aris\n", "evimaria\n" ] } ], "prompt_number": 31 }, { "cell_type": "markdown", "metadata": {}, "source": [ "When we use **for** for strings it loops over the letters of the string" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for l in 'python is magic':\n", " print l" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "p\n", "y\n", "t\n", "h\n", "o\n", "n\n", " \n", "i\n", "s\n", " \n", "m\n", "a\n", "g\n", "i\n", "c\n" ] } ], "prompt_number": 32 }, { "cell_type": "markdown", "metadata": {}, "source": [ "All these are **iterable** objects" ] }, { "cell_type": "code", "collapsed": false, "input": [ "list({'evimaria': 'terzi', 'aris': 'anagnostopoulos'})" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 33, "text": [ "['aris', 'evimaria']" ] } ], "prompt_number": 33 }, { "cell_type": "code", "collapsed": false, "input": [ "list('python is magic')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 34, "text": [ "['p', 'y', 't', 'h', 'o', 'n', ' ', 'i', 's', ' ', 'm', 'a', 'g', 'i', 'c']" ] } ], "prompt_number": 34 }, { "cell_type": "code", "collapsed": false, "input": [ "print '-'.join('evimaria')\n", "print '-'.join(['a','b','c'])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "e-v-i-m-a-r-i-a\n", "a-b-c\n" ] } ], "prompt_number": 35 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Function **iter** takes as input an iterable object and returns an iterator" ] }, { "cell_type": "code", "collapsed": false, "input": [ "i = iter('magic')\n", "print i\n", "print i.next()\n", "print i.next()\n", "print i.next()\n", "print i.next()\n", "print i.next()\n", "print i.next()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "m\n", "a\n", "g\n", "i\n", "c\n" ] }, { "ename": "StopIteration", "evalue": "", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mStopIteration\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 6\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mStopIteration\u001b[0m: " ] } ], "prompt_number": 36 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Many functions take iterators as inputs" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = [x for x in range(10)]\n", "print sum(iter(a))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "45\n" ] } ], "prompt_number": 37 }, { "cell_type": "markdown", "metadata": {}, "source": [ "**generators** are functions that produce sequences of results (and not a single value)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def func(n):\n", " for i in range(n):\n", " yield i" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 38 }, { "cell_type": "code", "collapsed": false, "input": [ "g = func(10)\n", "print g\n", "print g.next()\n", "print g.next()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "0\n", "1\n" ] } ], "prompt_number": 39 }, { "cell_type": "code", "collapsed": false, "input": [ "def demonstrate(n):\n", " print 'begin execution of the function'\n", " for i in range(n):\n", " print 'before yield'\n", " yield i*i\n", " print 'after yield'" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 40 }, { "cell_type": "code", "collapsed": false, "input": [ "g = demonstrate(5)\n", "print g.next()\n", "print g.next()\n", "print g.next()\n", "print g.next()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "begin execution of the function\n", "before yield\n", "0\n", "after yield\n", "before yield\n", "1\n", "after yield\n", "before yield\n", "4\n", "after yield\n", "before yield\n", "9\n" ] } ], "prompt_number": 41 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Combining everything you learned about iterators and generators" ] }, { "cell_type": "code", "collapsed": false, "input": [ "g = (x for x in range(10))\n", "print g\n", "print sum(g)\n", "y = [x for x in range(10)]\n", "print y" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " at 0x1026caf50>\n", "45\n", "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n" ] } ], "prompt_number": 42 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Functions" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def displayperson(name,age):\n", " print \"My name is \"+ name +\" and I am \"+age+\" years old.\"\n", " return\n", " \n", "displayperson(\"Bob\",\"40\")" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "My name is Bob and I am 40 years old.\n" ] } ], "prompt_number": 43 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Lambda functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python supports the creation of anonymous functions (i.e. functions that are not bound to a name) at runtime, using a construct called \"lambda\"." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def f (x): return x**2\n", "print f(8)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "64\n" ] } ], "prompt_number": 44 }, { "cell_type": "code", "collapsed": false, "input": [ "g = lambda x: x**2\n", "print g(8)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "64\n" ] } ], "prompt_number": 45 }, { "cell_type": "code", "collapsed": false, "input": [ "f = lambda x, y : x + y\n", "print f(2,3)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] } ], "prompt_number": 46 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The above pieces of code are equivalent to each other! Note that there is no ``return\" statement in the lambda function. A lambda function does not need to be assigned to variable, but it can be used within the code wherever a function is expected." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def multiply (n): return lambda x: x*n\n", " \n", "f = multiply(2)\n", "g = multiply(6)\n", "print f(10), g(10)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "20 60\n" ] } ], "prompt_number": 47 }, { "cell_type": "code", "collapsed": false, "input": [ "multiply(3)(30)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 48, "text": [ "90" ] } ], "prompt_number": 48 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "The map() function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The advantage of the lambda operator can be seen when it is used in combination with the map() function.\n", "map() is a function with two arguments: \n", "\n", "**r = map(func,s)**\n", "\n", "**func** is a function and **s** is a sequence (e.g., a list). **map** returns a sequence that applies function **func** to all the elements of **s**." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def dollar2euro(x):\n", " return 0.89*x\n", "def euro2dollar(x):\n", " return 1.12*x\n", "\n", "amounts= (100, 200, 300, 400)\n", "dollars = map(dollar2euro, amounts)\n", "print dollars" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[89.0, 178.0, 267.0, 356.0]\n" ] } ], "prompt_number": 49 }, { "cell_type": "code", "collapsed": false, "input": [ "amounts= (100, 200, 300, 400)\n", "euros = map(euro2dollar, amounts)\n", "print euros" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[112.00000000000001, 224.00000000000003, 336.00000000000006, 448.00000000000006]\n" ] } ], "prompt_number": 50 }, { "cell_type": "code", "collapsed": false, "input": [ "map(lambda x: 0.89*x, amounts)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 51, "text": [ "[89.0, 178.0, 267.0, 356.0]" ] } ], "prompt_number": 51 }, { "cell_type": "markdown", "metadata": {}, "source": [ "**map** can also be applied to more than one lists as long as they are of the same size and type" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = [1,2,3,4,5]\n", "b = [-1,-2,-3, -4, -5] \n", "c = [10, 20 , 30, 40, 50]\n", "\n", "l1 = map(lambda x,y: x+y, a,b)\n", "print l1\n", "l2 = map (lambda x,y,z: x-y+z, a,b,c)\n", "print l2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[0, 0, 0, 0, 0]\n", "[12, 24, 36, 48, 60]\n" ] } ], "prompt_number": 52 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "The filter() function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The function **filter(function, list)** filters out all the elements of a **list**, for which the function **function** returns True. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "nums = [i for i in range(100)]\n", "print nums" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]\n" ] } ], "prompt_number": 53 }, { "cell_type": "code", "collapsed": false, "input": [ "even = filter(lambda x: x%2==0 and x!=0, nums)\n", "print even" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]\n" ] } ], "prompt_number": 54 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "The reduce() function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The function **reduce(function, list)** sequentially applies the function **function** to the elements of the **list**. The output of **reduce(function,list)** is a single value. For example if list = [a1,a2,a3,...,a10], then the first step of \n", "**reduce(function, list)** will return **[function(a1,a2),a3,...,a10]**, and so on." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print reduce(lambda x,y: x+y, [x for x in range(10)])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "45\n" ] } ], "prompt_number": 55 }, { "cell_type": "code", "collapsed": false, "input": [ "print reduce (lambda x,y: x if x>y else y, [1, 15, 26, -27])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "26\n" ] } ], "prompt_number": 56 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Libraries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python is a high-level open-source language. But the _Python world_ is inhabited by many packages or libraries that provide useful things like array operations, plotting functions, and much more. We can (and we should) import libraries of functions to expand the capabilities of Python in our programs. \n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import random\n", "myList = [2, 109, False, 10, \"data\", 482, \"mining\"]\n", "random.choice(myList)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 57, "text": [ "109" ] } ], "prompt_number": 57 }, { "cell_type": "code", "collapsed": false, "input": [ "from random import shuffle\n", "x = [[i] for i in range(10)]\n", "shuffle(x)\n", "print x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[[2], [0], [6], [4], [8], [7], [1], [5], [9], [3]]\n" ] } ], "prompt_number": 58 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "APIs" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Getting data from an API\n", "\n", "import requests\n", "\n", "width = '200'\n", "height = '300'\n", "#response = requests.get('http://placekitten.com/' + width + '/' + height)\n", "response = requests.get('http://lorempixel.com/400/200/sports/1/')\n", "\n", "print response\n", "\n", "with open('image.jpg', 'wb') as f:\n", " f.write(response.content)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 68 }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import Image\n", "Image(filename=\"image.jpg\")" ], "language": "python", "metadata": {}, "outputs": [ { "jpeg": "/9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcg\nSlBFRyB2NjIpLCBxdWFsaXR5ID0gNzAK/9sAQwAKBwcIBwYKCAgICwoKCw4YEA4NDQ4dFRYRGCMf\nJSQiHyIhJis3LyYpNCkhIjBBMTQ5Oz4+PiUuRElDPEg3PT47/9sAQwEKCwsODQ4cEBAcOygiKDs7\nOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7/8AAEQgAyAGQ\nAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMF\nBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkq\nNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqi\no6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV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iq27mpYPuvVc9aFuJkjHim5oPSgKTTEF\nKue1OWL1qVVobGkTRt8gFPy3YUkaY5NS4rIshmY+SaSNiUApZ1Ijao4c7Rin0DqS4PvSYJIAH6VJ\nEhd8VMLcrKOeKlsdhVgYQMTU0cQEQBHappQFtSAefpVTzj0NRdsq1iYfKuBUUQZpzU8KCSMndVYS\neRKxNCBhPCxlxTZYWQAmke9LtnNQSXEkowKtXJdiwQQm7I4qRLuUqAAMVDHGTHl2FKJFVNqqD9KQ\nx0hEp+fOapQ8TOoz1q1EpeXJWmwRj7XKSBxTWgmiFfLVBzzTWdMdaKKtIm5DEf33FJKP3jUUVXUn\noMRVEgLdM0lxtMmV6UUU+ouhDSgUUVQh6rUgoopDDIoP3zRRSGS6fCLi9Xd/qY/nkP8Asj/P61Yu\nLkRxvcMB5833B/cX/P8Ank0UUfasG0TKJyc0lFFWZlhoDGik9TyacgyRRRWd9DS1iYCoZEJPFFFJ\nFMaybUJNV+9FFaIzkFTRR70Lehoooewo7khjAjNIicZooqLmlh5QYpuOaKKEBNbgYYVXKc0UULcH\nsSRRhutTiEY6UUVLeo0KEQU7AxxRRSGODYAwKcH9jRRSGNl+aJuDS2MG9Mmiik3oFtS3FblXJ7fW\npSyAgdT9aKKz3K2Ec5RuKgWNGYFyAB60UU0BHLcCOQJH0z2qKfLN9aKKvYkatq7+v5VYEYhAHzbv\nYUUUm2wSJCjumdxqK3QlyCPzoopdCupaClRwoFZ+WFzKooooj1Bn/9k=\n", "metadata": {}, "output_type": "pyout", "prompt_number": 69, "text": [ "" ] } ], "prompt_number": 69 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python is a high-level open-source language. But the _Python world_ is inhabited by many packages or libraries that provide useful things like array operations, plotting functions, and much more. We can (and we should) import libraries of functions to expand the capabilities of Python in our programs. \n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Code for setting the style of the notebook\n", "from IPython.core.display import HTML\n", "def css_styling():\n", " styles = open(\"theme/custom.css\", \"r\").read()\n", " return HTML(styles)\n", "css_styling()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\r\n", "\r\n", "\r\n", "\r\n", "" ], "metadata": {}, "output_type": "pyout", "prompt_number": 61, "text": [ "" ] } ], "prompt_number": 61 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 61 } ], "metadata": {} } ] }