typo fix in legb tutorial

This commit is contained in:
rasbt 2016-01-27 22:57:02 -05:00
parent 3fc278cae3
commit 0a567380c9

View File

@ -1,11 +1,3 @@
{
"metadata": {
"name": "",
"signature": "sha256:b33e0c5563d80d68580ea6ce62ae2703856ccde40aec8ff9fb1364ac70d70521"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{ {
"cells": [ "cells": [
{ {
@ -20,36 +12,36 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 1,
"input": [ "metadata": {
"%load_ext watermark" "collapsed": false
], },
"language": "python",
"metadata": {},
"outputs": [], "outputs": [],
"prompt_number": 1 "source": [
"%load_ext watermark"
]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 2,
"input": [ "metadata": {
"%watermark -a 'Sebastian Raschka' -v -d" "collapsed": false
], },
"language": "python",
"metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout",
"output_type": "stream", "output_type": "stream",
"stream": "stdout",
"text": [ "text": [
"Sebastian Raschka 04/07/2014 \n", "Sebastian Raschka 01/27/2016 \n",
"\n", "\n",
"CPython 3.3.5\n", "CPython 3.5.1\n",
"IPython 2.1.0\n" "IPython 4.0.3\n"
] ]
} }
], ],
"prompt_number": 3 "source": [
"%watermark -a 'Sebastian Raschka' -v -d"
]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -199,8 +191,21 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 1,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 global\n",
"5 in foo()\n"
]
}
],
"source": [
"i = 1\n", "i = 1\n",
"\n", "\n",
"def foo():\n", "def foo():\n",
@ -210,20 +215,7 @@
"print(i, 'global')\n", "print(i, 'global')\n",
"\n", "\n",
"foo()" "foo()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1 global\n",
"5 in foo()\n"
] ]
}
],
"prompt_number": 1
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -258,8 +250,22 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 11,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"loc in foo(): True\n",
"loc in global: False\n",
"glob in global: True\n"
]
}
],
"source": [
"#print(globals()) # prints global namespace\n", "#print(globals()) # prints global namespace\n",
"#print(locals()) # prints local namespace\n", "#print(locals()) # prints local namespace\n",
"\n", "\n",
@ -272,21 +278,7 @@
"foo()\n", "foo()\n",
"print('loc in global:', 'loc' in globals()) \n", "print('loc in global:', 'loc' in globals()) \n",
"print('glob in global:', 'foo' in globals())" "print('glob in global:', 'foo' in globals())"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"loc in foo(): True\n",
"loc in global: False\n",
"glob in global: True\n"
] ]
}
],
"prompt_number": 11
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -319,22 +311,14 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 8,
"input": [ "metadata": {
"import numpy\n", "collapsed": false
"import math\n", },
"import scipy\n",
"\n",
"print(math.pi, 'from the math module')\n",
"print(numpy.pi, 'from the numpy package')\n",
"print(scipy.pi, 'from the scipy package')"
],
"language": "python",
"metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout",
"output_type": "stream", "output_type": "stream",
"stream": "stdout",
"text": [ "text": [
"3.141592653589793 from the math module\n", "3.141592653589793 from the math module\n",
"3.141592653589793 from the numpy package\n", "3.141592653589793 from the numpy package\n",
@ -342,7 +326,15 @@
] ]
} }
], ],
"prompt_number": 8 "source": [
"import numpy\n",
"import math\n",
"import scipy\n",
"\n",
"print(math.pi, 'from the math module')\n",
"print(numpy.pi, 'from the numpy package')\n",
"print(scipy.pi, 'from the scipy package')"
]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -389,8 +381,12 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 1,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"a_var = 'global variable'\n", "a_var = 'global variable'\n",
"\n", "\n",
"def a_func():\n", "def a_func():\n",
@ -398,11 +394,7 @@
"\n", "\n",
"a_func()\n", "a_func()\n",
"print(a_var, '[ a_var outside a_func() ]')" "print(a_var, '[ a_var outside a_func() ]')"
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -451,8 +443,12 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 2,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"a_var = 'global value'\n", "a_var = 'global value'\n",
"\n", "\n",
"def a_func():\n", "def a_func():\n",
@ -461,11 +457,7 @@
"\n", "\n",
"a_func()\n", "a_func()\n",
"print(a_var, '[ a_var outside a_func() ]')" "print(a_var, '[ a_var outside a_func() ]')"
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -509,8 +501,22 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 3,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"global value [ a_var outside a_func() ]\n",
"local value [ a_var inside a_func() ]\n",
"local value [ a_var outside a_func() ]\n"
]
}
],
"source": [
"a_var = 'global value'\n", "a_var = 'global value'\n",
"\n", "\n",
"def a_func():\n", "def a_func():\n",
@ -521,21 +527,7 @@
"print(a_var, '[ a_var outside a_func() ]')\n", "print(a_var, '[ a_var outside a_func() ]')\n",
"a_func()\n", "a_func()\n",
"print(a_var, '[ a_var outside a_func() ]')" "print(a_var, '[ a_var outside a_func() ]')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"global value [ a_var outside a_func() ]\n",
"local value [ a_var inside a_func() ]\n",
"local value [ a_var outside a_func() ]\n"
] ]
}
],
"prompt_number": 3
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -546,24 +538,15 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 4,
"input": [ "metadata": {
"a_var = 1\n", "collapsed": false
"\n", },
"def a_func():\n",
" a_var = a_var + 1\n",
" print(a_var, '[ a_var inside a_func() ]')\n",
"\n",
"print(a_var, '[ a_var outside a_func() ]')\n",
"a_func()"
],
"language": "python",
"metadata": {},
"outputs": [ "outputs": [
{ {
"ename": "UnboundLocalError", "ename": "UnboundLocalError",
"evalue": "local variable 'a_var' referenced before assignment", "evalue": "local variable 'a_var' referenced before assignment",
"output_type": "pyerr", "output_type": "error",
"traceback": [ "traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mUnboundLocalError\u001b[0m Traceback (most recent call last)", "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mUnboundLocalError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-4-a6cdd0ee9a55>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma_var\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'[ a_var outside a_func() ]'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0ma_func\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m<ipython-input-4-a6cdd0ee9a55>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma_var\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'[ a_var outside a_func() ]'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0ma_func\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
@ -572,14 +555,23 @@
] ]
}, },
{ {
"name": "stdout",
"output_type": "stream", "output_type": "stream",
"stream": "stdout",
"text": [ "text": [
"1 [ a_var outside a_func() ]\n" "1 [ a_var outside a_func() ]\n"
] ]
} }
], ],
"prompt_number": 4 "source": [
"a_var = 1\n",
"\n",
"def a_func():\n",
" a_var = a_var + 1\n",
" print(a_var, '[ a_var inside a_func() ]')\n",
"\n",
"print(a_var, '[ a_var outside a_func() ]')\n",
"a_func()"
]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -618,8 +610,12 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 4,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"a_var = 'global value'\n", "a_var = 'global value'\n",
"\n", "\n",
"def outer():\n", "def outer():\n",
@ -632,11 +628,7 @@
" inner()\n", " inner()\n",
"\n", "\n",
"outer()" "outer()"
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -678,8 +670,22 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 5,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"outer before: local value\n",
"in inner(): inner value\n",
"outer after: inner value\n"
]
}
],
"source": [
"a_var = 'global value'\n", "a_var = 'global value'\n",
"\n", "\n",
"def outer():\n", "def outer():\n",
@ -692,21 +698,7 @@
" inner()\n", " inner()\n",
" print(\"outer after:\", a_var)\n", " print(\"outer after:\", a_var)\n",
"outer()" "outer()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"outer before: local value\n",
"in inner(): inner value\n",
"outer after: inner value\n"
] ]
}
],
"prompt_number": 5
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -735,8 +727,12 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 6,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"a_var = 'global variable'\n", "a_var = 'global variable'\n",
"\n", "\n",
"def len(in_var):\n", "def len(in_var):\n",
@ -751,11 +747,7 @@
" print('Input variable is of length', len_in_var)\n", " print('Input variable is of length', len_in_var)\n",
"\n", "\n",
"a_func('Hello, World!')" "a_func('Hello, World!')"
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -785,7 +777,7 @@
"source": [ "source": [
"### Here is why:\n", "### Here is why:\n",
"\n", "\n",
"Since the exact same names can be used to map names to different objects - as long as the names are in different name spaces - there is no problem of reusing the name `len` to define our own length function (this is just for demonstration pruposes, it is NOT recommended). As we go up in Python's L -> E -> G -> B hierarchy, the function `a_func()` finds `len()` already in the global scope first before it attempts" "Since the exact same names can be used to map names to different objects - as long as the names are in different name spaces - there is no problem of reusing the name `len` to define our own length function (this is just for demonstration pruposes, it is NOT recommended). As we go up in Python's L -> E -> G -> B hierarchy, the function `a_func()` finds `len()` already in the global scope (G) first before it attempts to search the built-in (B) namespace."
] ]
}, },
{ {
@ -813,8 +805,12 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 59,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"a = 'global'\n", "a = 'global'\n",
"\n", "\n",
"def outer():\n", "def outer():\n",
@ -841,11 +837,7 @@
"\n", "\n",
"print(len(a))\n", "print(len(a))\n",
"print('a is', a)" "print('a is', a)"
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 59
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -895,8 +887,20 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 42,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8\n"
]
}
],
"source": [
"a_var = 2\n", "a_var = 2\n",
"\n", "\n",
"def a_func(some_var):\n", "def a_func(some_var):\n",
@ -904,19 +908,7 @@
"\n", "\n",
"a_var = a_func(a_var)\n", "a_var = a_func(a_var)\n",
"print(a_var)" "print(a_var)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"8\n"
] ]
}
],
"prompt_number": 42
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -938,52 +930,56 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 6,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('Example 1.1:', chr(int('01100011',2)))" "print('Example 1.1:', chr(int('01100011',2)))"
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 7,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('Example 1.2:', chr(int('01100010',2)))" "print('Example 1.2:', chr(int('01100010',2)))"
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 8,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('Example 2.1:', chr(int('01100011',2)))" "print('Example 2.1:', chr(int('01100011',2)))"
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 9,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('Example 3.1:', chr(int('01100010',2)))" "print('Example 3.1:', chr(int('01100010',2)))"
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 9
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 58,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Execute to run the self-assessment solution\n", "# Execute to run the self-assessment solution\n",
"\n", "\n",
"sol = \"000010100110111101110101011101000110010101110010001010\"\\\n", "sol = \"000010100110111101110101011101000110010101110010001010\"\\\n",
@ -1001,11 +997,7 @@
"sol_str =''.join(chr(int(sol[i:i+8], 2)) for i in range(0, len(sol), 8))\n", "sol_str =''.join(chr(int(sol[i:i+8], 2)) for i in range(0, len(sol), 8))\n",
"for line in sol_str.split('\\n'):\n", "for line in sol_str.split('\\n'):\n",
" print(line)" " print(line)"
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 58
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -1032,26 +1024,26 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 5,
"input": [ "metadata": {
"for a in range(5):\n", "collapsed": false
" if a == 4:\n", },
" print(a, '-> a in for-loop')\n",
"print(a, '-> a in global')"
],
"language": "python",
"metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout",
"output_type": "stream", "output_type": "stream",
"stream": "stdout",
"text": [ "text": [
"4 -> a in for-loop\n", "4 -> a in for-loop\n",
"4 -> a in global\n" "4 -> a in global\n"
] ]
} }
], ],
"prompt_number": 5 "source": [
"for a in range(5):\n",
" if a == 4:\n",
" print(a, '-> a in for-loop')\n",
"print(a, '-> a in global')"
]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -1062,27 +1054,27 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 9,
"input": [ "metadata": {
"b = 1\n", "collapsed": false
"for b in range(5):\n", },
" if b == 4:\n",
" print(b, '-> b in for-loop')\n",
"print(b, '-> b in global')"
],
"language": "python",
"metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout",
"output_type": "stream", "output_type": "stream",
"stream": "stdout",
"text": [ "text": [
"4 -> b in for-loop\n", "4 -> b in for-loop\n",
"4 -> b in global\n" "4 -> b in global\n"
] ]
} }
], ],
"prompt_number": 9 "source": [
"b = 1\n",
"for b in range(5):\n",
" if b == 4:\n",
" print(b, '-> b in for-loop')\n",
"print(b, '-> b in global')"
]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -1093,25 +1085,25 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 1,
"input": [ "metadata": {
"i = 1\n", "collapsed": false
"print([i for i in range(5)])\n", },
"print(i, '-> i in global')"
],
"language": "python",
"metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout",
"output_type": "stream", "output_type": "stream",
"stream": "stdout",
"text": [ "text": [
"[0, 1, 2, 3, 4]\n", "[0, 1, 2, 3, 4]\n",
"1 -> i in global\n" "1 -> i in global\n"
] ]
} }
], ],
"prompt_number": 1 "source": [
"i = 1\n",
"print([i for i in range(5)])\n",
"print(i, '-> i in global')"
]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -1128,21 +1120,40 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"This goes back to a change that was made in Python 3.x and is described in [What\u2019s New In Python 3.0](https://docs.python.org/3/whatsnew/3.0.html) as follows:\n", "This goes back to a change that was made in Python 3.x and is described in [Whats New In Python 3.0](https://docs.python.org/3/whatsnew/3.0.html) as follows:\n",
"\n", "\n",
"\"List comprehensions no longer support the syntactic form `[... for var in item1, item2, ...]`. Use `[... for var in (item1, item2, ...)]` instead. Also note that list comprehensions have different semantics: they are closer to syntactic sugar for a generator expression inside a `list()` constructor, and in particular the loop control variables are no longer leaked into the surrounding scope.\"" "\"List comprehensions no longer support the syntactic form `[... for var in item1, item2, ...]`. Use `[... for var in (item1, item2, ...)]` instead. Also note that list comprehensions have different semantics: they are closer to syntactic sugar for a generator expression inside a `list()` constructor, and in particular the loop control variables are no longer leaked into the surrounding scope.\""
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": null,
"input": [], "metadata": {
"language": "python", "collapsed": false
"metadata": {}, },
"outputs": [] "outputs": [],
"source": []
} }
], ],
"metadata": {} "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.5.1"
} }
] },
"nbformat": 4,
"nbformat_minor": 0
} }