diff --git a/book/Introduction_to_python_2.ipynb b/book/Introduction_to_python_2.ipynb index 6a02d10..38445bb 100644 --- a/book/Introduction_to_python_2.ipynb +++ b/book/Introduction_to_python_2.ipynb @@ -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", @@ -114,73 +114,126 @@ "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": [ @@ -188,41 +241,49 @@ ] }, { - "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)" ] }, { @@ -230,7 +291,7 @@ "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`." ] @@ -242,8 +303,6 @@ "metadata": {}, "outputs": [], "source": [ - "a = True\n", - "b = True\n", "a and b" ] }, @@ -254,9 +313,7 @@ "metadata": {}, "outputs": [], "source": [ - "a = True\n", - "b = False\n", - "a and b" + "a and c" ] }, { @@ -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`." ] }, { @@ -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", diff --git a/book/Introduction_to_python_3.ipynb b/book/Introduction_to_python_3.ipynb index a585a31..9ca9bda 100644 --- a/book/Introduction_to_python_3.ipynb +++ b/book/Introduction_to_python_3.ipynb @@ -6,7 +6,7 @@ "metadata": {}, "source": [ "---\n", - "title: \"Data types, if-statements and for-loops \"\n", + "title: \"Data types, lists and for-loops \"\n", "execute: \n", " enabled: true\n", " error: true\n", @@ -14,157 +14,6 @@ "---\n" ] }, - { - "cell_type": "markdown", - "id": "3e6d1053", - "metadata": {}, - "source": [ - "## The if-statement\n", - "\n", - "If statements can be used to perform tasks only when a certain condition is met." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a1aa1488", - "metadata": {}, - "outputs": [], - "source": [ - "num = 101\n", - "\n", - "if num > 100:\n", - " print('number is greater than 100')" - ] - }, - { - "cell_type": "markdown", - "id": "6ee18a17-f066-4c90-8fa2-45f1bcf92636", - "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 to what part of the code. This is mostly important when defining e.g. if-statements, for loops or functions. After the if condition, all lines with indentation are only performed when the if-condition is met." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "049e9f99-4352-4bb6-86e5-e5535974cf18", - "metadata": {}, - "outputs": [], - "source": [ - "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": "c68ae1f9-746e-4fd8-82c6-e3f11a195842", - "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": "6df68b58-a1eb-4077-8b21-e60c82d5581f", - "metadata": {}, - "outputs": [], - "source": [ - "num = 37\n", - "\n", - "if num > 100:\n", - " print('number is greater than 100')\n", - "else:\n", - " print('number is not greater than 100')\n", - "\n", - "print('done')" - ] - }, - { - "cell_type": "markdown", - "id": "68fde7fc-004d-4da6-ae47-ad8e49a9ca29", - "metadata": {}, - "source": [ - "An `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. \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": "9d290217", - "metadata": {}, - "outputs": [], - "source": [ - "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": "4d724d00", - "metadata": {}, - "source": [ - "Along with the `>` and `==` comparison operators that we have already used for comparing values in our logical expressions 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", - "We can combine logical statements using `and` and `or` in more complex conditions in if statements." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "c67b6d9c", - "metadata": {}, - "outputs": [], - "source": [ - "if (1 < 0) or (1 >= 0):\n", - " print('at least one the above logical statements is true')" - ] - }, - { - "cell_type": "markdown", - "id": "a84f2112", - "metadata": {}, - "source": [ - "While `and` is only true if both parts are true" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "660a283a", - "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": "fb4caf19", diff --git a/book/solutions/afternoon_exercises_solutions.ipynb b/book/solutions/afternoon_exercises_solutions.ipynb index 78a0033..17571cd 100644 --- a/book/solutions/afternoon_exercises_solutions.ipynb +++ b/book/solutions/afternoon_exercises_solutions.ipynb @@ -167,22 +167,10 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "53f7777a", "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'surveys_df' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "Input \u001b[0;32mIn [1]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m df \u001b[38;5;241m=\u001b[39m \u001b[43msurveys_df\u001b[49m[(surveys_df[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124msex\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m!=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mM\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;241m&\u001b[39m (surveys_df[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124msex\u001b[39m\u001b[38;5;124m'\u001b[39m] \u001b[38;5;241m!=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mF\u001b[39m\u001b[38;5;124m'\u001b[39m)]\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNumber of rows not female or male:\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28mlen\u001b[39m(df))\n", - "\u001b[0;31mNameError\u001b[0m: name 'surveys_df' is not defined" - ] - } - ], + "outputs": [], "source": [ "df = surveys_df[(surveys_df['sex'] != 'M') & (surveys_df['sex'] != 'F')]\n", "print(\"Number of rows not female or male:\", len(df))" diff --git a/book/solutions/morning_exercises_solutions.ipynb b/book/solutions/morning_exercises_solutions.ipynb index c6c4bb4..f8f78c1 100644 --- a/book/solutions/morning_exercises_solutions.ipynb +++ b/book/solutions/morning_exercises_solutions.ipynb @@ -122,8 +122,7 @@ "\n", "`5 == 5` answer: True, 5 is equal to 5 \n", "`not 3 > 2` answer: False, not inverts True to False \n", - "`True == 'True'` answer: False, The first True is of type `bool` and does not equal the second 'True' of type string) \n", - "`False < True` answer: True, The Boolean value `False` is also treated as 0, where `True` is 1" + "`True == 'True'` answer: False, The first True is of type `bool` and does not equal the second 'True' of type string" ] }, { @@ -135,8 +134,7 @@ "source": [ "print(5 == 5)\n", "print(not 3 > 2)\n", - "print(True == 'True')\n", - "print(False < True)" + "print(True == 'True')" ] }, { diff --git a/course_materials/morning_exercises.ipynb b/course_materials/morning_exercises.ipynb index 9009422..ad0d3f9 100644 --- a/course_materials/morning_exercises.ipynb +++ b/course_materials/morning_exercises.ipynb @@ -25,7 +25,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "5b2453e4-d68b-49ad-85b1-1d28e94e54a7", "metadata": {}, "outputs": [], @@ -113,7 +113,6 @@ "`5 == 5` answer: \n", "`not 3 > 2` answer: \n", "`True == 'True'` answer: \n", - "`False < True` answer: \n", "\n", "[Course book chapter 2 for reference](https://utrechtuniversity.github.io/workshop-introduction-to-python/Introduction_to_python_2.html)" ] @@ -127,8 +126,7 @@ "source": [ "print(5 == 5)\n", "print(not 3 > 2)\n", - "print(True == 'True')\n", - "print(False < True)" + "print(True == 'True')" ] }, { diff --git a/course_materials/preparation.ipynb b/course_materials/preparation.ipynb index c46dac8..21e862c 100644 --- a/course_materials/preparation.ipynb +++ b/course_materials/preparation.ipynb @@ -21,6 +21,7 @@ }, { "cell_type": "markdown", + "id": "8138be48", "metadata": {}, "source": [ "A Jupyter Notebook is:\n", @@ -37,6 +38,7 @@ { "cell_type": "code", "execution_count": null, + "id": "3cc9090f", "metadata": {}, "outputs": [], "source": [ @@ -45,6 +47,7 @@ }, { "cell_type": "markdown", + "id": "035612d2", "metadata": {}, "source": [ "Check that the output is printed below the cell!\n", @@ -78,6 +81,7 @@ }, { "cell_type": "markdown", + "id": "0ee8528d", "metadata": {}, "source": [ "Click this cell, and hit `B` on your keyboard to create a new cell below. You can also click the `+` sign at the top of this screen. You can ignore the new cell and continue reading..." @@ -85,6 +89,7 @@ }, { "cell_type": "markdown", + "id": "80fb2598", "metadata": {}, "source": [ "Code cells can contain all of your Python code. You can put many lines of code into one cell, or create many cells with only a few lines of code. You can define variables and functions in one cell and call them in another cell. However, you need to be aware that the order in which you run the cells matters. Try to run the cells below to understand what we mean:" @@ -93,6 +98,7 @@ { "cell_type": "code", "execution_count": null, + "id": "57cc8201", "metadata": {}, "outputs": [], "source": [ @@ -102,6 +108,7 @@ { "cell_type": "code", "execution_count": null, + "id": "64f35251", "metadata": {}, "outputs": [], "source": [ @@ -111,6 +118,7 @@ { "cell_type": "code", "execution_count": null, + "id": "ef88754a", "metadata": {}, "outputs": [], "source": [ @@ -119,6 +127,7 @@ }, { "cell_type": "markdown", + "id": "a026827a", "metadata": {}, "source": [ "A finished notebook should contain python code cells that can be run from the top to the bottom of the notebook. When your notebook becomes more complex, typically what happens is that because you have been running and changing and rerunning code cells while developing, the logic of your python code from top to bottom breaks. To prevent this (or at least get notified in an early stage) it is good practice to regularly restart the Python 'kernel' and run all the cells from top to bottom (or up to the selected cell). Click the Kernel tab at the top of this window and verify if you see these options, and click `Restart Kernel and Run up to Selected Cell`." @@ -126,6 +135,7 @@ }, { "cell_type": "markdown", + "id": "c769e5fb", "metadata": {}, "source": [ "Here we end the brief introduction to Jupyter notebooks. If you are interested, check out the documentation at the [jupyter website](https://docs.jupyter.org/en/latest/)"