This page was generated from source/notebooks/day-1/python-basics.ipynb. Interactive online version: Binder badge

Basic elements of Python

This lesson aims to provide an overview of some of the basic elements in the Python programming language. In the first section these include how to define and use variables, the concept of a data type and how to determine the data type of a variable in Python, and Python lists. Later parts of the lesson introduce the NumPy numerical Python library, and the main conceptual programming components we will use: functions, loops, and conditional statements.

Please note that this is not intended to be a comprehensive introduction, but rather something to give you enough background to be able to work on the programming assignments later in this course. For more about basic Python programming we refer you to the Geo-Python course lessons, Codecademy, and the Interactive Python Tutorial.

Sources

This lesson is inspired by the Programming in Python lessons from the Software Carpentry organization.

About this document

This is a Jupyter Notebook designed to assess your understanding of the basic concepts of programming in Python. The contents of this document are divided into cells, which can contain Markdown-formatted text, Python code, or raw text. You can execute a snippet of code in a cell by pressing Shift-Enter. Try this out in the examples below.

Getting started

Presumably if you have made it this far, you have already opened this Jupyter Notebook on your own computer. If not, you can do the following to launch Jupyter Lab by typing the following in a terminal window.

$ jupyter lab

This should open a new Jupyter Lab session from which you can open this document by navigating its location in the Files tab and double clicking on it. After that you should be ready to go.

Variables, arithmetic and modules

We will start our Python lesson by learning a bit of the basic operations you can perform using Python.

1. Python can be used as a simple calculator.

Remember, you can press Shift-Enter to execute the code in the cells below. Try it out and see what you get.

In [1]:
1 + 1
Out[1]:
2
In [2]:
5 * 7
Out[2]:
35

If you want to edit and re-run some code, simply make changes to the cell and press Shift-Enter to execute the revised code.

2. The list of basic arithmetic operations that can be done by default in Python is in the table below.

Operation Symbol Example syntax Returned value
Addition + 2 + 2 4
Subtraction - 4 - 2 2
Multiplication * 2 * 3 6
Division / 4 / 2 2
Exponentiation ** 2**3 8

For anything more advanced, we need to load a module.

In [3]:
import math
In [4]:
math.sin(3)
Out[4]:
0.1411200080598672
In [5]:
math.sqrt(4)
Out[5]:
2.0

We can see a few interesting things above:

  1. A module, also known as a library, is a group of code items such as functions that are related to one another. Modules are loaded using import. Functions that are part of the module modulename could then be used by typing modulename.functionname(). For example, sin() is a function that is part of the math module, and used by typing math.sin() with some number between the parentheses.
  2. Within a given Jupyter Notebook the variables you define earlier in the notebook will be available for use in the cells that follow as long as you have already executed the cells.
  3. Modules may also contain constants such as math.pi.
In [6]:
math.pi
Out[6]:
3.141592653589793
In [7]:
math.sin(math.pi)
Out[7]:
1.2246467991473532e-16

3. Functions can also be combined.

In [8]:
print(math.sqrt(4))
2.0
In [9]:
print('The square root of 4 is',math.sqrt(4))
The square root of 4 is 2.0

4. Variables can be used to store values calculated in expressions and used for other calculations.

In [10]:
tempCelsius = 10.0
print(tempCelsius)
10.0
In [11]:
print('Temperature in Fahrenheit:', 9/5 * tempCelsius + 32)
Temperature in Fahrenheit: 50.0

Note that if you try to run some code that accesses a variable that has not yet been defined you will get a NameError message.

In [12]:
print('Temperature in Celsius:', 5/9 * (tempFahrenheit - 32))
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-12-2067e78f4444> in <module>()
----> 1 print('Temperature in Celsius:', 5/9 * (tempFahrenheit - 32))

NameError: name 'tempFahrenheit' is not defined

One of the cool things here is that if we define the undefined variable in a later cell and execute that cell, we can return to the earlier one and the code should now work. That was a bit of a complicated sentence, so let’s test this all out. First, execute the Python code in the cell below. Then, return to the cell above this text and run it again. See how the error message has gone away? tempFahrenheit has now been defined and thus the cell above no longer generates a NameError when the code is executed.

Note that the number beside the cell, for example In [2], tells you the order in which the Python cells have been executed. This way you can see a history of the commands you’ve executed

In [13]:
tempFahrenheit = 32

5. There are 4 basic data types in Python as shown in the table below.

Data type name Data type Example
int Whole integer values 4
float Decimal values 3.1415
str Character strings 'Hot'
bool True/false values True

The data type can be found using the type() function. As you will see, the data types are important because some are not compatible with one another.

In [14]:
weatherForecast = 'Hot'
type(weatherForecast)
Out[14]:
str
In [15]:
type(tempFahrenheit)
tempFahrenheit = tempFahrenheit + 5.0 * weatherForecast
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-41e07022ce91> in <module>()
      1 type(tempFahrenheit)
----> 2 tempFahrenheit = tempFahrenheit + 5.0 * weatherForecast

TypeError: can't multiply sequence by non-int of type 'float'

In this case we get at TypeError because we are trying to execute a math operation with data types that are not compatible. There is no way in Python to multpily numbers with a character string.

Lists and indices

Let’s now consider an example of some rock samples collected during a recent field excursion. Rather than having individual variables for each of the samples, we can store many related values in a collection. The simplest type of collection in Python is a list.

1. Let’s first create a list of selected SampleID values.

In [16]:
SampleIDs = ['DW-NP-03', 'DW-NP-12', 'DW-NP-33', 'DW-NP-48']
print(SampleIDs)
['DW-NP-03', 'DW-NP-12', 'DW-NP-33', 'DW-NP-48']
In [17]:
type(SampleIDs)
Out[17]:
list

Here we have a list of 4 SampleID values in a list called SampleIDs. As you can see, the type() function recognizes this as a list. Lists can be created using the square brackets ([ and ]), with commas separating the values in the list.

2. To access an individual value in the list we need to use an index value.

An index value is a number that refers to a given position in the list. Let’s check out the first value in our list as an example:

In [18]:
print(SampleIDs[1])
DW-NP-12

Note that Python (and many other programming languages) start values stored in collections with the index value 0.

3. We can find the length of a list using the len() function.

In [19]:
len(SampleIDs)
Out[19]:
4

Just as expected, there are 4 values in our list and len(SampleIDs) returns a value of 4.

Exercise - Getting to know lists

  • Create a Python list containing 4 items/values and store it with the variable myList.
  • After you have created the list, check its data type, then use the index values to check the data types of the contents of the list.
  • What kinds of data can you store in lists? Does each list value need to be the same data type?
In [20]:
# You can create your example list in this cell
# By the way, you can comment lines in Python (or a Jupyter Notebook Python cell)
# by starting the line with a number sign
# Comments are not executed, but can help explain what you code does

# Put your code below