Warning: This document is for the development version of Geo-Python. The main version is master.

This page was generated from source/notebooks/L1/a-taste-of-python.ipynb.
Binder badge
Binder badge CSC badge

A taste of Python

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

General information

Sources

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

About this document

This is a Jupyter Notebook. This particular notebook is designed to introduce you to a few 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 with the examples below.

Getting started

Presumably if you have made it this far, you have already opened this Jupyter Notebook using Binder, the CSC notebooks server, or on your own computer. If not, you can launch Jupyter Lab by clicking on either of the launch buttons at the top of this document or 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, math and functions

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

Simple Python math

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 by typing 1 + 1 or 5 * 7 and see what you get.

[1]:
1 + 1
[1]:
2
[2]:
5 * 7
[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.

Functions

You can use Python for more advanced math by using functions. Functions are pieces of code that perform a single action such as printing information to the screen (e.g., the print() function). Functions exist for a huge number of operations in Python.

Let’s try out a few simple examples using functions to find the sine or square root of a value. You can type sin(3) or sqrt(4) into the cells below to test this out.

[3]:
sin(3)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-2b5fc2868c51> in <module>()
----> 1 sin(3)

NameError: name 'sin' is not defined
[4]:
sqrt(4)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-317e033d29d5> in <module>()
----> 1 sqrt(4)

NameError: name 'sqrt' is not defined

Wait, what? Python can’t calculate square roots or do basic trigonometry? Of course it can, but we need one more step.

Math operations

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. For math operations, this module is called math and it can be loaded by typing import math. Try that below.

[5]:
import math
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/opt/python/3.8.0/lib/python3.8/codeop.py in __call__(self, source, filename, symbol)
    131
    132     def __call__(self, source, filename, symbol):
--> 133         codeob = compile(source, filename, symbol, self.flags, 1)
    134         for feature in _features:
    135             if codeob.co_flags & feature.compiler_flag:

TypeError: required field "type_ignores" missing from Module

Now that we have access to functions in the math module, we can use it by typing the module name, a period (dot), and the the name of the function we want to use. For example, math.sin(3). Try this with the sine and square root examples from above.

[6]:
math.sin(3)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-6bafe0030c44> in <module>()
----> 1 math.sin(3)

NameError: name 'math' is not defined
[7]:
math.sqrt(4)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-65fb388543e8> in <module>()
----> 1 math.sqrt(4)

NameError: name 'math' is not defined

Let’s summarize what we’ve just seen with modules:

  1. A module is a group of code items such as functions that are related to one another. Individual modules are often in a group referred to as a library.
  2. Modules can be loaded using import. Functions that are part of the module modulename can 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.
  3. 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.
  4. Modules may also contain constants such as math.pi. Type this in the cell below to see the value of the contant math.pi.
[8]:
math.pi
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-c49acc181da4> in <module>()
----> 1 math.pi

NameError: name 'math' is not defined

Check your understanding

Use the empty Python cell below to calculate the sine of pi. What value do you expect for this calculation? Did you get the expected result?

[9]:
# Place your code on the line(s) below. Note that lines starting with "#" are ignored in Python.


Combining functions

Functions can also be combined. The print() function returns values within the parentheses as text on the screen. Below, try printing the value of the square root of four.

[10]:
print(math.sqrt(4))
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-10-30811a5eb0c8> in <module>()
----> 1 print(math.sqrt(4))

NameError: name 'math' is not defined

You can also combine text with other calculated values using the print() function. For example, print('Two plus two is', 2+2) would generate text reading ‘Two plus two is 4’. Combine the print() function with the math.sqrt() function in the cell below to produce text that reads ‘The square root of 4 is 2.0’.

[11]:
print('The square root of 4 is', math.sqrt(4))
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-11-2b10ae01630e> in <module>()
----> 1 print('The square root of 4 is', math.sqrt(4))

NameError: name 'math' is not defined

Variables

Variables can be used to store values calculated in expressions and used for other calculations. Assigning value to variables is straightforward. To assign a value, you simply type variable_name = value, where variable_name is the name of the variable you wish to define. In the cell below, define a variable called temp_celsius, assign it a value of ‘10.0’, and then print that variable value using the print() function. Note that you need to do this on two separate lines.

[12]:
temp_celsius = 10.0
print(temp_celsius)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/opt/python/3.8.0/lib/python3.8/codeop.py in __call__(self, source, filename, symbol)
    131
    132     def __call__(self, source, filename, symbol):
--> 133         codeob = compile(source, filename, symbol, self.flags, 1)
    134         for feature in _features:
    135             if codeob.co_flags & feature.compiler_flag:

TypeError: required field "type_ignores" missing from Module

As we did above, you can combine text and even use some math when printing out variable values. The idea is similar to the examples of adding 2+2 or calculating the square root of four from the previous section. In the cell below, print out the value of temp_celsius in degrees Fahrenheit by multiplying temp_celsius by 9/5 and adding 32. This should be done within the print() function to produce output that reads ‘Temperature in Fahrenheit: 50.0’.

[13]:
print('Temperature in Fahrenheit:', 9/5 * temp_celsius + 32)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-13-f0f132280fc3> in <module>()
----> 1 print('Temperature in Fahrenheit:', 9/5 * temp_celsius + 32)

NameError: name 'temp_celsius' is not defined

Check your understanding

Use the empty Python cell below to define a variable and print its value to the screen using the print() function. The variable value can be anything you like, and you can even consider defining several variables and printing them out together. Consider using pothole_case_naming for your variable name.

[14]:
# Place your code on the line(s) below. Note that lines starting with "#" are ignored in Python.


Updating variables

Values stored in variables can also be updated. Let’s redefine the value of temp_celsius to be equal to 15.0 and print its value in the cells below.

[15]:
temp_celsius = 15.0
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/opt/python/3.8.0/lib/python3.8/codeop.py in __call__(self, source, filename, symbol)
    131
    132     def __call__(self, source, filename, symbol):
--> 133         codeob = compile(source, filename, symbol, self.flags, 1)
    134         for feature in _features:
    135             if codeob.co_flags & feature.compiler_flag:

TypeError: required field "type_ignores" missing from Module
[16]:
print('temperature in Celsius is now:', temp_celsius)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-16-b314163ea3b3> in <module>()
----> 1 print('temperature in Celsius is now:', temp_celsius)

NameError: name 'temp_celsius' is not defined

Warning

If you try to run some code that accesses a variable that has not yet been defined you will get a NameError message. Try printing out the value of the variable tempFahrenheit using the print() function in the cell below.

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

NameError: name 'tempFahrenheit' is not defined

Note

One of the interesting things here is that if we define the undefined variable in a cell lower down in the notebook and execute that cell, we can return to the earlier cell and the code should now work. That was a bit of a complicated sentence, so let’s test this all out. First, let’s define a variable called tempFahrenheit in the cell below and assign it to be equal to 9/5 * temp_celsius + 32, the conversion factor from temperatures in Celsius to Fahrenheit. Then, return to the cell above this text and run that cell 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.

Also, 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 order in which you have run the cells.

[18]:
tempFahrenheit = 9/5 * temp_celsius + 32
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/opt/python/3.8.0/lib/python3.8/codeop.py in __call__(self, source, filename, symbol)
    131
    132     def __call__(self, source, filename, symbol):
--> 133         codeob = compile(source, filename, symbol, self.flags, 1)
    134         for feature in _features:
    135             if codeob.co_flags & feature.compiler_flag:

TypeError: required field "type_ignores" missing from Module

Just to check their current values, print out the values of temp_celsius and tempFahrenheit in the cell below.

[19]:
print('temperature in Celsius:', temp_celsius, 'and in Fahrenheit:', tempFahrenheit)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-19-d8fb0e7143ac> in <module>()
----> 1 print('temperature in Celsius:', temp_celsius, 'and in Fahrenheit:', tempFahrenheit)

NameError: name 'temp_celsius' is not defined

Variable values

Changing the value of a variable does not affect other variable values. Let’s redefine temp_celsius to be equal to 20.0, and print out the values of temp_celsius and tempFahrenheit.

[20]:
temp_celsius = 20.0
print('temperature in Celsius is now:', temp_celsius, 'and temperature in Fahrenheit is still:', tempFahrenheit)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/opt/python/3.8.0/lib/python3.8/codeop.py in __call__(self, source, filename, symbol)
    131
    132     def __call__(self, source, filename, symbol):
--> 133         codeob = compile(source, filename, symbol, self.flags, 1)
    134         for feature in _features:
    135             if codeob.co_flags & feature.compiler_flag:

TypeError: required field "type_ignores" missing from Module

Data types

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.

Let’s define a variable weatherForecast and assign it the value 'Hot'. After this, we can check its data type using the type() function.

[21]:
weatherForecast = 'Hot'
type(weatherForecast)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/opt/python/3.8.0/lib/python3.8/codeop.py in __call__(self, source, filename, symbol)
    131
    132     def __call__(self, source, filename, symbol):
--> 133         codeob = compile(source, filename, symbol, self.flags, 1)
    134         for feature in _features:
    135             if codeob.co_flags & feature.compiler_flag:

TypeError: required field "type_ignores" missing from Module

Let’s also check the type of tempFahrenheit. What happens if you try to combine tempFahrenheit and weatherForecast in a single math equation such as tempFahrenheit = tempFahrenheit + 5.0 * weatherForecast?

[22]:
type(tempFahrenheit)
tempFahrenheit = tempFahrenheit + 5.0 * weatherForecast
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/opt/python/3.8.0/lib/python3.8/codeop.py in __call__(self, source, filename, symbol)
    131
    132     def __call__(self, source, filename, symbol):
--> 133         codeob = compile(source, filename, symbol, self.flags, 1)
    134         for feature in _features:
    135             if codeob.co_flags & feature.compiler_flag:

TypeError: required field "type_ignores" missing from Module

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.

Check your understanding

As it turns out, you can do some math with character strings in Python. Define two variables and assign them character string values in the Python cell below. What happens if you try to add two character strings together? Can you subtract them? Which other math operations work for character strings?

[23]:
# Place your code on the line(s) below.


Character input

Python and Jupyter notebooks also let us interact in another way with our code! The built-in input() function reads a line from input and returns it as a string.

Let’s try it out. To start, we can define a varaible place and assign its value using the input() function to prompt the user for the location where they are from (e.g., input('Where are you from? ')). When the cell is run, the user (you) can type in their response. Once place is defined, we can say something good about where the user is from (e.g., print(place, 'is a nice place!')).

Warning

Jupyter Notebooks might sometimes get stuck when using the input() function. If this happens, restart the kernel and run the cell again (Kernel -> Restart Kernel…).

Also, we realize these cells will not render properly on the course website, but should work just fine in Binder or using the CSC notebooks. Sorry.

[24]:
place = input('Where are you from? ')
print(place, 'is a nice place!')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/opt/python/3.8.0/lib/python3.8/codeop.py in __call__(self, source, filename, symbol)
    131
    132     def __call__(self, source, filename, symbol):
--> 133         codeob = compile(source, filename, symbol, self.flags, 1)
    134         for feature in _features:
    135             if codeob.co_flags & feature.compiler_flag:

TypeError: required field "type_ignores" missing from Module

Let’s try another example in the cell below using the similar approach. Ask the user for a temperature in Celsius using the input() function and print the input value to the screen.

[25]:
temp_celsius = input('How cold can it be in Vantaa (in degrees Celsius)? ')
print(temp_celsius, 'degrees Celsius is quite cold!')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/opt/python/3.8.0/lib/python3.8/codeop.py in __call__(self, source, filename, symbol)
    131
    132     def __call__(self, source, filename, symbol):
--> 133         codeob = compile(source, filename, symbol, self.flags, 1)
    134         for feature in _features:
    135             if codeob.co_flags & feature.compiler_flag:

TypeError: required field "type_ignores" missing from Module

What is the data type of variable place? What about the other variable you defined? Check their data types using the cell below.

[26]:
print(type(place))
print(type(temp_celsius))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/opt/python/3.8.0/lib/python3.8/codeop.py in __call__(self, source, filename, symbol)
    131
    132     def __call__(self, source, filename, symbol):
--> 133         codeob = compile(source, filename, symbol, self.flags, 1)
    134         for feature in _features:
    135             if codeob.co_flags & feature.compiler_flag:

TypeError: required field "type_ignores" missing from Module

What happens when you try to convert your Celsius temperature value to Fahrenheit using the equation from earlier in the lesson?

[27]:
tempFahrenheit = 9/5 * temp_celsius + 32
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/opt/python/3.8.0/lib/python3.8/codeop.py in __call__(self, source, filename, symbol)
    131
    132     def __call__(self, source, filename, symbol):
--> 133         codeob = compile(source, filename, symbol, self.flags, 1)
    134         for feature in _features:
    135             if codeob.co_flags & feature.compiler_flag:

TypeError: required field "type_ignores" missing from Module