{ "metadata": { "name": "02_03_class" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Scope of variables, Tabulating a function, lambda functions" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Scope of variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us study scopes of variables to a slight more detail." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The LGB rule : Python searches for variable names in the following order : \n", "\n", "1. First it looks at the local variable names.\n", "\n", "2. Then it searches for the variale in the list of global variables.\n", "\n", "3. Finally it tries to see if there is some builtin function having the same name." ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 4\n", "\n", "def test() :\n", " # print x\n", " x = 5\n", " print x\n", " print sum\n", "test()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "5\n", "\n" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "# An example from the book :\n", "print \"builtin sum = \", sum # Here no local or global variable is defined, so it takes the inbuilt sum.\n", "sum = 12 # Assigning 12 to sum. This is a global variable for this code.\n", "print \"global sum = \", sum\n", "\n", "# To see local variables we have to define a function :\n", "def somefn(n) :\n", " sum = n + 1 # Here it is a local variable. The global variable is untouched.\n", " print \"local sum = \", sum\n", " return sum\n", "\n", "print \"whuch sum am I?\", sum\n", "sum = somefn(4)\n", "print \"New global sum = \", sum\n", "somefn(12)\n", "print \"Global sum = \", sum" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "builtin sum = \n", "global sum = 12\n", "whuch sum am I? 12\n", "local sum = 5\n", "New global sum = 5\n", "local sum = 13\n", "Global sum = 5\n" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "del sum" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Look at what the keyword global does" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# An example from the book :\n", "print \"builtin sum = \", sum # Here no local or global variable is defined, so it takes the inbuilt sum.\n", "sum = 12 # Assigning 12 to sum. This is a global variable for this code.\n", "print \"global sum = \", sum\n", "\n", "# To see local variables we have to define a function :\n", "def somefn(n) :\n", " global sum\n", " sum = n + 1 # Here it is a local variable. The global variable is untouched.\n", " print \"local sum = \", sum\n", " return sum\n", "\n", "print \"whuch sum am I?\", sum\n", "sum = somefn(4)\n", "print \"New global sum = \", sum\n", "somefn(12)\n", "print \"global sum = \", sum" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "builtin sum = \n", "global sum = 12\n", "whuch sum am I? 12\n", "local sum = 5\n", "New global sum = 5\n", "local sum = 13\n", "global sum = 13\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can functions access a global variable without using the keyword global? (Or of what use is a global variable if you cannot access it everywhere?)\n", "\n", "The answer is that you can access it everywhere. Without using global you cannot modify it. Here is an example" ] }, { "cell_type": "code", "collapsed": false, "input": [ "globvar = 4\n", "\n", "def fun_just_accessing_globvar (n) :\n", " x = globvar\n", " print \"fun_just_accessing_globvar: 1 : \", x\n", " x = n\n", " print \"fun_just_accessing_globvar: 2 : \", x\n", " return globvar\n", "\n", "def fun_using_loc_var_globvar (n) :\n", " globvar = n\n", " print \"fun_using_loc_var_globvar :\", globvar\n", " return globvar\n", "\n", "def fun_modifying_globvar (n) :\n", " global globvar\n", " print \"fun_modifying_globvar: 1 : \", globvar\n", " globvar = n\n", " print \"fun_modifying_globvar: 2 : \", globvar\n", " return globvar\n", "\n", "fun_just_accessing_globvar(100)\n", "print \"After fun_just_accessing_globvar, globvar =\", globvar\n", "\n", "fun_modifying_globvar(200)\n", "print \"After fun_modifying_globvar, globvar =\", globvar\n", "\n", "fun_using_loc_var_globvar(30)\n", "print \"After fun_using_loc_var_globvar, globvar =\", globvar\n", "\n", "fun_just_accessing_globvar(300)\n", "print \"After fun_just_accessing_globvar, globvar =\", globvar\n", "\n", "fun_modifying_globvar(300)\n", "print \"After fun_modifying_globvar, globvar =\", globvar\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "fun_just_accessing_globvar: 1 : 4\n", "fun_just_accessing_globvar: 2 : 100\n", "After fun_just_accessing_globvar, globvar = 4\n", "fun_modifying_globvar: 1 : 4\n", "fun_modifying_globvar: 2 : 200\n", "After fun_modifying_globvar, globvar = 200\n", "fun_using_loc_var_globvar : 30\n", "After fun_using_loc_var_globvar, globvar = 200\n", "fun_just_accessing_globvar: 1 : 200\n", "fun_just_accessing_globvar: 2 : 300\n", "After fun_just_accessing_globvar, globvar = 200\n", "fun_modifying_globvar: 1 : 200\n", "fun_modifying_globvar: 2 : 300\n", "After fun_modifying_globvar, globvar = 300\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Exercise : Understand the output above by printing/copying tha above program and marking the globvar's occuring there as local or global." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another example : " ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 100\n", "\n", "def print_glob_x () :\n", " print \"print_glob_x : Global x = \", x\n", " \n", "\n", "def fn_loc_x (n) :\n", " x = n\n", " print \"fn_loc_x : Local x : \", x\n", " print_glob_x()\n", " \n", "def fn_glob_x (n) :\n", " global x\n", " x = n\n", " print \"fn_glob_x : Local x : \", x\n", " print_glob_x()\n", "\n", "print \"Global x = \", x\n", "fn_loc_x(200)\n", "print \"Global x = \", x\n", "fn_glob_x(200)\n", "print \"Global x = \", x\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Global x = 100\n", "fn_loc_x : Local x : 200\n", "print_glob_x : Global x = 100\n", "Global x = 100\n", "fn_glob_x : Local x : 200\n", "print_glob_x : Global x = 200\n", "Global x = 200\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Informal exercise : Experiment with variables as much as you can" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Tabulating functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In python one can pass functions to other functions, just as one passes variables" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def tabulate(fn, x_lo, x_hi, step=1) :\n", " no_of_steps = (x_hi - x_lo)/step + 1\n", " table = []\n", " for i in range(int(no_of_steps)) :\n", " xi = x_lo + i * step\n", " yi = fn(xi)\n", " table.append((xi, yi))\n", " return table\n", " \n", "def print_table(lst_of_tuples) :\n", " print \" x\\t y\"\n", " for (x, y) in lst_of_tuples :\n", " print \"%20.18f\\t%20.18f\" % (x, y)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [ "def square(x) :\n", " return x * x" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "print_table(tabulate(square, 0, 10))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " x\t y\n", " 0.00\t 0.00\n", " 1.00\t 1.00\n", " 2.00\t 4.00\n", " 3.00\t 9.00\n", " 4.00\t 16.00\n", " 5.00\t 25.00\n", " 6.00\t 36.00\n", " 7.00\t 49.00\n", " 8.00\t 64.00\n", " 9.00\t 81.00\n", " 10.00\t100.00\n" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "from math import exp\n", "print_table(tabulate(exp, -10, 10, .5))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " x\t y\n", "-10.00\t 0.00\n", " -9.50\t 0.00\n", " -9.00\t 0.00\n", " -8.50\t 0.00\n", " -8.00\t 0.00\n", " -7.50\t 0.00\n", " -7.00\t 0.00\n", " -6.50\t 0.00\n", " -6.00\t 0.00\n", " -5.50\t 0.00\n", " -5.00\t 0.01\n", " -4.50\t 0.01\n", " -4.00\t 0.02\n", " -3.50\t 0.03\n", " -3.00\t 0.05\n", " -2.50\t 0.08\n", " -2.00\t 0.14\n", " -1.50\t 0.22\n", " -1.00\t 0.37\n", " -0.50\t 0.61\n", " 0.00\t 1.00\n", " 0.50\t 1.65\n", " 1.00\t 2.72\n", " 1.50\t 4.48\n", " 2.00\t 7.39\n", " 2.50\t 12.18\n", " 3.00\t 20.09\n", " 3.50\t 33.12\n", " 4.00\t 54.60\n", " 4.50\t 90.02\n", " 5.00\t148.41\n", " 5.50\t244.69\n", " 6.00\t403.43\n", " 6.50\t665.14\n", " 7.00\t1096.63\n", " 7.50\t1808.04\n", " 8.00\t2980.96\n", " 8.50\t4914.77\n", " 9.00\t8103.08\n", " 9.50\t13359.73\n", " 10.00\t22026.47\n" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "# Soln to exp fn problem\n", "def my_exp(x, N=10000) :\n", " transition_factor = 1\n", " ith_factor = 1\n", " E = 0\n", " for i in range(N) :\n", " E += ith_factor\n", " ith_factor *= x/(i+1) # ith_factor for the next i\n", " return E\n", " \n", "print_table(tabulate(my_exp, -10,10,.5))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " x\t y\n", "-10.00\t 0.00\n", " -9.50\t 0.00\n", " -9.00\t 0.00\n", " -8.50\t 0.00\n", " -8.00\t 0.00\n", " -7.50\t 0.00\n", " -7.00\t 0.00\n", " -6.50\t 0.00\n", " -6.00\t 0.00\n", " -5.50\t 0.00\n", " -5.00\t 0.01\n", " -4.50\t 0.01\n", " -4.00\t 0.02\n", " -3.50\t 0.03\n", " -3.00\t 0.05\n", " -2.50\t 0.08\n", " -2.00\t 0.14\n", " -1.50\t 0.22\n", " -1.00\t 0.37\n", " -0.50\t 0.61\n", " 0.00\t 1.00\n", " 0.50\t 1.65\n", " 1.00\t 2.72\n", " 1.50\t 4.48\n", " 2.00\t 7.39\n", " 2.50\t 12.18\n", " 3.00\t 20.09\n", " 3.50\t 33.12\n", " 4.00\t 54.60\n", " 4.50\t 90.02\n", " 5.00\t148.41\n", " 5.50\t244.69\n", " 6.00\t403.43\n", " 6.50\t665.14\n", " 7.00\t1096.63\n", " 7.50\t1808.04\n", " 8.00\t2980.96\n", " 8.50\t4914.77\n", " 9.00\t8103.08\n", " 9.50\t13359.73\n", " 10.00\t22026.47\n" ] } ], "prompt_number": 16 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Lambda functions" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import math\n", "print_table(tabulate(lambda y : math.exp(math.log(y)), 1,10, 1))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " x\t y\n", "1.000000000000000000\t1.000000000000000000\n", "2.000000000000000000\t2.000000000000000000\n", "3.000000000000000000\t3.000000000000000444\n", "4.000000000000000000\t4.000000000000000000\n", "5.000000000000000000\t4.999999999999999112\n", "6.000000000000000000\t6.000000000000000000\n", "7.000000000000000000\t6.999999999999999112\n", "8.000000000000000000\t7.999999999999998224\n", "9.000000000000000000\t9.000000000000001776\n", "10.000000000000000000\t10.000000000000001776\n" ] } ], "prompt_number": 23 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Most basic plotting" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def very_simple_plot(f, xlow, xhigh, xscale, yscale, yoffset) :\n", " values = tabulate(f, xlow, xhigh, xscale)\n", " #print values\n", " for (x, y) in values :\n", " line = \" \" * int(yscale *(yoffset + y) - 1) + \"*\"\n", " print line\n", " \n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 54 }, { "cell_type": "code", "collapsed": false, "input": [ "very_simple_plot(math.sin, 0, 5, .1, 50, 1)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", "*\n", "*\n", "*\n", "*\n", "*\n", " *\n" ] } ], "prompt_number": 55 }, { "cell_type": "code", "collapsed": false, "input": [ "from math import sin" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "very_simple_plot(sin, 0, 14, .2, 10, 2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n", " *\n" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "def very_simple_plot_with_x_axis(f, xlow, xhigh, xscale, yscale, yoffset) :\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "very_simple_plot_with_x_axis(sin, 0, 14, .2, 20, 2)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n", " * *\n" ] } ], "prompt_number": 15 } ], "metadata": {} } ] }