You are on page 1of 19

{

"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Introduction to the Jupyter Notebook\n",
"\n",
"A jupyter notebook is divided in cells. Each cell can be of one of the
following types:\n",
"\n",
"- Markdown cell\n",
"- Code cell (Python)\n",
"- Raw\n",
"\n",
"In this Notebook we will introduce the basics about the Notebook, the markdown
cells, the code cells and the raw cells. You can add, cut, copy the cells using the
tools in the menu. When a cell is selected you can change its type using the drop-
down menu. The contents of the cell will be rendered differently according to the
type of cell. To type text into the cell you must click on it and press \"enter\".
It will behave as a regular plain text editor. It will highlight syntax depending
on the type of cell used.\n",
"\n",
"This cell is a markdown cell, by the way. Click on it and press enter to see
the unredered markdown code."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Markdown\n",
"\n",
"In this section we are going to learn to use the markdown cells. These cells
are cells that are used to describe the content of the notebook.\n",
"\n",
"In the next cell you have an example of the code that appears in a Markdown
cell. Markdown cells are rendered when they are executed and only the rendered
content is displayed in the cell after execution of the cell. Because of that, I
will use a spetial markdown syntax that allows to present code verbatim. After the
cell with the verbatim markdown code I will present the actual Markdown cell **with
the same code** but rendered. You can select and press enter in any Markdown cell
in this notebook to see the code **and change it!!**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"# This is a Heading\n",
"\n",
"## This is a heading of level two\n",
"\n",
"### This is a heading of level three\n",
"\n",
"#### This is a heading of level four\n",
"\n",
"##### This is a heading of level 5\n",
"\n",
"This is regular text.\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The above code, when rendered in a Markdown cell produces de following output.
For this tutorial I will present always the markdown code in verbatim mode, as well
as the rendered markdown content."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# This is a Heading\n",
"\n",
"## This is a heading of level two\n",
"\n",
"### This is a heading of level three\n",
"\n",
"#### This is a heading of level four\n",
"\n",
"##### This is a heading of level 5\n",
"\n",
"This is regular text. \n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Fonts, rules, paragraphs and newlines.\n",
"\n",
"In Markdown one can typeset different font types to emphasise certain words or
sentences.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"**Use double star to get bold text.**\n",
"\n",
"__You can get the same result using a double underscore.__\n",
"\n",
"*Use a single star to get italic font.*\n",
"\n",
"_You can get italics also using a single underscore._\n",
"\n",
"__*This works also*__\n",
"\n",
"***Try other combinations***\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Use double star to get bold text.**\n",
"\n",
"__You can get the same result using a double underscore.__\n",
"\n",
"*Use a single star to get italic font.*\n",
"\n",
"_You can get italics also using a single underscore._\n",
"\n",
"__*This works also*__\n",
"\n",
"***Try other combinations***"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"An important thing about Markdown is that a simple linebreak\n",
"like the one I just did will be interpreted as a whitespace.\n",
"\n",
"If you want a new paragraph you need to introduce an empty line, like the one
above these lines. When you do this a new paragraph will start. Sometimes, however,
you want not to start a new paragraph <br/> and want just a newline. In that case
you need to use the newline html symbol &lt;br/&gt;\n",
"\n",
"Another way of introducing \n",
"a line break is to end a line with two white spaces\n",
"\n",
"Finally, you might want to introduce a horizontal rule between paragraphs.
This is done using three dashes ---\n",
"\n",
"---\n",
"\n",
"Like that\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"An important thing about Markdown is that a simple linebreak\n",
"like the one I just did will be interpreted as a whitespace.\n",
"\n",
"If you want a new paragraph you need to introduce an empty line, like the one
above these lines. When you do this a new paragraph will start. Sometimes, however,
you want not to start a new paragraph <br/> and want just a newline. In that case
you need to use the newline html symbol &lt;br/&gt;\n",
"\n",
"Another way of introducing \n",
"a line break is to end a line with two white spaces\n",
"\n",
"Finally, you might want to introduce a horizontal rule between paragraphs.
This is done using three dashes ---\n",
"\n",
"---\n",
"\n",
"Like that"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Lists\n",
"\n",
"Lists, either ordered or unordered, can be constructed easily in Markdown."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"An unordered list\n",
"\n",
"- Apple\n",
"- Cherry\n",
"- Knot\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"An unordered list\n",
"\n",
"- Apple\n",
"- Cherry\n",
"- Knot"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"An ordered list\n",
"\n",
"1. One\n",
"2. Two\n",
"1. Three\n",
"6. Four\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It doesn't matter what number you put before the dot. The list will be ordered
appropriately. The first number will be the start of the sequence of labels."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"An ordered list\n",
"\n",
"1. One\n",
"2. Two\n",
"1. Three\n",
"6. Four"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"An ordered list\n",
"\n",
"6. Four\n",
"1. One\n",
"2. Two\n",
"1. Three\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"An ordered list\n",
"\n",
"6. Four\n",
"1. One\n",
"2. Two\n",
"1. Three\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Nested lists\n",
"\n",
"The different types of lists can be nested. To define a new level you need to
indent the corresponding item as many spaces as the beginning of the text in the
level you want to insert. Further nesting can be achieved with extra indentation."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"- Element of the outer list\n",
" - Second level of list. To start the second level you need to **indent 2
spaces**\n",
" - Another element in the second level\n",
"- An element in the outer list\n",
" - Second level\n",
" - Third Level\n",
"- Another element in the outer list\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Element of the outer list\n",
" - Second level of list. To start the second level you need to **indent 2
spaces**\n",
" - Another element in the second level\n",
"- An element in the outer list\n",
" - Second level\n",
" - Third Level\n",
"- Another element in the outer list\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ordered and unordered elements might be combined"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"- Element of the outer list\n",
" 1. Second level of list. To start the second level you need to **indent 2
spaces**\n",
" 1. Another element in the second level\n",
"- An element in the outer list\n",
" 4. Second level\n",
" - Third Level. **This requires _three_ extra spaces instead of just
_two_**. Check what happens if we use just two spaces.\n",
"- Another element in the outer list\n",
"\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Element of the outer list\n",
" 1. Second level of list. To start the second level you need to **indent 2
spaces**\n",
" 1. Another element in the second level\n",
"- An element in the outer list\n",
" 4. Second level\n",
" - Third Level. **This requires _three_ extra spaces instead of just
_two_**. Check what happens if we use just two spaces.\n",
"- Another element in the outer list\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"1. Element of the outer list\n",
" - Second level of list. To start the second level you need to **indent 3
spaces**\n",
" - Another element in the second level\n",
"3. An element in the outer list\n",
" - Second level\n",
" 1. Third Level **This requires just two extra spaces.**\n",
"5. Another element in the outer list\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. Element of the outer list\n",
" - Second level of list. To start the second level you need to **indent 3
spaces**\n",
" - Another element in the second level\n",
"3. An element in the outer list\n",
" - Second level\n",
" 1. Third Level **This requires just two extra spaces.**\n",
"5. Another element in the outer list"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Hyperlinks\n",
"\n",
"Hyperlinks can be included easily."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```\n",
"One can write hyperlinks too: [Hyperlink to the Repository of this subject]
(https://github.com/jmppardo/Perspectivas)\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"One can write hyperlinks too: [Hyperlink to the Repository of this subject]
(https://github.com/jmppardo/Perspectivas)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Verbatim\n",
"\n",
"What I have been using to display the markdown code is a block of verbatim
code. The beginning and ending of a block of verbatim code is denoted with three
backticks (```). All the text contained within two sequences of three backticks
will not be rendered by the markdown engine and displayed verbatim. The engine does
a good job and does not get confused by the bacticks above this lines.\n",
"\n",
"```\n",
"# This should be a markdown header after rendering\n",
"\n",
"And this should be _italised_\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"One nice feature is that the code can be highlighted in almost any common
language. You just need to name the programming language after the first three
backticks.\n",
"\n",
"**A block of HTML code** Obtained using ```html\n",
"\n",
"```html\n",
"<div class='perspective'>I am a block of HTML code<div/>\n",
"```\n",
"\n",
"**A block of python code** Obtained using ```python\n",
"\n",
"```python\n",
"for x in range(4):\n",
" print(x)\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code cells"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# This is a python cell \n",
"# This type of cells will we read by the python interpreter and rendered.\n",
"# Lines preceded by a '#' will be ignored by the interpreter and can be used
to introduce comments\n",
"# After exectution of the cell, the output, if any, will be displayed below
the cell.\n",
"# This cell has no output"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Code cells produce no output unless the code in them does so. "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"a=0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"An exception is when a variable is written at the end of the cell"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a=0\n",
"\n",
"a"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice that after execution of a code cell, the cell remains there. This is
opposed to what happens with the markdown cells. When the markdown cells are
rendered, the rendered text substitutes the markdown code. That is why I had to use
two different markdown cells in each of the examples above, to show explicitly the
markdown code.\n",
"\n",
"The variables that are defined in one cell, like the variable 'a' in the
previous cell, are available in the full notebook. You do not need to defined them
in each cell. Of course, and of course you can erase them if you are not carefull.\
n",
"\n",
"The following cell will display the value of the variable 'a' defined before,
redefine it, and display it again"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"2\n"
]
}
],
"source": [
"print(a)\n",
"\n",
"a=2\n",
"\n",
"print(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are many types of variables. Among the important ones there are
_strings_, _integers_, _floating point numbers_ and _boolean_."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'int'> <class 'float'> <class 'str'> <class 'bool'> <class 'bool'>\n"
]
}
],
"source": [
"a = 1\n",
"b = 1.0\n",
"c = \"Nobody expects the spanish inquisition\" #Strings are defined using
quotes and represent text\n",
"d = True\n",
"e = False\n",
"\n",
"print(type(a),type(b),type(c),type(d),type(e))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Formating strings\n",
"\n",
"It is very common to want to display a text (a string) whose contents depend
on some varibles whose values might change at run time. A very usefull tool to do
this (there are more) is to use so called _f-strings_\n",
"\n",
"These are particular strings which are defined with an _f_ before the quotes\
n",
"\n",
"```python\n",
" f\"This is an example of f-string\"\n",
"```\n",
" \n",
"These strings can represent the values of the variables"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Alice is 22 years old.\n"
]
}
],
"source": [
"name = \"Alice\"\n",
"age = 22\n",
"\n",
"fstring = f\"{name} is {age} years old.\"\n",
"\n",
"print(fstring)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"One can even format the value to be displayed in the string. For instance one
can use different representations of numbers."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Alice is 22.00 years old.\n",
"Alice is 2.200000e+01 years old.\n"
]
}
],
"source": [
"fstring2 = f\"{name} is {age:.2f} years old.\"\n",
"\n",
"print(fstring2)\n",
"\n",
"fstring3 = f\"{name} is {age:e} years old.\"\n",
"\n",
"print(fstring3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Python and indentation\n",
"\n",
"In Python the different programming loops and functions are characterised with
indentation. There is not and \"end\" statement to denote when a function or loop
ends. Instead, a given function or loop is indented and when the given indentation
finishes determines the end of the function.\n",
"\n",
"This makes the code written in Python by default very readable and shareable.
But it has the (small) disadvantage that Python interpreter is sensible to
indentation, so one must be very careful with it.\n",
"\n",
"Indentation is defined to be **4 blank spaces**"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n"
]
}
],
"source": [
"# This is an example of a for loop\n",
"\n",
"for i in range(5):\n",
"\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"First print in the loop: 0\n",
"0 This string is inside the if statement\n",
"Hello\n",
"First print in the loop: 1\n",
"Hello\n",
"First print in the loop: 2\n",
"2 This string is inside the if statement\n",
"Hello\n",
"First print in the loop: 3\n",
"Hello\n",
"First print in the loop: 4\n",
"4 This string is inside the if statement\n",
"Hello\n",
"I'm outside the for loop.\n"
]
}
],
"source": [
"# This is an example of an if clause nested in a for loop. \n",
"# Notice the double indentation, 8 spaces in total\n",
"\n",
"for i in range(5):\n",
" \n",
" print(f\"First print in the loop: {i}\") # This statement is directly
inside the for loop\n",
"\n",
" if i%2==0: # This 'if' statement is also directly inside the foor loop\n",
"\n",
" print(f\"{i} This string is inside the if statement\") # This
statement is inside the if statement and is printed only if the variable 'i' is
even.\n",
"\n",
" print(\"Hello\") # This statement is again in the for-loop and outside the
if-statement\n",
" \n",
"print(\"I'm outside the for loop.\") # This statement is outside the for loop"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"# We are going to define a function. \n",
"# A function has some input variables, might be none.\n",
"# It executes certain code using the variables.\n",
"# Indentation works in the same way than it works for loops.\n",
"\n",
"# This function depends on one variable.\n",
"\n",
"def my_func1(name):\n",
" \n",
" fstring = f\"{name} is a beautiful name.\"\n",
" \n",
" print(fstring)\n",
" \n",
"# This function depends on no variable.\n",
"\n",
"def my_func2(): \n",
" \n",
" fstring = f\"I will print this statement anyway.\"\n",
"\n",
" print(fstring)\n",
"\n",
"def my_func3(name,age):\n",
" \n",
" fstring = f\"{name}'s age is {age}.\"\n",
"\n",
" print(fstring)\n",
"\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bob is a beautiful name.\n"
]
}
],
"source": [
"my_func1(\"Bob\") "
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<function __main__.my_func2()>"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_func2 # Even if the function is defined with no variable one has to put the
parentesis. Otherwise the function returns its own information and is not executed"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I will print this statement anyway.\n"
]
}
],
"source": [
"my_func2() #Now the function is executed"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bob's age is 38.\n"
]
}
],
"source": [
"my_func3(\"Bob\", 38)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Lists and dictionaries in Python\n",
"\n",
"A list in python is a special type of variable that collects many elements.
Lists are iterable and are one of python's most powerful tools. List are defined
with square braquets and their elements are separated by commas. "
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Apple', 'Strawberry', 'Banana']"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_list = [\"Apple\", \"Strawberry\", \"Banana\"]\n",
"\n",
"my_list"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lists can be used as the generator for for-loops"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Apple\n",
"Strawberry\n",
"Banana\n"
]
}
],
"source": [
"for i in my_list:\n",
" \n",
" print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The elements of a list can be accesed by their index number (the position at
which they are). The first element has \"0\" index, so be careful. This is the same
convention as in other languages like C, and different from languages like Matlab"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Apple'"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_list[0]"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Banana'"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_list[2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Dictionaries are also iterables like lists. However, instead of having a
numbered index, each element has a label called \"key\" that you can use to recover
the value associated to it. Dictionaries are defined with curly brackets "
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"my_dict = {\"name\" : \"Bob\", \"age\" : 22, \"height\" : 1.68}"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bob is 22 years old and his height is 1.68m.\n"
]
}
],
"source": [
"print(f\"{my_dict['name']} is {my_dict['age']} years old and his height is
{my_dict['height']}m.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In a dictionary one can iterate over the keys, the values or both of them at
the same time."
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"name\n",
"age\n",
"height\n"
]
}
],
"source": [
"for x in my_dict:\n",
" print(x)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bob\n",
"22\n",
"1.68\n"
]
}
],
"source": [
"for x in my_dict.values():\n",
" print(x)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Key: name; Value: Bob\n",
"Key: age; Value: 22\n",
"Key: height; Value: 1.68\n"
]
}
],
"source": [
"for key, value in my_dict.items():\n",
" print(f\"Key: {key}; Value: {value}\")\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

You might also like