diff --git a/lessons/02 values and expressions.ipynb b/lessons/02 values and expressions.ipynb
index 33b1963..fb82463 100644
--- a/lessons/02 values and expressions.ipynb
+++ b/lessons/02 values and expressions.ipynb
@@ -1 +1 @@
-{"cells":[{"cell_type":"markdown","metadata":{"id":"fqMJHzNk5yXQ"},"source":["# Module 2: Values and Expressions\n","\n","### CDH course \"Programming in Python\"\n","\n","[index](https://colab.research.google.com/drive/1s05aR4wn2dU1C3se1oXfqKz2EY5ilrno)\n","\n","Previous module: [1. Introduction](https://colab.research.google.com/drive/1i4wJpUIr77Fh1renWNjt00Bd51NbC1nB)\n","\n","### This module\n","\n","- The basic types of values that exist in Python.\n","- How to store values for later use.\n","- How to create values from other values."]},{"cell_type":"markdown","metadata":{"id":"AqGUR1POYVNL"},"source":["## Primitive types and values\n","\n","> In computer science, primitive data types are a set of basic data types from which all other data types are constructed. [wikipedia](https://en.wikipedia.org/wiki/Primitive_data_type)\n","\n","In Python:\n","\n","- integer - `int`\n","- floating point - `float`\n","- string - `str`\n","- boolean - `bool`\n","- none - `None`\n"]},{"cell_type":"markdown","metadata":{"id":"8sSOrG7FdMT5"},"source":["### integer\n","'whole number'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"wTlfkN-Sa_MG"},"outputs":[],"source":["1\n","2\n","-12\n","289883891009329819081202\n","0"]},{"cell_type":"markdown","metadata":{"id":"iBCZZWfDdS7o"},"source":["### floating point\n","'decimal number'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"2JbaI2-pbTF5"},"outputs":[],"source":["2.1\n","-3.2\n","12.8\n","0.0\n",".8"]},{"cell_type":"markdown","metadata":{"id":"om0zZYCBdd8F"},"source":["### string\n","'text'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"DB6KZC5Sblwy"},"outputs":[],"source":["'hello'\n","'we can choose between single'\n","\"or double quotes!\"\n","\n","\"I don't want to do this\"\n","\n","# escaping difficult characters with \\ \n","\"I won't say \\\"banana\\\"\\\\\"\n","\n","# line breaks are preserved\n","'''a long string\n","\n","that can go over multiple lines'''\n","\n","''\n","\"\""]},{"cell_type":"markdown","metadata":{"id":"dQ6u3Syk4fS4"},"source":["*escape characters* only show when printing"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"7QCMtj3S4d6E"},"outputs":[],"source":["# escape character: \\n (newline)\n","print('hello \\n world')\n","'hello \\n world'"]},{"cell_type":"markdown","metadata":{"id":"ouf6r2zmdjY9"},"source":["### boolean\n","true or false"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"8n81rHXFb9cl"},"outputs":[],"source":["True\n","False\n","false\n","true\n","\"True\""]},{"cell_type":"markdown","metadata":{"id":"xS0efw6fdoW9"},"source":["### None\n","'nothing here'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"fOZdR0YUcFRb"},"outputs":[],"source":["None\n","none\n","\"none\""]},{"cell_type":"markdown","metadata":{"id":"jockLUXXd2Ad"},"source":["## Difference between types\n","\n","Use `type(value)` to find the type of a value."]},{"cell_type":"code","execution_count":1,"metadata":{"id":"lHtfczHxd89N"},"outputs":[],"source":["type(10)\n","type(3.14159)\n","type('hello')\n","type(True)\n","type('True')\n","type(None)"]},{"cell_type":"markdown","metadata":{"id":"kZffa20rXLOQ"},"source":["Careful! A number in quotes is not actually a number, but a string. A string that looks like a number will **NOT** behave like a number."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"-Zv10HJFXUW8"},"outputs":[],"source":["type('10')\n","type('3.14159')"]},{"cell_type":"markdown","metadata":{"id":"rJ_mNXXAYIpy"},"source":["Likewise, a string that looks like a Boolean will **NOT** behave like a boolean."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"PN0FTHz2YRPM"},"outputs":[],"source":["type('False')"]},{"cell_type":"markdown","metadata":{"id":"-0p3SmmgWdif"},"source":["`int`, `str` etcetera can be used to convert values between different types. Careful though, not all conversions you can think of will do what you expect!"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"FPeklTLXWuXl"},"outputs":[],"source":["int('10')\n","float('10')\n","str(10)\n","str(False)\n","bool('False') # !!!\n","None # no conversion exists"]},{"cell_type":"markdown","metadata":{"id":"YGU7CclEYz2y"},"source":["It is useful to know that each character in a string is stored as an integer. `ord(character)` lets you retrieve that integer."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"U_63E6jfab6N"},"outputs":[],"source":["ord('2')\n","ord('a')\n","ord('A')\n","ord('\\n')\n","ord('\\t')"]},{"cell_type":"markdown","metadata":{"id":"0dh3t4uZa9AT"},"source":["`chr(integer)` does the opposite: it lets you retrieve the character corresponding to a given integer."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ffIkwHahcGv9"},"outputs":[],"source":["chr(49)\n","chr(98)\n","chr(946)\n","chr(22823)\n","chr(129327)"]},{"cell_type":"markdown","metadata":{"id":"InNZNIOpezlG"},"source":["## Variables\n","\n","- container holding a value\n","- assigning\n"," - `variable_name = value`\n","- reassigning\n"," - `name = 'sheean'`\n"," - `name = 'julian'`\n","- in Python, no strict type\n"," - `a = 1`\n"," - `a = 'hello'`"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"wgaSYh4yfGCx"},"outputs":[],"source":["a = 1\n","b = \"hello\"\n","c = True\n","d = None\n","\n","print(a)"]},{"cell_type":"markdown","metadata":{"id":"BD2s2EqKfQtx"},"source":["Tip: the notebook remembers variables from previous cells, but only if you executed them."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"pJ-N_rc8fZia"},"outputs":[],"source":["print(a)\n","print(b)\n","print(c)"]},{"cell_type":"markdown","metadata":{"id":"uADEckPZgGt_"},"source":["Anything can go in a variable, not just single values"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ehEig-sCgL48"},"outputs":[],"source":["d = a\n","print(d)\n","\n","e = print\n","e(b)"]},{"cell_type":"markdown","metadata":{"id":"TaiYZ3R6ghq0"},"source":["Beware! When we assign a value, the variable stores it at that exact moment. \n","Changing one variable does not change another.\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"pIIq1la9g1Qr"},"outputs":[],"source":["number = 0\n","\n","container = number\n","\n","print(container)\n","\n","number = 1\n","\n","print(container)\n","print(number)\n"]},{"cell_type":"markdown","metadata":{"id":"nMn_xu6Ih3hJ"},"source":["### Variable names\n","\n","#### Rules\n","\n","- must start with a letter or underscore (`_`)\n","- can only contain letters (`a-z`, `A-Z`), numbers (`0-9`), and underscore (`_`)\n","- variable names are case sensitive. `name` is a different variable from `Name` \n","\n","#### Conventions\n","\n","Next to the rules, there are some conventions. \n","Please stick to them, so others will have no trouble reading and understanding your code.\n","- lowercase\n"," - DO: `name`\n"," - DON'T: `Name`\n"," - DON'T: `NAME`1\n","- readable\n"," - DO: `name`\n"," - DON'T: `n_a_m_e`\n"," - DON'T: `x098277`\n","- multiple words2\n"," - DO: `first_name` (*snake_case*)\n"," - DON'T: `FirstName` (*PascalCase*)\n"," - DON'T `firstName` (*camelCase*)\n","- comprehensive (but short)\n"," - DO: `name`\n"," - DON'T: `n`\n"," - DON'T: `the_name_of_the_person_i_want_to_print` \n","\n","1 *Fully uppercased variable names actually indicate constants, and adhere to Python conventions. We will explain this later.* \n","2 *This is purely a cultural thing. Other languages (and even other concepts within Python) use different casing*"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"m4pTI2BEn-Z-"},"outputs":[],"source":[]},{"cell_type":"markdown","metadata":{"id":"pDU1uK2Igmki"},"source":["## Exercise 2.1: Variables and state\n","\n","In each of the code blocks below, try to predict what will be printed, then run the code. If your guess was incorrect, try to figure out why the result is different. If your guess was correct, celebrate!"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"FydkKnx_hUPq"},"outputs":[],"source":["flavor = 'vanilla'\n","print(flavor)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"LTWods50h-Ij"},"outputs":[],"source":["temperature = 70\n","print(flavor)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"l2vT4L7piGRf"},"outputs":[],"source":["print(temperature)\n","temperature = 35\n","print(temperature)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"k1Z_yWLXiWy7"},"outputs":[],"source":["dog_name = 'Bobby'\n","cat_name = 'Garfield'\n","dog_name = cat_name\n","cat_name = dog_name\n","print(dog_name)\n","print(cat_name)"]},{"cell_type":"markdown","metadata":{"id":"BZ50KykuAYPs"},"source":["Before running the following code, try to explain why it does *not* output `chocolate`."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"X3xHg6K4Aicn"},"outputs":[],"source":["sweet = 'chocolate'\n","savory = 'cheese'\n","dessert = 'sweet'\n","print(dessert)"]},{"cell_type":"markdown","metadata":{"id":"0az3tNK7WD3G"},"source":["In each of the following lines of code, replace `None` by a value of your choice. Make it such that `my_int` is an integer, `my_float` is a float, `my_bool` is a boolean and `my_string` is a string."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"lY-M8mfSXDfG"},"outputs":[],"source":["my_int = None\n","my_float = None\n","my_bool = None\n","my_string = None"]},{"cell_type":"markdown","metadata":{"id":"dvNIQh7KYuJb"},"source":["## Exercise 2.2: Bonus"]},{"cell_type":"markdown","metadata":{"id":"GI9fyUO8XOcp"},"source":["How could you verify in code whether the variables you wrote above have the correct type? Write this code below.\n","\n","Hint: scroll back to the section [Difference between types](#scrollTo=Difference_between_types)."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Om6z53RXYBoS"},"outputs":[],"source":[]},{"cell_type":"markdown","metadata":{"id":"h6UKIMXCj3w9"},"source":["In the code block below, replace the question mark `?` by a variable name. Make it such that the final output is `'Hooray!'`."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"3MI6_DY7ku30"},"outputs":[],"source":["cheer = 'Hooray!'\n","alarm = 'Oh no!'\n","anthem = 'Wilhelmus van Nassauwe'\n","alarm = anthem\n","? = cheer\n","cheer = anthem\n","anthem = alarm\n","print(anthem)"]},{"cell_type":"markdown","metadata":{"id":"ZXd6jCn90CA_"},"source":["## Expressions\n","\n","- An expression is a combination between *operands* and *operators*. \n","- Think of arithmetic: `1 + 2`\n","\n"]},{"cell_type":"markdown","metadata":{"id":"s8uJy_kI9C8S"},"source":["### Arithmetic"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"f9ACtc0e13i-"},"outputs":[],"source":["1 + 6\n","5 - 2\n","10 / 3\n","5 * 5\n","\n","a = 8\n","b = 10\n","\n","c = b - a\n","print(c)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"w9cfOflP8DEJ"},"outputs":[],"source":["# multiple operators\n","4 + (3 * 5)\n","(4 + 3) * 5"]},{"cell_type":"markdown","metadata":{"id":"zkt9aNKm28Lc"},"source":["### String expressions"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"6os4-rt43ThF"},"outputs":[],"source":["\"hello\" + \" world!\"\n","a = \"my age is: \"\n","b = 33\n","\n","a + b"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"SyrelaMu42UZ"},"outputs":[],"source":["a = 'hello'\n","a * 5"]},{"cell_type":"markdown","metadata":{"id":"DPbfP65xXlfb"},"source":["## Reassigning variables\n","\n","- You can use a variable itself when reassigning. This is useful when trying to expand an existing variable."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ogL2Iw3-Xs15"},"outputs":[],"source":["a = \"hello\"\n","a = a + \", world!\"\n","print(a)\n","\n","b = 'bye'\n","b = b + b\n","print(b)\n","\n","b = b + b\n","print(b)"]},{"cell_type":"markdown","metadata":{"id":"z_bXvnya5J2_"},"source":["## Exercise 2.3: Expressions\n","\n"]},{"cell_type":"markdown","metadata":{"id":"j9xhznQlbCBf"},"source":["1. Try to predict the value of each of the following code blocks. Can you explain any surprises?"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"0QcMB4xwbSfL"},"outputs":[],"source":["1 + 1"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"eHKN9kP9bWkm"},"outputs":[],"source":["1 * 1"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"uINoRNNbbXwJ"},"outputs":[],"source":["a = 1\n","b = 2\n","a + b"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"4xyWkYlnbc_8"},"outputs":[],"source":["c = b\n","a * b * c"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"7tW2T4mebljv"},"outputs":[],"source":["'hakuna' + 'matata'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"uEdPHIhBb2Mw"},"outputs":[],"source":["liquid = 'water~'\n","liquid * 3"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"-D7BB50Qceo2"},"outputs":[],"source":["5 - 2 - 1"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"OQUGr5rGck3m"},"outputs":[],"source":["5 - (2 - 1)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Jc8fawaIdCD5"},"outputs":[],"source":["income = 100\n","tax = 20\n","net_income = income - tax\n","tax = 15\n","net_income"]},{"cell_type":"markdown","metadata":{"id":"TlLkcRv-droI"},"source":["2. In the code block below, change the values of `character` and `multiplier` so that the output becomes `'Goooood!'`."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"pYZUkeDJenob"},"outputs":[],"source":["character = 'o'\n","multiplier = 5\n","\n","begin = 'G'\n","middle = character * multiplier\n","end = 'd!'\n","\n","begin + middle + end"]},{"cell_type":"markdown","metadata":{"id":"G8X4n_a8a5tl"},"source":["3. Rewrite your Hello world-program:\n"," - Build the \"hello, world!\" string using multiple variables.\n"," - Make the program say \"hello, world!\" 3 times, each time on a new line"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"nDUVvhDEfIVI"},"outputs":[],"source":[]},{"cell_type":"markdown","metadata":{"id":"3HCqdTj2fVPK"},"source":["## Exercise 2.4: Bonus"]},{"cell_type":"markdown","metadata":{"id":"EKFdkLkWa8EY"},"source":["1. Find out and describe what the following operators do:\n"," - `%` (e.g. `10 % 2`)\n"," - `//` (e.g. `10 // 2`)\n"," - `**` (e.g. `10 ** 2`)\n"," - Tip: write the expressions using variables. Change the variables and see how it affects the outcome."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"V59a7vpDfzO2"},"outputs":[],"source":[]},{"cell_type":"markdown","metadata":{"id":"o0WYIAUla-AX"},"source":["2. Predict what will be printed on each line in the following code block, then run the code to check your answer. If you made any mistakes, try to figure out what is causing the difference."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Z9Vd9pdIUzT5"},"outputs":[],"source":["word = 'stylometry'\n","repeats = 3\n","extra = 2\n","\n","print((word * repeats) + str(extra))\n","print(word * (repeats + extra))\n","print(word * repeats + str(extra))\n","print((word + str(extra)) * repeats)\n","print(word + str(extra * repeats))\n","print(word + str(extra) * repeats)"]},{"cell_type":"markdown","metadata":{"id":"DRObBQZHgsIG"},"source":["3. Using just the variables `word`, `repeats` and `extra` from the previous code block, write a single big expression that evaluates to `'stylometry555stylometry'`."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"uLuiUk10gqwM"},"outputs":[],"source":[]},{"cell_type":"markdown","metadata":{"id":"QaamMzJY6ISR"},"source":["## Boolean expressions\n","Expressions that result in `True` or `False`\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"kLjya4Au6Ucu"},"outputs":[],"source":["# equals\n","1 == 1\n","\"hello\" == 'hello'\n","'2' == 2"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"tYNp6nUp7ixW"},"outputs":[],"source":["# does not equal\n","1 != 1\n","\"hello\" != 'hello'\n","'2' != 2"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"oixxKDHV7s5J"},"outputs":[],"source":["# greater than\n","2 > 1\n","2 > 5\n","'b' > 'a'\n","\n","2 > 2\n","\n","# greater than (or equal)\n","2 >= 2"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Z198kfCf75sU"},"outputs":[],"source":["# less than\n","2 < 1\n","2 < 5\n","'b' < 'a'\n","2 < 2\n","\n","# less than (or equal)\n","2 <= 2"]},{"cell_type":"markdown","metadata":{"id":"pvrcgKU18OrJ"},"source":["## Exercise 2.5: boolean expressions"]},{"cell_type":"markdown","metadata":{"id":"sWdnjezoil9j"},"source":["1. Predict for each boolean expression below: `True` or `False`? If any of your predictions is incorrect, try to find the reason."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"RpSvrom3Z8fQ"},"outputs":[],"source":["name = 'Sheean'\n","height = 2\n","\n","name * height == 'Sheean Sheean'\n","height < 2\n","2 <= height\n","1 < height <= 2\n","2 <= height < 1\n","name <= 'Julian'\n","height * 3 + 1 >= height"]},{"cell_type":"markdown","metadata":{"id":"zJiaGIlZBN_P"},"source":["2. Run the code block below. Did you expect this result? Can you explain it?"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"_PrnFf2lioMB"},"outputs":[],"source":["1 == True"]},{"cell_type":"markdown","metadata":{"id":"q8QviA70kdQE"},"source":["3. Replace one value in each of the following expressions so the expression becomes `True`."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"qDQ9Ob5Zkrqm"},"outputs":[],"source":["1 > height\n","height == 0\n","'Julian' < name * 2\n","name < 1 * name"]},{"cell_type":"markdown","metadata":{"id":"YbdV7SQVmDVV"},"source":["4. Replace one operator in each of the following expressions so the expression becomes `True`."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"hzjXIwkAmChv"},"outputs":[],"source":["5 < 4\n","2 + 1 == 1\n","3 + 3 == 3 + 2"]},{"cell_type":"markdown","metadata":{"id":"SwyAMDzDn2_X"},"source":["5. The *Groot woordenboek van de Nederlandse taal* by Van Dale publishers, colloquially known as \"De dikke Van Dale\", is a three-tome Dutch dictionary. The first tome contains words starting a–i, the second j–q and the third r–z. In the code block below, write an expression that will tell you whether `word` should be in the second tome. Try different values of `word` to verify that your solution is correct. You do not need to check whether the word is actually Dutch!"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"lmCGfx_DqMWe"},"outputs":[],"source":["word = 'archaïsch'\n","\n","# your expression here"]},{"cell_type":"markdown","metadata":{"id":"jXSxbjf4q6q5"},"source":["## Next module\n","\n","[3. Conditionals](https://colab.research.google.com/drive/1KkZ2fS75o7giccQJakRhyvYBV7JdFnVw)"]}],"metadata":{"colab":{"provenance":[]},"kernelspec":{"display_name":"Python 3","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.10.12"}},"nbformat":4,"nbformat_minor":0}
+{"cells":[{"cell_type":"markdown","metadata":{"id":"fqMJHzNk5yXQ"},"source":["# Module 2: Values and Expressions\n","\n","### CDH course \"Programming in Python\"\n","\n","[index](https://colab.research.google.com/drive/1s05aR4wn2dU1C3se1oXfqKz2EY5ilrno)\n","\n","Previous module: [1. Introduction](https://colab.research.google.com/drive/1i4wJpUIr77Fh1renWNjt00Bd51NbC1nB)\n","\n","### This module\n","\n","- The basic types of values that exist in Python.\n","- How to store values for later use.\n","- How to create values from other values."]},{"cell_type":"markdown","metadata":{"id":"AqGUR1POYVNL"},"source":["## Primitive types and values\n","\n","> In computer science, primitive data types are a set of basic data types from which all other data types are constructed. [wikipedia](https://en.wikipedia.org/wiki/Primitive_data_type)\n","\n","In Python:\n","\n","- integer - `int`\n","- floating point - `float`\n","- string - `str`\n","- boolean - `bool`\n","- none - `None`\n"]},{"cell_type":"markdown","metadata":{"id":"8sSOrG7FdMT5"},"source":["### integer\n","'whole number'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"wTlfkN-Sa_MG"},"outputs":[],"source":["1\n","2\n","-12\n","289883891009329819081202\n","0"]},{"cell_type":"markdown","metadata":{"id":"iBCZZWfDdS7o"},"source":["### floating point\n","'decimal number'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"2JbaI2-pbTF5"},"outputs":[],"source":["2.1\n","-3.2\n","12.8\n","0.0\n",".8"]},{"cell_type":"markdown","metadata":{"id":"om0zZYCBdd8F"},"source":["### string\n","'text'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"DB6KZC5Sblwy"},"outputs":[],"source":["'hello'\n","'we can choose between single'\n","\"or double quotes!\"\n","\n","\"I don't want to do this\"\n","\n","# escaping difficult characters with \\ \n","\"I won't say \\\"banana\\\"\\\\\"\n","\n","# line breaks are preserved\n","'''a long string\n","\n","that can go over multiple lines'''\n","\n","''\n","\"\""]},{"cell_type":"markdown","metadata":{"id":"dQ6u3Syk4fS4"},"source":["*escape characters* only show when printing"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"7QCMtj3S4d6E"},"outputs":[],"source":["# escape character: \\n (newline)\n","print('hello \\n world')\n","'hello \\n world'"]},{"cell_type":"markdown","metadata":{"id":"ouf6r2zmdjY9"},"source":["### boolean\n","true or false"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"8n81rHXFb9cl"},"outputs":[],"source":["True\n","False\n","false\n","true\n","\"True\""]},{"cell_type":"markdown","metadata":{"id":"xS0efw6fdoW9"},"source":["### None\n","'nothing here'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"fOZdR0YUcFRb"},"outputs":[],"source":["None\n","none\n","\"none\""]},{"cell_type":"markdown","metadata":{"id":"jockLUXXd2Ad"},"source":["## Difference between types\n","\n","Use `type(value)` to find the type of a value."]},{"cell_type":"code","execution_count":1,"metadata":{"id":"lHtfczHxd89N"},"outputs":[],"source":["type(10)\n","type(3.14159)\n","type('hello')\n","type(True)\n","type('True')\n","type(None)"]},{"cell_type":"markdown","metadata":{"id":"kZffa20rXLOQ"},"source":["Careful! A number in quotes is not actually a number, but a string. A string that looks like a number will **NOT** behave like a number."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"-Zv10HJFXUW8"},"outputs":[],"source":["type('10')\n","type('3.14159')"]},{"cell_type":"markdown","metadata":{"id":"rJ_mNXXAYIpy"},"source":["Likewise, a string that looks like a Boolean will **NOT** behave like a boolean."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"PN0FTHz2YRPM"},"outputs":[],"source":["type('False')"]},{"cell_type":"markdown","metadata":{"id":"-0p3SmmgWdif"},"source":["`int`, `str` etcetera can be used to convert values between different types. Careful though, not all conversions you can think of will do what you expect!"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"FPeklTLXWuXl"},"outputs":[],"source":["int('10')\n","float('10')\n","str(10)\n","str(False)\n","bool('False') # !!!\n","None # no conversion exists"]},{"cell_type":"markdown","metadata":{"id":"YGU7CclEYz2y"},"source":["It is useful to know that each character in a string is stored as an integer. `ord(character)` lets you retrieve that integer."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"U_63E6jfab6N"},"outputs":[],"source":["ord('2')\n","ord('a')\n","ord('A')\n","ord('\\n')\n","ord('\\t')"]},{"cell_type":"markdown","metadata":{"id":"0dh3t4uZa9AT"},"source":["`chr(integer)` does the opposite: it lets you retrieve the character corresponding to a given integer."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ffIkwHahcGv9"},"outputs":[],"source":["chr(49)\n","chr(98)\n","chr(946)\n","chr(22823)\n","chr(129327)"]},{"cell_type":"markdown","metadata":{"id":"InNZNIOpezlG"},"source":["## Variables\n","\n","- container holding a value\n","- assigning\n"," - `variable_name = value`\n","- reassigning\n"," - `name = 'sheean'`\n"," - `name = 'julian'`\n","- in Python, no strict type\n"," - `a = 1`\n"," - `a = 'hello'`"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"wgaSYh4yfGCx"},"outputs":[],"source":["a = 1\n","b = \"hello\"\n","c = True\n","d = None\n","\n","print(a)"]},{"cell_type":"markdown","metadata":{"id":"BD2s2EqKfQtx"},"source":["Tip: the notebook remembers variables from previous cells, but only if you executed them."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"pJ-N_rc8fZia"},"outputs":[],"source":["print(a)\n","print(b)\n","print(c)"]},{"cell_type":"markdown","metadata":{"id":"uADEckPZgGt_"},"source":["Anything can go in a variable, not just single values"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ehEig-sCgL48"},"outputs":[],"source":["d = a\n","print(d)\n","\n","e = print\n","e(b)"]},{"cell_type":"markdown","metadata":{"id":"TaiYZ3R6ghq0"},"source":["Beware! When we assign a value, the variable stores it at that exact moment. \n","Changing one variable does not change another.\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"pIIq1la9g1Qr"},"outputs":[],"source":["number = 0\n","\n","container = number\n","\n","print(container)\n","\n","number = 1\n","\n","print(container)\n","print(number)\n"]},{"cell_type":"markdown","metadata":{"id":"nMn_xu6Ih3hJ"},"source":["### Variable names\n","\n","#### Rules\n","\n","- must start with a letter or underscore (`_`)\n","- can only contain letters (`a-z`, `A-Z`), numbers (`0-9`), and underscore (`_`)\n","- variable names are case sensitive. `name` is a different variable from `Name` \n","\n","#### Conventions\n","\n","Next to the rules, there are some conventions. \n","Please stick to them, so others will have no trouble reading and understanding your code.\n","- lowercase\n"," - DO: `name`\n"," - DON'T: `Name`\n"," - DON'T: `NAME`1\n","- readable\n"," - DO: `name`\n"," - DON'T: `n_a_m_e`\n"," - DON'T: `x098277`\n","- multiple words2\n"," - DO: `first_name` (*snake_case*)\n"," - DON'T: `FirstName` (*PascalCase*)\n"," - DON'T `firstName` (*camelCase*)\n","- comprehensive (but short)\n"," - DO: `name`\n"," - DON'T: `n`\n"," - DON'T: `the_name_of_the_person_i_want_to_print` \n","\n","1 *Fully uppercased variable names actually indicate constants, and adhere to Python conventions. We will explain this later.* \n","2 *This is purely a cultural thing. Other languages (and even other concepts within Python) use different casing*"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"m4pTI2BEn-Z-"},"outputs":[],"source":[]},{"cell_type":"markdown","metadata":{"id":"pDU1uK2Igmki"},"source":["## Exercise 2.1: Variables and state\n","\n","In each of the code blocks below, try to predict what will be printed, then run the code. If your guess was incorrect, try to figure out why the result is different. If your guess was correct, celebrate!"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"FydkKnx_hUPq"},"outputs":[],"source":["flavor = 'vanilla'\n","print(flavor)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"LTWods50h-Ij"},"outputs":[],"source":["temperature = 70\n","print(flavor)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"l2vT4L7piGRf"},"outputs":[],"source":["print(temperature)\n","temperature = 35\n","print(temperature)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"k1Z_yWLXiWy7"},"outputs":[],"source":["dog_name = 'Bobby'\n","cat_name = 'Garfield'\n","dog_name = cat_name\n","cat_name = dog_name\n","print(dog_name)\n","print(cat_name)"]},{"cell_type":"markdown","metadata":{"id":"BZ50KykuAYPs"},"source":["Before running the following code, try to explain why it does *not* output `chocolate`."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"X3xHg6K4Aicn"},"outputs":[],"source":["sweet = 'chocolate'\n","savory = 'cheese'\n","dessert = 'sweet'\n","print(dessert)"]},{"cell_type":"markdown","metadata":{"id":"0az3tNK7WD3G"},"source":["In each of the following lines of code, replace `None` by a value of your choice. Make it such that `my_int` is an integer, `my_float` is a float, `my_bool` is a boolean and `my_string` is a string."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"lY-M8mfSXDfG"},"outputs":[],"source":["my_int = None\n","my_float = None\n","my_bool = None\n","my_string = None"]},{"cell_type":"markdown","metadata":{"id":"dvNIQh7KYuJb"},"source":["## Exercise 2.2: Bonus"]},{"cell_type":"markdown","metadata":{"id":"GI9fyUO8XOcp"},"source":["How could you verify in code whether the variables you wrote above have the correct type? Write this code below.\n","\n","Hint: scroll back to the section [Difference between types](#scrollTo=Difference_between_types)."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Om6z53RXYBoS"},"outputs":[],"source":[]},{"cell_type":"markdown","metadata":{"id":"h6UKIMXCj3w9"},"source":["In the code block below, replace the question mark `?` by a variable name. Make it such that the final output is `'Hooray!'`."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"3MI6_DY7ku30"},"outputs":[],"source":["cheer = 'Hooray!'\n","alarm = 'Oh no!'\n","anthem = 'Wilhelmus van Nassauwe'\n","alarm = anthem\n","? = cheer\n","cheer = anthem\n","anthem = alarm\n","print(anthem)"]},{"cell_type":"markdown","metadata":{"id":"ZXd6jCn90CA_"},"source":["## Expressions\n","\n","- An expression is a combination between *operands* and *operators*. \n","- Think of arithmetic: `1 + 2`\n","\n"]},{"cell_type":"markdown","metadata":{"id":"s8uJy_kI9C8S"},"source":["### Arithmetic"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"f9ACtc0e13i-"},"outputs":[],"source":["1 + 6\n","5 - 2\n","10 / 3\n","5 * 5\n","\n","a = 8\n","b = 10\n","\n","c = b - a\n","print(c)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"w9cfOflP8DEJ"},"outputs":[],"source":["# multiple operators\n","4 + (3 * 5)\n","(4 + 3) * 5"]},{"cell_type":"markdown","metadata":{"id":"zkt9aNKm28Lc"},"source":["### String expressions"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"6os4-rt43ThF"},"outputs":[],"source":["\"hello\" + \" world!\"\n","a = \"my age is: \"\n","b = 33\n","\n","a + b"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"SyrelaMu42UZ"},"outputs":[],"source":["a = 'hello'\n","a * 5"]},{"cell_type":"markdown","metadata":{"id":"DPbfP65xXlfb"},"source":["## Reassigning variables\n","\n","- You can use a variable itself when reassigning. This is useful when trying to expand an existing variable."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ogL2Iw3-Xs15"},"outputs":[],"source":["a = \"hello\"\n","a = a + \", world!\"\n","print(a)\n","\n","b = 'bye'\n","b = b + b\n","print(b)\n","\n","b = b + b\n","print(b)"]},{"cell_type":"markdown","metadata":{"id":"z_bXvnya5J2_"},"source":["## Exercise 2.3: Expressions\n","\n"]},{"cell_type":"markdown","metadata":{"id":"j9xhznQlbCBf"},"source":["1. Try to predict the value of each of the following code blocks. Can you explain any surprises?"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"0QcMB4xwbSfL"},"outputs":[],"source":["1 + 1"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"eHKN9kP9bWkm"},"outputs":[],"source":["1 * 1"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"uINoRNNbbXwJ"},"outputs":[],"source":["a = 1\n","b = 2\n","a + b"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"4xyWkYlnbc_8"},"outputs":[],"source":["c = b\n","a * b * c"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"7tW2T4mebljv"},"outputs":[],"source":["'hakuna' + 'matata'"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"uEdPHIhBb2Mw"},"outputs":[],"source":["liquid = 'water~'\n","liquid * 3"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"-D7BB50Qceo2"},"outputs":[],"source":["5 - 2 - 1"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"OQUGr5rGck3m"},"outputs":[],"source":["5 - (2 - 1)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Jc8fawaIdCD5"},"outputs":[],"source":["income = 100\n","tax = 20\n","net_income = income - tax\n","tax = 15\n","net_income"]},{"cell_type":"markdown","metadata":{"id":"TlLkcRv-droI"},"source":["2. In the code block below, change the values of `character` and `multiplier` so that the output becomes `'Goooood!'`."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"pYZUkeDJenob"},"outputs":[],"source":["character = 'o'\n","multiplier = 5\n","\n","begin = 'G'\n","middle = character * multiplier\n","end = 'd!'\n","\n","begin + middle + end"]},{"cell_type":"markdown","metadata":{"id":"G8X4n_a8a5tl"},"source":["3. Rewrite your Hello world-program:\n"," - Build the \"hello, world!\" string using multiple variables.\n"," - Make the program say \"hello, world!\" 3 times, each time on a new line"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"nDUVvhDEfIVI"},"outputs":[],"source":[]},{"cell_type":"markdown","metadata":{"id":"3HCqdTj2fVPK"},"source":["## Exercise 2.4: Bonus"]},{"cell_type":"markdown","metadata":{"id":"EKFdkLkWa8EY"},"source":["1. Find out and describe what the following operators do:\n"," - `%` (e.g. `10 % 2`)\n"," - `//` (e.g. `10 // 2`)\n"," - `**` (e.g. `10 ** 2`)\n"," - Tip: write the expressions using variables. Change the variables and see how it affects the outcome."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"V59a7vpDfzO2"},"outputs":[],"source":[]},{"cell_type":"markdown","metadata":{"id":"o0WYIAUla-AX"},"source":["2. Predict what will be printed on each line in the following code block, then run the code to check your answer. If you made any mistakes, try to figure out what is causing the difference."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Z9Vd9pdIUzT5"},"outputs":[],"source":["word = 'stylometry'\n","repeats = 3\n","extra = 2\n","\n","print((word * repeats) + str(extra))\n","print(word * (repeats + extra))\n","print(word * repeats + str(extra))\n","print((word + str(extra)) * repeats)\n","print(word + str(extra * repeats))\n","print(word + str(extra) * repeats)"]},{"cell_type":"markdown","metadata":{"id":"DRObBQZHgsIG"},"source":["3. Using just the variables `word`, `repeats` and `extra` from the previous code block, write a single big expression that evaluates to `'stylometry555stylometry'`."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"uLuiUk10gqwM"},"outputs":[],"source":[]},{"cell_type":"markdown","metadata":{"id":"QaamMzJY6ISR"},"source":["## Boolean expressions\n","Expressions that result in `True` or `False`\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"kLjya4Au6Ucu"},"outputs":[],"source":["# equals\n","1 == 1\n","\"hello\" == 'hello'\n","'2' == 2"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"tYNp6nUp7ixW"},"outputs":[],"source":["# does not equal\n","1 != 1\n","\"hello\" != 'hello'\n","'2' != 2"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"oixxKDHV7s5J"},"outputs":[],"source":["# greater than\n","2 > 1\n","2 > 5\n","'b' > 'a'\n","\n","2 > 2\n","\n","# greater than (or equal)\n","2 >= 2"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Z198kfCf75sU"},"outputs":[],"source":["# less than\n","2 < 1\n","2 < 5\n","'b' < 'a'\n","2 < 2\n","\n","# less than (or equal)\n","2 <= 2"]},{"cell_type":"markdown","metadata":{"id":"pvrcgKU18OrJ"},"source":["## Exercise 2.5: boolean expressions"]},{"cell_type":"markdown","metadata":{"id":"sWdnjezoil9j"},"source":["1. Predict for each boolean expression below: `True` or `False`? If any of your predictions is incorrect, try to find the reason."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"RpSvrom3Z8fQ"},"outputs":[],"source":["name = 'Sheean'\n","height = 2\n","\n","name * height == 'Sheean Sheean'\n","height < 2\n","2 <= height\n","1 < height <= 2\n","2 <= height < 1\n","name <= 'Julian'\n","height * 3 + 1 >= height"]},{"cell_type":"markdown","metadata":{"id":"zJiaGIlZBN_P"},"source":["2. Run the code block below. Did you expect this result? Can you explain it?"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"_PrnFf2lioMB"},"outputs":[],"source":["1 == True"]},{"cell_type":"markdown","metadata":{"id":"q8QviA70kdQE"},"source":["3. Replace one value in each of the following expressions so the expression becomes `True`."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"qDQ9Ob5Zkrqm"},"outputs":[],"source":["1 > height\n","height == 0\n","'Julian' > name * 2\n","name < 1 * name"]},{"cell_type":"markdown","metadata":{"id":"YbdV7SQVmDVV"},"source":["4. Replace one operator in each of the following expressions so the expression becomes `True`."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"hzjXIwkAmChv"},"outputs":[],"source":["5 < 4\n","2 + 1 == 1\n","3 + 3 == 3 + 2"]},{"cell_type":"markdown","metadata":{"id":"SwyAMDzDn2_X"},"source":["5. The *Groot woordenboek van de Nederlandse taal* by Van Dale publishers, colloquially known as \"De dikke Van Dale\", is a three-tome Dutch dictionary. The first tome contains words starting a–i, the second j–q and the third r–z. In the code block below, write an expression that will tell you whether `word` should be in the second tome. Try different values of `word` to verify that your solution is correct. You do not need to check whether the word is actually Dutch!"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"lmCGfx_DqMWe"},"outputs":[],"source":["word = 'archaïsch'\n","\n","# your expression here"]},{"cell_type":"markdown","metadata":{"id":"jXSxbjf4q6q5"},"source":["## Next module\n","\n","[3. Conditionals](https://colab.research.google.com/drive/1KkZ2fS75o7giccQJakRhyvYBV7JdFnVw)"]}],"metadata":{"colab":{"provenance":[]},"kernelspec":{"display_name":"Python 3","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.10.12"}},"nbformat":4,"nbformat_minor":0}
diff --git a/lessons/03 conditionals.ipynb b/lessons/03 conditionals.ipynb
index 099c1e4..4ffcb7c 100644
--- a/lessons/03 conditionals.ipynb
+++ b/lessons/03 conditionals.ipynb
@@ -1 +1 @@
-{"cells":[{"cell_type":"markdown","metadata":{"id":"fqMJHzNk5yXQ"},"source":["# Module 3: Conditionals\n","\n","### CDH course \"Programming in Python\"\n","\n","[index](https://colab.research.google.com/drive/1s05aR4wn2dU1C3se1oXfqKz2EY5ilrno)\n","\n","Previous module: [2. Values and expressions](https://colab.research.google.com/drive/17K6C_EZoeGtRxoTbYQvygFEdpWgQ2QFp)\n","\n","### This module\n","\n","- Execute code only under specific conditions."]},{"cell_type":"markdown","metadata":{"id":"SshSsbtF8ldm"},"source":["## `if`\n","\n","- Only evaluate part of the code if some condition is met\n","- Syntax: `if :`\n","- `` can be any expression that evaluates to `True` or `False`\n","- Introducing *indented blocks* "]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Va6X8kIz9ey0"},"outputs":[],"source":["a = 12\n","\n","if a > 10:\n"," print('the condition is met!')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ADXv-2u090ql"},"outputs":[],"source":["a = 15\n","\n","if a < 10:\n"," print('everything on the same indentation level belongs to this if-condition')\n"," print('so this will only get printed if the condition is met')\n"," print('and so does this')\n","print('but not this!')"]},{"cell_type":"markdown","metadata":{"id":"5gqn2ac_sg7Y"},"source":["- Indented blocks are the reason we sometimes need `pass` as a placeholder (remember [1. introduction](https://colab.research.google.com/drive/1i4wJpUIr77Fh1renWNjt00Bd51NbC1nB)). It lets us create an indented block, without defining any behavior for it yet."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ZdH7HnL6tDpS"},"outputs":[],"source":["if earth == 'square':\n"," # TODO not sure yet how to handle this case\n"," pass"]},{"cell_type":"markdown","metadata":{"id":"sQxxBZwm-FYm"},"source":["## `else`\n","\n","- `if` is nice, but what if we are interested in the other case?"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"atRhlkGq-Mh1"},"outputs":[],"source":["letter = 'b'\n","\n","if letter == 'a':\n"," print('we found the letter a')\n","\n","if letter == 'b': \n"," print('this is not a')\n","\n","if letter == 'c':\n"," print('this is not a')"]},{"cell_type":"markdown","metadata":{"id":"O1repWSS-3Y0"},"source":["Instead of specifying *all other* cases, use `else:`"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"4n9ksUuH-8kE"},"outputs":[],"source":["letter = 'b'\n","\n","if letter == 'a':\n"," print('we found the letter a')\n","else:\n"," print('this is not a')"]},{"cell_type":"markdown","metadata":{"id":"Rg1ohowkAGFh"},"source":["## `elif`\n","\n","Specify extra cases with `elif :` (else if)\n","\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"JbiihBgdANIM"},"outputs":[],"source":["letter = 'a'\n","\n","if letter == 'a':\n"," print('we found the letter a')\n","elif letter == 'b':\n"," print('we found the letter b')\n","else:\n"," print('this is not a or b')"]},{"cell_type":"markdown","metadata":{"id":"lVOu8HvVIEj6"},"source":["## Multiple conditions\n","- We can *nest* conditions"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"MH_RrrBRIPh4"},"outputs":[],"source":["number = 11\n","\n","if number > 2:\n"," if number < 10:\n"," print('between 2 and 10')\n"," print('larger than 2, but not smaller than 10')\n","\n","# There is a mistake in the code above, can you find\n","# and fix it?"]},{"cell_type":"markdown","metadata":{"id":"8Lvam7rpIms6"},"source":["- Even better: we can *combine* conditions\n","- use ` and `, ` or ` and `not `\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"zJ8rBgcfIw4I"},"outputs":[],"source":["number = 11\n","if number > 2 and number < 10:\n"," print('between 2 and 10')\n","\n","letter = 'd'\n","if letter == 'a' or letter == 'b' or letter == 'c':\n"," print('a or b or c')\n","\n","if not (letter == 'a' or letter == 'b'):\n"," print('neither a nor b')"]},{"cell_type":"markdown","metadata":{"id":"SmsIZBLEtg9r"},"source":["- `and`, `or`, `not` are operators, just like `+` and `==`. You can use them outside conditionals."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"XMWJeaujt2lj"},"outputs":[],"source":["height = 185\n","weight = 52\n","\n","tall = height > 180\n","light = weight < 65\n","\n","skinny = tall and light\n","print(skinny)"]},{"cell_type":"markdown","metadata":{"id":"tvXa9KWXAwge"},"source":["## Exercise 3.1: if/elif/else"]},{"cell_type":"markdown","metadata":{"id":"pKBuViM9u7ZC"},"source":["1. Try to predict the output of the following code blocks. Can you explain any surprises?"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"sEQyw2xJvzQN"},"outputs":[],"source":["if 3 <= 2:\n"," print('What a strange day.')\n","print('What a strange day.')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"FnCFfEb_v-0Y"},"outputs":[],"source":["if None == 0:\n"," pass\n"," print('I got nothing to do.')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"arta4ZQ0vDSC"},"outputs":[],"source":["name = 'Julian'\n","\n","if name < 'Sheean':\n"," print('Alphabetically before Sheean')\n","elif name > 'Sheean':\n"," print('Alphabetically after Sheean')\n","else:\n"," print('Same name as Sheean')"]},{"cell_type":"markdown","metadata":{"id":"4SVSiqHSu4WX"},"source":["2. In the following code block, replace `None` with your own condition in the `if` statement. If `value` is greater than `5`, `size` must be `'large'`; otherwise, `size` must be `'small'`. Change `value` a few times and rerun the code to check that your condition is correct."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"czmdFacjeUaL"},"outputs":[],"source":["value = 4\n","\n","if None:\n"," size = 'large'\n","else:\n"," size = 'small'\n","\n","print(size)"]},{"cell_type":"markdown","metadata":{"id":"sEOwMi04e6WW"},"source":["3. Write an `if`/`elif`/`else` statement like the previous, but with different cutoff points: if `value` is less than `4`, `size` must be `'small'`; if `value` is between `4` and `6` (inclusive), `size` must be `'medium'`; if `value` is greater than `6`, `size` must be `'large'`."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"8HeaY6l9f9iA"},"outputs":[],"source":["value = 4\n","\n","# your if/elif/else here\n","\n","print(size)"]},{"cell_type":"markdown","metadata":{"id":"TWzbez_2RUh4"},"source":["4. Rewrite the conditional that checks if a letter is neither 'a' nor 'b', using a different notation."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"XsIg1MD4JrPX"},"outputs":[],"source":["letter = 'c'\n","\n","# original\n","if not (letter == 'a' or letter == 'b'):\n"," print('neither a nor b')"]},{"cell_type":"markdown","metadata":{"id":"-XEYQZJ1ya1j"},"source":["## Exercise 3.2: Bonus"]},{"cell_type":"markdown","metadata":{"id":"POVFwRu_f91I"},"source":["*FizzBuzz part 1* (advanced).\n","Write an `if`/`elif`/`else` statement that behaves as follows:\n","\n","- if `value` is divisible by 3 but not by 5, print `'Fizz'`;\n","- if `value` is divisible by 5 but not by 3, print `'Buzz'`;\n","- if `value` is divisible by 3 *and* by 5, print `'FizzBuzz'`;\n","- in all remaining cases, print `value`.\n","\n","Tip: use the result of [Exercise 2.4.1](https://colab.research.google.com/drive/17K6C_EZoeGtRxoTbYQvygFEdpWgQ2QFp#scrollTo=Exercise_2_4_Bonus)!"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"SZGeQtqEhiAK"},"outputs":[],"source":["value = 9\n","\n","# Your code here"]},{"cell_type":"markdown","metadata":{"id":"YBC4OfihzFho"},"source":["## Next module\n","\n","[4. Datastructures](https://colab.research.google.com/drive/1JxzmIzwcipnwFBntv0WZOlT-d2fUjoRF)"]}],"metadata":{"colab":{"authorship_tag":"ABX9TyOwt+JcPyfyBfscI/sF+CKo","provenance":[],"toc_visible":true},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"name":"python"}},"nbformat":4,"nbformat_minor":0}
+{"cells":[{"cell_type":"markdown","metadata":{"id":"fqMJHzNk5yXQ"},"source":["# Module 3: Conditionals\n","\n","### CDH course \"Programming in Python\"\n","\n","[index](https://colab.research.google.com/drive/1s05aR4wn2dU1C3se1oXfqKz2EY5ilrno)\n","\n","Previous module: [2. Values and expressions](https://colab.research.google.com/drive/17K6C_EZoeGtRxoTbYQvygFEdpWgQ2QFp)\n","\n","### This module\n","\n","- Execute code only under specific conditions."]},{"cell_type":"markdown","metadata":{"id":"SshSsbtF8ldm"},"source":["## `if`\n","\n","- Only evaluate part of the code if some condition is met\n","- Syntax: `if :`\n","- `` can be any expression that evaluates to `True` or `False`\n","- Introducing *indented blocks* "]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Va6X8kIz9ey0"},"outputs":[],"source":["a = 12\n","\n","if a > 10:\n"," print('the condition is met!')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ADXv-2u090ql"},"outputs":[],"source":["a = 15\n","\n","if a < 10:\n"," print('everything on the same indentation level belongs to this if-condition')\n"," print('so this will only get printed if the condition is met')\n"," print('and so does this')\n","print('but not this!')"]},{"cell_type":"markdown","metadata":{"id":"5gqn2ac_sg7Y"},"source":["- Indented blocks are the reason we sometimes need `pass` as a placeholder (remember [1. introduction](https://colab.research.google.com/drive/1i4wJpUIr77Fh1renWNjt00Bd51NbC1nB)). It lets us create an indented block, without defining any behavior for it yet."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ZdH7HnL6tDpS"},"outputs":[],"source":["earth = 'round'\n","\n","if earth == 'square':\n"," # TODO not sure yet how to handle this case\n"," pass"]},{"cell_type":"markdown","metadata":{"id":"sQxxBZwm-FYm"},"source":["## `else`\n","\n","- `if` is nice, but what if we are interested in the other case?"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"atRhlkGq-Mh1"},"outputs":[],"source":["letter = 'b'\n","\n","if letter == 'a':\n"," print('we found the letter a')\n","\n","if letter == 'b': \n"," print('this is not a')\n","\n","if letter == 'c':\n"," print('this is not a')"]},{"cell_type":"markdown","metadata":{"id":"O1repWSS-3Y0"},"source":["Instead of specifying *all other* cases, use `else:`"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"4n9ksUuH-8kE"},"outputs":[],"source":["letter = 'b'\n","\n","if letter == 'a':\n"," print('we found the letter a')\n","else:\n"," print('this is not a')"]},{"cell_type":"markdown","metadata":{"id":"Rg1ohowkAGFh"},"source":["## `elif`\n","\n","Specify extra cases with `elif :` (else if)\n","\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"JbiihBgdANIM"},"outputs":[],"source":["letter = 'a'\n","\n","if letter == 'a':\n"," print('we found the letter a')\n","elif letter == 'b':\n"," print('we found the letter b')\n","else:\n"," print('this is not a or b')"]},{"cell_type":"markdown","metadata":{"id":"lVOu8HvVIEj6"},"source":["## Multiple conditions\n","- We can *nest* conditions"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"MH_RrrBRIPh4"},"outputs":[],"source":["number = 11\n","\n","if number > 2:\n"," if number < 10:\n"," print('between 2 and 10')\n"," print('larger than 2, but not smaller than 10')\n","\n","# There is a mistake in the code above, can you find\n","# and fix it?"]},{"cell_type":"markdown","metadata":{"id":"8Lvam7rpIms6"},"source":["- Even better: we can *combine* conditions\n","- use ` and `, ` or ` and `not `\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"zJ8rBgcfIw4I"},"outputs":[],"source":["number = 11\n","if number > 2 and number < 10:\n"," print('between 2 and 10')\n","\n","letter = 'd'\n","if letter == 'a' or letter == 'b' or letter == 'c':\n"," print('a or b or c')\n","\n","if not (letter == 'a' or letter == 'b'):\n"," print('neither a nor b')"]},{"cell_type":"markdown","metadata":{"id":"SmsIZBLEtg9r"},"source":["- `and`, `or`, `not` are operators, just like `+` and `==`. You can use them outside conditionals."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"XMWJeaujt2lj"},"outputs":[],"source":["height = 185\n","weight = 52\n","\n","tall = height > 180\n","light = weight < 65\n","\n","skinny = tall and light\n","print(skinny)"]},{"cell_type":"markdown","metadata":{"id":"tvXa9KWXAwge"},"source":["## Exercise 3.1: if/elif/else"]},{"cell_type":"markdown","metadata":{"id":"pKBuViM9u7ZC"},"source":["1. Try to predict the output of the following code blocks. Can you explain any surprises?"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"sEQyw2xJvzQN"},"outputs":[],"source":["if 3 <= 2:\n"," print('What a strange day.')\n","print('What a strange day.')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"FnCFfEb_v-0Y"},"outputs":[],"source":["if None == 0:\n"," pass\n"," print('I got nothing to do.')"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"arta4ZQ0vDSC"},"outputs":[],"source":["name = 'Julian'\n","\n","if name < 'Sheean':\n"," print('Alphabetically before Sheean')\n","elif name > 'Sheean':\n"," print('Alphabetically after Sheean')\n","else:\n"," print('Same name as Sheean')"]},{"cell_type":"markdown","metadata":{"id":"4SVSiqHSu4WX"},"source":["2. In the following code block, replace `None` with your own condition in the `if` statement. If `value` is greater than `5`, `size` must be `'large'`; otherwise, `size` must be `'small'`. Change `value` a few times and rerun the code to check that your condition is correct."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"czmdFacjeUaL"},"outputs":[],"source":["value = 4\n","\n","if None:\n"," size = 'large'\n","else:\n"," size = 'small'\n","\n","print(size)"]},{"cell_type":"markdown","metadata":{"id":"sEOwMi04e6WW"},"source":["3. Write an `if`/`elif`/`else` statement like the previous, but with different cutoff points: if `value` is less than `4`, `size` must be `'small'`; if `value` is between `4` and `6` (inclusive), `size` must be `'medium'`; if `value` is greater than `6`, `size` must be `'large'`."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"8HeaY6l9f9iA"},"outputs":[],"source":["value = 4\n","\n","# your if/elif/else here\n","\n","print(size)"]},{"cell_type":"markdown","metadata":{"id":"TWzbez_2RUh4"},"source":["4. Rewrite the conditional that checks if a letter is neither 'a' nor 'b', using a different notation."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"XsIg1MD4JrPX"},"outputs":[],"source":["letter = 'c'\n","\n","# original\n","if not (letter == 'a' or letter == 'b'):\n"," print('neither a nor b')"]},{"cell_type":"markdown","metadata":{"id":"-XEYQZJ1ya1j"},"source":["## Exercise 3.2: Bonus"]},{"cell_type":"markdown","metadata":{"id":"POVFwRu_f91I"},"source":["*FizzBuzz part 1* (advanced).\n","Write an `if`/`elif`/`else` statement that behaves as follows:\n","\n","- if `value` is divisible by 3 but not by 5, print `'Fizz'`;\n","- if `value` is divisible by 5 but not by 3, print `'Buzz'`;\n","- if `value` is divisible by 3 *and* by 5, print `'FizzBuzz'`;\n","- in all remaining cases, print `value`.\n","\n","Tip: use the result of [Exercise 2.4.1](https://colab.research.google.com/drive/17K6C_EZoeGtRxoTbYQvygFEdpWgQ2QFp#scrollTo=Exercise_2_4_Bonus)!"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"SZGeQtqEhiAK"},"outputs":[],"source":["value = 9\n","\n","# Your code here"]},{"cell_type":"markdown","metadata":{"id":"YBC4OfihzFho"},"source":["## Next module\n","\n","[4. Datastructures](https://colab.research.google.com/drive/1JxzmIzwcipnwFBntv0WZOlT-d2fUjoRF)"]}],"metadata":{"colab":{"authorship_tag":"ABX9TyOwt+JcPyfyBfscI/sF+CKo","provenance":[],"toc_visible":true},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"name":"python"}},"nbformat":4,"nbformat_minor":0}
diff --git a/lessons/04 datastructures.ipynb b/lessons/04 datastructures.ipynb
index 14f0d91..2491668 100644
--- a/lessons/04 datastructures.ipynb
+++ b/lessons/04 datastructures.ipynb
@@ -1 +1 @@
-{"cells":[{"cell_type":"markdown","metadata":{"id":"fqMJHzNk5yXQ"},"source":["# Module 4: Data Structures\n","\n","### CDH course \"Programming in Python\"\n","\n","[index](https://colab.research.google.com/drive/1s05aR4wn2dU1C3se1oXfqKz2EY5ilrno)\n","\n","Previous module: [3. Conditionals](https://colab.research.google.com/drive/1KkZ2fS75o7giccQJakRhyvYBV7JdFnVw)\n","\n","### This module\n","\n","- Working with collections of many values"]},{"cell_type":"markdown","metadata":{"id":"rDdBkbX5kmUD"},"source":["## Data structures\n","\n","- Way to organize data, to make accessing it efficient\n","- Different types of data structures available\n","- For now, we will work with `list` and `tuple`"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"bmAaWSOPm87H"},"outputs":[],"source":["student1 = 'jasmin'\n","student2 = 'ravi'\n","student3 = 'john'\n","# not very efficient, what if we want to add another student? Or take one out?"]},{"cell_type":"markdown","metadata":{"id":"8Vl5wbRunR82"},"source":["## Lists\n","\n","- `list`: an ordered collection of values\n","- One type of *iterable*, a collection you that allows iteration over its elements\n","- Syntax: `[element1, element2, ...]`\n","- Empty list also exists: `[]`"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"sewvhM8JnhJZ"},"outputs":[],"source":["students = ['jasmin', 'ravi', 'john']\n","\n","print(students)"]},{"cell_type":"markdown","metadata":{"id":"zmKyJPoQnwmK"},"source":["Lists can contain values of mixed types:"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Bp6_Mev2nzZV"},"outputs":[],"source":["['hello', 1, False]"]},{"cell_type":"markdown","metadata":{"id":"dt_IOpu_rqbk"},"source":["Lists can also contain variables"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"TTCPoO7QrtVy"},"outputs":[],"source":["usa = 'United States of America'\n","nl = 'The Netherlands'\n","countries = [usa, nl]"]},{"cell_type":"markdown","metadata":{"id":"uYR5FBUEoR0P"},"source":["### Accessing elements\n","- Every element has an *index*\n","- Index goes from 0 to length of the list - 1\n","- Negative index counts backwards from the last element"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"2eL0BOUJodLK"},"outputs":[],"source":["students = ['jasmin', 'ravi', 'john']\n","students[0]\n","students[1]\n","students[2]\n","students[-1]"]},{"cell_type":"markdown","metadata":{"id":"cyX0YcO5uZRa"},"source":["- Lists can be *unpacked* into variables"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"m6ETvPPXuc4z"},"outputs":[],"source":["numbers = [1, 2, 3]\n","one, two, three = numbers"]},{"cell_type":"markdown","metadata":{"id":"KvOEQrqRrS0T"},"source":["### Changing elements\n","- Assign element at index just like you would a variable"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"wFyceuSArcEB"},"outputs":[],"source":["students = ['jasmin', 'ravi', 'john']\n","students[0] = 'johanna'\n","\n","new_student = 'mark'\n","students[1] = new_student\n","\n","students"]},{"cell_type":"markdown","metadata":{"id":"DixopwTyr6gN"},"source":["### Adding and removing elements\n","- The `+` operator works for two lists\n","- The `.append(value)` and `.remove(index)` functions works on a list"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"nPyn0UHcsDbG"},"outputs":[],"source":["hello_world = ['hello', ',', 'world']\n","exclamation = ['!']\n","\n","full_sentence = hello_world + exclamation\n","print(full_sentence)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Y2B57KQRsO2a"},"outputs":[],"source":["# note: .append() works in-place, you don't need to reassign the variable\n","students = ['jasmin', 'ravi', 'john']\n","students.append('mark')\n","print(students)\n","\n","students.remove(2)\n","print(students)"]},{"cell_type":"markdown","metadata":{"id":"kUUwwkDVtXOC"},"source":["### Nested lists\n","- Anything goes in a list, including *another* list"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"EPxrRcL0tbpN"},"outputs":[],"source":["small_list = [4, 5, 6]\n","big_list = [1, 2, 3, small_list]\n","\n","print(big_list)\n","print(big_list[-1])\n","print(type(big_list[-1]))\n","\n","# Access the last element of the small_list inside big_list:"]},{"cell_type":"markdown","metadata":{"id":"1HDqXMbWwmbk"},"source":["### Accessing multiple elements\n","- Select multiple values at once: *slicing*\n","- Syntax: `list[start_index:end_index]`\n","- end_index is *exclusive*, so 'up to' end"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"wIS3jCYlw2P6"},"outputs":[],"source":["students = ['jasmin', 'ravi', 'john']\n","# 0 1 2 3\n","students[0:1]\n","students[0:2]\n"]},{"cell_type":"markdown","metadata":{"id":"BblECQZfw7Uy"},"source":["`start_index` and `end_index` are optional"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"6fIsX2VvxEq9"},"outputs":[],"source":["students = ['jasmin', 'ravi', 'john']\n","students[1:]\n","students[:-1]\n","students[:]\n"]},{"cell_type":"markdown","metadata":{"id":"n9lZQ72ExR4Z"},"source":["- slices can be used to reassign list elements\n","\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"C5AIMJEHxWnX"},"outputs":[],"source":["students = ['jasmin', 'ravi', 'john']\n","\n","students[0:2] = ['johanna', 'mark']\n","\n","print(students)"]},{"cell_type":"markdown","metadata":{"id":"CPHEdywi-IuC"},"source":["- in this way, you can also add or remove elements in the middle"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Tzhdcojp-TTn"},"outputs":[],"source":["students = ['jasmin', 'ravi', 'john']\n","\n","students[1:2] = []\n","print(students)\n","\n","students[1:1] = ['ravi']\n","print(students)"]},{"cell_type":"markdown","metadata":{"id":"nfpm1orRO34Q"},"source":["### Checking if an element is in a list\n","- Use the syntax ` in `"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"0A9JACKJPCYt"},"outputs":[],"source":["students = ['jasmin', 'ravi', 'john']\n","\n","'ravi' in students\n","'Ravi' in students"]},{"cell_type":"markdown","metadata":{"id":"2NX28b3sZscv"},"source":["### Useful tricks\n","- the `len` *function* (we will learn about functions later) gives us the length of a list"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"A7UHSeTtZ2nw"},"outputs":[],"source":["students = ['jasmin', 'ravi', 'john']\n","len(students)"]},{"cell_type":"markdown","metadata":{"id":"cO6hX3FBZ6cC"},"source":["- `list.index()` finds a value and gives us the index"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"VPmLssc7aByj"},"outputs":[],"source":["students = ['jasmin', 'ravi', 'john']\n","students.index('ravi')"]},{"cell_type":"markdown","metadata":{"id":"ZyOZeS2SuRJ6"},"source":["## Tuples\n","- Different type of *iterable*\n","- Syntax: `(element1, element2, ...)`\n","- Important difference: not *mutable* (cannot change elements)\n","- Often used to unpack, we will work with tuples in data analysis"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"etiZVM_puu4Z"},"outputs":[],"source":["students = ('jasmin', 'ravi', 'john')\n","students[0]"]},{"cell_type":"markdown","metadata":{"id":"70aMsClGPRy9"},"source":["## Exercise 4.1: Lists\n","\n","1. For each of the `print` statements below, what do you expect is printed? Run the lines to check predictions"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"KMUxwcSqPlU1"},"outputs":[],"source":["countries = ['japan', 'hungary', 'maldives', 'gabon', 'bhutan']\n","\n","print(countries[0])\n","print(countries[-3])\n","print(countries[0:1] + countries[2:4])\n","\n","more_countries = countries + ['mexico', 'haiti']\n","print(more_countries)\n","\n","countries.append(['mexico', 'haiti'])\n","print(countries)"]},{"cell_type":"markdown","metadata":{"id":"TyebsOIpU6hv"},"source":["2. Transform the list below into `['jasmin', 'john', 'ravi']` in one line of code. \n","\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"H8o6vsHKVKoq"},"outputs":[],"source":["students = ['jasmin', 'ravi', 'john']"]},{"cell_type":"markdown","metadata":{"id":"HMU5X7XFWbCw"},"source":["3. For each of the print statements below, what do you expect is printed? Run the lines to check predictions."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"u_RWc8wBWgMT"},"outputs":[],"source":["random_fruit = 'pineapple'\n","fruits = ['apple', 'pear', random_fruit]\n","print(fruits)\n","\n","random_fruit = 'blueberry'\n","print(fruits)\n","\n","random_veggie = ['brussel sprouts']\n","veggies = ['broccoli', 'green beans', random_veggie]\n","print(veggies)\n","\n","random_veggie.append('kale')\n","print(veggies)"]},{"cell_type":"markdown","metadata":{"id":"3BfUO-jKS_u1"},"source":["## Exercise 4.2: Bonus\n","\n","Below we introduce another parameter in the list slice. Try to explain what it does."]},{"cell_type":"code","execution_count":2,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":627,"status":"ok","timestamp":1681202305255,"user":{"displayName":"Mees van Stiphout","userId":"10520931415894572279"},"user_tz":-120},"id":"Y9oxyQb7TIPI","outputId":"158288d5-94e0-4068-d13e-af2a5c85177f"},"outputs":[{"name":"stdout","output_type":"stream","text":["['japan', 'hungary', 'maldives', 'gabon', 'bhutan']\n","['japan', 'maldives', 'bhutan']\n","['bhutan', 'gabon', 'maldives', 'hungary', 'japan']\n","['bhutan', 'maldives', 'japan']\n"]}],"source":["countries = ['japan', 'hungary', 'maldives', 'gabon', 'bhutan']\n","\n","print(countries[0:5:1])\n","print(countries[0:5:2])\n","print(countries[-1::-1])\n","print(countries[-1::-2])"]},{"cell_type":"markdown","metadata":{"id":"Mb6CvHt3CaA0"},"source":["The piece of code below is supposed to recognize \"fancy\" words: words that are longer than 5 characters, contain at least one copy of the letter 'a' and start with an uppercase. However, the code is broken. It does not recognize any of our fancy example words.\n","\n","1. Change the value of `word` into each of the examples in the comments on the first two lines and then run the code. See for yourself that the code considers none of the example words fancy. Try some other words as well.\n","3. Try to understand why the code is giving the wrong result. Can you come up with a word that the code does consider fancy?\n","4. Repair the code so that it gives the right result for all examples, and any other words that you come up with."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"QQyGzsqCCe3o"},"outputs":[],"source":["# fancy: Alhambra, Arthur, Jasmine, Turandot\n","# not so fancy: Jeep, paper, Python, Ada\n","word = 'Alhambra'\n","\n","lengthy = len(word) > 5\n","has_a = 'a' in word\n","first_uppercase = 'A' <= word[1] <= 'Z'\n","\n","if lengthy and has_a and first_uppercase:\n"," print('The word is fancy')\n","else:\n"," print('The word is not so fancy')"]},{"cell_type":"markdown","metadata":{"id":"HiEWGB1V1W4U"},"source":["## Next module\n","\n","[5. Assertions](https://colab.research.google.com/drive/1Nv6vgkH2zjJes7LXUCVhsDwFOGCtTbHM)"]}],"metadata":{"colab":{"provenance":[],"toc_visible":true},"kernelspec":{"display_name":"Python 3","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.10.12"}},"nbformat":4,"nbformat_minor":0}
+{"cells":[{"cell_type":"markdown","metadata":{"id":"fqMJHzNk5yXQ"},"source":["# Module 4: Data Structures\n","\n","### CDH course \"Programming in Python\"\n","\n","[index](https://colab.research.google.com/drive/1s05aR4wn2dU1C3se1oXfqKz2EY5ilrno)\n","\n","Previous module: [3. Conditionals](https://colab.research.google.com/drive/1KkZ2fS75o7giccQJakRhyvYBV7JdFnVw)\n","\n","### This module\n","\n","- Working with collections of many values"]},{"cell_type":"markdown","metadata":{"id":"rDdBkbX5kmUD"},"source":["## Data structures\n","\n","- Way to organize data, to make accessing it efficient\n","- Different types of data structures available\n","- For now, we will work with `list` and `tuple`"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"bmAaWSOPm87H"},"outputs":[],"source":["student1 = 'jasmin'\n","student2 = 'ravi'\n","student3 = 'john'\n","# not very efficient, what if we want to add another student? Or take one out?"]},{"cell_type":"markdown","metadata":{"id":"8Vl5wbRunR82"},"source":["## Lists\n","\n","- `list`: an ordered collection of values\n","- One type of *iterable*, a collection you that allows iteration over its elements\n","- Syntax: `[element1, element2, ...]`\n","- Empty list also exists: `[]`"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"sewvhM8JnhJZ"},"outputs":[],"source":["students = ['jasmin', 'ravi', 'john']\n","\n","print(students)"]},{"cell_type":"markdown","metadata":{"id":"zmKyJPoQnwmK"},"source":["Lists can contain values of mixed types:"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Bp6_Mev2nzZV"},"outputs":[],"source":["['hello', 1, False]"]},{"cell_type":"markdown","metadata":{"id":"dt_IOpu_rqbk"},"source":["Lists can also contain variables"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"TTCPoO7QrtVy"},"outputs":[],"source":["usa = 'United States of America'\n","nl = 'The Netherlands'\n","countries = [usa, nl]"]},{"cell_type":"markdown","metadata":{"id":"uYR5FBUEoR0P"},"source":["### Accessing elements\n","- Every element has an *index*\n","- Index goes from 0 to length of the list - 1\n","- Negative index counts backwards from the last element"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"2eL0BOUJodLK"},"outputs":[],"source":["students = ['jasmin', 'ravi', 'john']\n","students[0]\n","students[1]\n","students[2]\n","students[-1]"]},{"cell_type":"markdown","metadata":{"id":"cyX0YcO5uZRa"},"source":["- Lists can be *unpacked* into variables"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"m6ETvPPXuc4z"},"outputs":[],"source":["numbers = [1, 2, 3]\n","one, two, three = numbers"]},{"cell_type":"markdown","metadata":{"id":"KvOEQrqRrS0T"},"source":["### Changing elements\n","- Assign element at index just like you would a variable"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"wFyceuSArcEB"},"outputs":[],"source":["students = ['jasmin', 'ravi', 'john']\n","students[0] = 'johanna'\n","\n","new_student = 'mark'\n","students[1] = new_student\n","\n","students"]},{"cell_type":"markdown","metadata":{"id":"DixopwTyr6gN"},"source":["### Adding and removing elements\n","- The `+` operator works for two lists\n","- The `.append(value)` and `.remove(index)` functions works on a list"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"nPyn0UHcsDbG"},"outputs":[],"source":["hello_world = ['hello', ',', 'world']\n","exclamation = ['!']\n","\n","full_sentence = hello_world + exclamation\n","print(full_sentence)"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Y2B57KQRsO2a"},"outputs":[],"source":["# note: .append() works in-place, you don't need to reassign the variable\n","students = ['jasmin', 'ravi', 'john']\n","students.append('mark')\n","print(students)\n","\n","students.remove('john')\n","# or by index:\n","# del students[2]\n","print(students)"]},{"cell_type":"markdown","metadata":{"id":"kUUwwkDVtXOC"},"source":["### Nested lists\n","- Anything goes in a list, including *another* list"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"EPxrRcL0tbpN"},"outputs":[],"source":["small_list = [4, 5, 6]\n","big_list = [1, 2, 3, small_list]\n","\n","print(big_list)\n","print(big_list[-1])\n","print(type(big_list[-1]))\n","\n","# Access the last element of the small_list inside big_list:"]},{"cell_type":"markdown","metadata":{"id":"1HDqXMbWwmbk"},"source":["### Accessing multiple elements\n","- Select multiple values at once: *slicing*\n","- Syntax: `list[start_index:end_index]`\n","- end_index is *exclusive*, so 'up to' end"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"wIS3jCYlw2P6"},"outputs":[],"source":["students = ['jasmin', 'ravi', 'john']\n","# 0 1 2 3\n","students[0:1]\n","students[0:2]\n"]},{"cell_type":"markdown","metadata":{"id":"BblECQZfw7Uy"},"source":["`start_index` and `end_index` are optional"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"6fIsX2VvxEq9"},"outputs":[],"source":["students = ['jasmin', 'ravi', 'john']\n","students[1:]\n","students[:-1]\n","students[:]\n"]},{"cell_type":"markdown","metadata":{"id":"n9lZQ72ExR4Z"},"source":["- slices can be used to reassign list elements\n","\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"C5AIMJEHxWnX"},"outputs":[],"source":["students = ['jasmin', 'ravi', 'john']\n","\n","students[0:2] = ['johanna', 'mark']\n","\n","print(students)"]},{"cell_type":"markdown","metadata":{"id":"CPHEdywi-IuC"},"source":["- in this way, you can also add or remove elements in the middle"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Tzhdcojp-TTn"},"outputs":[],"source":["students = ['jasmin', 'ravi', 'john']\n","\n","students[1:2] = []\n","print(students)\n","\n","students[1:1] = ['ravi']\n","print(students)"]},{"cell_type":"markdown","metadata":{"id":"nfpm1orRO34Q"},"source":["### Checking if an element is in a list\n","- Use the syntax ` in `"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"0A9JACKJPCYt"},"outputs":[],"source":["students = ['jasmin', 'ravi', 'john']\n","\n","'ravi' in students\n","'Ravi' in students"]},{"cell_type":"markdown","metadata":{"id":"2NX28b3sZscv"},"source":["### Useful tricks\n","- the `len` *function* (we will learn about functions later) gives us the length of a list"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"A7UHSeTtZ2nw"},"outputs":[],"source":["students = ['jasmin', 'ravi', 'john']\n","len(students)"]},{"cell_type":"markdown","metadata":{"id":"cO6hX3FBZ6cC"},"source":["- `list.index()` finds a value and gives us the index"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"VPmLssc7aByj"},"outputs":[],"source":["students = ['jasmin', 'ravi', 'john']\n","students.index('ravi')"]},{"cell_type":"markdown","metadata":{"id":"ZyOZeS2SuRJ6"},"source":["## Tuples\n","- Different type of *iterable*\n","- Syntax: `(element1, element2, ...)`\n","- Important difference: not *mutable* (cannot change elements)\n","- Often used to unpack, we will work with tuples in data analysis"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"etiZVM_puu4Z"},"outputs":[],"source":["students = ('jasmin', 'ravi', 'john')\n","students[0]"]},{"cell_type":"markdown","metadata":{"id":"70aMsClGPRy9"},"source":["## Exercise 4.1: Lists\n","\n","1. For each of the `print` statements below, what do you expect is printed? Run the lines to check predictions"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"KMUxwcSqPlU1"},"outputs":[],"source":["countries = ['japan', 'hungary', 'maldives', 'gabon', 'bhutan']\n","\n","print(countries[0])\n","print(countries[-3])\n","print(countries[0:1] + countries[2:4])\n","\n","more_countries = countries + ['mexico', 'haiti']\n","print(more_countries)\n","\n","countries.append(['mexico', 'haiti'])\n","print(countries)"]},{"cell_type":"markdown","metadata":{"id":"TyebsOIpU6hv"},"source":["2. Transform the list below into `['jasmin', 'john', 'ravi']` in one line of code. \n","\n"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"H8o6vsHKVKoq"},"outputs":[],"source":["students = ['jasmin', 'ravi', 'john']"]},{"cell_type":"markdown","metadata":{"id":"HMU5X7XFWbCw"},"source":["3. For each of the print statements below, what do you expect is printed? Run the lines to check predictions."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"u_RWc8wBWgMT"},"outputs":[],"source":["random_fruit = 'pineapple'\n","fruits = ['apple', 'pear', random_fruit]\n","print(fruits)\n","\n","random_fruit = 'blueberry'\n","print(fruits)\n","\n","random_veggie = ['brussel sprouts']\n","veggies = ['broccoli', 'green beans', random_veggie]\n","print(veggies)\n","\n","random_veggie.append('kale')\n","print(veggies)"]},{"cell_type":"markdown","metadata":{"id":"3BfUO-jKS_u1"},"source":["## Exercise 4.2: Bonus\n","\n","Below we introduce another parameter in the list slice. Try to explain what it does."]},{"cell_type":"code","execution_count":2,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":627,"status":"ok","timestamp":1681202305255,"user":{"displayName":"Mees van Stiphout","userId":"10520931415894572279"},"user_tz":-120},"id":"Y9oxyQb7TIPI","outputId":"158288d5-94e0-4068-d13e-af2a5c85177f"},"outputs":[{"name":"stdout","output_type":"stream","text":["['japan', 'hungary', 'maldives', 'gabon', 'bhutan']\n","['japan', 'maldives', 'bhutan']\n","['bhutan', 'gabon', 'maldives', 'hungary', 'japan']\n","['bhutan', 'maldives', 'japan']\n"]}],"source":["countries = ['japan', 'hungary', 'maldives', 'gabon', 'bhutan']\n","\n","print(countries[0:5:1])\n","print(countries[0:5:2])\n","print(countries[-1::-1])\n","print(countries[-1::-2])"]},{"cell_type":"markdown","metadata":{"id":"Mb6CvHt3CaA0"},"source":["The piece of code below is supposed to recognize \"fancy\" words: words that are longer than 5 characters, contain at least one copy of the letter 'a' and start with an uppercase. However, the code is broken. It does not recognize any of our fancy example words.\n","\n","1. Change the value of `word` into each of the examples in the comments on the first two lines and then run the code. See for yourself that the code considers none of the example words fancy. Try some other words as well.\n","3. Try to understand why the code is giving the wrong result. Can you come up with a word that the code does consider fancy?\n","4. Repair the code so that it gives the right result for all examples, and any other words that you come up with."]},{"cell_type":"code","execution_count":null,"metadata":{"id":"QQyGzsqCCe3o"},"outputs":[],"source":["# fancy: Alhambra, Arthur, Jasmine, Turandot\n","# not so fancy: Jeep, paper, Python, Ada\n","word = 'Alhambra'\n","\n","lengthy = len(word) > 5\n","has_a = 'a' in word\n","first_uppercase = 'A' <= word[1] <= 'Z'\n","\n","if lengthy and has_a and first_uppercase:\n"," print('The word is fancy')\n","else:\n"," print('The word is not so fancy')"]},{"cell_type":"markdown","metadata":{"id":"HiEWGB1V1W4U"},"source":["## Next module\n","\n","[5. Assertions](https://colab.research.google.com/drive/1Nv6vgkH2zjJes7LXUCVhsDwFOGCtTbHM)"]}],"metadata":{"colab":{"provenance":[],"toc_visible":true},"kernelspec":{"display_name":"Python 3","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.10.12"}},"nbformat":4,"nbformat_minor":0}