Python basics#

The goal of this section is to provide you with some general functions, definitions for our work in python in the context of this class. This is by no means a complete list but instead represents a collection that is pretty much designed for the use in the class. At the bottom of this page we have provided a number of links where you can dig deeper and/or where you can find additional answers to your specific question.

Variables#

In python, a variable represents a tag or reference that points to an object in the memory. There is no specific command in python for declaring a variable (unlike in other programming languages such as C++ or Java). A variable is created the moment you first assign a value to it. You should always try to name a variable so that you can recognizue it later in your coding exercise - it can carry a long name or a short name and you are free to choose, but there are a few rules for naming python variables. Specifically, a variable

  • must start with a letter or the underscore character,

  • cannot start with a number,

  • can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ),

  • is case-sensitive (age, Age and AGE are three different variables).

A variable could like the following:

nStudents = 15
className = 'geopy'
averageAge = 25.6 # this is an arbitrary number and has nothing to do with the true average age of the students in class
coolTeachers = True

Data types#

You can see above that all three variables have different types of values. We speak of this as different data types, which basically represents a set of possible values and a set of allowed operations on it. You are probably aware of the following data types:

Table 1
type python pandas
numpy
 integer int
 int64  int_,int8,int16,int32,int64
 float  float  float64  float_,float16,float32,float64
 string  str  object  string_
 bolean  bool  bool  bool_

These are the most common data types we will probably be working with during the course. Later on, we will get to know some more types of data, including lists, tuples, sets, and dictionaries, which are all considered data types in python. It also means that with these data types we can only do a certain set of operations.

You can check for the data type in python using the function type(), e.g.:

type(averageAge)
float

Operators#

Now that we know the type of variables, we want to know what operations we can apply with/to them. Python divides the operators in the following groups:

  1. Arithmetic operators \(\rightarrow\) e.g., \(+, -, *, /\)

  2. Assignment operators \(\rightarrow\) e.g., \(=\), \(+=\)

  3. Comparison operators \(\rightarrow\) e.g., =, !=

  4. Logical operators \(\rightarrow\) and, or, not

  5. Identity operators \(\rightarrow\) is, is not

  6. Membership operators \(\rightarrow\) in, not in

  7. Bitwise operators \(\rightarrow\) e.g., &, |

See the two cells below for the application of the operators to some of the variables defined above. One works correctly without an error, the other one not. Try to read and understand the error message to see what went wrong:

op01 = nStudents + averageAge
op01
40.6
op02 = nStudents + className
op02
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[4], line 1
----> 1 op02 = nStudents + className
      2 op02

TypeError: unsupported operand type(s) for +: 'int' and 'str'

In addition to the data types dscribed above, there are a total of four more built-in data types: (1) tuples, (2) lists, (3) sets, (4) dictionary. Below is a summary of each of the four, alongside with some general operations you can apply to them.

Tuples#

The simplest one are tuples. A tuple is a collection of multiple objects, which is ordered (i.e., the items have a defined order, and that order will not change), unchangeable (i.e., we cannot change, add or remove items after the tuple has been created), and can contain items with the same value, as well as items of different data types. They are written using ().

tuple1 = ("python", "R", "java")
tuple1
('python', 'R', 'java')
tuple2 = (1, 5, 7, 9, 3)
tuple2
(1, 5, 7, 9, 3)
tuple3 = (True, False, False)
tuple3
(True, False, False)
tuple4 = ("python", 1, True)
tuple4
('python', 1, True)

You can see, that tuples represent another data type, by calling:

type(tuple4)
tuple

Note, enclosing a single value in parenthesis does not create a tuple.

not_a_tuple = (3)
type(not_a_tuple)
int

To create a tuple of one element, you need to follow the element with a comma:

my_tuple = (3, )
type(my_tuple)
tuple

Lists#

Lists are slightly different from tuples. While they are also ordered and allow duplicate values, in comparison to tuples they are changable, meaning that we can edit (e.g., add multiple elements to it and update or delete elements) them after they have been created. For example, think about an operation that you want to repeat several times and you want to store the results in a collection. In that context, lists are one thing you can use. Lists are written with [].

list_of_ints = [11, 13, 26, 90, 5, 22, 13]
list_of_ints
[11, 13, 26, 90, 5, 22, 13]

Below are now a few functions/methods, associated with with lists:

# access item
list_of_ints[0]
11
# add item to list
list_of_ints.append(87)
list_of_ints
[11, 13, 26, 90, 5, 22, 13, 87]
# remove item from list --> here '3' is the index of the item we want to remove
list_of_ints.pop(3)
list_of_ints
[11, 13, 26, 5, 22, 13, 87]

A list can also contain lists with data. This is important, when an item is composed of several pieces of information. Like with 1D-lists, one can access each individual item via indexing. This is also a disadvantage of lists, as for accessing individual items, one needs to know the correct index.

ints_and_strings = [['item1', 4], ['item2', 4], ['item3', 5]]
ints_and_strings[0][0]
'item1'

Sets#

Sets are the data type we will be least working with - we are rarely (if at all) using them in our course, and we present them only for completeness purposes. Sets represent collections which are unordered, unchangeable, and are unindexed. Unordered means, that you cannot be sure in which order the items will appear. They can appear in a different order every time you use them, and cannot be referred to by index or key. Sets are written with {}.

set1 = {"python", "R", "java",  1, True}
set1
{1, 'R', 'java', 'python'}

Dictionaries#

A more interesting data type are Dictionaries. Dictionaries are ordered, changable and they cannot contain duplicates. Dictionaries are at the base of certain data types (json, geojson, ee.FeatureCollection() ). They are written with curly brackets, and have keys and values:

profs = {"Dirk": ['EOL', 8], "Matthias": ['Biogeo', 4]}
profs
{'Dirk': ['EOL', 8], 'Matthias': ['Biogeo', 4]}

Important in the context of dictionaries is the following:

  • The keys of a dictionary are always unique

  • Once a key-value pair is added to the dictionary, then we cannot modify the key, but we can change value associated with it.

  • We can update a dictionary adding additional entries, there are two ways of doing this:

profs['Patrick'] = 'EOL'
profs.update({'Tobias': ['Biogeo', 9]})
profs
{'Dirk': ['EOL', 8],
 'Matthias': ['Biogeo', 4],
 'Patrick': 'EOL',
 'Tobias': ['Biogeo', 9]}

Loops#

We are now set with data types including the built-in data tyes. What you have noticed, however, is that we did all operations individually and sequentially. Though, in scripting or programing we want to repeat operations many times in an automated way. One way to do this is the use of loops. They are great to repeat the same operation several times. python offers two general types of loops:

while-loop#

A while-loop tells python to repeat an operation as long as a defined condition is true. The construction of a while-loop is very intuitive: it starts with a while keyword, followed by a condition and a colon in the end. After the while-statement, the block of the while loop starts. It includes a group of statements with one indent level. In python, the indentation is extremely important. Compared to e.g., \(\textbf{R}\), python does not use any additional characters (e.g., {}) to define where a loop starts or ends - it is purely defined by the indentation.

i = 1
g = 15
while i < g:
    print('i equals', str(i), 'which is smaller than ', str(g))
    i += 1
print('Now, i equals', str(i), 'which is not smaller than ', str(g))
i equals 1 which is smaller than  15
i equals 2 which is smaller than  15
i equals 3 which is smaller than  15
i equals 4 which is smaller than  15
i equals 5 which is smaller than  15
i equals 6 which is smaller than  15
i equals 7 which is smaller than  15
i equals 8 which is smaller than  15
i equals 9 which is smaller than  15
i equals 10 which is smaller than  15
i equals 11 which is smaller than  15
i equals 12 which is smaller than  15
i equals 13 which is smaller than  15
i equals 14 which is smaller than  15
Now, i equals 15 which is not smaller than  15

for-loop#

A for-loop in python (or elsewhere) is useful when you want to iterate over a sequence of elements. The construction of such a loop is also very intuitive, and we also need to pay attention to the indentation level. Below we present a simple operation inside a for-loop based on the list_of_ints we previously defined: we multiply each element of the list by five and print both numbers to the screen:

list_of_ints
[11, 13, 26, 5, 22, 13, 87]
# Mutliply each element with 5, print
for i in list_of_ints:
    print(i, i*5)
11 55
13 65
26 130
5 25
22 110
13 65
87 435

List comprehension#

List comprehensions are a elegant way to compress a list-building for loop into a single line. It is comparable to the mathmatic notation of sets \(\rightarrow\) in mathmatics, the quadratics of natural numbers are defined as: \(\{x^{2} | \text{ x } \in \mathbb{N}\}\)

[n ** 2 for n in range(1,12)]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121]

The statement is almost readable in plain English: “construct a list consisting of the square of n for each n up to 12”. * This basic syntax is: [**expr** for **var** in **iterable**], where

  • expr is any valid expression. we are free to do whatever we want here.

  • var is a variable name

  • iterable is any iterable Python object.

Functions#

Functions are generally helpful when you realize that you reuse the same bits of code over and over. Functions have the advantage that they can (a) be easily transfered between scripts, (b) developed towards “tools”. A function always starts with def followed by the function name. In the parentheses come the arguments that the function requires; these are followed by a colon. Inside the function (with an indent) we write the block of the function. The return-statement defines which value the function should return. Below is an example for a function that calculates the square value of a given input value, and returns a list containing the input value and the calculated squared value of the input.

def Square(x):
    sq = x * x
    return [x, sq]
Square(9)
[9, 81]

You may expect, that we combine different concepts we have introduced. For example, we can loop over our list_of_ints and call the function.

for i in list_of_ints:
    print(Square(i))
[11, 121]
[13, 169]
[26, 676]
[5, 25]
[22, 484]
[13, 169]
[87, 7569]

if/else-clauses#

The last thing we want to look at are if/else-clauses. In python, by default, statements are executed in sequential order, i.e., one after another. Often, however, we don’t want to run all links sequentially, but we want to introduce conditions (i.e., do some decision making) and execute specific statements at the correct time (i.e., only when the condition is fullfilled. An if-statement allows for this. Thereby, the if keyword is always be followed by a conditional expression, which should evaluate to a bool value (i.e., either True or False). If the condition evaluates to True, then the interpreter executes the statements in the if-block. Whereas, if the condition evaluates to False, then the interpreter skips the lines in the if-block and jumps directly to the end of if-block. See below example and change the value of x to see how the statement changes

x = 6
print('Value of x is: ', x)
if x < 10:
    print('x is less than 10')
    print('x is a single digit number')
print('This is last line')
Value of x is:  6
x is less than 10
x is a single digit number
This is last line

An extension of the normal if-statement is the if/else. The basic thinking is the same, but instead of skipping the code block in case the condition evaluates to False, we introduce an alternative codeblock for the code to run. As above, change the value of x to see how the the output changes:

x = 4
if x < 10:
    print('x is less than 10')
else:
    print('x is larger than 10')
x is less than 10

Using the functions described here, you can already do quite a bit of stuff. However, just from reading these lines you won’t learn how to apply them. Thus: get your hands dirty and try out some things. For more comprehensive information and materials you can check out some websites/courses, such as:

  1. https://programming-23.mooc.fi/

  2. YoutubeChannel with smaller lessions