4. Basic Python#
This page is a Jupyter Notebook that can be found and downloaded at the GitHub repository.
The aim of the Jupyter Notebooks in this section, as with the rest of the documentation in this collection, is to give you a very brief whirlwind tour of how we might use Python to perform relevant data analysis.
It’s not going to be an extremely detailed introduction that will allow to start doing advanced science with no other support - but it will point you in right direction as to the correct tools for geospatial task. After identifying the most common/established methods, you can use this as a springboard for googling/LLMing a solution.
4.1. Jupyter Notebook blocks#
This is very basic Python that you can find at any tutorial, so I will cover it extremely quickly. If you’re running this Notebook, and not just viewing it online, you will be able to edit and run the code blocks (“cells”) yourself by clicking the ‘play’ button, or by pressing SHIFT+ENTER
.
# Click on this cell and press SHIFT + ENTER to run this code,
# which outputs the text 'hello world'.
print('hello world!')
hello world!
You can run code again and again. You can’t hurt anything - if you break it, you can reboot the notebook by clicking Kernel
in the tool bar, and Restart Kernel
and Clear Output of All Cells
.
4.2. Basic operations#
At the most basic level, we can use Python to do operations between the basic data types, which include numeric, string (text), and Boolean (True
/False
) data types.
# By the way, this text is called a "comment". It's a message to other
# humans. Starting a line with "#" tells the program not to read this
# line. The program will go to the next line that doesn't start with '#'
5 - 4
1
If we end a code block with either (a) a command that is not passed to a variable (e.g. 5 - 4
as opposed to a = 5 - 4
), or (b) a variable with no other instruction (e.g. a
), then the output will automatically be evaluated and printed below the code block. Hence, running the cell containing 5 - 4
output 1
.
We can see more of this below:
8 * 4
32
(7-3) * 5
20
5 == (1 + 4) # Two equal signs ('==') evaluates whether the left
# side is equal to the right side, and returns True/False
# (a data type known as a 'boolean' or 'bool')
True
We can string together more complex ideas by ‘storing’ values as variables:
# the following lines define variables called "a" and "b"
a = 4
b = 3
# the next line shows us what a plus b is.
a+b
7
name = 'Tom' # Now we're defining a 'string' rather than a number!
print('Hello, my name is ' + name)
Hello, my name is Tom
Variables are remembered between cells:
c = a*a # this line calculates a times a and saves the result as a varialbe called "c"
c # this line tells the program to show us what "c" is.
16
However, the cells need to be run in order for Python to work. In the first of the two cells below, we try and use a variable not_defined_yet before we define it. It will produce an error message. If you then run the second, where we define the variable, and then return to the first, it will work:
not_defined_yet ** 2 # Two asterisks raises the variable to a
# power - here, we're calculating the square.
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[9], line 1
----> 1 not_defined_yet ** 2 # Two asterisks raises the variable to a
2 # power - here, we're calculating the square.
NameError: name 'not_defined_yet' is not defined
not_defined_yet = 5
4.3. Basic data structures and operations#
We can combine our data types into data structures. Lists and Tuples are both chains of multiple items, with some slight differences in the background that give them slightly different purposes (you can explore this yourself). Dictionaries are used to store items in key:value pairs.
my_list = [1, 2, 3, 4, 5]
for i in my_list:
print(i)
my_dictionary = {
'name': 'Tom',
'language': 'Python',
'framework': 'Jupyter'
}
print('Hello, my name is ' + my_dictionary['name'])
1
2
3
4
5
Hello, my name is Tom
Note that above I used a ‘for loop’ to iterate through my list - it will go through the list one at a time, applying the same commands to each of them.
There are other ways we can interact with our data, such as while
and if
-else
statements. while
will continue looping through a set of commands until a condition is reached. if
statements will only perform a certain task if a condition is met.
i = 0
while i < 5:
if i == 3:
print('The number three!')
else:
print(i)
i = i + 1
0
1
2
The number three!
4
4.4. Functions#
We can begin to create more complex proceses using ‘functions’. We’ve already seen one function in this notebook - the print function above. However, it’s possible to define more functions:
# Define a function that will take a generic variable ('x') and return an output ('y')
def multiply_by_two(x):
y = x * 2
return y
Now, we can use this function elsewhere in the Notebook:
d = multiply_by_two(4)
d
8
The strength of Python’s open-source capabilities is that we don’t just need to rely on our own functions - instead, we’ll be using those made by others. We can import packages made by others, giving us access to new functions.
A default package included within the Python standard library is math, which provides the sort of functions you might expect:
import math
# Now we have `math`, we can call its functions in the form math.name_of_function(input)
e = math.sqrt(16) # Calculate the square root
e
4.0
This is only a brief insight into basic python, but I hope that it gets us going and that other tutorials can fill the gaps (e.g. the first two or three lessons of this course).