diff --git a/pythonintro/DataTypes.ipynb b/pythonintro/DataTypes.ipynb index 792313a5a38d5a49991227280d2471c397f1e401..382031aa392bce3b3650e46c9a94551bbc0bc0fa 100644 --- a/pythonintro/DataTypes.ipynb +++ b/pythonintro/DataTypes.ipynb @@ -137,11 +137,19 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": { "id": "yzJ9yfQfIXda" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "None\n" + ] + } + ], "source": [ "my_list = [0,3,2,6,3,2,1,7,8,7]\n", "\n", diff --git a/pythonintro/FirstSteps.ipynb b/pythonintro/FirstSteps.ipynb index 626541c6d27d09cd288bf4ab8d91f60d2f4e561c..20130e7750b8c9cbb0f9f77e7783af9149c97a40 100644 --- a/pythonintro/FirstSteps.ipynb +++ b/pythonintro/FirstSteps.ipynb @@ -105,7 +105,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -114,7 +114,7 @@ "34" ] }, - "execution_count": 4, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -133,7 +133,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -153,7 +153,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -162,7 +162,7 @@ "int" ] }, - "execution_count": 6, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -192,7 +192,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -209,7 +209,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -236,7 +236,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -266,7 +266,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -291,7 +291,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -341,7 +341,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -381,7 +381,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -410,7 +410,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -445,7 +445,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -464,6 +464,11 @@ "print('The circumference is {} cm.'.format(circumference))" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] + }, { "cell_type": "markdown", "metadata": {}, diff --git a/pythonintro/Functions.ipynb b/pythonintro/Functions.ipynb index c01784e3d45ded585c31333a951b9cadc0a88507..bf59e3caff05d471df40420a57dcd586091918d5 100644 --- a/pythonintro/Functions.ipynb +++ b/pythonintro/Functions.ipynb @@ -419,7 +419,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -437,6 +437,7 @@ ] }, { + "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ @@ -446,28 +447,90 @@ "\n", "The key thing about recursive functions is to define a suitable stopping point at which the function no longer calls itself. Otherwise, we would call the functions repeatedly in an endless loop.\n", "\n", - "#### Exercise\n", + "#### Demo\n", "\n", "Remember the definition of the Fibonacci numbers we have encountered earlier: \\\n", "The [Fibonacci Numbers](https://en.wikipedia.org/wiki/Fibonacci_number) are a sequence where the current number is derived from the sum of the two preceeding ones, i.e. $F_n = F_{n-1} + F_{n-2}$. The first two numbers are $F_1 = 0$ and $F_2 = 1$.\n", "\n", "This already includes a recursive definition: $F_n = F_{n-1} + F_{n-2}$, apart from $F_1 = 0$ and $F_2 = 1$.\n", "\n", - "Write a recursive functions to calculate the $F_n$ Fibonacci number and compute the first 10 numbers.\n", - "\n", - "\n", - "#### Exercise 2\n", + "Write a recursive functions to calculate the $F_n$ Fibonacci number and compute the first 10 numbers." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the Fibonacci number is: 0\n", + "the Fibonacci number is: 1\n", + "the Fibonacci number is: 1\n", + "the Fibonacci number is: 2\n", + "the Fibonacci number is: 3\n", + "the Fibonacci number is: 5\n", + "the Fibonacci number is: 8\n", + "the Fibonacci number is: 13\n", + "the Fibonacci number is: 21\n", + "the Fibonacci number is: 34\n" + ] + } + ], + "source": [ + "def Fibonacci(n):\n", + " if n < 0: # not defined for negative numbers\n", + " return None\n", + " if n <= 1: # first two numbers are 0, 1, stopping criterion\n", + " return n\n", + " else: # recursive call\n", + " return Fibonacci(n-1) + Fibonacci(n-2)\n", + "\n", + "for i in range(0,10):\n", + " fib = Fibonacci(i)\n", + " print('the Fibonacci number is: {}'.format(fib))" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Demo\n", "\n", - "Rewrite this recursive function as a one-line Lambda function.\n" + "We can rewrite this recursive function as a one-line Lambda function." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the Fibonacci number is: 0\n", + "the Fibonacci number is: 1\n", + "the Fibonacci number is: 1\n", + "the Fibonacci number is: 2\n", + "the Fibonacci number is: 3\n", + "the Fibonacci number is: 5\n", + "the Fibonacci number is: 8\n", + "the Fibonacci number is: 13\n", + "the Fibonacci number is: 21\n", + "the Fibonacci number is: 34\n" + ] + } + ], "source": [ - "# ... your code here ...." + "Fibonacci = lambda n: n if n <= 1 else Fibonacci(n-1) + Fibonacci(n-2)\n", + "\n", + "for i in range(0,10):\n", + " fib = Fibonacci(i)\n", + " print('the Fibonacci number is: {}'.format(fib))\n" ] } ], diff --git a/pythonintro/PandasIntro.ipynb b/pythonintro/PandasIntro.ipynb index 2074b9ad9b3ddd5db1d2e8a04ea3256243a4459d..f4c07308d532d7b13faf97963f73bf93cee69773 100644 --- a/pythonintro/PandasIntro.ipynb +++ b/pythonintro/PandasIntro.ipynb @@ -53,7 +53,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -160,7 +160,7 @@ "4 5.0 3.6 1.4 0.2 setosa" ] }, - "execution_count": 4, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -182,7 +182,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -216,7 +216,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -248,7 +248,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -282,7 +282,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 6, "metadata": {}, "outputs": [ { diff --git a/pythonintro/solutions/Solutions_Functions.ipynb b/pythonintro/solutions/Solutions_Functions.ipynb index 02ab97d36ea3acb684c415ef6ca65e2e2b9a45e8..c27375bc95f6f66861c2297a44615acfb1d3b8d1 100644 --- a/pythonintro/solutions/Solutions_Functions.ipynb +++ b/pythonintro/solutions/Solutions_Functions.ipynb @@ -39,93 +39,6 @@ "print('The Fibonacci numbers are: {}'.format(Fib))\n" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Recursive Function\n", - "\n", - "Variant 1:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "the Fibonacci number is: 0\n", - "the Fibonacci number is: 1\n", - "the Fibonacci number is: 1\n", - "the Fibonacci number is: 2\n", - "the Fibonacci number is: 3\n", - "the Fibonacci number is: 5\n", - "the Fibonacci number is: 8\n", - "the Fibonacci number is: 13\n", - "the Fibonacci number is: 21\n", - "the Fibonacci number is: 34\n" - ] - } - ], - "source": [ - "def Fibonacci(n):\n", - " if n < 0: # not defined for negative numbers\n", - " return None\n", - " if n <= 1: # first two numbers are 0, 1, stopping criterion\n", - " return n\n", - " else: # recursive call\n", - " return Fibonacci(n-1) + Fibonacci(n-2)\n", - "\n", - "for i in range(0,10):\n", - " fib = Fibonacci(i)\n", - " print('the Fibonacci number is: {}'.format(fib))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Variant 2: Lambda function\n", - "\n", - "**N.B.** \\\n", - "While this one-line definition is very compact, we no longer have a safe-guard against, e.g. negative numbers.\n", - "Depending on our use-case, this definition is very helpful, but we need to be sure that we do not run into unexpected cases." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "the Fibonacci number is: 0\n", - "the Fibonacci number is: 1\n", - "the Fibonacci number is: 1\n", - "the Fibonacci number is: 2\n", - "the Fibonacci number is: 3\n", - "the Fibonacci number is: 5\n", - "the Fibonacci number is: 8\n", - "the Fibonacci number is: 13\n", - "the Fibonacci number is: 21\n", - "the Fibonacci number is: 34\n" - ] - } - ], - "source": [ - "# fib = lambda x: x if x <= 1 else fib(x - 1) + fib(x - 2)\n", - "Fibonacci = lambda n: n if n <= 1 else Fibonacci(n-1) + Fibonacci(n-2)\n", - "\n", - "for i in range(0,10):\n", - " fib = Fibonacci(i)\n", - " print('the Fibonacci number is: {}'.format(fib))\n" - ] - }, { "cell_type": "markdown", "metadata": {},