2.3. Using Spyder#

For those who are used to Matlab, Spyder’s interface should immediately look familiar. Spyder’s standard look is shown in Fig. 2.5.

Fig. 2.5 The three main panes of the standard Spyder interface.#

2.3.1. Writing code in the console#

Section A of Fig. 2.5 is the console. It executes one command at a time. It also shows the result of calculations. Try entering this simple calculation in the console:

1 + 2

The console will show the results just below your command:

3

You can also write multiple-line commands using CTRL+Enter. Both are executed, but only the result from the last command is printed:

1 + 2
3 + 4
7

To print the result of every command, we explicitly use the print command:

print("Hello world")
print("This is my first program")
Hello world
This is my first program

2.3.2. Writing code in a script#

Section B of Fig. 2.5 is the script editor. It is simply a text editor that allows saving code as a .py text file, to execute it all together later. You can execute a script by clicking on the “Run File” button (Fig. 2.6).

As an exercise, create a file that prints “Hello World”, save it as hello_world.py, and run it using the “Run File” button. You should see the text “Hello World” appear in the console.

Fig. 2.6 “Run file”, “Run current cell”, “Run current cell and advance”.#

2.3.3. Code cell#

When a script grows in length, it can be practical to run only one section at a time. A script can be split into cells using this sequence of characters that acts as a separator:

# %%

You can also name cells by adding a title next to the separator:

# %% Load results from previous acquisition

[...]

# %% Report these results in a local coordinate system

[...]

You can execute a cell by placing the cursor in that cell, then by clicking on the “Run current cell” or “Run current cell and advance” button (Fig. 2.6).

As an exercise, create a file named exercise.py that contains two cells. A first cell prints “Hello world”, and a second cell prints “This is my first program”. Run the cell of your choice using the toolbar icons.

2.3.4. Getting help#

Section C of Fig. 2.5 is a collection of various panes. The help pane is particularly helpful to navigate the documentation of a given module or package. For example, to better understand how to use the Python max function, write max in the help pane.

Fig. 2.7 Getting help on a function using Spyder’s help pane.#

Note that you can also get help from the console, using the help function:

help(max)
Help on built-in function max in module builtins:

max(...)
    max(iterable, *[, default=obj, key=func]) -> value
    max(arg1, arg2, *args, *[, key=func]) -> value
    
    With a single iterable argument, return its biggest item. The
    default keyword-only argument specifies an object to return if
    the provided iterable is empty.
    With two or more arguments, return the largest argument.