Skip to content

Commit

Permalink
move if-statements to chapter 2 (#108)
Browse files Browse the repository at this point in the history
* move if-statements to chapter 2

* Update book/Introduction_to_python_2.ipynb

Co-authored-by: chStaiger <[email protected]>

* Update book/Introduction_to_python_2.ipynb

Co-authored-by: chStaiger <[email protected]>

* Update book/Introduction_to_python_2.ipynb

Co-authored-by: chStaiger <[email protected]>

* Update book/Introduction_to_python_2.ipynb

Co-authored-by: chStaiger <[email protected]>

---------

Co-authored-by: chStaiger <[email protected]>
  • Loading branch information
jelletreep and chStaiger committed Feb 21, 2024
1 parent 0917eee commit 6d8f7e2
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 229 deletions.
188 changes: 133 additions & 55 deletions book/Introduction_to_python_2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"metadata": {},
"source": [
"---\n",
"title: \"Operators and built-in functions\"\n",
"title: \"Operators, built-in functions, if-statements\"\n",
"execute: \n",
" enabled: true\n",
" error: true\n",
Expand Down Expand Up @@ -114,123 +114,184 @@
"tags": []
},
"source": [
"## Boolean values, Logical expressions and operators\n",
"## If-statements and comparisons\n",
"\n",
"In programming you often need to know if something is `True` or `False`. `True` and `False` are called Boolean values and have their own data type (`bool` so they are not of type `str`!!). `True` and `False` are the only two Boolean values. "
"In programming it is possible to perform certain tasks only when a certain condition is met: Assume we have a table where each row is a person and the table contains a column `age`. We want to create a new variable `age_group` which can have the values `child` and `adult` based on the value in the column age. We can use an `if`-statement to decide whether a person is younger than 18 and asign the right value to `age_group`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "878b1de4",
"id": "8bfd4af2",
"metadata": {},
"outputs": [],
"source": [
"a = True\n",
"a"
"age = 17\n",
"\n",
"if age < 18:\n",
" print('this person is a child')\n"
]
},
{
"cell_type": "markdown",
"id": "b489a257",
"metadata": {},
"source": [
"As you can see, the line `print(...)` starts with 4 spaces indentation.\n",
"In Python indentation is very important. Python uses indentation to determine which lines of code belong together. Lines with the same identation are called a code block. We use code blocks to define what should be done in an if-statement, for-loop or in a functions. E.g. after the if condition, all lines with indentation are only performed when the if-condition is met."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d6143053",
"id": "ea2cb2ed",
"metadata": {},
"outputs": [],
"source": [
"b = False\n",
"b"
"num = 99\n",
"if num > 100:\n",
" print('This line is only executed when num > 100')\n",
" print('This line is only executed when num > 100')\n",
" \n",
" print('This line is only executed when num > 100')\n",
" \n",
"print('This line is always executed')"
]
},
{
"cell_type": "markdown",
"id": "d94ac770",
"metadata": {},
"source": [
"It is also possible to specify a task that is performed when the condition is not met using `else` (note the use of indentation):"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "18d579dc-1275-4b9f-8579-459aa9c194c4",
"id": "fe4cf9a4",
"metadata": {},
"outputs": [],
"source": [
"type(a)"
"age = 17\n",
"\n",
"if age < 18:\n",
" print('this person is a child')\n",
"else:\n",
" print('this person is an adult')\n",
"\n",
"print('done')"
]
},
{
"cell_type": "markdown",
"id": "8d76802a",
"id": "33d428ab",
"metadata": {},
"source": [
"Comparison operators (e.g. `>`, `<`, `==`) are used in an expression to compare two values. The result of this expression is either `True` or `False`. Why this is useful we will show later (see if-statements)."
"With an `if .. else` statement we can define one condition. If we need to check more conditions the `if ... else` statement can be extended with (one or more) `elif` to specify more tasks that need to be performed on other conditions. These extended `if ... else` statements always start with `if` followed by (one or more) `elif`. When an `else` statement is included it is always the last statement, it is the default which will be eceuted when all previous comparisons failed. \n",
"\n",
"**Order matters**:\n",
"The statements (or conditions) are checked in order from top to bottom and only the task belonging to the first condition that is met is being performed. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "57accc50-7e82-4541-ae47-02a198fa0606",
"id": "29c03372",
"metadata": {},
"outputs": [],
"source": [
"3 > 4"
"num = -3\n",
"\n",
"if num > 0:\n",
" print(num, 'is positive')\n",
"elif num == 0:\n",
" print(num, 'is zero')\n",
"else:\n",
" print(num, 'is negative')"
]
},
{
"cell_type": "markdown",
"id": "79b0322a-47bc-46f4-bddf-d1c4c517414a",
"id": "e3ffe576",
"metadata": {},
"source": [
"`3 > 4` is an example of a 'logical expression' (also known as condition), where `>` is the comparison operator. "
"If statements often use comparisons to check if a certain condition is met. Comparisons are done with comparison operators such as `>`, `==`.\n",
"\n",
"Along with the `>` and `==` comparison operators that we have already used for comparing values above, there are a few more options to know about:\n",
"\n",
"- \\>: greater than\n",
"- \\<: less than\n",
"- ==: equal to\n",
"- !=: does not equal\n",
"- \\>=: greater than or equal to\n",
"- \\<=: less than or equal to\n",
"\n",
"Let's now play around with comparisons to see how they work in more detail."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9ee5e542-0b9f-489f-b542-9edc3cdf289e",
"id": "57accc50-7e82-4541-ae47-02a198fa0606",
"metadata": {},
"outputs": [],
"source": [
"4 > 3"
]
},
{
"cell_type": "markdown",
"id": "7cc35555-b3ef-4c55-b8a9-033cef9939e3",
"cell_type": "code",
"execution_count": null,
"id": "9a2bd3af",
"metadata": {},
"outputs": [],
"source": [
"`==` is another comparison operator to check if two values or variables are the same. If this is the case it will return `True`"
"4 < 3"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2f36bacd-a602-49d6-9546-5b06d4b5629d",
"id": "1f8f7d8d",
"metadata": {},
"outputs": [],
"source": [
"four = 4 # first we assign the integer 4 to a variable\n",
"four == 4 # then we check if it is equal to 4"
"a = 4 > 3\n",
"type(a)"
]
},
{
"cell_type": "markdown",
"id": "27b5b291-f5f9-4bd5-8cda-da29f940ee3f",
"id": "dc8a339d",
"metadata": {},
"source": [
"`!=` is used to check if two values or variable are **not** the same. If this is the case it will return `True`"
"As you can see, comparisons return `True` or `False`. The data type of these values are `bool` which is short for boolean values.\n",
"\n",
"If-statements work with boolean values. If the value is `True`, the task is performed, if the value is `False`, the task is not performed.\n",
"\n",
"Boolean values can also be assigned to variables:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "84f8e1a5-f96d-450d-84e7-63aedf217400",
"id": "96d2c495",
"metadata": {},
"outputs": [],
"source": [
"print(\"Four is not equal to 5: \", four != 5)\n",
"print(\"Four is not equal to 4: \", four != 4)"
"a = True\n",
"b = True\n",
"c = False\n",
"type(a)"
]
},
{
"cell_type": "markdown",
"id": "1f0d3b4b-140c-4ad1-b264-d3af4e1abe2f",
"metadata": {},
"source": [
"`and`, `or` and `not` are 'logical operators', and are used to join two logical expressions (or revert a logical expression in the case of `not`) to create more complex conditions. \n",
"`and`, `or` and `not` are 'logical operators', and are used to join two comparisons (or revert a logical expression in the case of `not`) to create more complex conditions. \n",
"\n",
"`and` will return `True` if both expression on either side are `True`."
]
Expand All @@ -242,8 +303,6 @@
"metadata": {},
"outputs": [],
"source": [
"a = True\n",
"b = True\n",
"a and b"
]
},
Expand All @@ -254,9 +313,7 @@
"metadata": {},
"outputs": [],
"source": [
"a = True\n",
"b = False\n",
"a and b"
"a and c"
]
},
{
Expand Down Expand Up @@ -302,26 +359,7 @@
"id": "91047596-be5a-4f50-a92b-b0a32425c6fc",
"metadata": {},
"source": [
"In the last three examples you can see that multiple expressions can be combined in a single line of Python code. Python evaluates the expressions one by one. `4 > 3` would return `True`, `9 > 3` would return `True`, so `4 > 3 or 9 > 3` would translate to `True or True`."
]
},
{
"cell_type": "markdown",
"id": "2d5e3517-667c-4827-affd-c288ebc462b6",
"metadata": {},
"source": [
"It is also possible to assign the output of an expression to a variable:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "71f75fc1",
"metadata": {},
"outputs": [],
"source": [
"greater = 3 > 4\n",
"print(\"3 > 4 : \", greater)"
"In the last three examples you can see that multiple expressions can be combined in a single line of Python code. Python evaluates the expressions one by one. `4 > 3` would return `True`, `9 > 3` would return `True`, so `4 > 3 or 9 > 3` would translate to `True or True`. Because at least one of the expressions on either side of the `or` operator is `True`, the whole expression results to `True`."
]
},
{
Expand Down Expand Up @@ -366,6 +404,46 @@
"print(\"But this expression evaluates '(a or b) and c' first the 'or' and generates:\", (a or b) and c)"
]
},
{
"cell_type": "markdown",
"id": "fb75275a",
"metadata": {},
"source": [
"Now we know how to use comparisons and logical operators to create complex conditions. These complex conditions can be used in if-statements to perform tasks based on these conditions."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ddd44a2d",
"metadata": {},
"outputs": [],
"source": [
"if (1 < 0) or (1 >= 0):\n",
" print('at least one the above logical statements is true')"
]
},
{
"cell_type": "markdown",
"id": "5dd0f17e",
"metadata": {},
"source": [
"While `and` is only true if both parts are true"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "45666f42",
"metadata": {},
"outputs": [],
"source": [
"if (1 < 0) and (1 >= 0):\n",
" print('both tests are true')\n",
"else:\n",
" print('at least one of the tests is not true')"
]
},
{
"cell_type": "markdown",
"id": "a7e9935c",
Expand Down
Loading

0 comments on commit 6d8f7e2

Please sign in to comment.