Welcome to the first tutorial in this DEA for Geospatial Analysts course. In this section we introduce the Jupyter notebook environment. Jupyter offers an interactive platform to run Python code in your browser without the need of installing or configuring anything on you computer.

This is an interactive environment where you can combine text with with Python code that you can modify and execute. The gray cell below contains Python code that you can run. Place the cursor on the |► Run| button in the menu at the top of the notebook to execute the code inside.

Tip: Use the shortcut Shift-Enter for running the active cell (where your cursor is). It is a convenient ’trick’ that saves you from having to click on the Run icon each time.

print("Welcome to the DEA Sandbox!")

You'll see that, once the code has executed, any output generated by your code will show up just below the cell

print("3,2,1")

# Use the hash symbol to comment lines of your code. This does not get executed

print("this is fun!")

The code that you execute using the DEA Sandbox [Binder, Colab] does not run on your local computer. It runs in a remote server and you just receive the output of the code that gets presented in this browser's window.

Tip: The last line of code in a cell is evaluated and printed by default, so there is no need to type print() to show the value of a variable in the last line

# Sending a complicated operation to the server
result = 999 + 1

# This operation goes to the server and we just display its output in our screens once is completed
result

The [ ]: symbol to the left of each Code cell describes the state of the cell:

  • [ ]: means that the cell has not been run yet.
  • [*]: means that the cell is currently running.
  • [1]: means that the cell has finished running and was the first cell run.

Sometimes the code that you run in a cell takes a while to compute because it loads a large dataset or performs complex computations. You'll notice the number that appears next to the cell. Before the cell is run you'll see the symbol [ ] meaning that that cell has not been executed yet. While a cell is running it shows the [*] symbol and once it has completed running you'll see a number representing the number of cells being run, for example [4]. This allows you to keep track of the cells that have been run and their relative order.

Note: To check whether a cell is currently executing in a Jupyter notebook, inspect the small circle in the top-right of the window. The circle will turn black ("Kernel busy") when the cell is running, and return to white ("Kernel idle") when the process is complete.

# This is how a library gets imported in Python but don't worry about this for the moment:
import time

# Very complicated and long operation computed on the server -- not really we are just waiting or sleeping for 5 seconds :-)
time.sleep(5)

print("Done!")

Jupyter notebooks are an easy way to write Python code, although they work a little bit different to how traditional programs are written and executed. For example, consider this simple Python program:

a = 1
print(a)
a = a + 1
print(a)

We can break down this program and execute each line using separate cells:

a = 1
a
a = a + 1
a

If you run again the first a cell you'll see that it returns 2, the updated value. This is called global state and means that once a variable is declared it is accessible anywhere in the notebook, even in cells above where it has been declared. This is different to traditional programs which execute sequentially line by line from the top to the bottom and can be confusing in the beginning.

Jupyter notebooks are designed for interactivity and testing ideas fast. You can modify all the code in the cells and run them as many times as you want in any order (you can jump back and forth to update variables or re-run analysis)

message = "My name is ******"

message

You can also add new cells to insert new code at any point in a notebook. Click on the + icon in the top menu to add a new cell below the current one.

Tip: There is a shortcut to add new cells by pressing Esc + a to add a cell just above and Esc + b to add a cell below.

a = 365*24

# Add a new cell just below this one to print the value of variable `a`

The rest of the units in this course use Jupyter notebooks to introduce and get some practice with the different libraries and functionality required to understand the DEA Python interface. After a new concept is introduced there will be cells where you need to answer questions or solve small problems. To check your answers we are going to use an interactive way of

Exercise 0.1: The following cell contains some code. To complete this exercise you need to update the contents of the operation 2 + 2 for variable a. Delete the current value 0 and write the new value, then execute the cell and see the response.

from check_answer import check_answer

a = 0
check_answer("0.1", a)

Saving and exporting your work. Jupyter Notebooks are automatically saved every few minutes. To actively save the notebook, navigate to "File" in the menu bar, then select "Save Notebook" or click the 💾 "save" icon on the left of the graphical menu.

One last thing, you can use code cells to run commands as if you were using a terminal using the ! character before the command. This is a Linux system and to list the contents of the current directory you'll need to use ls

!ls

You are now ready to start learning Numpy!