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.
You can jump ahead to any chapter:
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 Anaconda which is a piece of software which includes Python along with lots of other tools. It is freely available for Windows, MacOS and Linux.
Once Anaconda is installed, start "Anaconda Navigator" and press the JupyterLab "Launch" button on the main screen:
This will open JupyterLab in your default web browser and will look something like this:
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, scroll 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 inside the interface, one labelled "untitled.txt" and the other labelled "Terminal 1":
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:
Release the mouse button and you'll end up with the two showing side-by-side:
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:
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.
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:
If you then press Shift-Enter you'll see the output printed directly below it. In these notes, you'll see a similar thing happening where any code that should be typed into the console having an "input prompt" next to it like
Next, make a list and again run the cell with Shift-Enter:
l = ["cat", "dog", "horse"]
You can access the value of
l in the next cell:
['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:
['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 on your computer's keyboard (the one with the arrow like ⇥ on it) 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
If we wanted to ask Python for some information about that function we can then follow it with a question mark (
Signature: l.append(object, /) Docstring: Append object to the end of the list. Type: builtin_function_or_method
This will print some information which may help you understand a function. In this case, we can use it by passing it an object:
['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.