diff --git a/08.decorators.ipynb b/08.decorators.ipynb index 8604af4..aa463fb 100644 --- a/08.decorators.ipynb +++ b/08.decorators.ipynb @@ -131,7 +131,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "1e9d6428-8f80-4707-b508-c15878459d1f", "metadata": {}, "outputs": [], @@ -159,6 +159,7 @@ "def expensive_calculation(a, b, *, c=0):\n", " print(f\"doing expensive calculation on {a} {b}...\")\n", " return a ** b\n", + "#expensive_calculation = cache(expensive_calculation)\n", "\n", "@cache\n", "def cheap_calculation(a, b):\n", @@ -230,53 +231,95 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "id": "b6da10be-562c-4968-86ed-c473db63eccf", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "TypeError", + "evalue": "repeatN() missing 1 required positional argument: 'n'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[31], line 8\u001b[0m\n\u001b[1;32m 4\u001b[0m func(a, b, c)\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m newfunc\n\u001b[1;32m 7\u001b[0m \u001b[38;5;129;43m@repeatN\u001b[39;49m\n\u001b[0;32m----> 8\u001b[0m \u001b[38;5;28;43;01mdef\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;21;43mprint_sum\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\u001b[43m:\u001b[49m\n\u001b[1;32m 9\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mprint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43msum\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: repeatN() missing 1 required positional argument: 'n'" + ] + } + ], "source": [ "def repeat5(func): # the decorator \n", " def newfunc(a, b, c): # the inner function\n", - " for i in range(5):\n", - " print(a, b, c)\n", + " return func(a, b, c, c, b, a)\n", " return newfunc\n", "\n", - "@repeat5 # the wrapped function\n", + "@repeatN\n", "def print_sum(*args):\n", - " print(sum(args))" + " print(sum(args))\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "id": "7f43e771-92d9-48d3-96ed-b95b1c94e942", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6\n", + "6\n", + "6\n", + "6\n", + "6\n" + ] + } + ], "source": [ "print_sum(1, 2, 3)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "4327b95d-b9ab-49e3-a530-b34e0688cff6", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "sdrawkcab\n", + "sdrawkcab\n", + "sdrawkcab\n", + "sdrawkcab\n", + "sdrawkcab\n", + "sdrawkcab\n", + "sdrawkcab\n", + "sdrawkcab\n", + "sdrawkcab\n", + "sdrawkcab\n" + ] + } + ], "source": [ "# to make a decorator that takes additional arguments\n", "# you need to write a decorator factory function that returns decorators\n", "\n", - "def repeat(n): # factory: takes integer, returns decorator\n", + "total = 123\n", + "\n", + "def repeat(start, end): # factory: takes integer, returns decorator\n", " def repeat_decorator(func): # decorator: takes function, returns function\n", " def newfunc(*args, **kwargs): # inner function: takes ?, returns ?\n", - " for i in range(n):\n", + " for i in range(total):\n", " func(*args, **kwargs)\n", " return newfunc\n", " return repeat_decorator\n", "\n", "@repeat(10)\n", "def print_backwards(s):\n", - " print(\"backwards\")\n", + " print(s[::-1])\n", "\n", "print_backwards(\"backwards\")" ] @@ -306,7 +349,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "id": "fb9e74b4-a0a2-4463-add7-45b04430edcd", "metadata": {}, "outputs": [], @@ -317,47 +360,89 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "id": "e64ec996-4085-4c03-a771-6c1073b04990", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello, Scott, Paul, Lauren\n" + ] + } + ], "source": [ - "print_hello_names(\"Scott\", \"Paul\", \"Lauren\")" + "print_hello_names(\"Scott\", \"Paul\", \"Lauren\")\n", + "# same as print(\"Hello\", \"Scott\", \"Paul\", \"Lauren\", sep=\", \")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "id": "eb0d1ad3-e636-4430-9139-265740adc8a1", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "('Hello',)" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "print_hello_names.args" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "id": "9e67124b-9014-49d9-af86-54b8a617521f", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'sep': ', '}" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "print_hello_names.keywords" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "id": "f4939684-6c92-4c07-8d88-f7c4349d0f02", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "print_hello_names.func" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "id": "1ed27205-7691-46ee-bdc6-f75dae7d53da", "metadata": {}, "outputs": [], @@ -367,28 +452,39 @@ " def newfunc(*args, **kwargs):\n", " return func(*args, **kwargs)\n", " # we can do whatever we like after defining newfunc, but before returning it\n", - " newfunc.is_wrapped = True\n", + " newfunc.xyz = \"hello\"*2\n", " return newfunc" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "id": "8e02bb23-7278-4799-85d6-9004af65565b", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# property is assigned to all wrapped functions\n", "@wrapper\n", "def our_function():\n", " print(\"inside our function\")\n", "\n", - "our_function.is_wrapped" + "our_function.xyz" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "id": "93b1ad42-b4a2-4947-a4bb-19c818968e2c", "metadata": {}, "outputs": [], @@ -406,41 +502,101 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "id": "092b1a1d-4300-4895-aa06-4d874bc61159", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Scott, Paul, Lauren, Hello!" + ] + } + ], "source": [ "print_hello_names2 = our_partial(print, \"Hello\", sep=\", \")\n", - "print_hello_names2(\"Scott\", \"Paul\", \"Lauren\")" + "print_hello_names2(\"Scott\", \"Paul\", \"Lauren\", end=\"!\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, + "id": "a0bd355b-63d5-41e2-8238-2e3dff22cb45", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello?Scott?Paul?Lauren!" + ] + } + ], + "source": [ + "#print_hello_names2 = our_partial(print, \"Hello\", sep=\", \")\n", + "print_hello_names2(\"Scott\", \"Paul\", \"Lauren\", end=\"!\", sep=\"?\")" + ] + }, + { + "cell_type": "code", + "execution_count": 25, "id": "c48a5dfb-0444-4f5e-a4bc-39f0770ff5a3", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "('Hello',)" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "print_hello_names2.args" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "id": "f5d68812-918b-42dd-b23f-925a692668aa", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'sep': ', '}" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "print_hello_names2.keywords" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "id": "6b41b480-43e1-49b2-ade7-44d8aa484a09", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "print_hello_names2.func" ] diff --git a/09.modules.ipynb b/09.modules.ipynb index e035c1d..eb40373 100644 --- a/09.modules.ipynb +++ b/09.modules.ipynb @@ -75,15 +75,676 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "id": "4d5f6e18", "metadata": { "tags": [] }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on module statistics:\n", + "\n", + "NAME\n", + " statistics - Basic statistics module.\n", + "\n", + "MODULE REFERENCE\n", + " https://docs.python.org/3.10/library/statistics.html\n", + " \n", + " The following documentation is automatically generated from the Python\n", + " source files. It may be incomplete, incorrect or include features that\n", + " are considered implementation detail and may vary between Python\n", + " implementations. When in doubt, consult the module reference at the\n", + " location listed above.\n", + "\n", + "DESCRIPTION\n", + " This module provides functions for calculating statistics of data, including\n", + " averages, variance, and standard deviation.\n", + " \n", + " Calculating averages\n", + " --------------------\n", + " \n", + " ================== ==================================================\n", + " Function Description\n", + " ================== ==================================================\n", + " mean Arithmetic mean (average) of data.\n", + " fmean Fast, floating point arithmetic mean.\n", + " geometric_mean Geometric mean of data.\n", + " harmonic_mean Harmonic mean of data.\n", + " median Median (middle value) of data.\n", + " median_low Low median of data.\n", + " median_high High median of data.\n", + " median_grouped Median, or 50th percentile, of grouped data.\n", + " mode Mode (most common value) of data.\n", + " multimode List of modes (most common values of data).\n", + " quantiles Divide data into intervals with equal probability.\n", + " ================== ==================================================\n", + " \n", + " Calculate the arithmetic mean (\"the average\") of data:\n", + " \n", + " >>> mean([-1.0, 2.5, 3.25, 5.75])\n", + " 2.625\n", + " \n", + " \n", + " Calculate the standard median of discrete data:\n", + " \n", + " >>> median([2, 3, 4, 5])\n", + " 3.5\n", + " \n", + " \n", + " Calculate the median, or 50th percentile, of data grouped into class intervals\n", + " centred on the data values provided. E.g. if your data points are rounded to\n", + " the nearest whole number:\n", + " \n", + " >>> median_grouped([2, 2, 3, 3, 3, 4]) #doctest: +ELLIPSIS\n", + " 2.8333333333...\n", + " \n", + " This should be interpreted in this way: you have two data points in the class\n", + " interval 1.5-2.5, three data points in the class interval 2.5-3.5, and one in\n", + " the class interval 3.5-4.5. The median of these data points is 2.8333...\n", + " \n", + " \n", + " Calculating variability or spread\n", + " ---------------------------------\n", + " \n", + " ================== =============================================\n", + " Function Description\n", + " ================== =============================================\n", + " pvariance Population variance of data.\n", + " variance Sample variance of data.\n", + " pstdev Population standard deviation of data.\n", + " stdev Sample standard deviation of data.\n", + " ================== =============================================\n", + " \n", + " Calculate the standard deviation of sample data:\n", + " \n", + " >>> stdev([2.5, 3.25, 5.5, 11.25, 11.75]) #doctest: +ELLIPSIS\n", + " 4.38961843444...\n", + " \n", + " If you have previously calculated the mean, you can pass it as the optional\n", + " second argument to the four \"spread\" functions to avoid recalculating it:\n", + " \n", + " >>> data = [1, 2, 2, 4, 4, 4, 5, 6]\n", + " >>> mu = mean(data)\n", + " >>> pvariance(data, mu)\n", + " 2.5\n", + " \n", + " \n", + " Statistics for relations between two inputs\n", + " -------------------------------------------\n", + " \n", + " ================== ====================================================\n", + " Function Description\n", + " ================== ====================================================\n", + " covariance Sample covariance for two variables.\n", + " correlation Pearson's correlation coefficient for two variables.\n", + " linear_regression Intercept and slope for simple linear regression.\n", + " ================== ====================================================\n", + " \n", + " Calculate covariance, Pearson's correlation, and simple linear regression\n", + " for two inputs:\n", + " \n", + " >>> x = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + " >>> y = [1, 2, 3, 1, 2, 3, 1, 2, 3]\n", + " >>> covariance(x, y)\n", + " 0.75\n", + " >>> correlation(x, y) #doctest: +ELLIPSIS\n", + " 0.31622776601...\n", + " >>> linear_regression(x, y) #doctest:\n", + " LinearRegression(slope=0.1, intercept=1.5)\n", + " \n", + " \n", + " Exceptions\n", + " ----------\n", + " \n", + " A single exception is defined: StatisticsError is a subclass of ValueError.\n", + "\n", + "CLASSES\n", + " builtins.ValueError(builtins.Exception)\n", + " StatisticsError\n", + " builtins.object\n", + " NormalDist\n", + " \n", + " class NormalDist(builtins.object)\n", + " | NormalDist(mu=0.0, sigma=1.0)\n", + " | \n", + " | Normal distribution of a random variable\n", + " | \n", + " | Methods defined here:\n", + " | \n", + " | __add__(x1, x2)\n", + " | Add a constant or another NormalDist instance.\n", + " | \n", + " | If *other* is a constant, translate mu by the constant,\n", + " | leaving sigma unchanged.\n", + " | \n", + " | If *other* is a NormalDist, add both the means and the variances.\n", + " | Mathematically, this works only if the two distributions are\n", + " | independent or if they are jointly normally distributed.\n", + " | \n", + " | __eq__(x1, x2)\n", + " | Two NormalDist objects are equal if their mu and sigma are both equal.\n", + " | \n", + " | __getstate__(self)\n", + " | \n", + " | __hash__(self)\n", + " | NormalDist objects hash equal if their mu and sigma are both equal.\n", + " | \n", + " | __init__(self, mu=0.0, sigma=1.0)\n", + " | NormalDist where mu is the mean and sigma is the standard deviation.\n", + " | \n", + " | __mul__(x1, x2)\n", + " | Multiply both mu and sigma by a constant.\n", + " | \n", + " | Used for rescaling, perhaps to change measurement units.\n", + " | Sigma is scaled with the absolute value of the constant.\n", + " | \n", + " | __neg__(x1)\n", + " | Negates mu while keeping sigma the same.\n", + " | \n", + " | __pos__(x1)\n", + " | Return a copy of the instance.\n", + " | \n", + " | __radd__ = __add__(x1, x2)\n", + " | \n", + " | __repr__(self)\n", + " | Return repr(self).\n", + " | \n", + " | __rmul__ = __mul__(x1, x2)\n", + " | \n", + " | __rsub__(x1, x2)\n", + " | Subtract a NormalDist from a constant or another NormalDist.\n", + " | \n", + " | __setstate__(self, state)\n", + " | \n", + " | __sub__(x1, x2)\n", + " | Subtract a constant or another NormalDist instance.\n", + " | \n", + " | If *other* is a constant, translate by the constant mu,\n", + " | leaving sigma unchanged.\n", + " | \n", + " | If *other* is a NormalDist, subtract the means and add the variances.\n", + " | Mathematically, this works only if the two distributions are\n", + " | independent or if they are jointly normally distributed.\n", + " | \n", + " | __truediv__(x1, x2)\n", + " | Divide both mu and sigma by a constant.\n", + " | \n", + " | Used for rescaling, perhaps to change measurement units.\n", + " | Sigma is scaled with the absolute value of the constant.\n", + " | \n", + " | cdf(self, x)\n", + " | Cumulative distribution function. P(X <= x)\n", + " | \n", + " | inv_cdf(self, p)\n", + " | Inverse cumulative distribution function. x : P(X <= x) = p\n", + " | \n", + " | Finds the value of the random variable such that the probability of\n", + " | the variable being less than or equal to that value equals the given\n", + " | probability.\n", + " | \n", + " | This function is also called the percent point function or quantile\n", + " | function.\n", + " | \n", + " | overlap(self, other)\n", + " | Compute the overlapping coefficient (OVL) between two normal distributions.\n", + " | \n", + " | Measures the agreement between two normal probability distributions.\n", + " | Returns a value between 0.0 and 1.0 giving the overlapping area in\n", + " | the two underlying probability density functions.\n", + " | \n", + " | >>> N1 = NormalDist(2.4, 1.6)\n", + " | >>> N2 = NormalDist(3.2, 2.0)\n", + " | >>> N1.overlap(N2)\n", + " | 0.8035050657330205\n", + " | \n", + " | pdf(self, x)\n", + " | Probability density function. P(x <= X < x+dx) / dx\n", + " | \n", + " | quantiles(self, n=4)\n", + " | Divide into *n* continuous intervals with equal probability.\n", + " | \n", + " | Returns a list of (n - 1) cut points separating the intervals.\n", + " | \n", + " | Set *n* to 4 for quartiles (the default). Set *n* to 10 for deciles.\n", + " | Set *n* to 100 for percentiles which gives the 99 cuts points that\n", + " | separate the normal distribution in to 100 equal sized groups.\n", + " | \n", + " | samples(self, n, *, seed=None)\n", + " | Generate *n* samples for a given mean and standard deviation.\n", + " | \n", + " | zscore(self, x)\n", + " | Compute the Standard Score. (x - mean) / stdev\n", + " | \n", + " | Describes *x* in terms of the number of standard deviations\n", + " | above or below the mean of the normal distribution.\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Class methods defined here:\n", + " | \n", + " | from_samples(data) from builtins.type\n", + " | Make a normal distribution instance from sample data.\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Readonly properties defined here:\n", + " | \n", + " | mean\n", + " | Arithmetic mean of the normal distribution.\n", + " | \n", + " | median\n", + " | Return the median of the normal distribution\n", + " | \n", + " | mode\n", + " | Return the mode of the normal distribution\n", + " | \n", + " | The mode is the value x where which the probability density\n", + " | function (pdf) takes its maximum value.\n", + " | \n", + " | stdev\n", + " | Standard deviation of the normal distribution.\n", + " | \n", + " | variance\n", + " | Square of the standard deviation.\n", + " \n", + " class StatisticsError(builtins.ValueError)\n", + " | Method resolution order:\n", + " | StatisticsError\n", + " | builtins.ValueError\n", + " | builtins.Exception\n", + " | builtins.BaseException\n", + " | builtins.object\n", + " | \n", + " | Data descriptors defined here:\n", + " | \n", + " | __weakref__\n", + " | list of weak references to the object (if defined)\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Methods inherited from builtins.ValueError:\n", + " | \n", + " | __init__(self, /, *args, **kwargs)\n", + " | Initialize self. See help(type(self)) for accurate signature.\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Static methods inherited from builtins.ValueError:\n", + " | \n", + " | __new__(*args, **kwargs) from builtins.type\n", + " | Create and return a new object. See help(type) for accurate signature.\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Methods inherited from builtins.BaseException:\n", + " | \n", + " | __delattr__(self, name, /)\n", + " | Implement delattr(self, name).\n", + " | \n", + " | __getattribute__(self, name, /)\n", + " | Return getattr(self, name).\n", + " | \n", + " | __reduce__(...)\n", + " | Helper for pickle.\n", + " | \n", + " | __repr__(self, /)\n", + " | Return repr(self).\n", + " | \n", + " | __setattr__(self, name, value, /)\n", + " | Implement setattr(self, name, value).\n", + " | \n", + " | __setstate__(...)\n", + " | \n", + " | __str__(self, /)\n", + " | Return str(self).\n", + " | \n", + " | with_traceback(...)\n", + " | Exception.with_traceback(tb) --\n", + " | set self.__traceback__ to tb and return self.\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Data descriptors inherited from builtins.BaseException:\n", + " | \n", + " | __cause__\n", + " | exception cause\n", + " | \n", + " | __context__\n", + " | exception context\n", + " | \n", + " | __dict__\n", + " | \n", + " | __suppress_context__\n", + " | \n", + " | __traceback__\n", + " | \n", + " | args\n", + "\n", + "FUNCTIONS\n", + " correlation(x, y, /)\n", + " Pearson's correlation coefficient\n", + " \n", + " Return the Pearson's correlation coefficient for two inputs. Pearson's\n", + " correlation coefficient *r* takes values between -1 and +1. It measures the\n", + " strength and direction of the linear relationship, where +1 means very\n", + " strong, positive linear relationship, -1 very strong, negative linear\n", + " relationship, and 0 no linear relationship.\n", + " \n", + " >>> x = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + " >>> y = [9, 8, 7, 6, 5, 4, 3, 2, 1]\n", + " >>> correlation(x, x)\n", + " 1.0\n", + " >>> correlation(x, y)\n", + " -1.0\n", + " \n", + " covariance(x, y, /)\n", + " Covariance\n", + " \n", + " Return the sample covariance of two inputs *x* and *y*. Covariance\n", + " is a measure of the joint variability of two inputs.\n", + " \n", + " >>> x = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n", + " >>> y = [1, 2, 3, 1, 2, 3, 1, 2, 3]\n", + " >>> covariance(x, y)\n", + " 0.75\n", + " >>> z = [9, 8, 7, 6, 5, 4, 3, 2, 1]\n", + " >>> covariance(x, z)\n", + " -7.5\n", + " >>> covariance(z, x)\n", + " -7.5\n", + " \n", + " fmean(data)\n", + " Convert data to floats and compute the arithmetic mean.\n", + " \n", + " This runs faster than the mean() function and it always returns a float.\n", + " If the input dataset is empty, it raises a StatisticsError.\n", + " \n", + " >>> fmean([3.5, 4.0, 5.25])\n", + " 4.25\n", + " \n", + " geometric_mean(data)\n", + " Convert data to floats and compute the geometric mean.\n", + " \n", + " Raises a StatisticsError if the input dataset is empty,\n", + " if it contains a zero, or if it contains a negative value.\n", + " \n", + " No special efforts are made to achieve exact results.\n", + " (However, this may change in the future.)\n", + " \n", + " >>> round(geometric_mean([54, 24, 36]), 9)\n", + " 36.0\n", + " \n", + " harmonic_mean(data, weights=None)\n", + " Return the harmonic mean of data.\n", + " \n", + " The harmonic mean is the reciprocal of the arithmetic mean of the\n", + " reciprocals of the data. It can be used for averaging ratios or\n", + " rates, for example speeds.\n", + " \n", + " Suppose a car travels 40 km/hr for 5 km and then speeds-up to\n", + " 60 km/hr for another 5 km. What is the average speed?\n", + " \n", + " >>> harmonic_mean([40, 60])\n", + " 48.0\n", + " \n", + " Suppose a car travels 40 km/hr for 5 km, and when traffic clears,\n", + " speeds-up to 60 km/hr for the remaining 30 km of the journey. What\n", + " is the average speed?\n", + " \n", + " >>> harmonic_mean([40, 60], weights=[5, 30])\n", + " 56.0\n", + " \n", + " If ``data`` is empty, or any element is less than zero,\n", + " ``harmonic_mean`` will raise ``StatisticsError``.\n", + " \n", + " linear_regression(x, y, /)\n", + " Slope and intercept for simple linear regression.\n", + " \n", + " Return the slope and intercept of simple linear regression\n", + " parameters estimated using ordinary least squares. Simple linear\n", + " regression describes relationship between an independent variable\n", + " *x* and a dependent variable *y* in terms of linear function:\n", + " \n", + " y = slope * x + intercept + noise\n", + " \n", + " where *slope* and *intercept* are the regression parameters that are\n", + " estimated, and noise represents the variability of the data that was\n", + " not explained by the linear regression (it is equal to the\n", + " difference between predicted and actual values of the dependent\n", + " variable).\n", + " \n", + " The parameters are returned as a named tuple.\n", + " \n", + " >>> x = [1, 2, 3, 4, 5]\n", + " >>> noise = NormalDist().samples(5, seed=42)\n", + " >>> y = [3 * x[i] + 2 + noise[i] for i in range(5)]\n", + " >>> linear_regression(x, y) #doctest: +ELLIPSIS\n", + " LinearRegression(slope=3.09078914170..., intercept=1.75684970486...)\n", + " \n", + " mean(data)\n", + " Return the sample arithmetic mean of data.\n", + " \n", + " >>> mean([1, 2, 3, 4, 4])\n", + " 2.8\n", + " \n", + " >>> from fractions import Fraction as F\n", + " >>> mean([F(3, 7), F(1, 21), F(5, 3), F(1, 3)])\n", + " Fraction(13, 21)\n", + " \n", + " >>> from decimal import Decimal as D\n", + " >>> mean([D(\"0.5\"), D(\"0.75\"), D(\"0.625\"), D(\"0.375\")])\n", + " Decimal('0.5625')\n", + " \n", + " If ``data`` is empty, StatisticsError will be raised.\n", + " \n", + " median(data)\n", + " Return the median (middle value) of numeric data.\n", + " \n", + " When the number of data points is odd, return the middle data point.\n", + " When the number of data points is even, the median is interpolated by\n", + " taking the average of the two middle values:\n", + " \n", + " >>> median([1, 3, 5])\n", + " 3\n", + " >>> median([1, 3, 5, 7])\n", + " 4.0\n", + " \n", + " median_grouped(data, interval=1)\n", + " Return the 50th percentile (median) of grouped continuous data.\n", + " \n", + " >>> median_grouped([1, 2, 2, 3, 4, 4, 4, 4, 4, 5])\n", + " 3.7\n", + " >>> median_grouped([52, 52, 53, 54])\n", + " 52.5\n", + " \n", + " This calculates the median as the 50th percentile, and should be\n", + " used when your data is continuous and grouped. In the above example,\n", + " the values 1, 2, 3, etc. actually represent the midpoint of classes\n", + " 0.5-1.5, 1.5-2.5, 2.5-3.5, etc. The middle value falls somewhere in\n", + " class 3.5-4.5, and interpolation is used to estimate it.\n", + " \n", + " Optional argument ``interval`` represents the class interval, and\n", + " defaults to 1. Changing the class interval naturally will change the\n", + " interpolated 50th percentile value:\n", + " \n", + " >>> median_grouped([1, 3, 3, 5, 7], interval=1)\n", + " 3.25\n", + " >>> median_grouped([1, 3, 3, 5, 7], interval=2)\n", + " 3.5\n", + " \n", + " This function does not check whether the data points are at least\n", + " ``interval`` apart.\n", + " \n", + " median_high(data)\n", + " Return the high median of data.\n", + " \n", + " When the number of data points is odd, the middle value is returned.\n", + " When it is even, the larger of the two middle values is returned.\n", + " \n", + " >>> median_high([1, 3, 5])\n", + " 3\n", + " >>> median_high([1, 3, 5, 7])\n", + " 5\n", + " \n", + " median_low(data)\n", + " Return the low median of numeric data.\n", + " \n", + " When the number of data points is odd, the middle value is returned.\n", + " When it is even, the smaller of the two middle values is returned.\n", + " \n", + " >>> median_low([1, 3, 5])\n", + " 3\n", + " >>> median_low([1, 3, 5, 7])\n", + " 3\n", + " \n", + " mode(data)\n", + " Return the most common data point from discrete or nominal data.\n", + " \n", + " ``mode`` assumes discrete data, and returns a single value. This is the\n", + " standard treatment of the mode as commonly taught in schools:\n", + " \n", + " >>> mode([1, 1, 2, 3, 3, 3, 3, 4])\n", + " 3\n", + " \n", + " This also works with nominal (non-numeric) data:\n", + " \n", + " >>> mode([\"red\", \"blue\", \"blue\", \"red\", \"green\", \"red\", \"red\"])\n", + " 'red'\n", + " \n", + " If there are multiple modes with same frequency, return the first one\n", + " encountered:\n", + " \n", + " >>> mode(['red', 'red', 'green', 'blue', 'blue'])\n", + " 'red'\n", + " \n", + " If *data* is empty, ``mode``, raises StatisticsError.\n", + " \n", + " multimode(data)\n", + " Return a list of the most frequently occurring values.\n", + " \n", + " Will return more than one result if there are multiple modes\n", + " or an empty list if *data* is empty.\n", + " \n", + " >>> multimode('aabbbbbbbbcc')\n", + " ['b']\n", + " >>> multimode('aabbbbccddddeeffffgg')\n", + " ['b', 'd', 'f']\n", + " >>> multimode('')\n", + " []\n", + " \n", + " pstdev(data, mu=None)\n", + " Return the square root of the population variance.\n", + " \n", + " See ``pvariance`` for arguments and other details.\n", + " \n", + " >>> pstdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])\n", + " 0.986893273527251\n", + " \n", + " pvariance(data, mu=None)\n", + " Return the population variance of ``data``.\n", + " \n", + " data should be a sequence or iterable of Real-valued numbers, with at least one\n", + " value. The optional argument mu, if given, should be the mean of\n", + " the data. If it is missing or None, the mean is automatically calculated.\n", + " \n", + " Use this function to calculate the variance from the entire population.\n", + " To estimate the variance from a sample, the ``variance`` function is\n", + " usually a better choice.\n", + " \n", + " Examples:\n", + " \n", + " >>> data = [0.0, 0.25, 0.25, 1.25, 1.5, 1.75, 2.75, 3.25]\n", + " >>> pvariance(data)\n", + " 1.25\n", + " \n", + " If you have already calculated the mean of the data, you can pass it as\n", + " the optional second argument to avoid recalculating it:\n", + " \n", + " >>> mu = mean(data)\n", + " >>> pvariance(data, mu)\n", + " 1.25\n", + " \n", + " Decimals and Fractions are supported:\n", + " \n", + " >>> from decimal import Decimal as D\n", + " >>> pvariance([D(\"27.5\"), D(\"30.25\"), D(\"30.25\"), D(\"34.5\"), D(\"41.75\")])\n", + " Decimal('24.815')\n", + " \n", + " >>> from fractions import Fraction as F\n", + " >>> pvariance([F(1, 4), F(5, 4), F(1, 2)])\n", + " Fraction(13, 72)\n", + " \n", + " quantiles(data, *, n=4, method='exclusive')\n", + " Divide *data* into *n* continuous intervals with equal probability.\n", + " \n", + " Returns a list of (n - 1) cut points separating the intervals.\n", + " \n", + " Set *n* to 4 for quartiles (the default). Set *n* to 10 for deciles.\n", + " Set *n* to 100 for percentiles which gives the 99 cuts points that\n", + " separate *data* in to 100 equal sized groups.\n", + " \n", + " The *data* can be any iterable containing sample.\n", + " The cut points are linearly interpolated between data points.\n", + " \n", + " If *method* is set to *inclusive*, *data* is treated as population\n", + " data. The minimum value is treated as the 0th percentile and the\n", + " maximum value is treated as the 100th percentile.\n", + " \n", + " stdev(data, xbar=None)\n", + " Return the square root of the sample variance.\n", + " \n", + " See ``variance`` for arguments and other details.\n", + " \n", + " >>> stdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])\n", + " 1.0810874155219827\n", + " \n", + " variance(data, xbar=None)\n", + " Return the sample variance of data.\n", + " \n", + " data should be an iterable of Real-valued numbers, with at least two\n", + " values. The optional argument xbar, if given, should be the mean of\n", + " the data. If it is missing or None, the mean is automatically calculated.\n", + " \n", + " Use this function when your data is a sample from a population. To\n", + " calculate the variance from the entire population, see ``pvariance``.\n", + " \n", + " Examples:\n", + " \n", + " >>> data = [2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5]\n", + " >>> variance(data)\n", + " 1.3720238095238095\n", + " \n", + " If you have already calculated the mean of your data, you can pass it as\n", + " the optional second argument ``xbar`` to avoid recalculating it:\n", + " \n", + " >>> m = mean(data)\n", + " >>> variance(data, m)\n", + " 1.3720238095238095\n", + " \n", + " This function does not check that ``xbar`` is actually the mean of\n", + " ``data``. Giving arbitrary values for ``xbar`` may lead to invalid or\n", + " impossible results.\n", + " \n", + " Decimals and Fractions are supported:\n", + " \n", + " >>> from decimal import Decimal as D\n", + " >>> variance([D(\"27.5\"), D(\"30.25\"), D(\"30.25\"), D(\"34.5\"), D(\"41.75\")])\n", + " Decimal('31.01875')\n", + " \n", + " >>> from fractions import Fraction as F\n", + " >>> variance([F(1, 6), F(1, 2), F(5, 3)])\n", + " Fraction(67, 108)\n", + "\n", + "DATA\n", + " __all__ = ['NormalDist', 'StatisticsError', 'correlation', 'covariance...\n", + "\n", + "FILE\n", + " /Users/jamesturk/.local/share/uv/python/cpython-3.10.15-macos-aarch64-none/lib/python3.10/statistics.py\n", + "\n", + "\n" + ] + } + ], "source": [ "import statistics\n", - "#help(statistics)" + "help(statistics)" ] }, { @@ -96,19 +757,110 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 3, + "id": "5db28bf9-2ff6-4847-bd18-ae46ea99b57c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Counter',\n", + " 'Decimal',\n", + " 'Fraction',\n", + " 'LinearRegression',\n", + " 'NormalDist',\n", + " 'StatisticsError',\n", + " '__all__',\n", + " '__builtins__',\n", + " '__cached__',\n", + " '__doc__',\n", + " '__file__',\n", + " '__loader__',\n", + " '__name__',\n", + " '__package__',\n", + " '__spec__',\n", + " '_coerce',\n", + " '_convert',\n", + " '_exact_ratio',\n", + " '_fail_neg',\n", + " '_find_lteq',\n", + " '_find_rteq',\n", + " '_isfinite',\n", + " '_normal_dist_inv_cdf',\n", + " '_ss',\n", + " '_sum',\n", + " 'bisect_left',\n", + " 'bisect_right',\n", + " 'correlation',\n", + " 'covariance',\n", + " 'erf',\n", + " 'exp',\n", + " 'fabs',\n", + " 'fmean',\n", + " 'fsum',\n", + " 'geometric_mean',\n", + " 'groupby',\n", + " 'harmonic_mean',\n", + " 'hypot',\n", + " 'itemgetter',\n", + " 'linear_regression',\n", + " 'log',\n", + " 'math',\n", + " 'mean',\n", + " 'median',\n", + " 'median_grouped',\n", + " 'median_high',\n", + " 'median_low',\n", + " 'mode',\n", + " 'multimode',\n", + " 'namedtuple',\n", + " 'numbers',\n", + " 'pstdev',\n", + " 'pvariance',\n", + " 'quantiles',\n", + " 'random',\n", + " 'repeat',\n", + " 'sqrt',\n", + " 'stdev',\n", + " 'tau',\n", + " 'variance']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir(statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, "id": "09c27388", "metadata": { "tags": [] }, "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on built-in function cos in module math:\n", + "\n", + "cos(x, /)\n", + " Return the cosine of x (measured in radians).\n", + "\n" + ] + }, { "data": { "text/plain": [ "-1.0" ] }, - "execution_count": 6, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -116,9 +868,60 @@ "source": [ "import math\n", "\n", + "help(math.cos)\n", "math.cos(math.pi)" ] }, + { + "cell_type": "code", + "execution_count": 4, + "id": "4b76400a-82af-4ce6-91a9-2b4c4e125e5b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on class repeat in module itertools:\n", + "\n", + "class repeat(builtins.object)\n", + " | repeat(object [,times]) -> create an iterator which returns the object\n", + " | for the specified number of times. If not specified, returns the object\n", + " | endlessly.\n", + " | \n", + " | Methods defined here:\n", + " | \n", + " | __getattribute__(self, name, /)\n", + " | Return getattr(self, name).\n", + " | \n", + " | __iter__(self, /)\n", + " | Implement iter(self).\n", + " | \n", + " | __length_hint__(...)\n", + " | Private method returning an estimate of len(list(it)).\n", + " | \n", + " | __next__(self, /)\n", + " | Implement next(self).\n", + " | \n", + " | __reduce__(...)\n", + " | Return state information for pickling.\n", + " | \n", + " | __repr__(self, /)\n", + " | Return repr(self).\n", + " | \n", + " | ----------------------------------------------------------------------\n", + " | Static methods defined here:\n", + " | \n", + " | __new__(*args, **kwargs) from builtins.type\n", + " | Create and return a new object. See help(type) for accurate signature.\n", + "\n" + ] + } + ], + "source": [ + "help(statistics.repeat)" + ] + }, { "cell_type": "markdown", "id": "9d5f6a9f", @@ -202,9 +1005,41 @@ "execution_count": 6, "id": "1604f2d7", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Help on function mode in module statistics:\n", + "\n", + "mode(data)\n", + " Return the most common data point from discrete or nominal data.\n", + " \n", + " ``mode`` assumes discrete data, and returns a single value. This is the\n", + " standard treatment of the mode as commonly taught in schools:\n", + " \n", + " >>> mode([1, 1, 2, 3, 3, 3, 3, 4])\n", + " 3\n", + " \n", + " This also works with nominal (non-numeric) data:\n", + " \n", + " >>> mode([\"red\", \"blue\", \"blue\", \"red\", \"green\", \"red\", \"red\"])\n", + " 'red'\n", + " \n", + " If there are multiple modes with same frequency, return the first one\n", + " encountered:\n", + " \n", + " >>> mode(['red', 'red', 'green', 'blue', 'blue'])\n", + " 'red'\n", + " \n", + " If *data* is empty, ``mode``, raises StatisticsError.\n", + "\n" + ] + } + ], "source": [ - "#help(statistics.mode)" + "import statistics\n", + "help(statistics.mode)" ] }, { @@ -245,11 +1080,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "57d95a79", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "#from .. import symbol" + ] }, { "cell_type": "code", diff --git a/10.OOP.ipynb b/10.OOP.ipynb index 90d5e67..4422bff 100644 --- a/10.OOP.ipynb +++ b/10.OOP.ipynb @@ -17,6 +17,16 @@ "In a hypothetical language that only had data structures and functions, we might write code like:" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "f1101c65-ba9b-4a87-a0cb-71cdc3c73726", + "metadata": {}, + "outputs": [], + "source": [ + "#costume_is_scary(person_a)" + ] + }, { "cell_type": "code", "execution_count": null, @@ -32,7 +42,7 @@ "\n", "candy_bag = [\"Kit Kat\", \"Kit Kat\", \"Lollipop\", \"M&Ms\"]\n", "\n", - "def costume_is_scary(person):\n", + "def costume_is_scary(person : dict) -> bool:\n", " return person[\"costume\"] in (\"Ghost\", \"Wolfman\", \"Mummy\")\n", "\n", "def do_trick(person):\n", @@ -116,6 +126,22 @@ " do_trick(person, house)" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "3b581e45-4da7-4c39-8dbd-bf55d5836441", + "metadata": {}, + "outputs": [], + "source": [ + "p = Person(\"James\", \"Wolfman\")\n", + "p2 = Person(\"Fred\", \"Mummy\")\n", + "l1 = list()\n", + "l2 = list()\n", + "p.is_scary()\n", + "p.accept_candy(\"Chocolate\")\n", + "p.candy" + ] + }, { "cell_type": "markdown", "id": "50cfecf9-db6a-46a3-8441-4b4765e97324", @@ -155,85 +181,44 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "id": "eb098ee4-813d-4ede-9fba-04caf9922a76", "metadata": { "tags": [] }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "isinstance([1, 2, 3], list)" ] }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "id": "8f904840-e5f1-4f44-a11a-dcb568914b98", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "isinstance([1, 2, 3], tuple)" ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "id": "b7c087da-0213-41e4-a298-873d2e677ab9", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "isinstance([1, 2, 3], object)" ] }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "id": "1ad4e49c-dbc2-406b-9148-6f708c01f4b9", "metadata": { "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "['', 0, 0.0, {1, 2, 3}, ]\n" - ] - } - ], + "outputs": [], "source": [ "s = set([1,2,3])\n", "\n", @@ -250,23 +235,12 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "id": "ea08451b-862a-446f-beb4-13bd8fcaa869", "metadata": { "tags": [] }, - "outputs": [ - { - "data": { - "text/plain": [ - "[True, True, True, True, True]" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "[isinstance(item, object) for item in ll]" ] @@ -290,6 +264,7 @@ "id": "e870d8fe-786d-4499-86fc-4808a89981a3", "metadata": {}, "source": [ + "(Placeholder: MW class got here)\n", "## Classes in Python" ] }, @@ -307,7 +282,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 3, "id": "1fce0f7d-7065-432d-8672-8fefd2b11f67", "metadata": { "tags": [] @@ -327,7 +302,9 @@ " # __init__ is a special method\n", " # known as a double-underscore or dunder method\n", " # in Python it represents our constructor\n", + "\n", " def __init__(self, make, model, year=2000):\n", + " #print(type(self))\n", " self.make = make\n", " self.model = model\n", " self.year = year\n", @@ -344,7 +321,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "id": "8cb58e57-c4ba-4066-ab43-2c8469757918", "metadata": { "tags": [] @@ -356,7 +333,7 @@ "True" ] }, - "execution_count": 5, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -365,6 +342,34 @@ "car3 is car2" ] }, + { + "cell_type": "code", + "execution_count": 5, + "id": "18729ce3-7250-4a72-833c-de48ca03ac93", + "metadata": {}, + "outputs": [], + "source": [ + "car2.year += 1" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "b8612eda-9d93-4c17-a724-e4f93064da20", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2023\n" + ] + } + ], + "source": [ + "print(car3.year)" + ] + }, { "cell_type": "markdown", "id": "07f8b1f1-c2cc-4e93-98a4-0f5cbdee72f4", @@ -381,12 +386,23 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 14, "id": "68e49035-d0cc-4f24-b19b-c84d568a1233", "metadata": { "tags": [] }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "class Car:\n", " def __init__(self, make, model, year):\n", @@ -395,6 +411,7 @@ " self.year = year\n", " self.mileage = 0\n", " self.hybrid = False\n", + " self.driver = None\n", " \n", " def print_report(self):\n", " print(f\"{self.year} {self.make} {self.model} with {self.mileage} miles\")\n", @@ -403,12 +420,13 @@ " self.mileage += miles\n", " \n", "car1 = Car(\"Honda\", \"Civic\", 2019)\n", - "car2 = Car(\"Chevy\", \"Volt\", 2022)\n" + "car2 = Car(\"Chevy\", \"Volt\", 2022)\n", + "car2.mileage" ] }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 15, "id": "0e2162f2-7e5a-451b-96e9-b3fd4c5bcfaa", "metadata": { "tags": [] @@ -428,7 +446,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 17, "id": "dbb352b0-7cc4-476f-9d58-e7fff9c3ae35", "metadata": { "tags": [] @@ -438,53 +456,39 @@ "name": "stdout", "output_type": "stream", "text": [ - "2022 Chevy Volt with 500 miles\n" + "1000\n", + "2022 Chevy Volt with 1000 miles\n" ] } ], "source": [ "car2.drive(500)\n", + "print(car2.mileage)\n", "car2.print_report()" ] }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "id": "756e9562-79bc-4e38-9eb5-e5c0e8622124", "metadata": { "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2019 Honda Civic with 0 miles\n" - ] - } - ], + "outputs": [], "source": [ "car1.print_report()" ] }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "id": "4afa851b-562e-4b81-a92a-017275f7d246", "metadata": { "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "500\n" - ] - } - ], + "outputs": [], "source": [ - "print(car2.mileage)" + "print(car1.mileage)" ] }, { @@ -509,29 +513,22 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": null, "id": "0c0e0e3b-308d-411f-842d-e422d8dc7968", "metadata": { "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2022 Chevy Volt with 500 miles\n" - ] - } - ], + "outputs": [], "source": [ "# explicitly call Car.print_report and pass self\n", + "#car2.print_report()\n", "Car.print_report(car2) \n", "# this is not how we call class methods! (but it works)" ] }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 18, "id": "6f0599d6-df4f-413d-9c87-ac7ca479b35f", "metadata": { "tags": [] @@ -540,16 +537,17 @@ { "data": { "text/plain": [ - "[4]" + "[4, 4]" ] }, - "execution_count": 23, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ll = []\n", + "ll.append(4)\n", "list.append(ll, 4) # list is class, ll is self here\n", "ll" ] @@ -564,7 +562,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 22, "id": "184e8e69-0eb8-4068-bb3c-a2aec2433fec", "metadata": { "tags": [] @@ -575,13 +573,13 @@ " def __init__(self):\n", " print(\"constructor!\")\n", " \n", - " def method_no_self(self):\n", + " def method_no_self():\n", " print(\"method!\")" ] }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 23, "id": "42fb46df-2abb-4387-9e49-5ddb27d0c23a", "metadata": { "tags": [] @@ -591,8 +589,18 @@ "name": "stdout", "output_type": "stream", "text": [ - "constructor!\n", - "method!\n" + "constructor!\n" + ] + }, + { + "ename": "TypeError", + "evalue": "Mistake.method_no_self() takes 0 positional arguments but 1 was given", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[23], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m m \u001b[38;5;241m=\u001b[39m Mistake()\n\u001b[0;32m----> 2\u001b[0m \u001b[43mm\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmethod_no_self\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;66;03m# rewritten as Mistake.method_no_self(m)\u001b[39;00m\n", + "\u001b[0;31mTypeError\u001b[0m: Mistake.method_no_self() takes 0 positional arguments but 1 was given" ] } ], @@ -622,7 +630,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 25, "id": "31f1a926-7099-4740-9f86-769967fa41a9", "metadata": { "tags": [] @@ -630,8 +638,8 @@ "outputs": [], "source": [ "my_car = Car(\"DMC\", \"DeLorean\", 1982)\n", - "#print(my_car.miles_driven)\n", - "my_car.driver = \"Marty\" # allowed, but to be avoided" + "my_car.driver_name = \"Marty\" # allowed, but to be avoided\n", + "my_car.whatever_i_want = [1, 2, 3]" ] }, { @@ -640,7 +648,9 @@ "id": "8c0afa4e-0219-4b45-a527-b63263a04d5d", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "print(my_car.driver)" + ] }, { "cell_type": "markdown", @@ -656,7 +666,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": null, "id": "f0e0212d-363e-4bde-bfa4-3e717b800f5b", "metadata": { "tags": [] @@ -671,21 +681,12 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": null, "id": "b15355ff-359d-4fc8-b47a-04e4c0d4afd5", "metadata": { "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "called f()\n", - "1\n" - ] - } - ], + "outputs": [], "source": [ "f.call_count += 1\n", "f()\n", @@ -694,7 +695,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": null, "id": "1cbc7aa8-19b6-43eb-91bb-69b889bd2a59", "metadata": { "tags": [] @@ -714,7 +715,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": null, "id": "ad71f5ff-e97d-4559-8c37-c4b1af0c0ec2", "metadata": { "tags": [] @@ -728,7 +729,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": null, "id": "4654848c-b793-4847-a18c-072da3188868", "metadata": { "tags": [] @@ -742,25 +743,12 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": null, "id": "d2e7192b-bb8c-4b55-9a38-1f9d00e650c7", "metadata": { "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "call count 1\n", - "called f()\n", - "call count 2\n", - "called f()\n", - "call count 3\n", - "called f()\n" - ] - } - ], + "outputs": [], "source": [ "f()\n", "f()\n", @@ -786,12 +774,14 @@ "source": [ "## Encapsulation\n", "\n", + "A class should be responsible for modifications to its own state.\n", + "\n", "Why might it be a bad idea to allow users to change attributes?\n" ] }, { "cell_type": "code", - "execution_count": 34, + "execution_count": null, "id": "68b9d90d-17ed-40c2-a3f2-736d7d6357ca", "metadata": { "tags": [] @@ -817,6 +807,7 @@ " self.model = model\n", " self.year = int(year)\n", " self.mileage = 0\n", + " self.hybrid = False\n", " \n", " def drive(self, miles):\n", " if miles < 0:\n", @@ -853,7 +844,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 26, "id": "41292841-0aa9-4bfd-9cfe-c12f860bbdd9", "metadata": { "tags": [] @@ -893,7 +884,17 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, + "id": "3b3fcebf-571c-424b-88f2-dbb8fb029705", + "metadata": {}, + "outputs": [], + "source": [ + "car2._year" + ] + }, + { + "cell_type": "code", + "execution_count": 28, "id": "ef88e138-13c2-4174-b51f-03d1c3aad5aa", "metadata": { "tags": [] @@ -906,35 +907,111 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[20], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mcar2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m__mileage\u001b[49m)\n", + "Cell \u001b[0;32mIn[28], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m car2\u001b[38;5;241m.\u001b[39m__mileage \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m100\u001b[39m\n", "\u001b[0;31mAttributeError\u001b[0m: 'Car' object has no attribute '__mileage'" ] } ], "source": [ - "print(car2.__mileage)" + "car2.__Car_mileage -= 100" ] }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 29, + "id": "d09cbac0-fba3-420c-9760-1efe24a9ce48", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['_Car__mileage',\n", + " '__class__',\n", + " '__delattr__',\n", + " '__dict__',\n", + " '__dir__',\n", + " '__doc__',\n", + " '__eq__',\n", + " '__format__',\n", + " '__ge__',\n", + " '__getattribute__',\n", + " '__gt__',\n", + " '__hash__',\n", + " '__init__',\n", + " '__init_subclass__',\n", + " '__le__',\n", + " '__lt__',\n", + " '__module__',\n", + " '__ne__',\n", + " '__new__',\n", + " '__reduce__',\n", + " '__reduce_ex__',\n", + " '__repr__',\n", + " '__setattr__',\n", + " '__sizeof__',\n", + " '__str__',\n", + " '__subclasshook__',\n", + " '__weakref__',\n", + " '_make',\n", + " '_model',\n", + " '_year',\n", + " 'drive',\n", + " 'print_report']" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dir(car2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, "id": "cb2ba9e0-a5af-45bd-bc21-c25bc9414637", "metadata": { "tags": [] }, + "outputs": [], + "source": [ + "car2._make = \"???\"\n", + "print(car2._make) \n", + "# soft protection, can still access but \"at your own risk\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4ea90f2f-6530-4732-b151-4fca1c431ab1", + "metadata": {}, + "outputs": [], + "source": [ + "print(car2)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "886b1b5a-d47a-4112-b37d-7117fd16e37a", + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "???\n" + "<__main__.Car object at 0x103792d40>\n" ] } ], "source": [ - "car2._make = \"???\"\n", - "print(car2._make) \n", - "# soft protection, can still access but \"at your own risk\"" + "carA = Car(\"Honda\", \"Civic\", 2019)\n", + "carB = Car(\"Honda\", \"Civic\", 2019)\n", + "carA == carB\n", + "\n", + "print(carA)" ] }, { @@ -953,7 +1030,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 50, "id": "9647e5ad-86a4-486f-bf7e-44668d25a472", "metadata": { "tags": [] @@ -980,10 +1057,10 @@ " and self._year == other._year)\n", " \n", " def __repr__(self):\n", - " return f\"Car({self._make}, {self._model}, {self._year})\"\n", - "\n", - " def __str__(self):\n", - " return f\"{self._year} {self._make} {self._model} with {self.__mileage} miles\"" + " return f\"repr Car({self._make}, {self._model}, {self._year}, mileage={self.__mileage})\"\n", + " __str__ = __repr__\n", + " #def __str__(self):\n", + " # return f\"str {self._year} {self._make} {self._model} with {self.__mileage} miles\"" ] }, { @@ -998,7 +1075,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 51, "id": "eb77364b-b30d-4a10-b0fb-355fa83e003d", "metadata": { "tags": [] @@ -1011,17 +1088,17 @@ }, { "cell_type": "code", - "execution_count": 42, - "id": "5c124af8-9d7d-4909-9124-1ba2bb170f16", + "execution_count": 52, + "id": "d8302542-27c1-4b1b-9bce-59c6d7ce527c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "Car(Ford, F-150, 1985)" + "repr Car(Ford, F-150, 1985, mileage=0)" ] }, - "execution_count": 42, + "execution_count": 52, "metadata": {}, "output_type": "execute_result" } @@ -1032,31 +1109,31 @@ }, { "cell_type": "code", - "execution_count": 40, - "id": "e5b3f849-83b8-4625-b160-1141e39a1a99", - "metadata": { - "tags": [] - }, + "execution_count": 53, + "id": "5c124af8-9d7d-4909-9124-1ba2bb170f16", + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'1985 Ford F-150 with 0 miles'" + "repr Car(Ford, F-150, 1985, mileage=0)" ] }, - "execution_count": 40, + "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "str(truck) # implicit conversion to string, uses repr(truck)" + "def f():\n", + " return truck\n", + "f()" ] }, { "cell_type": "code", - "execution_count": 41, - "id": "8f4919d6-bab6-49d4-9c73-dbf0790b118c", + "execution_count": 54, + "id": "e5b3f849-83b8-4625-b160-1141e39a1a99", "metadata": { "tags": [] }, @@ -1065,17 +1142,40 @@ "name": "stdout", "output_type": "stream", "text": [ - "1985 Ford F-150 with 0 miles\n" + "repr Car(Ford, F-150, 1985, mileage=0)\n" ] } ], "source": [ - "print(str(truck)) # uses str(truck)" + "print(truck)" ] }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 48, + "id": "8f4919d6-bab6-49d4-9c73-dbf0790b118c", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'str 1985 Ford F-150 with 0 miles'" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "str(truck)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, "id": "74bf6a50-b18d-443d-a0ef-e2f1b8004898", "metadata": { "tags": [] @@ -1087,7 +1187,7 @@ "False" ] }, - "execution_count": 34, + "execution_count": 37, "metadata": {}, "output_type": "execute_result" } @@ -1098,7 +1198,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 40, "id": "01bdde7f-c4a8-4014-9f75-ff041370892d", "metadata": { "tags": [] @@ -1107,21 +1207,21 @@ { "data": { "text/plain": [ - "False" + "True" ] }, - "execution_count": 36, + "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "truck.__eq__(car1)" + "truck.__eq__(truck2)" ] }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 38, "id": "b29e3c72-a392-4991-aeb5-fee9463495f2", "metadata": { "tags": [] @@ -1133,7 +1233,7 @@ "True" ] }, - "execution_count": 35, + "execution_count": 38, "metadata": {}, "output_type": "execute_result" } @@ -1144,12 +1244,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "id": "26edc5e9-84ad-4e6f-9e9f-c7a546f7f7b7", "metadata": { "tags": [] }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "truck is truck2" ] @@ -1229,7 +1340,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 56, "id": "a161eedc-520a-4827-a0ba-701f1cba3376", "metadata": { "tags": [] @@ -1240,10 +1351,10 @@ "output_type": "stream", "text": [ "1\n", - "2\n", - "3\n", "4\n", - "5\n" + "9\n", + "16\n", + "25\n" ] } ], @@ -1253,29 +1364,18 @@ "r = range(8)\n", "\n", "# iterable: we can use it in a for loop\n", - "for x in l: # or g, or r\n", + "for x in g: # or g, or r\n", " print(x)" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "id": "9f73f6a7-b97d-457d-92bc-f96ee9f45246", "metadata": { "tags": [] }, - "outputs": [ - { - "data": { - "text/plain": [ - " at 0x1085c0860>" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "g" ] @@ -1298,7 +1398,28 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 58, + "id": "2616192f-8adc-45a8-a988-3558aebe1eb3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "iter([1, 2, 3])" + ] + }, + { + "cell_type": "code", + "execution_count": 59, "id": "a71a043a-1c1b-4c88-b489-b5e47b9f2d4f", "metadata": { "tags": [] @@ -1308,10 +1429,10 @@ "name": "stdout", "output_type": "stream", "text": [ - "\n", - " at 0x1085c2c20>\n", - "\n", - "\n" + "\n", + " at 0x104622a40>\n", + "\n", + "\n" ] } ], @@ -1333,22 +1454,21 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 62, "id": "6aec1be0-9094-450c-95f7-91ebec9c3c76", "metadata": { "tags": [] }, "outputs": [ { - "ename": "StopIteration", - "evalue": "", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[13], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43mnext\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mli\u001b[49m\u001b[43m)\u001b[49m\n", - "\u001b[0;31mStopIteration\u001b[0m: " - ] + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ @@ -1357,7 +1477,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 63, "id": "032cfb1f-25f6-4359-8190-53129ec1b67b", "metadata": { "tags": [] @@ -1369,7 +1489,7 @@ "1" ] }, - "execution_count": 10, + "execution_count": 63, "metadata": {}, "output_type": "execute_result" } @@ -1387,19 +1507,31 @@ }, "outputs": [], "source": [ - "print(next(li))\n", - "print(next(gi))\n", + "#print(next(li))\n", + "#print(next(gi))\n", "print(next(ri))" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 64, "id": "4260dadd-9476-4103-bf7f-496812904a1e", "metadata": { "tags": [] }, - "outputs": [], + "outputs": [ + { + "ename": "StopIteration", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[64], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28;43mnext\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mli\u001b[49m\u001b[43m)\u001b[49m)\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28mnext\u001b[39m(gi))\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28mnext\u001b[39m(ri))\n", + "\u001b[0;31mStopIteration\u001b[0m: " + ] + } + ], "source": [ "print(next(li))\n", "print(next(gi))\n", @@ -1422,11 +1554,50 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 65, "id": "6b4cb301-3152-4cd2-bba7-6f0cdd258afe", "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n" + ] + }, + { + "ename": "StopIteration", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[65], line 4\u001b[0m\n\u001b[1;32m 2\u001b[0m iterator \u001b[38;5;241m=\u001b[39m \u001b[38;5;28miter\u001b[39m(iterable)\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[0;32m----> 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28;43mnext\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43miterator\u001b[49m\u001b[43m)\u001b[49m)\n", + "\u001b[0;31mStopIteration\u001b[0m: " + ] + } + ], + "source": [ + "iterable = [1, 2, 3, 4] # iterable\n", + "iterator = iter(iterable)\n", + "while True:\n", + " print(next(iterator))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f7e925d3-f7ad-4ef1-97bd-8300dbb346e7", + "metadata": {}, "outputs": [], - "source": [] + "source": [ + "f(x[0] + y[\"test\"])\n", + "\n", + "f.__call__(x.__getitem__(0).__add__(y.__getitem__(\"test\")))" + ] } ], "metadata": { diff --git a/11.exceptions.ipynb b/11.exceptions.ipynb index b96deed..60101e9 100644 --- a/11.exceptions.ipynb +++ b/11.exceptions.ipynb @@ -13,7 +13,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "id": "c69f70a0-f070-4d3c-adaa-a4520cd73af5", "metadata": { "tags": [] @@ -28,6 +28,27 @@ " return a * c" ] }, + { + "cell_type": "code", + "execution_count": 6, + "id": "6eabe23e-d4dc-442c-b405-4adc9fae7417", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1.3333333333333333" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_func(4, 3)" + ] + }, { "cell_type": "code", "execution_count": 2, @@ -53,7 +74,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "id": "1ef88eb5-0852-4d97-bd25-340b83730442", "metadata": { "tags": [] @@ -66,8 +87,8 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[3], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mmy_func\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m)\u001b[49m\n", - "Cell \u001b[0;32mIn[1], line 4\u001b[0m, in \u001b[0;36mmy_func\u001b[0;34m(a, b)\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\" what can go wrong with this function? \"\"\"\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m a \u001b[38;5;241m>\u001b[39m b:\n\u001b[0;32m----> 4\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43ma\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m a \u001b[38;5;241m*\u001b[39m c\n", + "Cell \u001b[0;32mIn[4], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mmy_func\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m)\u001b[49m\n", + "Cell \u001b[0;32mIn[2], line 4\u001b[0m, in \u001b[0;36mmy_func\u001b[0;34m(a, b)\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\" what can go wrong with this function? \"\"\"\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m a \u001b[38;5;241m>\u001b[39m b:\n\u001b[0;32m----> 4\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43ma\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m a \u001b[38;5;241m*\u001b[39m c\n", "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" ] } @@ -214,17 +235,18 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 10, "id": "5022b7c8-a19f-45cb-9d40-58abc48de961", "metadata": { "tags": [] }, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "can't divide by zero, set c = 0\n" + "ename": "SyntaxError", + "evalue": "expected 'except' or 'finally' block (572013031.py, line 6)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m Cell \u001b[0;32mIn[10], line 6\u001b[0;36m\u001b[0m\n\u001b[0;31m print(\"hello\")\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m expected 'except' or 'finally' block\n" ] } ], @@ -233,9 +255,11 @@ " a = 3\n", " b = 0\n", " c = a / b\n", + " print(\"last line of try\")\n", "except ZeroDivisionError:\n", " c = 0\n", - " print(f\"can't divide by zero, set c = {c}\")" + " print(f\"can't divide by zero, set c = {c}\")\n", + "print(\"and then this happens\")" ] }, {