"### Week 02 - (gentle) Introduction to Python\n",
"\n",
"\n",
"This interactive lecture provides a gentle introduction to syntax and semantics of Python programming language. It is based on the material created and curated by Marvin van Aalst, Tobias Pfennig and Anna Matuszyńska, iterated over the years while teaching introduction to Python at the university level and during the summer schools in computational modelling."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Why to learn how to program?\n",
"\n",
"* Teaches abstract thinking.\n",
"* Is solution oriented.\n",
"* Speeds up many processes."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Why to learn Python?\n",
"\n",
"* It is beginner-friendly and has accessible syntax.\n",
"* It is versatile.\n",
"* Python developers are in high demand on the job market. \n",
"* It is community driven and there is a lot of support on-line.\n",
"* It is the fastest-growing programming language."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Why Python and not R?\n",
"\n",
"- Many people are using it, including NASA (take a look at [lecture for astronomers](https://swift.gsfc.nasa.gov/results/BATbursts/ASTR_288C/Lecture6.pdf)\n",
"- Easy to start\n",
"- Created by a researcher for researchers (conceived in the late 1980s by Guido van Rossum at Centrum Wiskunde & Informatica (CWI) in the Netherlands)\n",
"- (A lot) more human friendly than basic languages like C\n",
"- Powerful (combine coding and ploting)\n",
"- Many, many supporing packages for various disciplines (including our own [modelbase](https://pypi.org/project/modelbase/))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Python philosophy\n",
"The core philosophy of the language is summarized by the document \"PEP 20 (The Zen of Python)”"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"import this"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Who is using Python?\n",
"\n",
"* Web Development: Google, Yahoo, Shopzilla\n",
"* Games: Battelfield 2, Civilization 4, Star Trek Bridge Commander\n",
"* Financial: Altis Investment Management\n",
"* Graphics: Walt Disney Feature Animation\n",
"* Software Development: Nokia, Red Hat\n",
"* Science: The National Research Council of Canada,\n",
" Los Alamos National Laboratory Theoretical Physics Division\n",
" NASA\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Jupyter notebook overview\n",
"\n",
"* Splits code into cells\n",
" * Code cells\n",
" * Markdown cells\n",
"* Command mode (**no blue border around code cell**)\n",
" * `Esc` to enter\n",
" * `A` to create cell above\n",
" * `B` to create cell below\n",
" * `D + D` to delete cell\n",
" * `M` to change cell type to Markdown\n",
" * `Y` to change cell type to Code\n",
"* Edit mode (**with blue border around code cell**)\n",
" * `Shift + Enter` run selected cell and select below"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Getting help and understanding errors\n",
"\n",
"Your code will produce errors and you will forget how certain functions work. \n",
"That's absolutely fine and nothing to worry about. \n",
"Since no-one can remember everything, programmers usually build helpers for themselves. \n",
"\n",
"Your first tool for that is a **comment**. You write comments following the `#` character. \n",
"\n",
"```python\n",
"# Future me: this is why I implemented the function in the following way\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# How to comment your code properly?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"## Code Tells You How, Comments Tell You Why"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"The second tool is a documentation string (or `docstring` for short). \n",
"You write docstrings in the first line of a function (more on that later). \n",
"\n",
"```python\n",
"def my_func():\n",
" \"\"\"Some very important information\"\"\"\n",
" return None\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"def sum_this(a, b, c):\n",
" \"\"\"Use this multi-line string to explain what your function does,\n",
" which arguments it takes and what it returns\n",
"\n",
" Arguments\n",
" ---------\n",
" a specify type (int, float?)\n",
" b int\n",
" c int\n",
"\n",
" Returns\n",
" -------\n",
" Sum of a, b and c\n",
" \"\"\"\n",
" return a + b + c"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Other people also (hopefully) write `docstrings`. \n",
"For any function, you can call the `help` function to get its `docstring`.\n",
"\n",
"```python\n",
"help(print)\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"If you want to know what methods are available for a given object, thet `dir()` function will tell you that. \n",
"Another quick way of checking this is to write `object.` and then use the `<TAB>` key to auto-complete. \n",
"\n",
"```python\n",
"dir(\"Hello, World\")\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"You will frequently encounter errors when programming - that perfectly normal, however you should learn to understand what they *mean*. \n",
"For example, if you type `\"1\" + 1` you will get something along the lines of \n",
"\n",
"```TypeError: can only concatenate str (not \"int\") to str```. \n",
"\n",
"Don't worry if you don't understand yet what that means, it hopefully will get clear in the next minutes. \n",
"Of course, you can always ask us if you don't know how to proceed"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Syntax and semantix"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"Assign value 5 to a variable called x. This is semantics."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"Programming languages offer different ways to provide the same semantix:\n",
" \n",
" R: x<- 5\n",
" \n",
" Pascal: x := 5"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"#answer"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"The syntax is the set of rules that defines how a program should be written. It is language-specific constraint on how we express semantics."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Syntax and semantix\n",
"The **syntax** of a programming language describes which strings of of characters comprise a valid program. The **semantics** of a programming language describes what syntactically valid programs mean, what they do. In the larger world of linguistics, syntax is about the form of language, semantics about meaning"
"next to sequence, mappind, binary and set types. \n",
"\n",
"We will go through each the four most important types (`None`, `bool`, `int` and `float`), showing their use case and how they can be transformed.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Bool\n",
"\n",
"The boolean type `bool` can only hold the values `True` and `False`. \n",
"It can be used for simple logic operations. \n",
"There are five operators for `bool`. \n",
"\n",
"| Operator | `True` if |\n",
"| ------- | --------- |\n",
"| `and` | **both** the inputs are `True` | \n",
"| `or` | one or both of the inputs are `True` | \n",
"| `not` | the input is `False` | \n",
"| `==` | both inputs are equal | \n",
"| `!=` | both inputs are unequal | \n",
"\n",
"Now print the results and check if you were correct."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"# answer"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Exercise: logic / truth table\n",
"\n",
"Fill out the following truth table for two inputs `p` and `q`\n",
"\n",
"| p | q | p and q | p or q | \n",
"| --- | --- | --- | --- |\n",
"| T | T | ? | ? |\n",
"| T | F | ? | ? |\n",
"| F | T | ? | ? |\n",
"| F | F | ? | ? |\n",
"\n",
"and next print the results.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"# answer"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"In the example above, you can check the type of object x by typing`type(x)`. Try it"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"# answer"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Function definition\n",
"\n",
"Before we continue exploring things to do with those data types, let's introduce an important building block for programs: **functions**. \n",
"\n",
"With functions you can save a specific sequence of instructions to be run, so that you don't have to write out the entire sequence every time you want to run it."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"To create your own custom functions you need to define them before they can be called. \n",
"The basic syntax for this is \n",
"\n",
"- the keyword `def` followed by a `name` you can choose\n",
"- the function parameters in parentheses followed by colon (`:`), and a line break\n",
"- and then the **indented** function body\n",
"- a **return statement**, indicating where to exit the function while returning a value"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"A function can have zero `()`, one `(a)` or multiple `(a, b, ...)` arguments. \n",
"An example (with the **indentation of four spaces being part of the syntax**) below:\n",
"You have to use this syntax exactly. Try around a bit to see what happens, if you change minor things about it"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"# define the function"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"# change the code 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"# change the code 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"# change the code 3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"# change the code 4"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Function calls\n",
"\n",
"To call a function, you use it's name and then supply the arguments in parentheses:\n",
"\n",
"```python\n",
"print(True and False)\n",
"```\n",
"\n",
"Again, you have to be precise about the syntax."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Call the previously created function add to see what is the sum of 4 and 5."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"# answer"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Try printing several boolean values, like `True` or `True, False`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"# answer"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Exercise: xor\n",
"\n",
"`XOR` (exclusive or) is `True` when **only one** of its inputs is `True`, so its closer to how we usually think of what the word `or` means. \n",
"For two propositions `p` and `q` the `xor` function can be written as \n",
"\n",
"$(p \\land \\neg q) \\lor (\\neg p \\land q)$\n",
"\n",
"with $\\land$ meaning `and`, $\\lor$ meaning `or` and $\\neg$ meaning `not`.\n",
"\n",
"Write a function `xor(p, q)` that takes two boolean values and returns the exclusive or of them."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"#### Note on solving the exercises\n",
"\n",
"In all following exercises you will be asked to write functions to implement certain behaviour. \n",
"In order to help you solve the puzzles on your own, we already supply the name of the functions, while you will have to replace the Ellipses (`...`) with actual code. \n",
"\n",
"```python\n",
"def xor(p, q):\n",
" ... # replace this\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Assert\n",
"Note that below the function we have already supplied `assert` statements. \n",
"\n",
"\n",
"```python\n",
"assert xor(True, True) == False\n",
"```\n",
"\n",
"These asserts will test your function, so you can check if it was implemented correctly and if not, for *which input* the output fails, which you can use as a guide to where the error in your implementation might be. \n",
"**Please don't change or remove those `assert` statements**. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"# answer. Make sure your functions name is xor\n",
"\n",
"assert xor(True, True) == False\n",
"assert xor(True, False) == True\n",
"assert xor(False, True) == True\n",
"assert xor(False, False) == False"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Conditional control flow\n",
"\n",
"The real use case of boolean values becomes apparent when one uses them to control how the program executes. \n",
"This can be done with the `if` / `elif` / `else` statements, `elif` being short for `else if`. \n",
"Note that the `if` statement can stand completely alone, be optionally followed by an arbitrary amount of `elif` statements and lastly finished with an optional `else` statement. \n",
"So in the example below, both the `elif` and `else` block could have been omitted. "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Just like with functions, **indentation of four spaces is part of the syntax**. \n",
"\n",
"```python\n",
"if it_rains():\n",
" pack_raincoat()\n",
"elif it_is_sunny(): \n",
" pack_tshirt()\n",
"else:\n",
" pack_jacket()\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Exercise: xor with branching\n",
"\n",
"Rewrite your `xor` function from above with `if` and `else` statements instead of the `and` or `or` operators."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"# answer. Make sure your functions name is xor_if_else\n",
"\n",
"\n",
"assert xor_if_else(True, True) == False\n",
"assert xor_if_else(True, False) == True\n",
"assert xor_if_else(False, True) == True\n",
"assert xor_if_else(False, False) == False"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Numbers\n",
"\n",
"There are two basic number types in Python: `int` and `float`. `int` numbers don't have decimal digits, while `float` values do. The usual arithmetic operations are defined, however they come with some sharp edges one should be aware of. We will come back to them later on.\n",
"\n",
"| Operation | Operator | \n",
"| --- | --- |\n",
"| Addition | `+` |\n",
"| Subtraction | `-` |\n",
"| Multiplication | `*` |\n",
"| Exponentiation | `**` |\n",
"| Float Division | `/` |\n",
"| Int (Floor) Division | `//` |\n",
"| Modulus | `%` |"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Just like with booleans we can compare if two numbers are exactly equal\n",
"\n",
"| Operator | Meaning | \n",
"| --- | --- |\n",
"| `==` | Exactly equal |\n",
"| `!=` | Not equal |"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"But we can also check whether one is larger or smaller than the other\n",
"\n",
"| Operator | Meaning | \n",
"| --- | --- |\n",
"| `>` | Larger |\n",
"| `>=` | Larger or equal |\n",
"| `<` | Smaller |\n",
"| `<=` | Smaller or equal |"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Exercise: multiple of two numbers\n",
"\n",
"Create a function that checks if a number is a multiple of two other numbers `a` and `b`\n",
"\n",
"**Hint**: the **modulo operator (%)** returns the remainder of a division\n",
"\n",
"| Input | Output |\n",
"| ----- | ------ |\n",
"| 4 % 3 | 1 |\n",
"| 5 % 3 | 2 |\n",
"| 6 % 3 | 0 |"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"# answer. Make sure your functions name is is_multiple_of_both\n",
"\n",
"\n",
"assert is_multiple_of_both(2, 2, 1)\n",
"assert is_multiple_of_both(6, 3, 2)\n",
"assert not is_multiple_of_both(7, 3, 2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Assignment\n",
"\n",
"Before we continue exploring things to do with numbers, let's talk about **assignment statements**. \n",
"You can assign a value to a variable using the `name = value` syntax, for example\n",
"\n",
"```python\n",
"x1 = 1\n",
"```\n",
"\n",
"After you assigned a value to a variable, you can use the name to refer to the value\n",
"\n",
"```python\n",
"print(x1) # This prints \"1\"\n",
"```\n",
"\n",
"Note that the syntax requires your name to start with an character. \n",
This interactive lecture provides a gentle introduction to syntax and semantics of Python programming language. It is based on the material created and curated by Marvin van Aalst, Tobias Pfennig and Anna Matuszyńska, iterated over the years while teaching introduction to Python at the university level and during the summer schools in computational modelling.
%% Cell type:markdown id: tags:
# Why to learn how to program?
* Teaches abstract thinking.
* Is solution oriented.
* Speeds up many processes.
%% Cell type:markdown id: tags:
# Why to learn Python?
* It is beginner-friendly and has accessible syntax.
* It is versatile.
* Python developers are in high demand on the job market.
* It is community driven and there is a lot of support on-line.
* It is the fastest-growing programming language.
%% Cell type:markdown id: tags:
# Why Python and not R?
- Many people are using it, including NASA (take a look at [lecture for astronomers](https://swift.gsfc.nasa.gov/results/BATbursts/ASTR_288C/Lecture6.pdf)
- Easy to start
- Created by a researcher for researchers (conceived in the late 1980s by Guido van Rossum at Centrum Wiskunde & Informatica (CWI) in the Netherlands)
- (A lot) more human friendly than basic languages like C
- Powerful (combine coding and ploting)
- Many, many supporing packages for various disciplines (including our own [modelbase](https://pypi.org/project/modelbase/))
%% Cell type:markdown id: tags:
# Python philosophy
The core philosophy of the language is summarized by the document "PEP 20 (The Zen of Python)”
%% Cell type:code id: tags:
``` python
importthis
```
%% Cell type:markdown id: tags:
# Who is using Python?
* Web Development: Google, Yahoo, Shopzilla
* Games: Battelfield 2, Civilization 4, Star Trek Bridge Commander
* Financial: Altis Investment Management
* Graphics: Walt Disney Feature Animation
* Software Development: Nokia, Red Hat
* Science: The National Research Council of Canada,
Los Alamos National Laboratory Theoretical Physics Division
NASA
%% Cell type:markdown id: tags:
## Jupyter notebook overview
* Splits code into cells
* Code cells
* Markdown cells
* Command mode (**no blue border around code cell**)
*`Esc` to enter
*`A` to create cell above
*`B` to create cell below
*`D + D` to delete cell
*`M` to change cell type to Markdown
*`Y` to change cell type to Code
* Edit mode (**with blue border around code cell**)
*`Enter`
*`Ctrl + Shift + -` Split cell at cursor position
* Execute code in a cell
*`Ctrl + Enter` run selected cell
*`Shift + Enter` run selected cell and select below
%% Cell type:markdown id: tags:
## Getting help and understanding errors
Your code will produce errors and you will forget how certain functions work.
That's absolutely fine and nothing to worry about.
Since no-one can remember everything, programmers usually build helpers for themselves.
Your first tool for that is a **comment**. You write comments following the `#` character.
```python
# Future me: this is why I implemented the function in the following way
```
%% Cell type:markdown id: tags:
# How to comment your code properly?
%% Cell type:code id: tags:
``` python
## Code Tells You How, Comments Tell You Why
```
%% Cell type:markdown id: tags:
The second tool is a documentation string (or `docstring` for short).
You write docstrings in the first line of a function (more on that later).
```python
defmy_func():
"""Some very important information"""
returnNone
```
%% Cell type:code id: tags:
``` python
defsum_this(a,b,c):
"""Use this multi-line string to explain what your function does,
which arguments it takes and what it returns
Arguments
---------
a specify type (int, float?)
b int
c int
Returns
-------
Sum of a, b and c
"""
returna+b+c
```
%% Cell type:markdown id: tags:
Other people also (hopefully) write `docstrings`.
For any function, you can call the `help` function to get its `docstring`.
```python
help(print)
```
%% Cell type:markdown id: tags:
If you want to know what methods are available for a given object, thet `dir()` function will tell you that.
Another quick way of checking this is to write `object.` and then use the `<TAB>` key to auto-complete.
```python
dir("Hello, World")
```
%% Cell type:markdown id: tags:
You will frequently encounter errors when programming - that perfectly normal, however you should learn to understand what they *mean*.
For example, if you type `"1" + 1` you will get something along the lines of
```TypeError: can only concatenate str (not "int") to str```.
Don't worry if you don't understand yet what that means, it hopefully will get clear in the next minutes.
Of course, you can always ask us if you don't know how to proceed
%% Cell type:markdown id: tags:
# Syntax and semantix
%% Cell type:markdown id: tags:
Assign value 5 to a variable called x. This is semantics.
%% Cell type:markdown id: tags:
Programming languages offer different ways to provide the same semantix:
R: x<- 5
Pascal: x := 5
%% Cell type:code id: tags:
``` python
#answer
```
%% Cell type:markdown id: tags:
The syntax is the set of rules that defines how a program should be written. It is language-specific constraint on how we express semantics.
%% Cell type:markdown id: tags:
# Syntax and semantix
The **syntax** of a programming language describes which strings of of characters comprise a valid program. The **semantics** of a programming language describes what syntactically valid programs mean, what they do. In the larger world of linguistics, syntax is about the form of language, semantics about meaning
We will go through each the four most important types (`None`, `bool`, `int` and `float`), showing their use case and how they can be transformed.
%% Cell type:markdown id: tags:
## Bool
The boolean type `bool` can only hold the values `True` and `False`.
It can be used for simple logic operations.
There are five operators for `bool`.
| Operator | `True` if |
| ------- | --------- |
| `and` | **both** the inputs are `True` |
| `or` | one or both of the inputs are `True` |
| `not` | the input is `False` |
| `==` | both inputs are equal |
| `!=` | both inputs are unequal |
Now print the results and check if you were correct.
%% Cell type:code id: tags:
``` python
# answer
```
%% Cell type:markdown id: tags:
### Exercise: logic / truth table
Fill out the following truth table for two inputs `p` and `q`
| p | q | p and q | p or q |
| --- | --- | --- | --- |
| T | T | ? | ? |
| T | F | ? | ? |
| F | T | ? | ? |
| F | F | ? | ? |
and next print the results.
%% Cell type:code id: tags:
``` python
# answer
```
%% Cell type:markdown id: tags:
In the example above, you can check the type of object x by typing`type(x)`. Try it
%% Cell type:code id: tags:
``` python
# answer
```
%% Cell type:markdown id: tags:
## Function definition
Before we continue exploring things to do with those data types, let's introduce an important building block for programs: **functions**.
With functions you can save a specific sequence of instructions to be run, so that you don't have to write out the entire sequence every time you want to run it.
%% Cell type:markdown id: tags:
To create your own custom functions you need to define them before they can be called.
The basic syntax for this is
- the keyword `def` followed by a `name` you can choose
- the function parameters in parentheses followed by colon (`:`), and a line break
- and then the **indented** function body
- a **return statement**, indicating where to exit the function while returning a value
%% Cell type:markdown id: tags:
A function can have zero `()`, one `(a)` or multiple `(a, b, ...)` arguments.
An example (with the **indentation of four spaces being part of the syntax**) below:
You have to use this syntax exactly. Try around a bit to see what happens, if you change minor things about it
%% Cell type:code id: tags:
``` python
# define the function
```
%% Cell type:code id: tags:
``` python
# change the code 1
```
%% Cell type:code id: tags:
``` python
# change the code 2
```
%% Cell type:code id: tags:
``` python
# change the code 3
```
%% Cell type:code id: tags:
``` python
# change the code 4
```
%% Cell type:markdown id: tags:
### Function calls
To call a function, you use it's name and then supply the arguments in parentheses:
```python
print(True and False)
```
Again, you have to be precise about the syntax.
%% Cell type:markdown id: tags:
Call the previously created function add to see what is the sum of 4 and 5.
%% Cell type:code id: tags:
``` python
# answer
```
%% Cell type:markdown id: tags:
Try printing several boolean values, like `True` or `True, False`.
%% Cell type:code id: tags:
``` python
# answer
```
%% Cell type:markdown id: tags:
### Exercise: xor
`XOR` (exclusive or) is `True` when **only one** of its inputs is `True`, so its closer to how we usually think of what the word `or` means.
For two propositions `p` and `q` the `xor` function can be written as
$(p \land \neg q) \lor (\neg p \land q)$
with $\land$ meaning `and`, $\lor$ meaning `or` and $\neg$ meaning `not`.
Write a function `xor(p, q)` that takes two boolean values and returns the exclusive or of them.
%% Cell type:markdown id: tags:
#### Note on solving the exercises
In all following exercises you will be asked to write functions to implement certain behaviour.
In order to help you solve the puzzles on your own, we already supply the name of the functions, while you will have to replace the Ellipses (`...`) with actual code.
```python
def xor(p, q):
... # replace this
```
%% Cell type:markdown id: tags:
## Assert
Note that below the function we have already supplied `assert` statements.
```python
assert xor(True, True) == False
```
These asserts will test your function, so you can check if it was implemented correctly and if not, for *which input* the output fails, which you can use as a guide to where the error in your implementation might be.
**Please don't change or remove those `assert` statements**.
%% Cell type:code id: tags:
``` python
# answer. Make sure your functions name is xor
assert xor(True, True) == False
assert xor(True, False) == True
assert xor(False, True) == True
assert xor(False, False) == False
```
%% Cell type:markdown id: tags:
## Conditional control flow
The real use case of boolean values becomes apparent when one uses them to control how the program executes.
This can be done with the `if` / `elif` / `else` statements, `elif` being short for `else if`.
Note that the `if` statement can stand completely alone, be optionally followed by an arbitrary amount of `elif` statements and lastly finished with an optional `else` statement.
So in the example below, both the `elif` and `else` block could have been omitted.
%% Cell type:markdown id: tags:
Just like with functions, **indentation of four spaces is part of the syntax**.
```python
if it_rains():
pack_raincoat()
elif it_is_sunny():
pack_tshirt()
else:
pack_jacket()
```
%% Cell type:markdown id: tags:
### Exercise: xor with branching
Rewrite your `xor` function from above with `if` and `else` statements instead of the `and` or `or` operators.
%% Cell type:code id: tags:
``` python
# answer. Make sure your functions name is xor_if_else
assert xor_if_else(True, True) == False
assert xor_if_else(True, False) == True
assert xor_if_else(False, True) == True
assert xor_if_else(False, False) == False
```
%% Cell type:markdown id: tags:
## Numbers
There are two basic number types in Python: `int` and `float`. `int` numbers don't have decimal digits, while `float` values do. The usual arithmetic operations are defined, however they come with some sharp edges one should be aware of. We will come back to them later on.
| Operation | Operator |
| --- | --- |
| Addition | `+` |
| Subtraction | `-` |
| Multiplication | `*` |
| Exponentiation | `**` |
| Float Division | `/` |
| Int (Floor) Division | `//` |
| Modulus | `%` |
%% Cell type:markdown id: tags:
Just like with booleans we can compare if two numbers are exactly equal
| Operator | Meaning |
| --- | --- |
| `==` | Exactly equal |
| `!=` | Not equal |
%% Cell type:markdown id: tags:
But we can also check whether one is larger or smaller than the other
| Operator | Meaning |
| --- | --- |
| `>` | Larger |
| `>=` | Larger or equal |
| `<` | Smaller |
| `<=` | Smaller or equal |
%% Cell type:markdown id: tags:
### Exercise: multiple of two numbers
Create a function that checks if a number is a multiple of two other numbers `a` and `b`
**Hint**: the **modulo operator (%)** returns the remainder of a division
| Input | Output |
| ----- | ------ |
| 4 % 3 | 1 |
| 5 % 3 | 2 |
| 6 % 3 | 0 |
%% Cell type:code id: tags:
``` python
# answer. Make sure your functions name is is_multiple_of_both
assert is_multiple_of_both(2, 2, 1)
assert is_multiple_of_both(6, 3, 2)
assert not is_multiple_of_both(7, 3, 2)
```
%% Cell type:markdown id: tags:
## Assignment
Before we continue exploring things to do with numbers, let's talk about **assignment statements**.
You can assign a value to a variable using the `name = value` syntax, for example
```python
x1 = 1
```
After you assigned a value to a variable, you can use the name to refer to the value
```python
print(x1) # This prints "1"
```
Note that the syntax requires your name to start with an character.
```python
1x = 1 # this doesn't work!
```
%% Cell type:markdown id: tags:
### Exercise: convert 24 hour clock to 12 hour clock face
Write a function that transform a `[0, 24)` hour input to the `[1, 12]` hour range of a clock face.
**Hint**: again use the modulo operator `%`
**Hint**: `0` and `12` are special cases - why?
%% Cell type:code id: tags:
``` python
# answer. Make sure your functions name is clock_face