Congratulations on making it this far! You now have a working Python environment.
During the lecture, we will do a small exercise on data analysis of magnetoencephalography (MEG) data. We'll be using Python. The main exercise requires you to be able to do these five things:
I'll walk you through these five things. I'll be oversimplifying things a little to shield you from some underlying complexity and just give you the absolute bare minimum knowledge required to tackle the exercise during the lecture. For a proper introduction to Python, I recommend reading "A Whirlwind Tour of Python".
This notebook environment consists of "cells". Cells can either contain text (like this one) or Python programming code (like the cell below). To exectute a cell, place your cursor in it and press CTRL
+Enter
. Try it now:
print('Hello, world!')
Variables allow us to assign names to chunks of data, so we can keep it in the computer's memory and refer to it later. Python defines different types of "chunks of data". Here are some types that are relevant to us, and how to write them in Python:
42
, 3.1415
, 1e-6
, 1_000_000
'Hello, world!'
, 'banana'
, 'π ≈ 3.1415'
, '😄😄😄'
True
, False
. These are commonly used as an on/off switch.[1, 2, 3, 4]
, ['apple', 'banana', 'strawberry']
, [1, 'banana', 2]
Assigning a chunk of data to a variable is done with the =
symbol:
x = 2
name = 'Marijn van Vliet'
fibonacci = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
In Python, anything following the #
symbol is ignored. This allows us to add comments to Python code:
# Look at this beautiful piece of Python code. I am so very proud of it!
color = 'yellow' # Define the color of our submarine to be yellow
# Watch the magic of variables produce a wonderful song:
print('We all live in a', color, 'submarine,', color, 'submarine,', color, 'submarine.')
Using the above knowledge, write some Python code in the cell below that will create a variable planets
that holds a list of all the names of the planets in our solar system:
# Write your Python code here
# If your code in the cell above was correct, executing this cell will display all the planets of our solar system
print('The planets in our solar system are:', ', '.join(planets))
Good news! The majority of all the code you'll ever need is already written for you by others. You just have to know how to use it. The most common way to re-use code that other people wrote is to call Python functions.
For example, the print
function has been written for you by the people who build Python. You can "call" this function like this:
print('Hello, world!') # Displays a string
print(planets) # Displays a variable
The amount of functions already written is nearly endless. Here is how to call the max
function that computes the maximum of two values:
max(10, 42) # Produces 42
Very often, a function will produce a result. In programming lingo, we say it "returns" a result. For example max(10, 42)
will return 42
. Unless we assign this result to a variable, it gets lost. So here is a more useful example of using the max
function:
max_value = max(10, 42) # Assigns 42 to the max_value variable
Functions can have optional parameters, that you can specify, but if you don't, they have a default value assigned that will be used instead. By convention, you specify optional parameters using parameter=value
:
print(*planets, sep=' | ') # mercury | venus | earth | ...
Ok, your turn: in the cell below, write some Python code that calles the sorted
function with numbers
as parameter and assigns the result to the numbers_sorted
variable:
numbers = [1, 6, 5, 2, 3, 7, 4]
# Replace this line with your code to sort the `numbers` variable
# If your code in the cell above was correct, executing this cell should display a sorted list of numbers
print(numbers_sorted)
A "method" is a function that is attached to a variable. In Python, all variables are "objects", which is programming lingo meaning they do not only contain a chunk of data, but also meta-data (called "properties" in programming lingo) and tools to manipulate the chunk of data (called "methods" in programming lingo).
For example, here are some methods that a variable that holds a Python list has to offer you:
numbers = [1, 2, 3, 4, 5] # Make a list of numbers and assign it to a variable
numbers.sort() # After calling this, you'll find the list is now sorted
numbers.reverse() # The list is now reversed
numbers.count(3) # Returns the number of 3's in the list
Your turn: use the .sort()
method to sort the list of planets you coded earlier:
# Write your Python code here
# If your code in the cell above was correct, executing this cell should display the planets in alphabetical order
print('The planets in alphabetical order:', ', '.join(planets))
To find out what a function or method does, which parameters it wants, and what it returns, append a question mark ?
to the name of the function/method. Try it by executing the cell below, which should pop up the documentation on the print
function:
print?
Ok, final test: sort the planets
variable using its planets.sort()
method. Give the .sort()
method an optional parameter to make it sort in reverse order. To find out the name of this optional parameter, you'll need to read the method's help text.
# Write your Python code here
# If your code in the cell above was correct, executing this cell
# should display the planets in reverse alphabetical order
print('The planeters in reverse alphabetical order:', ', '.join(planets))