Two of the central building blocks of the EarthAI Notebook environment are Jupyter Notebooks and the Python programming language. This article will guide the Python programming novice through some of the most basics context and syntax.
Note: if you would like to follow along with the code blocks in this tutorial, you can download the companion notebook attached at the end of this article.
Python Overview
Python is a widely used programming language in data science, machine learning and artificial intelligence, data visualization, web development and a wide range of other use cases. The "Swiss Army Knife" aspect is part of the reason for its widespread use in many fields.
Python is an interpreted language. Unlike compiled languages, such as C++, interpreted languages do not first go through a compilation step before they are executed. Python is executed more "on the fly" through its interpreter. Compiled versus interpreted is beyond the scope of this crash course in Python, but the interpreted nature of Python lends itself nicely to the Notebook environment where experimenting with code and quickly seeing the results is a central design trait.
Primitive Data Types and Variable Assignment
Like other programming languages, Python relies on a few primitive data types to build up more complex expressions and data structures. First, there are integers or whole numbers, such as 1, 2, and 5413. Next, there are floating point numbers that are often termed floats, such as 3.14 and -45.1283. Essentially, these are decimal numbers. A string is a sequence of alphanumeric characters surrounded by single or double quotes, such as "Hello". Finally, there is a boolean data type that is either True or False and results from evaluation of comparison statements like 1 > 2
.
Variable assignment in Python is straightforward. Unlike "typed" languages, such as C, where you have to declare the variable data type when creating it, Python instead infers the type from the type of the assigned value. Variables can be assigned to any data type or data structure.
my_value = 2
After running the code cell above, the variable my_value is a container that holds the value of 2. You can now refer to the variable name in place of the value in other expressions and statements throughout your code. We said that a type need not be declared when creating a variable. So we can check the type of my_value which should be of type int since 2 in an integer:
type(my_value)
Checking the data type of the variable my_value using the type
function does indeed return a type int. We can do anything with my_value that we could do with the number 2 in Python as my_value is a reference to 2.
my_value * 2
my_value * my_value * my_value
We can reassign the variable my_value to a new value that need not be the same data type.
my_value = 3.14
my_value
type(my_value)
Reassigning my_value to 3.14 not only changes the value of the variable, but also the type. We can also reassign my_value using my_value itself in an expression:
my_value = my_value + 1 my_value
String data types can also be assigned to variables:
my_string = 'Astraea'
type(my_string)
Using single or double quotes is equally valid in constructing a string, but note that you cannot mix them or an error will be thrown:
my_num_string = "1234"
You can perform a few math-like operations on strings that combine or concatenate strings:
my_string * 2
Multiplying a string by a integer essentially copies the string a number of times. One of the most common string operations is adding or concatenating two or more strings together.
my_string + ' ' + my_num_string
In the case above we added a blank space character string. Note however that strings cannot be combined with other data types.
Combining a string and an integer throws a TypeError. But what about combining a float and an integer?
3 + 4.3
This evaluates to a float. So under the hood the integer 3 is type converted to a float. The result of combining a float and an integer will always result in a float. There are functions for explicit type conversion if you need them. Below the float 4.3 is converted to an int.
int(4.3)
int(5.8)
As you can see type converting between a float and integer is not a rounding process. The decimal portion is simply truncated.
float('3')
type(float('3'))
Strings can also be converted to floats and integers or vice-versa.
Finally, there is the Boolean data type. This is either True or False upon evaluation of a logical expression.
# A double equal sign evaluates equality whereas a single equal sign is used for assignment. 1 == 1
So the above evaluates to True as 1 does indeed equal 1. We've also added a comment into a code cell. This is common practice in programming and in Python is accomplished by preceding text intended for comments with a #
. Multi-line comments in a code cell begin and end with three backticks:
'''This is a long multiline comment. This shows you how to add a lot of text to a code cell.''' 1 == 2
You can also check the equivalence of strings.
'Dog' == 'Dog'
'Dog' == 'Cat'
You can also evaluate compound logicals. So True and False expressions evaluate to False whereas True or False expressions evaluate to True. These are just a couple of examples of how Boolean expressions in Python can be used.
(3 > 2) & (4 > 5) # The & stands for the "And" comparison.
(3 > 2) | (4 > 5) # The | stands for the "Or" comparison.
Python Data Structures
The primitive data types in Python can be combined into more complex data collections.
Lists
Perhaps the most common Python data structure is the list. A Python list is just a collection of either homogenous or heterogenous data types. A list is constructed with square brackets []
with list elements separated by commas.
my_list = [1, 1, 2, 3, 5, 8] # homogeneous data type list.
type(my_list)
A list in Python is of "list" type and can be assigned to a variable.
my_mixed_list = [1, 'hi', 3.4, True, 42] # heterogeneous data type list.
my_mixed_list * 2
Like strings lists can be concatenated.
my_combined_list = my_list + my_mixed_list my_combined_list
One of the most powerful operations in a list is accessing specific elements of that list. This is done by referencing the index or location of the list element. One note on Python is that indexing starts at 0. So the first element in a list is at index 0. You simply access the element by passing the list name followed by the index in square brackets.
my_list[0] # this returns the first element of my_list.
You can check the length of a list. We see that there are six elements in my_list.
len(my_list)
You can select multiple elements by use of a colon. The first index is inclusive the last is not. So if you wanted to select the first three elements of my_list you would use my_list[0:3]
to obtain element indices 0, 1, 2.
my_list[0:3]
Strings also behave as lists in the case of indexing. String elements can be accessed by index or position as lists are.
my_string
my_string[2]
But an important difference between strings and lists is that strings are immutable. Elements in strings cannot be reassigned whereas they can in lists.
If we wanted to change the second element in my_list from 1 to 'Earth' would simply reassign that element.
my_list[1] = 'Earth'
my_list
We can also delete and append elements to a list.
my_list = [1, 2, 3, 4] my_list.pop(1) # This removes the index 1 element from the list. print(my_list)
my_list.append('hi') # This appends the string 'hi' to the end of the list. print(my_list)
Finally, lists can be nested in other lists. There is no practical limit to the amount of nesting that can happen in a list.
my_new_list = [1, 'hi', [1, 2, 3, 4], 9.87]
my_new_list[2] # the third element of "my_new_list" is a list.
How do we access the second element in the third element of my_new_list?
my_new_list[2][1]
Tuples
Tuples are data collections that are similar to lists except that like strings they are immutable. Once a tuple is created its elements cannot be changed. Tuples are formed with ()
and elements separated by commas. Tuples are used to store data that should not change during execution of a Python script.
my_tuple = (1, 2, 3, 4, 5)
my_tuple[0] # Tuple elements are accessed as those in lists are.
Dictionaries
The final data collection type is a dictionary. Dictionaries are constructed with key-value pairs within braces {}
. For lists and tuples elements are accessed by their index. Values in dictionaries are accessed by their associated key.
my_scores = {'Fred': 65, 'Sally': 98, 'Sven': 87, 'Jeb': 90} type(my_scores)
my_scores['Fred'] # If we want Fred's score we just use the 'Fred' key.
As with lists we can change (or mutate) values or add new key-value pairs to an existing dictionary.
my_scores['Fred'] = 70 my_scores
my_scores['Amanda'] = 95 my_scores
my_scores['Tim'] = 'did not show up' # we can also mix data types in the values
my_scores
Functions and Methods
This last section covers functions and methods. Functions are reusable blocks of code. Methods are basically functions that are associated with specific object types in Python.
We've already seen functions in use when we ran type(my_list)
above. We were using the type
function. Likewise, when we ran my_list.append('hi')
we were using the append
method on the object my_list.
Objects can have specific methods associated with them. For instance, the object type string has a method toUpper()
that capitalizes the first element in a string. The float object does not have this method. When creating classes of objects the user can define methods of their own to act on those objects.
Functions
First, we'll write a simple function that prints "Hello, Astraea!" when it is called and then call that function in the following cell.
def my_function(): print('Hello, Astraea!')
my_function()
All Python functions have the same structure. There is first the def
followed by whatever function name you want to give it followed by parenthesis. The function parameters go inside the parenthesis. In this simple function there are none. A colon follows the parentheses to indicate the start of the function definition.
Unlike other languages, whitespace before lines of code that are part of the function play a critical role in Python. Whitespace is automatically added when you hit Return after the function declaration in a Jupyter Notebook, but if you were working in a code editor that did not automatically do that you need to add whitespace manually, otherwise you get a syntax error.
Once the function is defined it can be used at any time by simply calling it. Functions are first-class citizens in Python. That is you can assign them to variables and pass them to other functions.
my_variable = my_function
my_variable()
Now let's write a slightly more interesting function that requires an input parameter. We'll write a function that sums the elements for a list that is passed to the function.
def listSum(list): sum = 0 for item in list: sum += item return(sum)
my_list = [1, 2, 3, 4,] listSum(my_list)
The above function is called listSum
and takes one parameter we've chosen to name list
. We could've called the parameter anything but it is recommended to make parameter names descriptive. The first thing we do within the function is define a variable called sum and set it to 0. Basically, we need to initialize our summing variable. Now we run a for loop over all of the items in list and add them to the running sum. Again we could have named item anything as long as we're consistent throughout the function code block. The final step is we return the sum at the end of the loop.
We then construct a list called my_list and call listSum
passing my_list as the function parameter. The function returns the expected value of 10, which is the sum of the my_list elements.
Importing Libraries
Functions are critical in Python as they provide reusable code blocks that eliminates the need for repetitive coding. However, in EarthAI workflows and indeed in many Python workflows most needed functions will have already been written and accessible through a rich collection of Python libraries. Accessing these libraries and their functions is as simple as importing the libraries into your Notebook workspace once they are installed. One important benefit of the EarthAI notebook environment is most of the critical libraries for Earth Observation analyses come pre-installed, so you need only import the library as needed. Once a library is imported it is available throughout the Notebook session.
The code cell below imports a library called Folium that is used to build interactive maps.
import folium as f
We now have access to all of the functions or methods in the Folium library. Oftentimes libraries will be imported with an alias to simplify coding. Basically, just less typing. In this case we've aliased folium as "f". The functions available in a library can be seen by simply typing the library name followed by a .
and Tab in a code cell. This dot notation is common where the library name is followed by a dot to access the functions and methods for the library.
Below we'll create as simple interactive map using the Map
function in Folium. The single input parameter is a location variable with latitude and longitude coordinates. However, there are many more input parameters underneath the hood. When typing f.Map(
after the open parenthesis hitting Shift+Tab pulls up a dialogue box listing all of the possible input parameters to the function as well as more information about the function behavior. The other parameters are automatically set to default values if omitted in the function call. Also, we can assign the output of the function to a variable. You now have a fully interactive map in your Notebook!
m = f.Map(location = (45.5236, -122.6750)) m

You could have written your own Map
function from scratch, but utilizing existing libraries and functions is a key component to streamlining Python workflows. The EarthAI Notebook environment has been designed to make many of the necessary Python libraries for Earth observation workflows easily accessible to the user. This includes the earthai library designed specifically for acquiring and analyzing Earth observation data through the Astraea platform.
Comments
0 comments
Please sign in to leave a comment.