{ "cells": [ { "cell_type": "markdown", "id": "f36e1aec", "metadata": {}, "source": [ "# Functions (Part 2)" ] }, { "cell_type": "markdown", "id": "341150ad", "metadata": {}, "source": [ "## How many `x` variables? Which ones?" ] }, { "cell_type": "code", "execution_count": null, "id": "918d644b", "metadata": {}, "outputs": [], "source": [ "def square(x):\n", " return x * x\n", "\n", "def abs(x):\n", " if x < 0:\n", " return -x\n", " else:\n", " return x\n", "\n", "# main program\n", "x = 42\n", "sq3 = square(3)\n", "sq4 = square(4)\n", "print(sq3 + sq4)\n", "print(x)\n", "x = -22\n", "result = abs(x)\n", "print(result)" ] }, { "cell_type": "markdown", "id": "992b7fb6", "metadata": {}, "source": [ "In the preceding code, there are really only three xes: one in the global scope, one for `square`, and one for `abs`.\n", "\n", "Here's another example that's no different, where there are three different scopes, each with their own `x`. The only major difference is that one function calls the other." ] }, { "cell_type": "code", "execution_count": null, "id": "c9a449c0", "metadata": {}, "outputs": [], "source": [ "x = 42\n", "\n", "def abs_square(x):\n", " return abs(x * x)\n", "\n", "def abs(n):\n", " x = n\n", " if x < 0:\n", " return -x\n", " else:\n", " return x\n", "\n", "sq = abs_square(-4)\n", "print(sq)\n", "print(x)\n", "x = -22\n", "result = abs(x)\n", "print(result)" ] }, { "cell_type": "markdown", "id": "c4509203", "metadata": {}, "source": [ "Here are a few more examples to work through:" ] }, { "cell_type": "code", "execution_count": null, "id": "926f9807", "metadata": {}, "outputs": [], "source": [ "def do_stuff(a):\n", " print(\"a is \" + a)\n", "\n", "do_stuff(\"here\")" ] }, { "cell_type": "code", "execution_count": null, "id": "597921a7", "metadata": {}, "outputs": [], "source": [ "def do_stuff(a, b):\n", " return a ** b\n", "\n", "a = 4\n", "b = 2\n", "do_stuff(b, a)" ] }, { "cell_type": "code", "execution_count": null, "id": "10ffe9a6", "metadata": {}, "outputs": [], "source": [ "def do_stuff(a, b):\n", " return a ** b\n", "\n", "do_stuff(4)" ] }, { "cell_type": "markdown", "id": "a9e8a80b", "metadata": {}, "source": [ "Finally, what's the difference between the following two examples:" ] }, { "cell_type": "code", "execution_count": null, "id": "fe370427", "metadata": {}, "outputs": [], "source": [ "def thing_one(s):\n", " print(\"Hello \" + s)\n", "\n", "thing_one(\"World!\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "b7b111bc", "metadata": {}, "outputs": [], "source": [ "def thing_two(s):\n", " return \"Hello \" + s\n", "\n", "x = thing_two(\"World!\")" ] }, { "cell_type": "markdown", "id": "ea16ed07", "metadata": {}, "source": [ "## Lists and Functions" ] }, { "cell_type": "code", "execution_count": null, "id": "7a571609", "metadata": {}, "outputs": [], "source": [ "def add_to_list(nums, num):\n", " if num * 0 == 0:\n", " nums.append(num)\n", " elif num * 0 == []:\n", " nums.extend(num)\n", "\n", "nums = [3, 1, 4, 1, 5, 9]\n", "add_to_list(nums, 2)\n", "print(nums)\n", "\n", "add_to_list(nums, [6, 5, 3, 5, 8, 9])\n", "print(nums)" ] }, { "cell_type": "code", "execution_count": null, "id": "d2663855", "metadata": {}, "outputs": [], "source": [ "def negate_first(nums):\n", " nums[0] *= -1\n", "\n", "nums = [3, 1, 4, 1, 5, 9]\n", "negate_first(nums)\n", "print(nums)" ] }, { "cell_type": "code", "execution_count": null, "id": "c63790b5", "metadata": {}, "outputs": [], "source": [ "def square_all(nums):\n", " result = nums\n", " for n in nums:\n", " result[n] = n ** 2\n", " return result\n", "\n", "nums = [0, 1, 2, 3]\n", "squares = square_all(nums)\n", "print(squares)" ] }, { "cell_type": "code", "execution_count": null, "id": "33ae701a", "metadata": {}, "outputs": [], "source": [ "# TODO fix the above" ] }, { "cell_type": "code", "execution_count": null, "id": "89003d3a", "metadata": {}, "outputs": [], "source": [ "def change1(lst):\n", " lst = lst + [99]\n", "\n", "def change2(lst): \n", " lst[0] = 13\n", "\n", "def change3(lst): \n", " lst.append(99) \n", "\n", "lst2 = [1, 2]\n", "change1(lst2)\n", "change2(lst2)\n", "change3(lst2)" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "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.13.3" } }, "nbformat": 4, "nbformat_minor": 5 }