diff --git a/lectures/Lecture0.3_Functions.ipynb b/lectures/Lecture0.3_Functions.ipynb index 8a07e5a..ed9a0ac 100644 --- a/lectures/Lecture0.3_Functions.ipynb +++ b/lectures/Lecture0.3_Functions.ipynb @@ -20,7 +20,7 @@ }, { "cell_type": "markdown", - "id": "1a618701", + "id": "02f32aac", "metadata": { "slideshow": { "slide_type": "subslide" @@ -36,7 +36,7 @@ }, { "cell_type": "markdown", - "id": "a0e2798c", + "id": "bafdd45d", "metadata": { "slideshow": { "slide_type": "subslide" @@ -48,7 +48,7 @@ }, { "cell_type": "markdown", - "id": "ac00363e", + "id": "63fb8c22", "metadata": {}, "source": [ "If a variable is `None` no value is assigned to it!" @@ -57,7 +57,7 @@ { "cell_type": "code", "execution_count": null, - "id": "adb665b4", + "id": "0f22be20", "metadata": {}, "outputs": [], "source": [ @@ -67,7 +67,7 @@ }, { "cell_type": "markdown", - "id": "b8956d3d", + "id": "794af21b", "metadata": { "slideshow": { "slide_type": "subslide" @@ -80,7 +80,7 @@ { "cell_type": "code", "execution_count": 22, - "id": "3d29fc8b", + "id": "c016a966", "metadata": {}, "outputs": [ { @@ -102,7 +102,7 @@ { "cell_type": "code", "execution_count": 23, - "id": "756ceeba", + "id": "dcfeef9d", "metadata": {}, "outputs": [ { @@ -122,7 +122,7 @@ }, { "cell_type": "markdown", - "id": "5e7da0db", + "id": "b0da8ad3", "metadata": { "slideshow": { "slide_type": "subslide" @@ -134,7 +134,7 @@ }, { "cell_type": "markdown", - "id": "e9139877", + "id": "79a7ce4c", "metadata": { "slideshow": { "slide_type": "-" @@ -147,7 +147,7 @@ { "cell_type": "code", "execution_count": 6, - "id": "f6cea7d9", + "id": "2ff29b35", "metadata": { "slideshow": { "slide_type": "subslide" @@ -161,7 +161,7 @@ { "cell_type": "code", "execution_count": 7, - "id": "f205fd11", + "id": "9320ffa4", "metadata": {}, "outputs": [ { @@ -182,7 +182,7 @@ { "cell_type": "code", "execution_count": 8, - "id": "306d5fcc", + "id": "b02611dc", "metadata": {}, "outputs": [ { @@ -203,7 +203,7 @@ { "cell_type": "code", "execution_count": 9, - "id": "f3b8ddee", + "id": "695e8344", "metadata": {}, "outputs": [ { @@ -238,11 +238,7 @@ "id": "b358df84", "metadata": {}, "source": [ - "With `while` we can keep repeating code until one condition is met.\n", - "\n", - "Sometimes we can use alternatively `for` or `while`, but in general:\n", - " - we use `for` when we know in advance the number of iterations\n", - " - we use `while` if we do not know number of iterations beforehand\n" + "With `while` we can keep repeating code until one condition is met." ] }, { @@ -311,6 +307,16 @@ " print(\"Flip: \", coin_flip)" ] }, + { + "cell_type": "markdown", + "id": "ef8e473c", + "metadata": {}, + "source": [ + "Sometimes we can use alternatively `for` or `while`, but in general:\n", + " - we use `for` when we know in advance the number of iterations\n", + " - we use `while` if we do not know number of iterations beforehand" + ] + }, { "cell_type": "markdown", "id": "e3fbba40", @@ -988,7 +994,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 16, "id": "fee83430", "metadata": { "slideshow": { @@ -1000,8 +1006,9 @@ "name": "stdout", "output_type": "stream", "text": [ - "Input: 2; result: 4\n", - "Input: 2; result: 22\n" + "Input: 2 (); result: 4\n", + "Input: 2.0 (); result: 4.0\n", + "Input: 2 (); result: 22\n" ] } ], @@ -1012,8 +1019,8 @@ " return input_number * 2\n", "\n", "# Test different inputs to the function:\n", - "for input_to_test in [2, \"2\"]:\n", - " print(f\"Input: {input_to_test}; result: {multiply_numbers(input_to_test)}\")" + "for input_to_test in [2, 2., \"2\"]:\n", + " print(f\"Input: {input_to_test} ({type(input_to_test)}); result: {multiply_numbers(input_to_test)}\")" ] }, { @@ -1098,44 +1105,6 @@ "In this way, smart IDEs can tell us when we code if there is something strange, and type checkers can be run on code to make sure functions are called with the correct inputs." ] }, - { - "cell_type": "markdown", - "id": "b0d97d17", - "metadata": {}, - "source": [] - }, - { - "cell_type": "markdown", - "id": "dec5a722", - "metadata": { - "slideshow": { - "slide_type": "subslide" - } - }, - "source": [ - "# Imported functions" - ] - }, - { - "cell_type": "markdown", - "id": "68c62379", - "metadata": {}, - "source": [ - "Many times you will import functions from external modules, like `random`. In those cases, you can be sure that the functions docs give you all the information you need about how to use the function:" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "8c383931", - "metadata": {}, - "outputs": [], - "source": [ - "import random\n", - "\n", - "?random.randint" - ] - }, { "cell_type": "markdown", "id": "fbf8fc87", @@ -1366,16 +1335,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "id": "e648b1c4", "metadata": { "slideshow": { "slide_type": "subslide" } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Object `exponentiate` not found.\n" + ] + } + ], "source": [ - "# We can read out documentation also for our custom functions:\n", + "# We can read out documentation for our custom functions:\n", "?exponentiate" ] }, @@ -1486,6 +1463,8 @@ } }, "source": [ + "## Side effects\n", + "\n", "**Avoid sides effects!** Do not modify the passed values or variables existing ouside the function scope. In the cases you do it, do it consciously and make it very explicit in the name of the function or in the docstring!" ] }, @@ -1518,6 +1497,38 @@ "print(a_list)" ] }, + { + "cell_type": "markdown", + "id": "e8b536d5", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "# Imported functions" + ] + }, + { + "cell_type": "markdown", + "id": "bedcbdf1", + "metadata": {}, + "source": [ + "Many times you will import functions from external modules, like `random`. the functions docs give you all the information you need about how to use the function:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "486b2f25", + "metadata": {}, + "outputs": [], + "source": [ + "import random\n", + "\n", + "?random.randint" + ] + }, { "cell_type": "markdown", "id": "24911992", diff --git a/practicals/Practicals_0.3.ipynb b/practicals/Practicals_0.3.ipynb index df42aa8..8fe5f9e 100644 --- a/practicals/Practicals_0.3.ipynb +++ b/practicals/Practicals_0.3.ipynb @@ -7,7 +7,7 @@ "source": [ "# Practicals for lecture 0.3\n", "\n", - "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/vigji/python-cimec/blob/main/practicals/Practicals_0.3.ipynb)" + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/vigji/python-cimec-2024/blob/main/practicals/Practicals_0.3.ipynb)" ] }, { @@ -33,8 +33,9 @@ "metadata": {}, "outputs": [], "source": [ - "# Write a while loop over integer numbers that terminates \n", - "# when you encounter a multiple of both 10 and 12:\n", + "# Write a while loop over integer numbers incrementing the i \n", + "# variably by one at every iteration, and stop when i\n", + "# is a multiple of both 10 and 12:\n", "\n", "\n", "\n"