Getting Started With The Notebooks

Welcome to the world of Jupyter Notebooks on the one hand, and programming in the Python programming language on the other.

Be warned, though. This is not a fully blown Python programming course, nor is this a computing focussed module.

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.

So What is a Jupyter Notebook?

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.

Notebooks are comprised mainly of two sorts of cell. Text based markdown cells, like this one, in which we can write "human" text...

In [ ]:
#...and code cells like this one, that can be used to execute code
print("Hello world")

If you select the previous cell - click on it - and press the play button in the toolbar to "run the cell", what happens?

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.

As well as running the cell from the toolbar, you can use a keyboard shortcut - press Shift-Return to run the cell.

(A full list of keyboard shortcuts can be found from the Help menu.)

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.

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.)

To "render" a markdown cell, run it in the same way you would run a code cell.

The notebook design encourages you to explore code examples yourself and the design of these educational materials reflects that.

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.

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.

And What is Python?

Python is a programming language that helps you get stuff done.

You've already run your first line of Python code in the hello world example above.

The syntax is defined to be quite straightforward - just watch out for using white space correctly - and naturally expressive: the code should read well.

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.

XKCD cartoon: Python

(Actually,print "hello world" doesn't work in this version of Python - we need the brackets as shown in the example previously!).

In [3]:
#Run this cell
print "hello world"
  File "<ipython-input-3-f4f153418a52>", line 2
    print "hello world"
                      ^
SyntaxError: Missing parentheses in call to 'print'

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.

You may get an error message, though, which may be helpful. (Or it may not!)

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.

Code Cell Outputs

You may notice that different sorts of "stuff" can appear when you run a code cell.

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.)

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.

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.

What happens if you add a print statement (remember the brackets!) and run the cell again?

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?

In [ ]:
#Put your first sum here


#Put a print statement here


#And then after running the cell, a second one here

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.

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.