{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Information Retrieval Lab: Python Tutorial 🐍" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### (Re)sources:\n", "- [Python Data Science Handbook](https://jakevdp.github.io/PythonDataScienceHandbook/index.html) _by Jake VanderPlas (Code released under the MIT License)_\n", "- ➡️ [A Whirlwind Tour of Python](https://jakevdp.github.io/WhirlwindTourOfPython/) _by Jake VanderPlas released under the \"No Rights Reserved\" CC0 license (O’Reilly). Copyright 2016 O’Reilly Media, Inc., 978-1-491-96465-1_ __(What the present tutorial is mostly based on)__\n", "- [Python for Data Analysis](https://github.com/wesm/pydata-book) _by Wes McKinney (Code released under the MIT License)_\n", "- [Python 3.8 Documentation](https://docs.python.org/3.8/)\n", "\n", "__Obligatory Wikipedia excerpt:__\n", "\n", "\n", ">_Python is an interpreted, high-level and general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python's design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects._\n", "\n", "\n", ">_Python is dynamically typed and garbage-collected. It supports multiple programming paradigms, including structured (particularly, procedural), object-oriented, and functional programming. Python is often described as a \"batteries included\" language due to its comprehensive standard library._\n", "\n", ">_An important goal of Python's developers is keeping it fun to use. This is reflected in the language's name—a tribute to the British comedy group Monty Python—and in occasionally playful approaches to tutorials and reference materials, such as examples that refer to spam and eggs (from a famous Monty Python sketch) instead of the standard foo and bar._\n", "\n", ">_A common neologism in the Python community is pythonic, which can have a wide range of meanings related to program style. To say that code is pythonic is to say that it uses Python idioms well, that it is natural or shows fluency in the language, that it conforms with Python's minimalist philosophy and emphasis on readability. In contrast, code that is difficult to understand or reads like a rough transcription from another programming language is called unpythonic._\n", "\n", ">_The language's core philosophy is summarized in the document __The Zen of Python__ ([PEP](https://www.python.org/dev/peps) 20) \\[...\\]_" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#MAKE PAGE WIDER\n", "from IPython.display import HTML\n", "display(HTML(\"\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import this" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Python is Dynamically Typed" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = 1 # x is an integer\n", "x = 1. # x is now a float\n", "x = 'Hello, world!' # x is now a string\n", "x = [1, 2, 3, 4] # x is now a list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### A Python integer is not a C integer:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The __C struct__ behind the curtain:\n", "\n", "```C\n", "struct _longobject {\n", " long ob_refcnt;\n", " PyTypeObject *ob_type;\n", " size_t ob_size;\n", " long ob_digit[1];\n", "};\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Everything is an object (even functions)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x=1\n", "type(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x=1.\n", "type(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = 'Hello, World!'\n", "type(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = [1,2,3,4]\n", "type(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = [1, 2, 3]\n", "y = x" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x.append(4) # append 4 to x\n", "print(y) # y's list is modified as well!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = 'something else'\n", "print(y) # y is unchanged" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Arithmetic Operations\n", "Python implements seven basic binary arithmetic operators:\n", "\n", "| Operator | Name | Description |\n", "|--------------|----------------|--------------------------------------------------------|\n", "| ``a + b`` | Addition | Sum of ``a`` and ``b`` |\n", "| ``a - b`` | Subtraction | Difference of ``a`` and ``b`` |\n", "| ``a * b`` | Multiplication | Product of ``a`` and ``b`` |\n", "| ``a / b`` | True division | Quotient of ``a`` and ``b`` |\n", "| ``a // b`` | Floor division | Quotient of ``a`` and ``b``, removing fractional parts |\n", "| ``a % b`` | Modulus | Integer remainder after division of ``a`` by ``b`` |\n", "| ``a ** b`` | Exponentiation | ``a`` raised to the power of ``b`` |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Boolean Operations\n", "When working with Boolean values, Python provides operators to combine the values using the standard concepts of \"and\", \"or\", and \"not\". Predictably, these operators are expressed using the words `and`, `or`, and `not`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a=5\n", "b=2\n", "c=3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a==5 and b==2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a==5 and not c==5" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(a==5 or not b==6) and not c==3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a==5 or not b==6 and not c==3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(a==5 or not b==6) and not c==3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Identity and Membership Operators\n", "\n", "Python also contains prose-like operators to check for identity and membership.\n", "They are the following:\n", "\n", "| Operator | Description |\n", "|---------------|---------------------------------------------------|\n", "| ``a is b`` | True if ``a`` and ``b`` are identical objects |\n", "| ``a is not b``| True if ``a`` and ``b`` are not identical objects |\n", "| ``a in b`` | True if ``a`` is a member of ``b`` |\n", "| ``a not in b``| True if ``a`` is not a member of ``b`` |" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = [1,2,3]\n", "b = a\n", "b is a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = [1,2,3]\n", "b = [1,2,3]\n", "b is a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "1 in [1,2,3]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'foo' in [1,2,3]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "5 not in [1,2,3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Python Scalar Types\n", "\n", "| Type | Example | Description |\n", "|-------------|----------------|--------------------------------------------------------------|\n", "| ``int`` | ``x = 1`` | integers (i.e., whole numbers) |\n", "| ``float`` | ``x = 1.0`` | floating-point numbers (i.e., real numbers) |\n", "| ``complex`` | ``x = 1 + 2j`` | Complex numbers (i.e., numbers with real and imaginary part) |\n", "| ``bool`` | ``x = True`` | Boolean: True/False values |\n", "| ``str`` | ``x = 'abc'`` | String: characters or text |\n", "| ``NoneType``| ``x = None`` | Special object indicating nulls |" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Python Data Structures\n", "\n", "| Type Name | Example |Description |\n", "|-----------|---------------------------|---------------------------------------|\n", "| ``list`` | ``[1, 2, 3]`` | Ordered collection |\n", "| ``tuple`` | ``(1, 2, 3)`` | Immutable ordered collection |\n", "| ``dict`` | ``{'a':1, 'b':2, 'c':3}`` | Unordered (key,value) mapping |\n", "| ``set`` | ``{1, 2, 3}`` | Unordered collection of unique values |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Lists" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L = [2, 3, 5, 7]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Length of a list\n", "len(L)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Append a value to the end\n", "L.append(11)\n", "L" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Addition concatenates lists\n", "L + [13, 17, 19]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# sort() method sorts in-place\n", "L = [2, 5, 1, 6, 3, 4]\n", "L.sort()\n", "L" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L = [1, 'two', 3.14, [0, 3, 5]]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L = [2, 3, 5, 7, 11]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L[1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L[-1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L[-2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L[0:3]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L[:3]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L[3:]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L[:3]+L[3:]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "L.reverse() #This happen\n", "L" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l = [7, 1, 2]\n", "r = [9, 6, 8]\n", "l + r*2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(l)\n", "l.sort()\n", "print(l)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(r)\n", "sorted(r)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(r)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sorted?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dir(l)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Dicts" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers = {'one':1, 'two':2, 'three':3}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers['two']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers['ninety'] = 90" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "k = numbers.keys()\n", "k" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v = numbers.values()\n", "v" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dir(numbers)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for thing in numbers.keys():\n", " print(thing)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Sets" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "primes = {2, 3, 5, 7}\n", "odds = {1, 3, 5, 7, 9}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# union: items appearing in either\n", "primes | odds # with an operator\n", "primes.union(odds) # equivalently with a method" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# intersection: items appearing in both\n", "primes & odds # with an operator\n", "primes.intersection(odds) # equivalently with a method" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# difference: items in primes but not in odds\n", "primes - odds # with an operator\n", "primes.difference(odds) # equivalently with a method" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# symmetric difference: items appearing in only one set\n", "primes ^ odds # with an operator\n", "primes.symmetric_difference(odds) # equivalently with a method" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers = {'one', 'four', 'twenty'}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "k & numbers" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "k | numbers" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "k - numbers" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "k ^ numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Control Flow" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = -15\n", "\n", "if x == 0:\n", " print(x, \"is zero\")\n", "elif x > 0:\n", " print(x, \"is positive\")\n", "elif x < 0:\n", " print(x, \"is negative\")\n", "else:\n", " print(x, \"is unlike anything I've ever seen...\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Conditional Expression\n", "\n", "Introduced in PEP 308, and often referred to as a ternary operator:\n", "\n", "```python\n", "x = x_if_true if condition else x_if_false\n", "```\n", "which is the succint version of:\n", "\n", "```python\n", "if condition:\n", " x = x_if_true\n", "else:\n", " x = x_if_false\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sun_shining = False\n", "x = 35 if sun_shining else -4\n", "print(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def sun_shining(sun_shining=True):\n", " return 35 if sun_shining else -4" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sun_shining(True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sun_shining(False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### for loops" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for N in [2, 3, 5, 7]:\n", " print(N, end=' ') # print all on same line" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in range(10):\n", " print(i, end=' ')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for n in range(20):\n", " # if the remainder of n / 2 is 0, skip the rest of the loop\n", " if n % 2 == 0:\n", " continue\n", " print(n, end=' ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### while loops" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "i = 0\n", "while i < 10:\n", " print(i, end=' ')\n", " i += 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a, b = 0, 1\n", "amax = 100\n", "L = []\n", "\n", "while True:\n", " (a, b) = (b, a + b)\n", " if a > amax:\n", " break\n", " L.append(a)\n", "\n", "print(L)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Functions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def fibonacci(N, a=0, b=1):\n", " L = []\n", " while len(L) < N:\n", " a, b = b, a + b\n", " L.append(a)\n", " return L" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fibonacci(10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fibonacci(10, b=3, a=1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def catch_all(*args, **kwargs):\n", " print(\"args =\", args)\n", " print(\"kwargs = \", kwargs)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "catch_all(1, 2, 3, a=4, b=5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "catch_all('a', keyword=2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "inputs = (1, 2, 3)\n", "keywords = {'pi': 3.14}\n", "\n", "catch_all(*inputs, **keywords)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "add = lambda x, y: x + y\n", "add(1, 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "things = [\"cat\", \"apple\", \"boat\"]\n", "sorted(things) # alphabetically, upper case first" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sorted(things, key=lambda x: len(x))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Standard Library" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Math" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import math\n", "\n", "math.log2(1024)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "math.log(math.e)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "math.cos(math.pi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Random" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import random as rnd ## you can re-name imported modules\n", "\n", "rnd.randint(1, 6) ## Here, the end points are both included" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "things = ['cat', 'apple', 'boat']\n", "rnd.choice(things)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### urllib" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "## Modules can have sub-modules\n", "import urllib.request as rq\n", "\n", "response = rq.urlopen(\"http://en.wikipedia.org/wiki/Python\")\n", "\n", "print(response.read(151).decode('utf8'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### itertools" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import itertools\n", "\n", "perms = itertools.permutations([1, 2, 3], r=2)\n", "# r-length tuples, all possible orderings, no repeated elements\n", "# default r: length of the iterable\n", "\n", "for p in perms:\n", " print(p)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "combs = itertools.combinations([1, 2, 3], r=2)\n", "# r-length tuples, in sorted order, no repeated elements\n", "\n", "print(list(combs))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Comprehensions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### List Comprehensions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "[n for n in range(11)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "perms = itertools.permutations([1, 2, 3], r=2)\n", "[p for p in perms]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "perms = itertools.permutations([1, 2, 3], r=2)\n", "[thing for thing in perms]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "[n**2 for n in range(11)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "[n for n in range(11) if n%3] # n%3 is shorthand for n%3!=0" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "[n if n%2 else -n for n in range(11) if n%3] " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "[(n,n) if n%2 else (-n,9) for n in range(11) if n%3] " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Dictionary Comprehensions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "list_of_tuples = [(n,n) if n%2 else (-n,9) for n in range(11) if n%3] \n", "{a:b for a,b in list_of_tuples}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "{v:k for k,v in numbers.items()}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Set Comprehensions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "{a%4 for a in range(1000)}" ] }, { "cell_type": "markdown", "source": [ "#### Lambdas\n", "Lambdas can be used to create anonymous functions." ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "sum_lambda = lambda x, y: x+y\n", "\n", "sum_lambda(3,2)" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "#### Map, Filter, Reduce\n", "\n", "Supports `map`, `filter`, and `reduce` functions.\n", "All three can be replaced with List Comprehensions or loops, but often provide a more elegant solution.\n", "Keep in mind that they return a generator by default, so we have to cast them back into a list to actually apply the transformation." ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "list(map(lambda x: x+1, [1,2,3,4,5]))" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "list(filter(lambda x: x % 2 == 0, [1,2,3,4,5]))" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "# in Python3, reduce() isn't a built-in function anymore\n", "# and has to be imported from the functools module\n", "from functools import reduce\n", "\n", "reduce(lambda x, y: x+y, [1,2,3,4,5])" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } } ], "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.8.6" } }, "nbformat": 4, "nbformat_minor": 4 }