{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Pre-assignment for the MEG analysis lecture\n", "\n", "Congratulations on making it this far! You now have a working Python environment.\n", "\n", "\n", "\n", "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:\n", "\n", " 1. Run a code cell\n", " 1. Use a variable\n", " 1. Call a Python function\n", " 1. Call a Python method\n", " 1. Access the help text for a function or method\n", "\n", "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](http://www.oreilly.com/programming/free/files/a-whirlwind-tour-of-python.pdf)\"." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Run a code cell\n", "\n", "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:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('Hello, world!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Use a variable\n", "\n", "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](https://docs.python.org/3/library/stdtypes.html) of \"chunks of data\". Here are some types that are relevant to us, and how to write them in Python:\n", "\n", "1. A number: `42`, `3.1415`, `1e-6`, `1_000_000`\n", "1. A (unicode) string: `'Hello, world!'`, `'banana'`, `'π ≈ 3.1415'`, `'😄😄😄'`\n", "1. A boolean \"truth\" value: `True`, `False`. These are commonly used as an on/off switch.\n", "1. A list of things (things can be of any data type): \n", " `[1, 2, 3, 4]`, `['apple', 'banana', 'strawberry']`, `[1, 'banana', 2]`\n", "\n", "Assigning a chunk of data to a variable is done with the `=` symbol:\n", "\n", "```python\n", "x = 2\n", "name = 'Marijn van Vliet'\n", "fibonacci = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\n", "```\n", "\n", "### (A note on Python comments)\n", "In Python, anything following the `#` symbol is ignored. This allows us to add comments to Python code:\n", "\n", "```python\n", "# Look at this beautiful piece of Python code. I am so very proud of it!\n", "color = 'yellow' # Define the color of our submarine to be yellow\n", "# Watch the magic of variables produce a wonderful song:\n", "print('We all live in a', color, 'submarine,', color, 'submarine,', color, 'submarine.')\n", "```\n", "\n", "### (ok, back to the lesson)\n", "\n", "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](https://en.wikipedia.org/wiki/Planet):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Write your Python code here" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# If your code in the cell above was correct, executing this cell will display all the planets of our solar system\n", "print('The planets in our solar system are:', ', '.join(planets))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Call a Python function\n", "\n", "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.\n", "The most common way to re-use code that other people wrote is to call Python functions.\n", "\n", "For example, the `print` function has been written for you by the people who build Python. You can \"call\" this function like this:\n", "\n", "```python\n", "print('Hello, world!') # Displays a string\n", "print(planets) # Displays a variable\n", "```\n", "\n", "The amount of functions already written is nearly endless. Here is how to call the `max` function that computes the maximum of two values:\n", "\n", "```python\n", "max(10, 42) # Produces 42\n", "```\n", "\n", "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:\n", "\n", "```python\n", "max_value = max(10, 42) # Assigns 42 to the max_value variable\n", "```\n", "\n", "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`:\n", "\n", "```python\n", "print(*planets, sep=' | ') # mercury | venus | earth | ...\n", "```\n", "\n", "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:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers = [1, 6, 5, 2, 3, 7, 4]\n", "# Replace this line with your code to sort the `numbers` variable" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# If your code in the cell above was correct, executing this cell should display a sorted list of numbers\n", "print(numbers_sorted)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Call a Python method\n", "\n", "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).\n", "\n", "For example, here are some methods that a variable that holds a Python list has to offer you:\n", "\n", "```python\n", "numbers = [1, 2, 3, 4, 5] # Make a list of numbers and assign it to a variable\n", "numbers.sort() # After calling this, you'll find the list is now sorted\n", "numbers.reverse() # The list is now reversed\n", "numbers.count(3) # Returns the number of 3's in the list\n", "```\n", "\n", "Your turn: use the `.sort()` method to sort the list of planets you coded earlier:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Write your Python code here" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# If your code in the cell above was correct, executing this cell should display the planets in alphabetical order\n", "print('The planets in alphabetical order:', ', '.join(planets))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5. Access the help text for a function or method\n", "\n", "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:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Write your Python code here" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# If your code in the cell above was correct, executing this cell\n", "# should display the planets in reverse alphabetical order\n", "print('The planeters in reverse alphabetical order:', ', '.join(planets))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }