"This will open the slides in a new browser window."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%html\n",
"<style type=\"text/css\">\n",
"/* nbconvert adds a vertical scrollbar to the slides. */\n",
".reveal {\n",
" overflow-y: hidden;\n",
"}\n",
"</style>"
],
"language": "python",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
...
...
@@ -204,13 +184,11 @@
"\n",
"Start it by typing `python` on the command line:\n",
"\n",
"```markdown\n",
"$ python\n",
"Python 2.7.3 (default, Jan 2 2013, 13:56:14) \n",
"[GCC 4.7.2] on linux2\n",
"Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n",
">>>\n",
"```\n",
" $ python\n",
" Python 2.7.3 (default, Jan 2 2013, 13:56:14) \n",
" [GCC 4.7.2] on linux2\n",
" Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n",
" >>>\n",
"\n",
"* It shows an interpreter prompt.\n",
"* You can give it Python code to interpret."
...
...
@@ -237,19 +215,17 @@
"\n",
"Start it by typing `ipython` on the command line:\n",
"\n",
"```markdown\n",
"$ ipython\n",
"Python 2.7.3 (default, Jan 2 2013, 13:56:14) \n",
"Type \"copyright\", \"credits\" or \"license\" for more information.\n",
" $ ipython\n",
" Python 2.7.3 (default, Jan 2 2013, 13:56:14) \n",
" Type \"copyright\", \"credits\" or \"license\" for more information.\n",
"\n",
"IPython 0.13.1 -- An enhanced Interactive Python.\n",
"? -> Introduction and overview of IPython's features.\n",
"%quickref -> Quick reference.\n",
"help -> Python's own help system.\n",
"object? -> Details about 'object', use 'object??' for extra details.\n",
"IPython 0.13.1 -- An enhanced Interactive Python.\n",
"? -> Introduction and overview of IPython's features.\n",
"%quickref -> Quick reference.\n",
"help -> Python's own help system.\n",
"object? -> Details about 'object', use 'object??' for extra details.\n",
"\n",
"In [1]:\n",
"```\n"
" In [1]:"
]
},
{
...
...
@@ -3567,24 +3543,18 @@
}
},
"source": [
"Modules\n",
"Code in files\n",
"===\n",
"\n",
"Non-interactive programming\n",
"---\n",
"\n",
"* Real work needs to be saved to files.\n",
"* Larger programs need modular development.\n",
"* A `.py` file (with Python code) is called a module.\n",
"\n",
"Example:"
"Running code from a file\n",
"---"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"cat fibonacci.py"
"cat examples/fsquare.py"
],
"language": "python",
"metadata": {},
...
...
@@ -3593,72 +3563,23 @@
"output_type": "stream",
"stream": "stdout",
"text": [
"def fib(n):\r\n",
" if n == 0:\r\n",
" return 0\r\n",
" if n == 1:\r\n",
" return 1\r\n",
" return fib(n - 1) + fib(n - 2)\r\n",
"d = {}\r\n",
"for i in range(10):\r\n",
" d[i] = i ** 2\r\n",
"\r\n",
"if __name__ == '__main__':\r\n",
" for n in range(5, 10):\r\n",
" print 'fib({0}) = {1}'.format(n, fib(n))\r\n"
"for i in d:\r\n",
" print \"{0} is the square of {1}.\".format(d[i], i)\r\n"
]
}
],
"prompt_number": 130
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"Modules\n",
"===\n",
"\n",
"Using code from a module\n",
"---\n",
"\n",
"You can import a module into your code."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import fibonacci\n",
"fibonacci.fib(7)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 122,
"text": [
"13"
]
}
],
"prompt_number": 122
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Or you can run the module directly."
]
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%sh\n",
"python fibonacci.py"
"python examples/fsquare.py"
],
"language": "python",
"metadata": {},
...
...
@@ -3667,15 +3588,20 @@
"output_type": "stream",
"stream": "stdout",
"text": [
"fib(5) = 5\n",
"fib(6) = 8\n",
"fib(7) = 13\n",
"fib(8) = 21\n",
"fib(9) = 34\n"
"0 is the square of 0.\n",
"1 is the square of 1.\n",
"4 is the square of 2.\n",
"9 is the square of 3.\n",
"16 is the square of 4.\n",
"25 is the square of 5.\n",
"36 is the square of 6.\n",
"49 is the square of 7.\n",
"64 is the square of 8.\n",
"81 is the square of 9.\n"
]
}
],
"prompt_number": 131
"prompt_number": 3
},
{
"cell_type": "markdown",
...
...
@@ -3685,20 +3611,20 @@
}
},
"source": [
"Modules\n",
"Code in files\n",
"===\n",
"\n",
"Working with files in IPython\n",
"---\n",
"\n",
"You can run a module directly in IPython."
"The `%run` magic runs the code from a file directly in IPython:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%run fibonacci.py"
"%run examples/fsquare.py"
],
"language": "python",
"metadata": {},
...
...
@@ -3707,35 +3633,20 @@
"output_type": "stream",
"stream": "stdout",
"text": [
"fib(5) = 5\n",
"fib(6) = 8\n",
"fib(7) = 13\n",
"fib(8) = 21\n",
"fib(9) = 34\n"
]
}
],
"prompt_number": 135
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"fib(12)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 136,
"text": [
"144"
"0 is the square of 0.\n",
"1 is the square of 1.\n",
"4 is the square of 2.\n",
"9 is the square of 3.\n",
"16 is the square of 4.\n",
"25 is the square of 5.\n",
"36 is the square of 6.\n",
"49 is the square of 7.\n",
"64 is the square of 8.\n",
"81 is the square of 9.\n"
]
}
],
"prompt_number": 136
"prompt_number": 4
},
{
"cell_type": "markdown",
...
...
@@ -3752,12 +3663,35 @@
"cell_type": "code",
"collapsed": false,
"input": [
"%edit fibonacci.py"
"%edit examples/fsquare.py"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 134
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"Code in files\n",
"===\n",
"\n",
"Saving your IPython session history to a file\n",
"---\n",
"\n",
"Give the `%save` magic a name and a range of input lines and it will save them to a `.py` file with that name:\n",
"\n",
" In [4]: %save my_session 1-3\n",
" The following commands were written to file `my_session.py`:\n",
" a = 4\n",
" a += 3\n",
" b = a"
]
},
{
"cell_type": "markdown",
...
...
@@ -3767,13 +3701,78 @@
}
},
"source": [
"Todo\n",
"Further reading\n",
"===\n",
"\n",
"* Handling exceptions with `try ... except`.\n",
"* Interpreting a stack trace.\n",
"* Don't show importing your module here, leave that for a next lesson, only run a file directly so that we can remove the `if __name__ == '__main__'` trick."
"* [Learn Python The Hard Way](http://learnpythonthehardway.org/book/)\n",
" <br>\n",
" Book on learning Python by exercises, online available for free.\n",
" Repository for an in-depth 3-hour tutorial on IPython, first presented at PyCon 2012.\n",
"\n",
"\n",
"* [The Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/)\n",
" <br>\n",
" This opinionated guide exists to provide both novice and expert Python developers a best-practice handbook to the installation, configuration, and usage of Python on a daily basis.\n",
"\n",
"\n",
"* [A Primer on Scientific Programming with Python](http://codingcat.com/knjige/python/A%20Primer%20on%20Scientific%20Programming%20with%20Python.pdf)\n",
" <br>\n",
" Complete PDF version of the book. The aim of this book is to teach computer programming using examples from mathematics and the natural sciences.\n",
"\n",
"\n",
"* [Python Module of the Week](http://pymotw.com/)\n",
" <br>\n",
" Series of articles providing a tour of the Python standard library through short examples."