Welcome to the INF264 Python crash course. This repository contain guides (Jupyter notebooks) to help you get familiar with NumPy and Matplotlib, two Python libraries required for completing homeworks and the two mandatory projects in INF264.
If you encounter any problems, feel free to ask a group leader or post your questions in the Discord channel #python-crash-course.
You are also very welcome to join the dedicated crash course session on Monday, August 19th from 0815 to 1000 in the room Aktivt rom 2+3 - Marineholmen (ground floor at Høyteknologisenteret).
We will assume that you are familiar with the basics of Python programming including topics such as data types and structures, conditional statements, functions, loops etc. If you feel you need to brush up on your Python skills, have a look at the free online course Python Programming MOOC 2023 (part 1 – 7).
To install Anaconda, click the link below corresponding to your operating system and follow the instructions there.
We will now create a new Anaconda environment for INF264.
- Open a terminal window.
- Run the command
conda create --name=INF264 python
to create a new environment namedINF264
with the latest Python version. - Run the command
conda activate INF264
to activate the newly create environment. - Run
conda install -c conda-forge numpy scikit-learn jupyter pandas matplotlib ipykernel
to download and install the required packages.
To test that everything is working as expected, create a new notebook by running jupyter-lab
(if you use VS Code, see below) and create and run a code cell with the following content:
import sklearn
import numpy
import pandas
import matplotlib.pyplot
Notebooks in VS Code Instructions
If your are using VSCode, you can also work with Jupyter notebooks there.
- Install the Jupyter extension in VS Code (if you do not have it).
- Create a new file
File >> New File ...
and chooseJupyter Notebook
. - Click
Select Kernel
in the upper right corner and choose the kernelINF264
that we created using Anaconda. - Edit the code cell by clicking on it and paste the above code with imports.
- Press the "play button" on the cell (or
Ctrl + Enter
) to run the code cell. - If everything is installed properly, you should see no error messages.
Useful notebook shortcuts:
- Arrow keys to navigate between cells.
Shift + Enter
to run the current cell and move to the next one.Ctrl + Enter
to run the current cell and stay in the same cell.Esc
to enter command mode.Enter
to enter edit mode.A
to insert a new cell above the current cell.B
to insert a new cell below the current cell.DD
to delete the current cell.M
to change the current cell to a Markdown cell.Y
to change the current cell to a code cell.
Enter the environment by running conda activate INF264
. To exit the INF264
environment, simply run conda deactivate
.
(Optional) To update all packages in your environment, run conda update --all
(when the environment is already activated).
(Optional) At the end of the semester, if you want to clean up and delete the INF264
environment from your system, run the command conda env remove --name INF264
.
The course content mainly consists of Jupyter notebooks (see below) containing many examples and some exercies to help you get familiar with both NumPy and Matplotlib.
NumPy, short for Numerical Python, is a powerful library for numerical computing in Python. It provides support for arrays, matrices, and many mathematical functions to operate on these data structures efficiently.
For more details about the differences and advantages of using NumPy arrays as oposed to Python lists when working with numerical data, see "What is NumPy?" from the NumPy documentation.
It is convention to import NumPy under the alias np
by writing
import numpy as np
in the beginning of the Python file or notebook where you want to use NumPy functions.
The (very useful) NumPy documentation can be found here (link).
Matplotlib is a popular and flexible library for creating many types of data visualizations and plots.
It is convention to import matplotlib's pyplot module under the alias plt
by writing
import matplotlib.pyplot as plt
The documentation for matplotlib can be found here (link).
You can download all the notebooks by downloading this repository as a ZIP file (see screenshot below), or use git clone
if you are familiar with git.
If you use VS Code, go to File >> Open Folder...
and choose the directory notebooks
(after unzipping the repository). Remember to choose the INF264
environment as the kernel for the notebooks.
NumPy Notebooks
- Creating NumPy Arrays
- Indexing NumPy Arrays
- Reshaping, Transposing and Concatenating Arrays
- Basic Array Operations and Broadcasting
- Matrix and Vector Algebra in NumPy
- Reduction Operations in NumPy
- Other Useful NumPy Functions
It is recommended to work through the NumPy notebooks in the order they are listed.
Matplotlib Notebook
Download and go through the notebook A Short Introduction to Matplotlib.
Solutions
You are encouraged to try to complete all the exercises on your own. If you get completly stuck, notebooks with solutions can be found in the directory notebooks/with_solutions
.