PyNoon Starter Lesson 1 - Tutorial

Intro to Python Coding

The following tutorial is an introduction to writing and running Python code in the futurecoder.io and Google Colab/Jupyter environments, loosely based on swcarpentry.github.io/python-novice-gapminder/01-run-quit.html, (but more streamlined - not covering topics like shortcut keys).

In this tutorial, we will look at three ways of running Python code inside JupyterLite:

The Python Console

Open a Python Console in Futurecoder:

Enter the following code, then press the enter key to run it:

print('Hello world')

Congratulations! You’ve just run your first Python program!

Note: Casing is important in Python, so make sure you write print in lower-case.

We can also use Python to perform maths; try running the following:

1 + 1
5 - 3

Note: The spaces around mathematical operators are optional, but they are important to make the code more readable!

It is important to remember that any work you do in the console is lost when you close the console (or refresh the futurecoder webpage).

Tip: You can press the “up arrow” key in the Console to bring up lines you’ve previously run (saving you from typing them again).

Python scripts

Python scripts are ideal for creating re-usable programs.

print('Hello world')
1 + 1
print(1 + 1)
Hello world
2

Running python scripts locally: You can use a plain text editor (e.g. Notepad, Notepad++, Visual Studio Code) to create a Python script file that ends in a .py extension. If you have Python installed on your computer, you can run your script from the Terminal/Command Prompt with python my_script.py (assuming you have used cd commands to move your terminal session into the same folder as my_script.py). It is is important to remember to save changes to your script files before you re-run them!

Python Notebooks

To create a new notebook in Google Colab:

In the “cell” prompt at the at the top of your notebook, enter the following code, and run it with shift + enter (like at the Console):

print('Hello world')

The result is printed and a new “cell” appears below.

Run this code in the new cell:

1.5 + 10

Each time you press shift+enter, a new cell is created

You can edit and re-run cells; update your previous cell to have the following content, then re-run it:

(1.5 * 10) / (5 - 2)

Note that we can use * for multiplication, / for division, and group operations with parentheses()`.

You can add non-code cells for formatted notes:

My First Notebook

You can add formatting in Markdown cells:

# My First Notebook

Markdown code is similar to shortcuts supported by software like Slack and Teams

For example, add a bullet list with:

* item 1
* item 2
* item 3

You can re-order notebook cells

Notebooks also support rich output from Python code

%pip install plotly nbformat pandas
import plotly.express as px
px.bar(x=['a', 'b', 'c'], y=[1, 2, 3])

You should see an interactive plot

Try changing y=[1, 2, 3] in your cell to use different numbers, then re-execute the cell.

Note: Your notebooks will all be saved to your Google Drive account, you can find previous notebooks from the File -> Open notebook menu.