{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Getting Started With The Notebooks\n", "\n", "Welcome to the world of *Jupyter Notebooks* on the one hand, and programming in the *Python* programming language on the other.\n", "\n", "Be warned, though. This is not a fully blown Python programming course, nor is this a computing focussed module.\n", "\n", "Instead, what we hope to do is provide you with a flavour of the programming language and show you how we can use it to start to programme several simulated robots usng the Coppelia V-REP robotics simulator." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## So What is a Jupyter Notebook?\n", "\n", "A Jupyter notebook is a browser based interface to a programming language kernel. The kernel is a computer process that will execute the programming language code presented to it from the notebook, and provide a response to the notebook after executing the code.\n", "\n", "Notebooks are comprised mainly of two sorts of *cell*. Text based markdown cells, like this one, in which we can write \"human\" text..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#...and code cells like this one, that can be used to execute code\n", "print(\"Hello world\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you select the previous cell - click on it - and press the play button in the toolbar to \"run the cell\", what happens?\n", "\n", "If you click *in* the cell, you can edit the code. See if you can change the message to `\"Hello, Castle Donington\"` and then re-run the cell." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As well as running the cell from the toolbar, you can use a keyboard shortcut - press `Shift-Return` to run the cell.\n", "\n", "(A full list of keyboard shortcuts can be found from the `Help` menu.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To create a cell, click on the + button in the toolbar. Set the type of a highlighted cell from the drop down list in the toolbar.\n", "\n", "To \"open\" a markdown cell for editing, double click on it. (Double click on a cell to see how the markdown is written that defines different styles, such as headings, *emphasis*, or __strong emphasis__.)\n", "\n", "To \"render\" a markdown cell, run it in the same way you would run a code cell." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The notebook design encourages you to explore code examples yourself and the design of these educational materials reflects that.\n", "\n", "For example, rather than giving you lots and lots of examples, one after the other, we encourage you to play around with each new code idea we introduce. Create your own code cells and explore you own understanding of each code idea by copying the example code, making modifications to it, and running it.\n", "\n", "You can speed this process up by copying a code cell and then pasting it as a duplicate back into the notebook using either the toolbar buttos or the options on the *Edit* menu." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## And What is Python?\n", "\n", "*Python* is a programming language that helps you get stuff done.\n", "\n", "You've already run your first line of Python code in the *hello world* example above.\n", "\n", "The syntax is defined to be quite straightforward - just watch out for using white space correctly - and naturally expressive: the code should read well.\n", "\n", "There is a wide range of third party packages that extend the language with high level programming commands that let you do quite a lot with a single line of code.\n", "\n", "[![XKCD cartoon: Python](https://imgs.xkcd.com/comics/python.png)](https://xkcd.com/353/)\n", "\n", "*(Actually,`print \"hello world\"` doesn't work in this version of Python - we need the brackets as shown in the example previously!).*" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "Missing parentheses in call to 'print' (, line 2)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m print \"hello world\"\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m Missing parentheses in call to 'print'\n" ] } ], "source": [ "#Run this cell\n", "print \"hello world\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When you see an error like the one above, *DON'T PANIC*. You won't break the internet, and you won't break your computer either. Probably.\n", "\n", "You may get an error message, though, which may be helpful. (Or it may not!)\n", "\n", "If you do have problems and keep getting error messages when writing your own programmes, copy the error message and put it in a message on the module forums along with the code you were trying to run." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Code Cell Outputs\n", "\n", "You may notice that different sorts of \"stuff\" can appear when you run a code cell.\n", "\n", "Firstly, you can `print()` things that should appear as the code runs. (Actually, this is not necessarily true when running programmes that call the V-REP simulator.)\n", "\n", "Secondly, if the last programme statement in a cell (typically, the last line of code in the cell) returns a value, that value is printed out when the code in the cell finishes running.\n", "\n", "For example, if you type a sum, such as `1 + 2`, in the code cell below, and run the cell, you should see the result displayed underneath the cell.\n", "\n", "What happens if you add a `print` statement (remember the brackets!) and run the cell again?\n", "\n", "Then what happens if you add a second sum, on a line of its own, in the same code cell, below the `print` statement? For example, `3 + 4`. What happens when you run the cell now?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Put your first sum here\n", "\n", "\n", "#Put a print statement here\n", "\n", "\n", "#And then after running the cell, a second one here\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's pretty much it. Feel free to create you own notebooks (from the *New* menu on the nteobook homepage) and edit the ones your working in, perhaps adding your own notes in markdown cells.\n", "\n", "You may also have noticed that you can write so-called explanatory *comments* in the code cells, that are prefixed with the `#` symbol. These are *not* executed when the code cell is run - they are there purely to annotate the code, or mask out a line of code you do not want to execute." ] } ], "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.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }