import math
6 Python Basics
This section provides a crash course on basic Python concepts used throughout the course. It is purposefully brief, with additional resources provided at the end.
Follow along by running these cells in a Jupyter Notebook. Alternatively, run them in the Python Interpreter which can be started with uv run python
.
6.1 Using libraries
Most functionality useful for MIKE+ modelling exists in Python packages (e.g. mikeio). Therefore, it’s important to understand how to access functionality in a Python package.
The terms package, library, and module are used interchangeably throughout this course.
6.1.1 Import libraries
Import libraries using the import
statement:
Or import specific functionality from a library:
from math import sqrt
6.1.2 Objects
All imports are objects containing some functionality. Objects have members accessible via the dot notation:
math.pi
3.141592653589793
Dot accessors can be chained together, since all members are also objects.
math.pi.is_integer()
False
There are a few common types of objects to be aware of:
- Modules: reusable code you can import into your program.
- Classes: templates for creating objects with specific properties and behaviors.
- Functions / Methods: blocks of code that return a result.
- Data: any stored information (e.g. numbers, text).
See the type of an object with:
type(math)
module
See the members of an object with:
dir(math)
Get help for an object:
help(math)
Good libraries have documentation. For example, see the documentation for math.
6.1.3 Using Functions / Methods
This course will use the terms ‘function’ and ‘method’ interchangeably.
Use a function by invoking it with round brackets:
25) sqrt(
5.0
Between the brackets is the function arguments. There’s different ways of specifying arguments. For example, there could be a list of arguments:
pow(2, 3) math.
8.0
6.1.4 Using Classes
Some library functionality is provided via a class that needs to be instantiated before using it.
Below, the Random
class is instantiated and assigned to the identifier my_random
for reference later on.
from random import Random
= Random() my_random
An instantiation of a class is called an instance, and is also an object whose functionality is accessible with the dot notation:
# returns a random number my_random.random()
0.8025334765081212
6.2 Variables
Store data/objects in named variables by using the assignment operator =
.
= 1 + 1
result result
2
A valid name must be used. In general, this means it must start with a letter or underscore.
Variable names can be referenced anywhere after their definition.
= result * 2
result result
4
6.3 Collections
A common need is to have a collection of related data. Perhaps the most common type of collection is a list
, which is briefly introduced below.
Create a list with square brackets. Optionally include comma separated elements, otherwise an empty list is created.
= [1, 2, 3]
my_numbers my_numbers
[1, 2, 3]
Append elements to an existing list.
4) my_numbers.append(
Access a specific element by indexing the list with the zero-based index. Zero refers to the first element, one the second, and so on.
0] my_numbers[
1
Access a subset of a list by slicing it. The example below accesses elements with index 0 up to, but excluding, 2.
0:2] my_numbers[
[1, 2]
6.4 Control Logic
Control logic allows the flow of a program to be controlled via boolean conditions.
6.4.1 Conditional statements
Use if
statements to execute code only if the specified condition is true.
if 100 > 10:
print("100 is greater than 10")
100 is greater than 10
The code that the if
statement applies to is called a block, which must be indented.
Use else
statements after an if
statement to execute code only if the condition is untrue.
if 100 < 10:
print("100 is less than 10")
else:
print("of course, 100 is not less than 10")
of course, 100 is not less than 10
6.4.2 Loops
A while
loop continuously executes a block of code while the specified condition is true.
= 0
i while i < 3:
print(i)
= i + 1 i
0
1
2
A for
loop executes a block of code per element in a specified collection.
for fruit in ["Apple", "Banana", "Orange"]:
print(fruit)
Apple
Banana
Orange
6.5 Additional resources
Learning Python should be a continuous endeavor through practice. Luckily there’s an abundance of high quality resources online. Here’s a few examples:
Python is used by a wide variety of domains (e.g. web development). Try to use resources specific for engineering/science applications for a more efficient learning path.