Variables, data types, and structures
Contents
Variables, data types, and structures¶
Welcome to the first demo of the course! In these demos, the instructor will work through some key programming concepts and demonstrate with plenty of examples. Understanding these concepts will be required to complete the weekly assignments.
Variables¶
This is great but to do something more useful with data, we need to assign its value to a variable. Variables are one of the fundamental building blocks of Python. A variable is like a tiny container where you store values and data, such as filenames, words, numbers, collections of words and numbers. In Python, we can assign a variable using the equals sign =
. For example, we can track the height of a tree by assigning an integer value of 30
to a variable height_m
.
height_m = 30
We can also make a string variable by adding single ('
) or double quotes ("
) around some text.
tree = 'douglas_fir'
Note
Python is “dynamically typed” meaning that it automatically interprets the correct data type at run-time
![trees](../_images/trees.jpg)
Variable usage¶
Once we have data stored with variable names, we can make use of it in calculations. We may want to store our tree height value in feet as well as meters
height_ft = height_m * 3.281
Likewise, we might want to add a suffix to our tree so we can identify it later.
tree1 = tree + '_1'
Variable names¶
It is good practice to make variable names as descriptive as possible. They can include:
✅ upper and lower-case letters (a-z
, A-Z
)
✅ digits (0-9
)
✅ underscores (_
)
However, variable names cannot include:
❌ other punctuation (-.!?@
)
❌ spaces (
)
❌ a reserved Python word (e.g. print
, type
)
Built-in functions¶
Python has a number of built-in functions to carry out common tasks with data and variables. For example, we can use the print
function to display information to the screen.
print(height_ft)
98.43
print(tree1)
douglas_fir_1
Python also has a built-in function called type
which outputs a value’s data type
type(height_ft)
float
type(tree1)
str
The three most common data types that we will come across in Spatial Data Science are integer numbers (int
), floating-point numbers (float
) and strings (str
). We may also encounter boolean data types (bool
) which may have one of two values, True
or False
.
Data Type |
Explanation |
Example |
---|---|---|
String |
Text |
|
Integer |
Whole numbers |
|
Float |
Decimal numbers |
|
Boolean |
True/False |
|
We can use another built-in function to change the data type of our variable (e.g. to int
).
int(height_ft)
98
evergreen = True
type(evergreen)
bool
int(evergreen)
1
A full list of built-in functions and their usage can be found here.
Lists¶
Sometimes we might want to store a collection of items in a single variable. The simplest type of collection in Python is a list
which can be defined using square brackets [
and commas ,
.
places = ['Eugene', 'Veneta', 'Noti', 'Mapleton', 'Florence']
type(places)
list
Indexing¶
We can access individual items in a list using an index value. An index value is a number that refers to a position in the list. We can access the second value of the list by running:
places[1]
'Veneta'
We can find the number of items in a list using the len()
function.
len(places)
5
If we know the number of items in the list, we can access the last item by running:
places[4]
'Florence'
But if the number of items in the list changes, we would have to update our code. A more robust way of finding the last item of the list is to run:
places[len(places) - 1]
'Florence'
We can also refer to the last item using negative numbers which index the list in reverse. The shortest, most Pythonic way to find the last element is a list is therefore to use -1
.
places[-1]
'Florence'
Slicing¶
Slicing is similar to indexing except that we are looking to return a subset of items based their indices. We use a colon :
to slice lists. For example, we can return the second and third items from out list.
places[1:3]
['Veneta', 'Noti']
Leaving either side of the colon blank means start from (or go to) the end of the list. For example:
places[3:]
['Mapleton', 'Florence']
places[:3]
['Eugene', 'Veneta', 'Noti']
Stepping¶
We can use double colons ::
to set the interval at which items are included in the slice. So we can get every second item in the list (starting from index 0
by running:
places[::2]
['Eugene', 'Noti', 'Florence']
If we wanted to start at index 1
, we could run:
places[1::2]
['Veneta', 'Mapleton']
List methods¶
Methods are similar to the built-in functions we used earlier but are called on the object itself. A complete list of list methods can be found here. Here are some examples.
We can add items to a list using the .append
method.
places.append("Coos Bay")
print(places)
['Eugene', 'Veneta', 'Noti', 'Mapleton', 'Florence', 'Coos Bay']
Other useful list methods¶
We can use methods to count the number of occurences of an item:
places.count('Eugene')
1
Find the index value of a given item in a list:
places.index('Eugene')
0
Reverse list
places.reverse()
print(places)
['Coos Bay', 'Veneta', 'Noti', 'Mapleton', 'Florence', 'Eugene']
Sort a list alphabetically
places.sort()
print(places)
['Coos Bay', 'Eugene', 'Florence', 'Mapleton', 'Noti', 'Veneta']
Remove an item from the list
places.remove('Coos Bay')
print(places)
['Eugene', 'Florence', 'Mapleton', 'Noti', 'Veneta']
Note
Some methods return numbers so we can use the =
sign to assign the output as a variable (e.g. number = places.count('Eugene')
). Other methods update the variable automatically so we don’t need an =
sign.
Acknowledgments¶
This lesson was inspired by Programming in Python lessons by Software Carpentary and the Geo-Python course taught at the University of Helsinki.