{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python: As a functional programming language\n", "Gonzalo Rios - grios@dim.uchile.cl\n", "\n", "https://docs.python.org/3/howto/functional.html" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:22:49.943036Z", "start_time": "2018-09-27T19:22:49.931758Z" } }, "outputs": [], "source": [ "def p(s):\n", " print(s)\n", "\n", "\n", "def Spam():\n", " p(\"spam\")\n", " p(\"and\")\n", " p(\"eggs...\")\n", "\n", "\n", "def hello(s):\n", " p(\"ham \" * s)\n", "\n", "\n", "Spam()\n", "hello(2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:25:42.099899Z", "start_time": "2018-09-27T19:25:42.095030Z" } }, "outputs": [], "source": [ "# Fibonacci series up to n\n", "def fib(n):\n", " \"\"\"Print a Fibonacci series up to n.\"\"\"\n", " a, b = 0, 1\n", " while a < n:\n", " print(a, end=' ')\n", " a, b = b, a + b\n", " print()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:25:42.512156Z", "start_time": "2018-09-27T19:25:42.506268Z" } }, "outputs": [], "source": [ "help(fib)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:25:45.213491Z", "start_time": "2018-09-27T19:25:45.210220Z" } }, "outputs": [], "source": [ "fib(1000)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:25:58.814212Z", "start_time": "2018-09-27T19:25:58.810541Z" } }, "outputs": [], "source": [ "f = fib\n", "f(10000)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:26:07.868302Z", "start_time": "2018-09-27T19:26:07.864436Z" } }, "outputs": [], "source": [ "r = fib(100)\n", "print(r)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:29:28.859683Z", "start_time": "2018-09-27T19:29:28.856959Z" } }, "outputs": [], "source": [ "# return Fibonacci series up to n\n", "def fib2(n):\n", " \"\"\"Return a list containing the Fibonacci series up to n.\"\"\"\n", " result = []\n", " a, b = 0, 1\n", " while a < n:\n", " result.append(a)\n", " a, b = b, a + b\n", " return result, \"hola\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:29:48.516222Z", "start_time": "2018-09-27T19:29:48.513233Z" } }, "outputs": [], "source": [ "r,t = fib2(100)\n", "print(r)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Recursion" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:30:53.568439Z", "start_time": "2018-09-27T19:30:53.565272Z" } }, "outputs": [], "source": [ "# recursive Fibonacci numbers\n", "def fib3(n):\n", " \"\"\"Return a list containing the first n Fibonacci numbers.\"\"\"\n", " if n is 0:\n", " return [0]\n", " elif n is 1:\n", " return [0, 1]\n", " else:\n", " result = fib3(n - 1)\n", " result.append(result[-1] + result[-2])\n", " return result" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:30:54.200193Z", "start_time": "2018-09-27T19:30:54.193648Z" } }, "outputs": [], "source": [ "fib3(100)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:31:36.674808Z", "start_time": "2018-09-27T19:31:36.670323Z" } }, "outputs": [], "source": [ "def factorial(x):\n", " if x == 1:\n", " return 1\n", " else: \n", " return x * factorial(x-1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:31:38.674518Z", "start_time": "2018-09-27T19:31:38.670436Z" } }, "outputs": [], "source": [ "factorial(970)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:35:01.693412Z", "start_time": "2018-09-27T19:35:01.689709Z" } }, "outputs": [], "source": [ "def is_even(x):\n", " if x == 0:\n", " return True\n", " elif x > 0:\n", " return is_odd(x-1)\n", " else:\n", " return is_odd(x+1)\n", " \n", "def is_odd(x):\n", " return not is_even(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:35:02.305941Z", "start_time": "2018-09-27T19:35:02.297507Z" } }, "outputs": [], "source": [ "is_odd(17)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:35:02.459360Z", "start_time": "2018-09-27T19:35:02.456034Z" } }, "outputs": [], "source": [ "is_even(23)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:35:22.861931Z", "start_time": "2018-09-27T19:35:22.854225Z" } }, "outputs": [], "source": [ "is_even(-10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Variables and Scope" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:36:00.255718Z", "start_time": "2018-09-27T19:36:00.249776Z" } }, "outputs": [], "source": [ "def plus1(variable):\n", " variable += 1\n", " return variable" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:36:21.906214Z", "start_time": "2018-09-27T19:36:21.903158Z" } }, "outputs": [], "source": [ "x = 7\n", "y = plus1(x)\n", "\n", "p(x)\n", "p(y)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:36:58.713788Z", "start_time": "2018-09-27T19:36:58.707980Z" } }, "outputs": [], "source": [ "def plus1x():\n", " x += 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:37:03.655109Z", "start_time": "2018-09-27T19:37:03.645517Z" } }, "outputs": [], "source": [ "p(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:37:07.271246Z", "start_time": "2018-09-27T19:37:07.260419Z" } }, "outputs": [], "source": [ "plus1x()\n", "p(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:37:24.622812Z", "start_time": "2018-09-27T19:37:24.618681Z" } }, "outputs": [], "source": [ "def plus1x():\n", " global x\n", " x += 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:37:25.095722Z", "start_time": "2018-09-27T19:37:25.091104Z" } }, "outputs": [], "source": [ "plus1x()\n", "p(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:40:28.713721Z", "start_time": "2018-09-27T19:40:28.706742Z" } }, "outputs": [], "source": [ "n = 2\n", "def plusn(variable):\n", " n = 1\n", " variable += n\n", " #n +=1\n", " return variable" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:40:29.753291Z", "start_time": "2018-09-27T19:40:29.750226Z" } }, "outputs": [], "source": [ "x = 7\n", "y = plusn(x)\n", "\n", "p(x)\n", "p(y)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:45:07.264849Z", "start_time": "2018-09-27T19:45:07.262411Z" } }, "outputs": [], "source": [ "def append1(variable):\n", " variable.append(1)\n", " #variable = [8,2]\n", " return variable" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:45:07.593776Z", "start_time": "2018-09-27T19:45:07.585269Z" } }, "outputs": [], "source": [ "x = [7]\n", "y = append1(x)\n", "\n", "p(x)\n", "p(y)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:45:09.173414Z", "start_time": "2018-09-27T19:45:09.170907Z" } }, "outputs": [], "source": [ "append1(x)\n", "p(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:48:40.376082Z", "start_time": "2018-09-27T19:48:40.373785Z" } }, "outputs": [], "source": [ "def append1x():\n", " x.append(1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:47:36.535431Z", "start_time": "2018-09-27T19:47:36.531308Z" } }, "outputs": [], "source": [ "append1x()\n", "p(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:51:48.804679Z", "start_time": "2018-09-27T19:51:48.798911Z" } }, "outputs": [], "source": [ "def append1z(z):\n", " z.append(1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:51:49.122222Z", "start_time": "2018-09-27T19:51:49.118032Z" } }, "outputs": [], "source": [ "append1z(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:51:49.464244Z", "start_time": "2018-09-27T19:51:49.457189Z" } }, "outputs": [], "source": [ "x" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:51:50.361182Z", "start_time": "2018-09-27T19:51:50.334723Z" } }, "outputs": [], "source": [ "globals()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:52:01.282534Z", "start_time": "2018-09-27T19:52:01.279173Z" } }, "outputs": [], "source": [ "locals() == globals()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:52:54.828017Z", "start_time": "2018-09-27T19:52:54.813767Z" } }, "outputs": [], "source": [ "def plusn(variable):\n", " print(locals())\n", " variable += n\n", " return variable\n", "\n", "plusn(8)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Arguments" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:54:02.669355Z", "start_time": "2018-09-27T19:54:02.666794Z" } }, "outputs": [], "source": [ "def maxN(x, *args):\n", " for y in args:\n", " if x < y:\n", " x = y\n", " return x" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:54:07.477101Z", "start_time": "2018-09-27T19:54:07.473672Z" } }, "outputs": [], "source": [ "maxN(100)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:54:10.746782Z", "start_time": "2018-09-27T19:54:10.743495Z" } }, "outputs": [], "source": [ "maxN(1, 3, 65, 6, 9)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T19:54:19.857859Z", "start_time": "2018-09-27T19:54:19.850469Z" } }, "outputs": [], "source": [ "p(1, 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:01:22.954911Z", "start_time": "2018-09-27T20:01:22.948299Z" } }, "outputs": [], "source": [ "def p(*args):\n", " print(*args)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:01:23.350840Z", "start_time": "2018-09-27T20:01:23.346200Z" } }, "outputs": [], "source": [ "p(1, 2, 'chao')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:01:23.698950Z", "start_time": "2018-09-27T20:01:23.694789Z" } }, "outputs": [], "source": [ "def my_func(x, y=7, *args, **kwargs):\n", " p(x, y, args, kwargs)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:01:24.135173Z", "start_time": "2018-09-27T20:01:24.122069Z" } }, "outputs": [], "source": [ "my_func()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:01:24.370121Z", "start_time": "2018-09-27T20:01:24.361864Z" } }, "outputs": [], "source": [ "my_func(0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:01:24.636470Z", "start_time": "2018-09-27T20:01:24.628390Z" } }, "outputs": [], "source": [ "my_func(0, 3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:01:24.960068Z", "start_time": "2018-09-27T20:01:24.956271Z" } }, "outputs": [], "source": [ "my_func(x=0, y=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:01:25.165708Z", "start_time": "2018-09-27T20:01:25.160461Z" } }, "outputs": [], "source": [ "my_func(y=0, x=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:01:25.372045Z", "start_time": "2018-09-27T20:01:25.367022Z" } }, "outputs": [], "source": [ "my_func(2, 3, 6)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:01:25.723736Z", "start_time": "2018-09-27T20:01:25.716724Z" } }, "outputs": [], "source": [ "my_func(2, 3, 6, 7)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:01:26.061791Z", "start_time": "2018-09-27T20:01:26.055704Z" } }, "outputs": [], "source": [ "my_func(2, y=3, 6)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:01:26.482042Z", "start_time": "2018-09-27T20:01:26.472926Z" } }, "outputs": [], "source": [ "my_func(2, 3, y=6)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:01:26.910401Z", "start_time": "2018-09-27T20:01:26.903792Z" } }, "outputs": [], "source": [ "my_func(2, y=3, z=6)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:01:27.250125Z", "start_time": "2018-09-27T20:01:27.243116Z" } }, "outputs": [], "source": [ "my_func(2, 3, 4, 5, z=6, w=7)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:01:27.701114Z", "start_time": "2018-09-27T20:01:27.693934Z" } }, "outputs": [], "source": [ "my_func(None, 3, 4, 5, 6, a=7, b=8)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Lambda and High-Order Functions" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:02:42.893947Z", "start_time": "2018-09-27T20:02:42.889767Z" } }, "outputs": [], "source": [ "pure_lambda = lambda x: 2*x*x\n", "\n", "pure_lambda(1.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:03:13.113053Z", "start_time": "2018-09-27T20:03:13.109189Z" } }, "outputs": [], "source": [ "n = 2\n", "impure_lambda = lambda x: n*x*x\n", "\n", "impure_lambda(1.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:03:15.119725Z", "start_time": "2018-09-27T20:03:15.116576Z" } }, "outputs": [], "source": [ "n = 3\n", "impure_lambda(1.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:03:31.618777Z", "start_time": "2018-09-27T20:03:31.615219Z" } }, "outputs": [], "source": [ "def add_n(x):\n", " return x + n\n", "\n", "add_n(1.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:03:44.087477Z", "start_time": "2018-09-27T20:03:44.085203Z" } }, "outputs": [], "source": [ "print(pure_lambda, add_n)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:04:19.308976Z", "start_time": "2018-09-27T20:04:19.301266Z" } }, "outputs": [], "source": [ "def apply_twice(func, arg):\n", " return func(func(arg))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:04:22.902395Z", "start_time": "2018-09-27T20:04:22.898424Z" } }, "outputs": [], "source": [ "apply_twice(pure_lambda, 1.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:04:35.912883Z", "start_time": "2018-09-27T20:04:35.909085Z" } }, "outputs": [], "source": [ "apply_twice(impure_lambda, 1.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:04:36.834772Z", "start_time": "2018-09-27T20:04:36.831608Z" } }, "outputs": [], "source": [ "apply_twice(add_n, 1.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:05:21.056778Z", "start_time": "2018-09-27T20:05:21.052490Z" } }, "outputs": [], "source": [ "apply_twice(lambda x: x**3, 1.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:05:28.573214Z", "start_time": "2018-09-27T20:05:28.569849Z" } }, "outputs": [], "source": [ "apply_twice(lambda x: n+x**3, 1.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:06:10.165260Z", "start_time": "2018-09-27T20:06:10.161379Z" } }, "outputs": [], "source": [ "def apply_twice_l(func):\n", " return lambda x: func(func(x))\n", "\n", "f2 = apply_twice_l(pure_lambda)\n", "f2(1.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:06:51.244130Z", "start_time": "2018-09-27T20:06:51.241737Z" } }, "outputs": [], "source": [ "def sum_n(n):\n", " return lambda x: n+x" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:06:59.671318Z", "start_time": "2018-09-27T20:06:59.667676Z" } }, "outputs": [], "source": [ "sum1 = sum_n(1)\n", "sum1(65.68)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:07:39.775701Z", "start_time": "2018-09-27T20:07:39.771289Z" } }, "outputs": [], "source": [ "def mult_n(n):\n", " def mult(x):\n", " return x*n\n", " return mult" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:07:45.569420Z", "start_time": "2018-09-27T20:07:45.566250Z" } }, "outputs": [], "source": [ "mult10 = mult_n(10)\n", "mult10(65.6)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:10:50.495170Z", "start_time": "2018-09-27T20:10:50.490086Z" } }, "outputs": [], "source": [ "z = 125\n", "def mult_nz(n):\n", " def mult(x):\n", " return x*n*z\n", " return mult" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:10:50.761435Z", "start_time": "2018-09-27T20:10:50.757889Z" } }, "outputs": [], "source": [ "mz = mult_nz(2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:10:51.109727Z", "start_time": "2018-09-27T20:10:51.103579Z" } }, "outputs": [], "source": [ "mz" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:10:57.314341Z", "start_time": "2018-09-27T20:10:57.305992Z" } }, "outputs": [], "source": [ "mz(1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:12:15.747580Z", "start_time": "2018-09-27T20:12:15.744049Z" } }, "outputs": [], "source": [ "max(-1,0,3,5, key=lambda x:abs(x-3))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:13:42.233600Z", "start_time": "2018-09-27T20:13:42.229311Z" } }, "outputs": [], "source": [ "max((-1,0),(3,5), key=lambda x:abs(x[0]-x[1]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Decorators" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:14:34.779523Z", "start_time": "2018-09-27T20:14:34.776824Z" } }, "outputs": [], "source": [ "def print_text():\n", " print(\"Hello world!\")\n", "print_text()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:15:11.888305Z", "start_time": "2018-09-27T20:15:11.884858Z" } }, "outputs": [], "source": [ "def decor(func):\n", " def wrap():\n", " print(\"\\n============\")\n", " func()\n", " print(\"============\\n\")\n", " return wrap" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:15:16.783595Z", "start_time": "2018-09-27T20:15:16.780508Z" } }, "outputs": [], "source": [ "print_text = decor(print_text)\n", "print_text" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:15:27.834833Z", "start_time": "2018-09-27T20:15:27.832024Z" } }, "outputs": [], "source": [ "print_text()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:17:23.068248Z", "start_time": "2018-09-27T20:17:23.063397Z" } }, "outputs": [], "source": [ "def decor2(func):\n", " def wrap():\n", " print(\"\\n------------\")\n", " func()\n", " print(\"------------\\n\")\n", " return wrap" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:17:23.416063Z", "start_time": "2018-09-27T20:17:23.408044Z" } }, "outputs": [], "source": [ "@decor2\n", "def print_text2():\n", " print(\"Hello world!\")\n", "\n", "print_text2()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:17:50.587875Z", "start_time": "2018-09-27T20:17:50.582977Z" } }, "outputs": [], "source": [ "@decor2\n", "def print_text3():\n", " print(\"Hell world!\")\n", "\n", "@decor\n", "@decor2\n", "def print_text4():\n", " print(\"Bye world!\")\n", "print_text4()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:22:16.853578Z", "start_time": "2018-09-27T20:22:16.851270Z" } }, "outputs": [], "source": [ "def decorsum1(func):\n", " return lambda : func() + 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:22:17.014794Z", "start_time": "2018-09-27T20:22:17.011837Z" } }, "outputs": [], "source": [ "def two():\n", " return 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:22:17.177433Z", "start_time": "2018-09-27T20:22:17.170931Z" } }, "outputs": [], "source": [ "two()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:22:17.300327Z", "start_time": "2018-09-27T20:22:17.297789Z" } }, "outputs": [], "source": [ "@decorsum1\n", "@decorsum1\n", "def two():\n", " return 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:22:17.480275Z", "start_time": "2018-09-27T20:22:17.476388Z" } }, "outputs": [], "source": [ "two()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Iterators" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:22:48.061439Z", "start_time": "2018-09-27T20:22:48.058158Z" } }, "outputs": [], "source": [ "for i in [1, 2, 3, 4]:\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:22:52.978033Z", "start_time": "2018-09-27T20:22:52.975392Z" } }, "outputs": [], "source": [ "for i in (1, 2, 3, 4):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:22:58.102443Z", "start_time": "2018-09-27T20:22:58.099499Z" } }, "outputs": [], "source": [ "for c in \"1234\":\n", " print(c)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:24:51.909815Z", "start_time": "2018-09-27T20:24:51.900367Z" } }, "outputs": [], "source": [ "for c in {1, 2, 4, 3}|{'two'}:\n", " print(c)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:26:03.416761Z", "start_time": "2018-09-27T20:26:03.409412Z" } }, "outputs": [], "source": [ "for c in {1:2, 3:4}:\n", " print(c)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:26:03.885453Z", "start_time": "2018-09-27T20:26:03.877886Z" } }, "outputs": [], "source": [ "for c,d in {1:2, 3:4}.items():\n", " print(c, d)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:26:11.814004Z", "start_time": "2018-09-27T20:26:11.810729Z" } }, "outputs": [], "source": [ "range(10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:26:23.437221Z", "start_time": "2018-09-27T20:26:23.433211Z" } }, "outputs": [], "source": [ "range(1, 10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:26:44.207873Z", "start_time": "2018-09-27T20:26:44.203919Z" } }, "outputs": [], "source": [ "r = range(1, 15, 3)\n", "r" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:26:53.235832Z", "start_time": "2018-09-27T20:26:53.232039Z" } }, "outputs": [], "source": [ "list(r)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:26:57.029836Z", "start_time": "2018-09-27T20:26:57.026008Z" }, "scrolled": false }, "outputs": [], "source": [ "min(r)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:26:59.600751Z", "start_time": "2018-09-27T20:26:59.597255Z" } }, "outputs": [], "source": [ "max(r)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:27:01.310236Z", "start_time": "2018-09-27T20:27:01.307081Z" } }, "outputs": [], "source": [ "sum(r)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:27:45.118437Z", "start_time": "2018-09-27T20:27:45.113345Z" } }, "outputs": [], "source": [ "[i for i in r]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:27:45.566891Z", "start_time": "2018-09-27T20:27:45.561780Z" } }, "outputs": [], "source": [ "[i**2 for i in r]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:27:47.317436Z", "start_time": "2018-09-27T20:27:47.311548Z" } }, "outputs": [], "source": [ "[i**2 for i in r if i**2%3==1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:28:01.478784Z", "start_time": "2018-09-27T20:28:01.474826Z" } }, "outputs": [], "source": [ "ff = lambda x: x%3\n", "[i**2 for i in r if ff(i*3)==0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:28:12.457096Z", "start_time": "2018-09-27T20:28:12.452194Z" } }, "outputs": [], "source": [ "r" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:28:47.793452Z", "start_time": "2018-09-27T20:28:47.784081Z" } }, "outputs": [], "source": [ "it = iter(r)\n", "it" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:28:48.039823Z", "start_time": "2018-09-27T20:28:48.034721Z" } }, "outputs": [], "source": [ "next(it)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:28:48.376093Z", "start_time": "2018-09-27T20:28:48.368126Z" } }, "outputs": [], "source": [ "for i in it:\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:29:05.838131Z", "start_time": "2018-09-27T20:29:05.834013Z" } }, "outputs": [], "source": [ "rit = reversed(r)\n", "rit" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:29:08.122751Z", "start_time": "2018-09-27T20:29:08.119947Z" } }, "outputs": [], "source": [ "for i in rit:\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:29:15.609771Z", "start_time": "2018-09-27T20:29:15.606572Z" } }, "outputs": [], "source": [ "enum = enumerate(r)\n", "enum" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:29:30.258931Z", "start_time": "2018-09-27T20:29:30.253423Z" } }, "outputs": [], "source": [ "for e,v in enum:\n", " print(e,v)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:31:07.840833Z", "start_time": "2018-09-27T20:31:07.837319Z" } }, "outputs": [], "source": [ "[i < 10 for i in r]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:31:10.577685Z", "start_time": "2018-09-27T20:31:10.574950Z" } }, "outputs": [], "source": [ "if all([i < 15 for i in r]):\n", " print(\"All lesser that 15\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:31:12.661187Z", "start_time": "2018-09-27T20:31:12.657619Z" } }, "outputs": [], "source": [ "if any([i > 5 for i in r]):\n", " print(\"Any larger than 5\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:31:27.744248Z", "start_time": "2018-09-27T20:31:27.737571Z" } }, "outputs": [], "source": [ "r = range(1000)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:31:33.880907Z", "start_time": "2018-09-27T20:31:33.869979Z" } }, "outputs": [], "source": [ "list(r)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Map - Filter" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:31:46.580419Z", "start_time": "2018-09-27T20:31:46.572312Z" } }, "outputs": [], "source": [ "r = range(1, 15, 3)\n", "r" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:32:04.838833Z", "start_time": "2018-09-27T20:32:04.834971Z" } }, "outputs": [], "source": [ "f = lambda x: x+5\n", "map(f, r)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:32:25.837918Z", "start_time": "2018-09-27T20:32:25.834066Z" } }, "outputs": [], "source": [ "list(map(f, r))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:32:32.924261Z", "start_time": "2018-09-27T20:32:32.918191Z" } }, "outputs": [], "source": [ "[i**2 for i in map(f,r)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:32:41.734940Z", "start_time": "2018-09-27T20:32:41.732633Z" } }, "outputs": [], "source": [ "def upper(s):\n", " return s.upper()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:32:47.406178Z", "start_time": "2018-09-27T20:32:47.402989Z" } }, "outputs": [], "source": [ "list(map(upper, ['sentence', 'fragment']))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:43:13.480670Z", "start_time": "2018-09-27T20:43:13.475044Z" } }, "outputs": [], "source": [ "ft = lambda x: x%2==0\n", "fil = filter(ft, map(f, r))\n", "fil" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:43:15.590619Z", "start_time": "2018-09-27T20:43:15.587207Z" } }, "outputs": [], "source": [ "list(fil)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generators" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:36:01.845128Z", "start_time": "2018-09-27T20:36:01.842656Z" } }, "outputs": [], "source": [ "#Generators - yield \n", "def countdown(i=5):\n", " while i >= 0:\n", " yield i*2\n", " i -= 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:36:02.167058Z", "start_time": "2018-09-27T20:36:02.162175Z" } }, "outputs": [], "source": [ "for k in countdown():\n", " print(k)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:36:18.365246Z", "start_time": "2018-09-27T20:36:18.362769Z" } }, "outputs": [], "source": [ "def even(x):\n", " for i in range(x):\n", " if i % 2 == 0:\n", " yield i" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:36:22.458349Z", "start_time": "2018-09-27T20:36:22.453370Z" } }, "outputs": [], "source": [ "list(even(42))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:36:42.784078Z", "start_time": "2018-09-27T20:36:42.779910Z" } }, "outputs": [], "source": [ "sorted(countdown(20))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:36:46.065508Z", "start_time": "2018-09-27T20:36:46.062332Z" } }, "outputs": [], "source": [ "max(countdown(20))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:36:49.873259Z", "start_time": "2018-09-27T20:36:49.863608Z" } }, "outputs": [], "source": [ "min(countdown(20))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:37:21.830543Z", "start_time": "2018-09-27T20:37:21.828231Z" } }, "outputs": [], "source": [ "def infinite_sevens():\n", " while True:\n", " yield 7" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:37:26.275015Z", "start_time": "2018-09-27T20:37:26.271484Z" } }, "outputs": [], "source": [ "inf7 = infinite_sevens()\n", "inf7" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:37:28.788851Z", "start_time": "2018-09-27T20:37:28.784931Z" } }, "outputs": [], "source": [ "next(inf7)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:37:29.780284Z", "start_time": "2018-09-27T20:37:29.776856Z" } }, "outputs": [], "source": [ "next(inf7)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:37:38.836818Z", "start_time": "2018-09-27T20:37:38.832883Z" } }, "outputs": [], "source": [ "for i in zip(range(7), inf7):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:37:53.243688Z", "start_time": "2018-09-27T20:37:46.027835Z" } }, "outputs": [], "source": [ "for i in infinite_sevens():\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:38:21.479706Z", "start_time": "2018-09-27T20:38:21.477228Z" } }, "outputs": [], "source": [ "def natural_number():\n", " i = 0\n", " while True:\n", " yield i\n", " i +=1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:38:30.785435Z", "start_time": "2018-09-27T20:38:30.783236Z" } }, "outputs": [], "source": [ "nn = natural_number()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:39:48.291433Z", "start_time": "2018-09-27T20:39:27.043101Z" } }, "outputs": [], "source": [ "list(nn)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:40:33.417265Z", "start_time": "2018-09-27T20:40:33.410771Z" } }, "outputs": [], "source": [ "[i for i in globals().keys() if not '_' in i]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:42:13.346356Z", "start_time": "2018-09-27T20:42:13.342304Z" } }, "outputs": [], "source": [ "%lsmagic" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-09-27T20:42:19.870397Z", "start_time": "2018-09-27T20:42:19.861835Z" } }, "outputs": [], "source": [ "%who" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [default]", "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.5" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": true } }, "nbformat": 4, "nbformat_minor": 2 }