Lesson 2 of 3
How Python runs in your browser here, the difference between scripts and the interactive prompt, and how to install Python on your own computer.
About 15 minutes
print() to see results in a scriptIn SPASCLE you don't need to install anything. Python runs directly inside your web browser, on your own computer, using a version of Python built for browsers called Pyodide. Your code is not sent to a server.
Every example with a Try it hands-on button opens an editor. It has the following parts:
Ctrl + Enter (or Cmd + Enter on a Mac)..py file, and Reset the original example.The first time you press Run, Python itself has to load, which takes a few seconds. After that, programs run almost instantly. Try it with this program, which prints the version of Python that is running.
import sysprint("Python version:", sys.version.split()[0])import sys loads sys, a module that comes with Python and gives information about the Python system. You will learn how modules and import work later in the course.
A Python program saved in a file is called a script. Script files end in .py, for example density.py. When you run a script, Python reads the whole file from top to bottom and carries out each statement. The editors in this course work the same way.
In a script, Python only shows something if you ask it to with print(). A calculation on its own is worked out, but the result is not displayed. This surprises many beginners, so try it:
1085 + 273.15 # calculated, but not shownprint(1085 + 273.15) # calculated and shownOnce Python is installed on your computer, you can also use the interactive prompt (also called the REPL). You type one statement at a time after the >>> symbol and see the result immediately, without print(). It is handy for quick calculations and for testing small ideas. For anything you want to keep, write a script.
Everything in this course works in the browser, but for your own research you will want Python installed locally. These are the common options:
| Option | What it is | Good for |
|---|---|---|
| python.org | The official Python installer for Windows, macOS and Linux. | A clean, minimal installation. |
| Anaconda / Miniconda | A Python distribution that includes the conda package manager. Anaconda also bundles hundreds of scientific packages. | Scientific work: NumPy, Matplotlib and others are easy to install. |
| VS Code or PyCharm | Code editors with features such as autocompletion and debugging. | Writing and organising larger projects. |
| JupyterLab | Notebooks that mix code, results, plots and notes in one document. | Exploring and analysing data step by step. |
To run a script from a terminal (Command Prompt on Windows), move to the folder that contains it and type python density.py. On some systems the command is python3.
Python comes with a large standard library, but much of its scientific power comes from packages: collections of code written by others that you install separately. NumPy and Matplotlib are packages. On your own computer you install them with a package manager, for example pip install numpy or conda install numpy. In SPASCLE, the packages used in the course are loaded automatically when your code needs them.
You can skip installation for now
Choose an answer and press Check answer. You can retry as often as you like.