Welcome to Jupyter Notebooks!

Notebooks are interactive documents, almost like web pages, where you can easily combine text, pictures, code and data in a very user friendly manner. You can easily use them with your students for a wide variety of things. Let's take a look at some of their features.

What, why?

Notebooks present a very approachable and accessible platform for rich and varied spectrum of possible exercises. As you can see, the user interface is pretty intuitive and innocent looking: you can combine instructions and assignments very easily, put readable text between code blocks and everything is in front of you without a ton of pop ups or extra windows getting tangled up with each other. This can be an important step in making programming less daunting for newcomers.

One important feature is the ease of setup: Notebooks can be run entirely in browser without any installations or log-ins required. MyBinder is probably the easiest tool to use when the teacher has their materials in a Github-repository, either their own or one like this. Binder creates a virtual workspace that you can use for some time and which then disappears when you're finished. This usually doesn't take more than a few minutes and you're good to go. As the workspace is only temporal, the students are free to explore the documents in front of them, make changes and try things out without altering the master copy. If you wish to save your work, you can download the file as it currently is via File $\rightarrow$ Download as $\rightarrow$ Notebook (.ipynb). That file can then later on be uploaded to a new workspace and you can carry on with it.

Jupyter Notebooks aren't CERN-specific, nor is MyBinder. Their reasonably wide userbase means it's pretty easy to google things out if you wish to expand your use, say for an example, to other programming languages like C++ or Ruby. In these materials we use Python 3 as it is a very powerful and intuitive language which is also used in real scientific computing.

How?

Try it! This whole document is your playground.

As you can see, this document is made from cells. These cells may contain either text (markdown, which also understands HTML-code) or code (in the language used by the current kernel). Click around and see how different styles can be used to format text. Help-menu or stuff like this may help. LATEX is also supported, for all your equation needs.

Also, pressing h brings up the list for shortkeys, when you haven't chosen any cell to be active.

Let's get to it, then.

Computers are much simpler than humans, especially in figuring out what they should be doing. Usually that means we have to explicitly tell them what they should do. In Jupyter, each block of code is in its own cell, which you can run by selecting the cell and pressing ctrl + Enter. If the brackets on its left side are empty, the cell has not been run in this session. If there is an asterisk, it is currently running. If there is a number, it has been run.

If you somehow manage to get your notebook in a twist, Kernel $\rightarrow$ Restart & Clear Output kills all processes and sweeps all outputs away. It does not reset any changes to the notebook itself, however.

With Python, we first need to give our machine the tools to do things. These are called modules and are vital for our code. ALWAYS RUN THIS CELL FIRST.

In [ ]:
# I'm a comment! Hashed lines are not run with the rest of the cell, thus making it easy to make your code
# easier for someone else to read.

import pandas as pd     # this deals with data
import numpy as np      # this incorporates things like natural constants
import matplotlib.pyplot as plt    # this gives you tools to draw graphs
import random as rand   # this allows for random number generation

%matplotlib inline
# this one makes your outputs prettier and let's you see your graphs directly

There are lots of modules out there, but we'll start with these as they cover a pretty wide area already.

1. Basics

In [ ]:
print(2+2)

print(2*5)

numbers = [2,10]
print(numbers)

# Try these out with different numbers, operations and so forth.
In [ ]:
lottery = rand.randint(1,100)
text = "Today's winning ticket is: "
print (text + str(lottery))

# Text and variables go together just fine.

2. Plot we go

Graphs are terrific teaching tools. With computers it is effortless to generate and evaluate them either from manual input, random generetion or by reading tables of data from another document.

In [ ]:
numbah = [1,2,3,6,7,8,12,16]
plt.plot(numbah, 'b*')
plt.show()
In [ ]:
# Let's define the variable.

ages = [14,15,14,16,17,16,14,15,16,16,14,15,15,12,34,12,15,16,16]

# Plotting a histogram. If you change 'ages', adjust range accordingly.

plt.hist(ages, bins=100, range=(10,40))

# Here we create headers.

plt.xlabel('Age in years')
plt.ylabel('Number')
plt.title('Age distribution in class \n') # \n makes a new line, for aesthetic reasons

# This shows the output cleanly.

plt.show()

Right, let's delve into some data. Here we take solar data from 1992 to present day. The data isn't labeled, but you can see from the site that the fifth column will contain the number of effective sunspots on that day, sixth from the Northern hemispehere and seventh from the Southern hemisphere. Remember, indexing begins from zero!

In [ ]:
# read data from the internet

sun = pd.read_table('http://sidc.oma.be/silso/INFO/sndhemcsv.php', sep=';', encoding = "ISO-8859-1", header=-1)
In [ ]:
# check what it looks like

sun.head()
In [ ]:
# Let's plot that with time.

fig = plt.figure(figsize=(15, 4))
plt.scatter(sun[3], sun[4], s=0.1)

# Label it.

plt.title("Sunspot activity \n")
plt.ylabel('Daily sunspot number')
plt.xlabel('Year')

plt.show()
In [ ]:
# Let's differentiate between the hemispheres.

fig = plt.figure(figsize=(15, 4))
plt.scatter(sun[3], sun[5], s=0.5, label='North', alpha=0.5)
plt.scatter(sun[3], sun[6], s=0.5, label='South', alpha=0.5)

# Label 'em.

plt.title("Sunspot activity \n")
plt.ylabel('Daily sunspot number')
plt.xlabel('Year')

plt.legend(loc='upper right')

plt.show()

3. Try it out!

Try creating something here. Write some text, embed pictures in it, write some code. The "Guide to using Python" -document may help here.

In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]: