{ "metadata": { "name": "", "signature": "sha256:b95ac2cf2addd6d9efccc996108baeb8c290503a53b8c3de6d9cff19dbe43a23" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Running Python code" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Interactively (demo)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To run Python code interactively, one can use the standard Python prompt, which can be launched by typing ``python`` in your standard shell:\n", "\n", " $ python\n", " Python 3.4.1 (default, May 21 2014, 21:17:51) \n", " [GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin\n", " Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n", " >>>\n", "\n", "The ``>>>`` indicates that Python is ready to accept commands. If you type ``a = 1`` then press enter, this will assign the value ``1`` to ``a``. If you then type ``a`` you will see the value of ``a`` (this is equivalent to ``print a``):\n", "\n", " >>> a = 1\n", " >>> a\n", " 1\n", "\n", "The Python shell can execute any Python code, even multi-line statements, though it is often more convenient to use Python non-interactively for such cases.\n", "\n", "The default Python shell is limited, and in practice, you will want instead to use the IPython (or interactive Python) shell. This is an add-on package that adds many features to the default Python shell, including the ability to edit and navigate the history of previous commands, as well as the ability to tab-complete variable and function names. To start up IPython, type:\n", "\n", " $ ipython\n", " Python 3.4.1 (default, May 21 2014, 21:17:51) \n", " Type \"copyright\", \"credits\" or \"license\" for more information.\n", "\n", " IPython 2.1.0 -- 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", "The first time you start up IPython, it will display a message which you can skip over by pressing ``ENTER``. The ``>>>`` symbols are now replaced by ``In [x]``, and output, when present, is prepended with ``Out [x]``. If we now type the same commands as before, we get:\n", "\n", " In [1]: a = 1\n", "\n", " In [2]: a\n", " Out[2]: 1\n", "\n", "If you now type the up arrow twice, you will get back to ``a = 1``." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Running scripts (demo)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "While the interactive Python mode is very useful to exploring and trying out code, you will eventually want to write a script to record and reproduce what you did, or to do things that are too complex to type in interactively (defining functions, classes, etc.). To write a Python script, just use your favorite code editor to put the code in a file with a ``.py`` extension. For example, we can create a file called ``test.py`` containing:\n", "\n", " a = 1\n", " print(a)\n", "\n", "On the CIP pool machines you can use for example the ``emacs`` editor which you can open by typing:\n", " \n", " emacs &\n", " \n", "(ignore the warnings that it prints to the terminal).\n", "\n", "We can then run the script on the command-line with:\n", "\n", " $ python test.py\n", " 1\n", "\n", "Note: The ``print`` statement is necessary, because typing ``a`` on its own will only print out the value in interactive mode. In scripts, the printing has to be explicitly requested with the print command. To print multiple variables, just separate them with a comma after the print command:\n", "\n", " print(a, 1.5, \"spam\")" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Soap box: Get to know your shell" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Yes, there are cool IDEs out there that save you from ever having to read.\n", "Dodging documentation will bite you, though.\n", "\n", "Where GUIs require you to point, the shell lets you talk. Which on the\n", "long run helps a lot.\n", "\n", "Go to the library, check a few \"Unix Introductions\". They'll typically\n", "really be introductions to talking with a shell. Choose any you like." ] } ], "metadata": {} } ] }