{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# PySeison - Tutorial 0: First steps into Python and Ipython" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Launching Ipython" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "From your shell, ***cd*** to your python workspace/folder and type:" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "$ipython" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This command will launch Ipython with the *pylab* option on. This option will provide an interactive environment, to generate plots." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Importing libraries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Importing libraries permits to extend python default functionality by calling exterior code packages. There are several ways to [import libraries in python](http://effbot.org/zone/import-confusion.htm) with different implications. In this tutorial, two different *import* statements will be use.\n", "\n", "Let us import a very useful library, *numpy*. [*numpy*](http://www.numpy.org/) has very similar capability to *matlab* and is therefore widely use in the scientific community." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here the entire package has been imported and renamed *np*. This format of *import* format is good-practise since it allows to keep track of the package and their attached functions and avoid confusions. for example:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.round" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "is different from:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Ipython useful shortcuts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is a non-exhaustive list of useful shortcuts available in Ipython:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- **Tab:** *Tab* completion, especially for attributes, is a convenient way to explore the structure of any object you’re dealing with. Simply type ***object_name.Tab*** to view the object’s attributes (see the [readline section](http://ipython.org/ipython-doc/2/interactive/reference.html#readline) for more). Besides Python objects and keywords, tab completion also works on file and directory names.\n", "\n", "- **Up & down arrows:** IPython stores both the commands you enter, and the results it produces. You can easily go through previous commands with the up- and down-arrow keys, or access your history in more sophisticated ways. One can also use up- and down-arrow keys for auto-completion purposes based on one's command history.\n", "\n", "- **?:** *?* permits a quick access to the documentation of any object or function. Typing ***object_name?*** will unravel the documentation related to the object. Sometimes, one has to type ***q*** to quit the documentation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Numpy vs. Matlab" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Despite their capability similitudes, there are few major differences between *numpy* and *matlab*. Here follows probably the most important ones:\n", "- ***[ ] vs. ( ):*** if M is a matrix, i th and j th element of M would be respectively called **M[i,j]** in *numpy* and **M(j,i)** in *matlab*.\n", "- ***Row-major vs. Column-major:*** as one may has notice the previous point, **the order of row and column indices are inverse** between *python* and *matlab*. Consequently, be extra cautious when translating *matlab* scripts to *python* and similarly when optimisizing double for loops.\n", "- ***0 to N-1 vs. 1 to N:*** Additionally to the index order, the number attributed to first element of a matrix is different between *python* and *matlab*. **In *matlab*, the number attributed to first element of a matrix is one whereas, in *python* it is zero**, for instance: " ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Shape of M: (10,)\n", "First element= 1.0\n", "Last element= 1.0\n" ] } ], "source": [ "N = 10\n", "M = np.ones((N)) #define a 1D array of N elements\n", "print \"Shape of M: \", M.shape\n", "print \"First element= \", M[0]\n", "print \"Last element= \", M[9]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- ***Indentation: *** Python uses whitespace indentation, rather than curly braces or keywords, to delimit blocks; this feature is also termed the off-side rule. An increase in indentation comes after certain statements; a decrease in indentation signifies the end of the current block...this features will be illustrated later on with exercises." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Fortunately, *numpy* and *matlab* have more similitudes than differences, so much that [*matlab* users](http://mathesaurus.sourceforge.net/matlab-numpy.html) will rapidly make their heads around *numpy* " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5. PEP convetions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "PEP stands for Python Enhancement Proposals. They are Best-practises type documents for Python programming. It is recommended to follow as much as possible PEP 20, PEP 8 and PEP 257 (i.e.http://legacy.python.org/dev/peps/) \n" ] } ], "metadata": { "celltoolbar": "Raw Cell Format", "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 0 }