1.3 Exercises#

Exercise 1#

Course contents that contain exercises are implemented in .ipynb files. These files can either be opened and used locally or in Google Colab by clicking on the Rocket icon in the top right corner of the book. Please open and run the following code chunk:

  1. On your local computer (in Visual Studio Code)

  2. On the internet (in Google Colab)

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.collections import LineCollection

colors = ["indigo", "blue", "green", "yellow", "orange", "red"]

# Create a list of half-circles with varying radii
theta = np.linspace(0, np.pi, 36)
radii = np.linspace(4, 5, num=len(colors))
arcs = [np.column_stack([r * np.cos(theta), r * np.sin(theta)]) for r in radii]

# Plot the half-circles
fig, ax = plt.subplots()
ax.set_xlim(-6, 6)
ax.set_ylim(0, 6)
ax.set_aspect("equal")

line_collection = LineCollection(arcs, colors=colors, linewidths=4)
ax.add_collection(line_collection)
ax.text(0, 1, "Welcome to psy111!", fontsize=14, ha='center')

plt.show()
../../../_images/e0ed515f7e21c367d4ee85e63e536f0b32fd36ece7dceb6b5511b3d52eabe960.png

Exercise 2#

Please do the following:

  1. Create a my_first_script.py Python file and save it to your computer

  2. Copy the code from the previous exercise into it and run it

Exercise 3#

Please do the following:

  1. Create a my_first_notebook.ipynb Jupyter notebook and save it to your computer

  2. Create a Markdown cell and write some text

  3. Create a Code cell and make it print the message “Hello World”

Note: You might get a pop-up telling you to install the ipykernel package. Please do so.

Exercise 4#

You have previously created a .py Python file as well as a .ipynb Jupyter notebook. Discuss with your neigbour:

  1. What are the differences between these files

  2. Can you come up with examples, where using one over the other would be preferred?

Exercise 5#

Let’s imagine the following scenario. You have completed the first year of your Master’s course and you are ready to start with your practical project! As you need to do some data analysis in Python, you want to create a new Conda envirionment so you can have all the necessary Python packages in an isolated environment and avoid any potential version conflicts.

  1. Create a new Conda environment and call it practical_project

  2. Open the previously created my_first_script.py Python file and run it in the practical_project environment. Does it work?

  3. You encountered an error in the previous exercise. Please fix it.

  4. Check the packages that are installed in the practical_project environment

  5. Delete the practical_project environment

Hints:

  • Look at the “Installing Python: 1.2 Usage” section of the book if you are unsure what to do

  • Refer to the Conda cheatsheet if you are unsure about the required commands

Exercise 6#

Install the :emojisense: extension in Visual Studio Code, so you can add emojis to your future scripts 🚀 🥳