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:
Open a Python Console in Futurecoder:
>>>
(three angle brackets) called the
prompt.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
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 are ideal for creating re-usable programs.
print('Hello world')
Run
button to run your scriptHello world
printed again in the
console.1 + 1
1 + 1
.1 + 1
, change the line in your
script to be: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 withpython my_script.py
(assuming you have usedcd
commands to move your terminal session into the same folder asmy_script.py
). It is is important to remember to save changes to your script files before you re-run them!
To create a new notebook in Google Colab:
New Notebook
linkpynoon_starter_1.ipynb
- helpful names are important!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:
+ Text
button at the top of the notebookMy First Notebook
shift+enter
, and see the text displayed.You can add formatting in Markdown cells:
My First Notebook
to edit the cell#
at the beginning of the line:# 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.