Pre-assignment for the MEG analysis lecture

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:

  1. Run a code cell
  2. Use a variable
  3. Call a Python function
  4. Call a Python method
  5. Access the help text for a function or method

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".

1. Run a code cell

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:


In [ ]:
print('Hello, world!')

2. Use a variable

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:

  1. A number: 42, 3.1415, 1e-6, 1_000_000
  2. A (unicode) string: 'Hello, world!', 'banana', 'π ≈ 3.1415', '😄😄😄'
  3. A boolean "truth" value: True, False. These are commonly used as an on/off switch.
  4. A list of things (things can be of any data type):
    [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]

(A note on Python comments)

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.')

(ok, back to the lesson)

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:


In [ ]:
# Write your Python code here

In [ ]:
# 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))

3. Call a Python function

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:


In [ ]:
numbers = [1, 6, 5, 2, 3, 7, 4]
# Replace this line with your code to sort the `numbers` variable

In [ ]:
# If your code in the cell above was correct, executing this cell should display a sorted list of numbers
print(numbers_sorted)

4. Call a Python method

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:


In [ ]:
# Write your Python code here

In [ ]:
# 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))

5. Access the help text for a function or method

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:


In [ ]:
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.


In [ ]:
# Write your Python code here

In [ ]:
# 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))