Open In Colab

Running Python Code

Python is a flexible language, and there are several ways to use it depending on your particular task. One thing that distinguishes Python from other programming languages is that it is interpreted rather than compiled. This means that it is executed line by line, which allows programming to be interactive in a way that is not directly possible with compiled languages like Fortran, C, or Java. This section will describe three primary ways you can run Python code: the Python interpreter, via Self-contained Scripts, or in the Jupyter notebook.

The Python Interpreter

The most basic way to execute Python code is line by line within the Python interpreter. The Python interpreter can be started by installing the Python language (see the previous section) and typing python at the command prompt (look for the Terminal on Mac OS X and Unix/Linux systems, or the Command Prompt application in Windows):

$ python
Python 3.5.1 |Continuum Analytics, Inc.| (default, Dec  7 2015, 11:24:55)
Type "help", "copyright", "credits" or "license" for more information.
>>>

With the interpreter running, you can begin to type and execute code snippets. Here we'll use the interpreter as a simple calculator, performing calculations and assigning values to variables:

>>> 1 + 1
2
>>> x = 5
>>> x * 3
15

The interpreter makes it very convenient to try out small snippets of Python code and to experiment with short sequences of operations.

terminal.PNG

A Picture of the Terminal

Self-contained Python scripts

Running Python snippets line by line is useful in some cases, but for more complicated programs it is more convenient to save code to file, and execute it all at once. By convention, Python scripts are saved in files with a .py extension. For example, let's create a script called test.py which contains the following:

# file: test.py
print("Running test.py")
x = 5
print("Result is", 3 * x)

To run this file, we make sure it is in the current directory and type python filename at the command prompt:

$ python test.py
Running test.py
Result is 15

While it is technically possible to write self-contained scripts in a normal .txt file, most programmers use so called IDEs (Integrated Development Environments) such as VSCode, Thonny or PyCharm for writing code. IDEs are coding environments, which usually incorporate tools such as syntax highlighting, automatic code correction, or file management that make the process of programming as easy as possible.

vscode.PNG

An IDE with integrated syntax highlighting and file management

The Jupyter notebook (what you are reading right now)

A useful hybrid of the interactive terminal and the self-contained script is the Jupyter notebook, a document format that allows executable code, formatted text, graphics, and even interactive features to be combined into a single document. Though the notebook began as a Python-only format, it has since been made compatible with a large number of programming languages, and is now an essential part of the Jupyter Project. The notebook is useful both as a development environment, and as a means of sharing work via rich computational and data-driven narratives that mix together code, figures, data, and text.

This coding crashcourse is entirely written in Jupyter notebooks that are hosted on the Google Colab platform. Google Colab provides an easy solution to experiment with your code directly in your browser. This means there is no need to install anything and collaborating becomes much easier.

In essence, a jupyter notebook consists of text cells with explanations as well as code cells that can be run by pressing either the play button or Shift+ENTER (try it out below).

In [ ]:
# This is a code cell
# Press the play button or Shift + Enter to run the code below

a = 1
b = 2

print("The Sum of a and b is", a+b)
The Sum of a and b is 3

Note: The notebooks of this crashcourse can be edited without changing the original file, allowing you to practice by yourself. You can even save changes to your personal Google Drive.

To access/run the notebooks just click on the respective Google Colab links. If you get the warning “This notebook was not authored by Google” don’t worry, just press “Run Anyway”. If you want to know more about Google Colab Notebook, feel free to explore this notebook provided by Google.