{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Ipython vs Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "All valid Python syntax works as-is in IPython. Ipython also provides a number of extra features." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Running code and getting help" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the notebook, to run a cell of code, hit `Shift-Enter`. This executes the cell and puts the cursor in the next cell below, or makes a new one if you are at the end. Alternately, you can use:\n", " \n", "- `Alt-Enter` to force the creation of a new cell unconditionally.\n", "- `Control-Enter` executes the cell and keeps the cursor in the same cell." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Running code:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello world!\n" ] } ], "source": [ "print(\"Hello world!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Getting help:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import distutils\n", "distutils.fancy_getopt?" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "distutils.fancy_getopt??" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "distutils.f*?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pressing `Shift-Tab` is another way of getting help" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "distutils.fancy_getopt()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Tab completion:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "distutils.f" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "this_is_a_variable = 42\n", "this_is_a_function = lambda x: x" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "this_" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Accessing the underlying operating system" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### You can use shell commands by placing a `!` in front of the commands" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/loris/projects/python_meetup/intro_ipython_notebook/notebooks\r\n" ] } ], "source": [ "!pwd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The results of shell commands can be assigned to python variables" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [], "source": [ "dir_list = !ls" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The content of the directory is:\n", "\n", "['analysis.html', 'analysis_slideshow.slides.html', 'display.ipynb', 'enviroment.ipynb', 'example', 'example_file.txt', 'example_script.py', 'export.ipynb', 'figs', 'index.ipynb', 'installation.ipynb', 'magics.ipynb', 'output_toggle.tpl', 'parallel.ipynb', 'references.ipynb', 'use_of_ipython.ipynb']\n" ] } ], "source": [ "print(\"The content of the directory is:\\n\\n{}\".format(dir_list))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Python variables (and expressions) can then be used in shell commands" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The content of the directory is:\n", "\n", "[analysis.html, analysis_slideshow.slides.html, display.ipynb, enviroment.ipynb, example, example_file.txt, example_script.py, export.ipynb, figs, index.ipynb, installation.ipynb, magics.ipynb, output_toggle.tpl, parallel.ipynb, references.ipynb, use_of_ipython.ipynb]\n" ] } ], "source": [ "!echo \"The content of the directory is:\\n\"\n", "!echo $dir_list" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[display.ipynb, enviroment.ipynb, export.ipynb, index.ipynb, installation.ipynb, magics.ipynb, parallel.ipynb, references.ipynb, use_of_ipython.ipynb]\r\n" ] } ], "source": [ "!echo {[item for item in dir_list if item.endswith(\".ipynb\")]}" ] } ], "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.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }