Basic Python#

Python is a high level general purpose programming language. Python is easy to read, understand and learn.

You can run python code in different ways:

  • Python interpreter: line-by-line in a shell (like an advanced calculator)

  • IPython intepreter: Interactive Python shell (with syntax highlighting etc)

  • Script: run all your code in a txt-file

  • Jupyter notebook: interactive web-based environment for combining text, code and plots

We will use Jupyter notebooks in this course.

Jupyter#

A cell in jupyter is either “code” or “markdown”. See markdown cheat sheet for help on writing markdown text.

You will be navigating Jupyter cells alot so it’s important to learn a few keyboard shortcuts:

  • ‘Ctrl’+’Enter’: run cell

  • ‘Shift’+’Enter’: run cell and jump to next

You can exit “edit mode” (and enter “command mode”) by pressing ‘Esc’, you can now press:

  • ‘a’: insert cell above

  • ‘b’: insert cell below

  • ‘m’: change cell to markdown

  • ‘y’: change cell to code

  • ‘d’+’d’: delete cell

Code completion and help#

Jupyter lab can help you complete Python code, if you press “tab”. If you have your cursor on a function, you can get the function signature by pressing “shift”+”tab”.

# You can get help by writting ? before or after a function/variable
pwd?

Current working directory#

When reading (or writing) files from the local file system, it is important to know your current path. You you print the current path by the command pwd:

pwd
'/home/runner/work/getting-started-with-mikeio/getting-started-with-mikeio/mini_book'

We recommend that you start jupyter from the “mini_book” folder in this course as it will make the relative paths to the data files work.

Windows paths#

Backslash \ is used to separate folders in Windows. In Python strings, backslash \ is an escape character. You can write windows paths in Python the following ways:

  • use “raw-string” representation by pre-fixing the string with r, like this: r”folder\file.ext”

  • use slash “/” (as on linux and http), like this: “folder/file.ext”

  • use double backslash, like this: “folder\\file.ext”

We recommend using slash “/” where possible as it also works on linux.

Variables#

var1 = 2.2
var1
2.2
var2 = var1
var2 = "3.3"    # now changed type
var2
'3.3'
var1     # var1 stays the same, when we change var2 (numeric type)
2.2
type(var1)
float

Lists#

# A list is created with [..,..]
myvals = [1.0, 2.0, 1.5]
myvals
[1.0, 2.0, 1.5]
myvals[0]
1.0
myvals2 = myvals     # this is *not* a copy!
myvals2[1] = 3.3
myvals     # myvals has also changed! (myvals and myvals2 reference the same object)
[1.0, 3.3, 1.5]
id(myvals) == id(myvals2)     
True
# lists can contain all sorts of different variables
stuff = [5, 3.0, "MIKE ZERO", b'a', [1,2]]
type(stuff[0])
int

Tuple#

Tuples are similar to lists but immutable (once they are created they cannot be changed).

my_tuple = (34, 0.2, "txt")
my_tuple
(34, 0.2, 'txt')
my_tuple[2]
'txt'
# my_tuple[2] = 'new_txt'   # executing this line will fail

Dictionary#

fruits = {'banana':4, 'apple':7}
fruits
{'banana': 4, 'apple': 7}
fruits['orange'] = 9
fruits
{'banana': 4, 'apple': 7, 'orange': 9}
fruits.keys()
dict_keys(['banana', 'apple', 'orange'])
fruits['banana']
4
fruits.values()
dict_values([4, 7, 9])
fruits.items()
dict_items([('banana', 4), ('apple', 7), ('orange', 9)])

Control structures#

Notice the colons and identation!

i_am = 'ok'
if i_am == 'ok':
    print('You are ok')
elif i_am == 'great':
    print('You are great')
else:
    print("I don't know how you are!")
You are ok
for j in range(3):
    print(j)
0
1
2
names = ['Carl','Chan','Clarice']
for name in names:
    print(f'Hi {name}!')
Hi Carl!
Hi Chan!
Hi Clarice!

A loop can also be expressed using special syntax known as a list comprehension.

lnames = [name.lower() for name in names]
lnames
['carl', 'chan', 'clarice']

Functions#

It is very useful to create your own functions to collect code that belongs together in a function, which can be reused in several places in your code without copying and pasting the entire snippet.

import re

def clean_name(name):
    "Clean and short name"
    clean = re.sub('[^A-Za-z0-9]+', '', name)
    short = clean[:15]
    lower = short.lower()
    return lower
clean_name("#What a lousy & long name")
'whatalousylongn'
clean_name("goodname")
'goodname'
long_and_ugly_names = ["Wave Height # Modelled", "Wave Period # Modelled", "Wave Direction # Modelled"]

Combine a list comprehension with your own function

[clean_name(x) for x in long_and_ugly_names]
['waveheightmodel', 'waveperiodmodel', 'wavedirectionmo']