{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Welcome to Jupyter Notebooks!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### What, why?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n", "\n", "One important feature is the ease of setup: Notebooks can be run entirely in browser without any installations or log-ins required. [MyBinder](https://mybinder.org) is probably the easiest tool to use when the teacher has their materials in a Github-repository, either their own or one like [this](https://github.com/cms-opendata-education). 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.\n", "\n", "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. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### How?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try it! This whole document is your playground.\n", "\n", "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](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) may help. LATEX is also supported, for all your equation needs.\n", "\n", "Also, pressing *h* brings up the list for shortkeys, when you haven't chosen any cell to be active." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Let's get to it, then." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n", "\n", "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.\n", "\n", "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.**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# I'm a comment! Hashed lines are not run with the rest of the cell, thus making it easy to make your code\n", "# easier for someone else to read.\n", "\n", "import pandas as pd # this deals with data\n", "import numpy as np # this incorporates things like natural constants\n", "import matplotlib.pyplot as plt # this gives you tools to draw graphs\n", "import random as rand # this allows for random number generation\n", "\n", "%matplotlib inline\n", "# this one makes your outputs prettier and let's you see your graphs directly" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are lots of modules out there, but we'll start with these as they cover a pretty wide area already." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1. Basics" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(2+2)\n", "\n", "print(2*5)\n", "\n", "numbers = [2,10]\n", "print(numbers)\n", "\n", "# Try these out with different numbers, operations and so forth." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lottery = rand.randint(1,100)\n", "text = \"Today's winning ticket is: \"\n", "print (text + str(lottery))\n", "\n", "# Text and variables go together just fine." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2. Plot we go" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbah = [1,2,3,6,7,8,12,16]\n", "plt.plot(numbah, 'b*')\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Let's define the variable.\n", "\n", "ages = [14,15,14,16,17,16,14,15,16,16,14,15,15,12,34,12,15,16,16]\n", "\n", "# Plotting a histogram. If you change 'ages', adjust range accordingly.\n", "\n", "plt.hist(ages, bins=100, range=(10,40))\n", "\n", "# Here we create headers.\n", "\n", "plt.xlabel('Age in years')\n", "plt.ylabel('Number')\n", "plt.title('Age distribution in class \\n') # \\n makes a new line, for aesthetic reasons\n", "\n", "# This shows the output cleanly.\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Right, let's delve into some data. [Here](http://sidc.oma.be/silso/infosndhem) 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!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# read data from the internet\n", "\n", "sun = pd.read_table('http://sidc.oma.be/silso/INFO/sndhemcsv.php', sep=';', encoding = \"ISO-8859-1\", header=-1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# check what it looks like\n", "\n", "sun.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Let's plot that with time.\n", "\n", "fig = plt.figure(figsize=(15, 4))\n", "plt.scatter(sun[3], sun[4], s=0.1)\n", "\n", "# Label it.\n", "\n", "plt.title(\"Sunspot activity \\n\")\n", "plt.ylabel('Daily sunspot number')\n", "plt.xlabel('Year')\n", "\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Let's differentiate between the hemispheres.\n", "\n", "fig = plt.figure(figsize=(15, 4))\n", "plt.scatter(sun[3], sun[5], s=0.5, label='North', alpha=0.5)\n", "plt.scatter(sun[3], sun[6], s=0.5, label='South', alpha=0.5)\n", "\n", "# Label 'em.\n", "\n", "plt.title(\"Sunspot activity \\n\")\n", "plt.ylabel('Daily sunspot number')\n", "plt.xlabel('Year')\n", "\n", "plt.legend(loc='upper right')\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3. Try it out!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try creating something here. Write some text, embed pictures in it, write some code. The \"Guide to using Python\" -document may help here." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.5" } }, "nbformat": 4, "nbformat_minor": 2 }