Intermediate Python

Welcome to a short course which will introduce you to some more of the Python programming language and show how they can make it easier to share your code with other people.

For the purpose of this course we will be using a free tool called JupyterLab which provides you with a local editor in your web browser where you can write and run Python code. The easiest way to get access to JupyterLab is to install the Anaconda Distribution which is a piece of software which includes Python alson with lots of other tools. It is available for Windows, MacOS and Linux and is also available through the University of Bristol Software Centre.

Once Anaconda is installed, start "Anaconda Navigator" and press the JupyterLab button on the main screen:

Anaconda Navigator

This will open JupyterLab in your default web browser and will look something like this:

JupyterLab

The way that we will be setting up the space is to have a text editor on the left-hand side of the screen and a terminal on the right hand side. We'll use the editor to write our code and the terminal to run it.

In the launcher tab, scoll down to the "Text File" entry and click that. It will turn the editor into a text editor. Then go to File → New and select "Terminal". It will now have two tabs niside the interface, one labelled "untitled.txt" and the other labelled "Terminal 1":

JupyterLab

To make our lives easier, let's rearange things so that we can see the text editor at the same time as the terminal. Do this by pressing and holding down the left mouse button on the tab that says "Terminal 1" and slowly dragging it to the right-hand side of the window. You'll see a blue outline like this:

JupyterLab

Release the mouse button and you'll end up with the two showing side-by-side:

JupyterLab

Finally, we want to open a Python Console for doing some interactive exploration in. Open this by going to File → New → Console. When it asks you to select a "kernel", choose Python 3 which should be the default. Drag that tab so that it is overlapping with the "Terminal 1" pane. You should have it looking something like:

JupyterLab

This layout is just a recomendation and if you'd rather do it differently, that's fine. Also if you're comfortable using an IDE like VS Code, PyCharm or Spyder then you're welcome to.

Python Console

The Python console (historically referred to as the IPython console) is a place to experiment with Python and try things out before comitting them to a Python script. It allows you to easily find things out about the code you're writing such as what functions are available and what they do.

The Console runs code as you ask it to, rather than you writing a whole script and then running it in the terminal.

Click in the grey box at the bottom of the Console window and type into it:

In [1]:
print("Hello Python!")
Hello Python!

If you then press Shift-Enter you'll see the output printed directly below it. In these notes, you'll see a similar thing hapenning where any code that should be typed into the console having an "input prompt" next to it like In [1]:.

Next, make a list:

In [2]:
l = ["cat", "dog", "horse"]

You can access the value of l in the next cell:

In [3]:
print(l)
['cat', 'dog', 'horse']

Since interactive Python Consoles like this are used for exploring, it's very common to want to print the value of a variable. To that end, you can simply write the name of a variable in a cell and when you press Shift-Enter, the value will be output:

In [4]:
l
Out[4]:
['cat', 'dog', 'horse']

We can also use the Console to discover things about the objects we're dealing with.

For example, if we want to add some more elements to the end of this list, we might not be able to remember the name of the method. But, if you type l. in the prompt and press the "tab" key once you should see a little window pop up with a list of functions in it. Have a look through and you should find the append function listed. Use the arrow keys and enter key or click with the mouse to select the function you want. The prompt will now read l.append.

If we wanted to ask Python for some information about that function we can then follow it with a question mark (?):

In [5]:
l.append?

This will print some information which may help you understand a function. In this case, we can use it by passing it an object:

In [6]:
l.append("pig")
In [7]:
l
Out[7]:
['cat', 'dog', 'horse', 'pig']

In general I recommend that you use the Python Console to experiment while going through the examples and then use the text editor to write the answers to the exercises before running them in the terminal.

With that, we can now get on to learning some great new things about Python.