Skip to content

Instantly share code, notes, and snippets.

@AbdealiLoKo
Created September 24, 2016 10:24
Show Gist options
  • Save AbdealiLoKo/05b8d2e6ded9bcb58e10deb16c7bacd5 to your computer and use it in GitHub Desktop.
Save AbdealiLoKo/05b8d2e6ded9bcb58e10deb16c7bacd5 to your computer and use it in GitHub Desktop.
WIkimedia Hackathon - Bits Pilani Hyderabad Campus
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Introduction to Jupyter"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Jupyter is a web version of the python shell. It's useful to write text like this, or can also write python code and execute them block by block. It can also be used for other languages like Julia, R, Java, Javascript, and so on (pretty much anything!). This notebook introduces the basic blocks that help us use the notebook.\n",
"\n",
"It provides an interactive interface to be able to run blocks of code and also to write documentation to understand the code."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1. Cells"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Jupyter's basic building block is a cell. There are majorly two different types of cells in jupyter:\n",
" - **Code cells** - Code cells have code which is executed by the kernel. Hence, the also have an output section right underneath the cell.\n",
" - **Markdown cells** - Markdown cells are parsed into HTML and using markdown stylization.\n",
"\n",
"**Focusing on cells**: Cells can be selected by clicking on them. The currently focused cell is shown with a blue border. It is also possible to change the focused cell by using the arrow keys."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 2. Editing cells"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are two modes that are available on Jupyter notebooks (Similar to vim or emacs):\n",
"\n",
" - **Edit mode** - If a cell is in an edit mode, it is possible to type content into it. When a cell is in the edit mode, it has a <span style=\"color: green\">**green border**</span>.\n",
" - **Command mode** - If a cell is in the command mode, it can't be edited. But it's easier to move, copy, cut, run, etc. and perform operations on the cell itself. When a cell is in a command mode, it has a <span style=\"color: blue\">**blue border**</span>."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Creating a new cell"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To create a new cell, you can either:\n",
" - Choose the \"Insert\" > \"Insert cell below\" or \"Insert cell above\" option in the menubar on the top.\n",
" - Type <kbd>b</kbd> to add a cell below the focused cell. Type <kbd>a</kbd> to add a cell above the focused cell. This needs to be done in the command mode."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Deleting a cell"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To delete a cell, you can either:\n",
" - Choose the \"Edit\" > \"Delete a cell\" option in the menubar on the top.\n",
" - Type <kbd>dd</kbd> to delete the cell in the command mode. (2 times <kbd>d</kbd>)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3. Running Code"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Code cells allow you to enter and run code in them, unline markdown cells.\n",
"\n",
"Run a code cell by:\n",
" 1. Focus on the cell by clicking on it.\n",
" 2. Then, press `Shift-Enter` or press the <button class='btn btn-default btn-xs'><i class=\"fa fa-step-forward\"></i></button> button in the toolbar above to run the cell."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# This creates a variable a. Hence, there's no output.\n",
"a = 10"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(\"The value of a is =\", a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are two other keyboard shortcuts for running code:\n",
"\n",
"* `Alt-Enter` runs the current cell and inserts a new cell below the one that was executed.\n",
"* `Ctrl-Enter` run the current cell and enters the edit mode of the cell to edit it.\n",
"\n",
"You can check if a cell is running by looking on it's left. If it shows <span class=\"prompt\" style=\"color: #303F9F;\">In [*]:</span>, it means that the cell is still being run. Once the cell has completed running, it is assigned a number. For example: <span class=\"prompt\" style=\"color: #303F9F;\">In [4]:</span>. The number denotes the order in which the cells are run. Hence, if `4` is shown, the cell was the fourth to be run in the notebook.\n",
"\n",
"Code cells show the output of the code below the cell. If there is no output (Like in `a = 10`) there is no output shown. If there is an error, it shows the error with helpful debugging information instead:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"a ="
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 4. The Kernel"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"All code is run using a kernel. The kernel is a separate process which links the notebook with the compiler/interpreter of the appropriate type (based on the language). The Kernel can be interrupted or restarted.\n",
"\n",
"When you stop the execution of a kernel, python throws a `KeyboardInterrupt` exception.\n",
"\n",
"Try running the following cell. It pauses and sleeps for 30 seconds. While it is running, hit the <button class='btn btn-default btn-xs'><i class='fa fa-stop'></i></button> (stop) button in the toolbar above. It should give the `KeyboardInterrupt` exception:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import time\n",
"time.sleep(30)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Restarting the kernels"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The kernel maintains the state of a notebook's computations. You can reset this state by restarting the kernel. This is done by clicking on the <button class='btn btn-default btn-xs'><i class='fa fa-repeat icon-repeat'></i></button> in the toolbar above. On doing this, all your stored variables will be deleted from the session and will need to be redefined."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 5. Output formatting"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Handling sys.stdout and sys.stderr"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The stdout and stderr streams are displayed as text in the output area."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(\"hi, stdout\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from __future__ import print_function\n",
"import sys\n",
"print('hi, stderr', file=sys.stderr)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Output is asynchronous"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"All output is displayed asynchronously as it is generated in the Kernel. If you execute the next cell, you will see the output one piece at a time, not all at the end."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import time, sys\n",
"for i in range(5):\n",
" print(i)\n",
" time.sleep(0.5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Large outputs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Beyond a certain point, output will scroll automatically:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [],
"source": [
"for i in range(500):\n",
" print(2**i)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"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.4.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment