{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to Python" ], "id": "64f898cd-1e06-4377-a17b-b3d675957f7b" }, { "cell_type": "code", "execution_count": 132, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "4" ] } } ], "source": [ "2 + 2" ], "id": "c327a89c-e519-4709-8c29-515e7c00dfa5" }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python has the following types:\n", "\n", "- `str`\n", "- `bool`\n", "- `int`\n", "- `float` \\<- a C `double`\n", "- `dict`\n", "- `list`\n", "- `set`\n", "- `tuple`" ], "id": "04549dda-c815-482f-af1d-719055c39da0" }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "4294967295" ] } } ], "source": [ "2 ** 32 - 1" ], "id": "e8741ecc-7655-4cdc-ba4d-8c12cec565f7" }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "8228863001835066529632658874512640596519760779404643181058684868103588231651983628740999193787882296163044366493632157270168550417673732837190022570379624359677896388978172052495892656722068451801215349472765139014318812471863009877701280158593080431221425432944328893737411043878272790470707895191304714844545104171879670960362918595278699535244673367090285651259233330614178023118290719313365134610411047047471669360811979405231925836161763944679737525318975335081478616357443060688819657564259801031831799666226609793670519258091624540623073202809652099366028831784182370424464956587657891971708754740727495828880356404026436672396009224339114167289394488157526146727812023740637240031427723432225144832" ] } } ], "source": [ "2 ** 2345" ], "id": "d8e90507-d09b-4bf8-9d4d-46993ef14caa" }, { "cell_type": "code", "execution_count": 135, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "True" ] } } ], "source": [ "True" ], "id": "bec5084b-93e4-411c-83b1-e4db2b158ec6" }, { "cell_type": "code", "execution_count": 136, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "False" ] } } ], "source": [ "False" ], "id": "28c64096-d55c-41aa-9b25-832fc53dfcac" }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "str" ] } } ], "source": [ "type('a')" ], "id": "b8fa71cd-4470-4462-9d05-9f77cc3d7dda" }, { "cell_type": "code", "execution_count": 138, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "str" ] } } ], "source": [ "type(\"a\")" ], "id": "580168fb-0efd-4b92-8e67-648a3c264932" }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "'lee'" ] } } ], "source": [ "f = 23.2234\n", "f = \"lee\"\n", "f" ], "id": "97e08e47-49a3-4dc3-90c4-ccbead1e3a6a" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dictionaries" ], "id": "f3a66185-511b-4cbb-87b4-a0e85c273740" }, { "cell_type": "code", "execution_count": 140, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "{'a': 2, 'edward': 1.1}" ] } } ], "source": [ "d = {\"a\": 2, \"edward\": 1.1}\n", "d" ], "id": "ebcb376b-357d-4161-b980-e9118ab2d429" }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "{'a': 6, 'edward': 1.1}" ] } } ], "source": [ "d[\"a\"] = 6\n", "d" ], "id": "74d470b0-d5be-4ab3-853e-b48322d528b5" }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [], "source": [ "d[\"b\"] = \"new variable\"" ], "id": "5d9f266d-934d-45c5-b329-c1362910a021" }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "{'a': 6, 'edward': 1.1, 'b': 'new variable'}" ] } } ], "source": [ "d" ], "id": "e62d467a-e539-490c-b3fa-dc777604c3ba" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## List" ], "id": "5b5d8417-f0d3-4ac9-9c58-17b6bbe9ff86" }, { "cell_type": "code", "execution_count": 144, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "[1, 2, 3, {'a': 6, 'edward': 1.1, 'b': 'new variable'}]" ] } } ], "source": [ "l = [1, 2, 3, d]\n", "l" ], "id": "e36a471d-959f-4187-9b51-1514bfc82896" }, { "cell_type": "code", "execution_count": 145, "metadata": {}, "outputs": [], "source": [ "l[3] = \"mutable\"" ], "id": "3935b592-1944-48b7-9243-e2bb3fdbfdac" }, { "cell_type": "code", "execution_count": 146, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "1" ] } } ], "source": [ "l[0]" ], "id": "98f5242b-4a56-49c7-9bff-01bd5f68bbe3" }, { "cell_type": "code", "execution_count": 147, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "'mutable'" ] } } ], "source": [ "l[-1]" ], "id": "27a0fa03-f9ab-4200-bae0-e37cb1703994" }, { "cell_type": "code", "execution_count": 148, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "[1, 2, 3, 'mutable', 2980]" ] } } ], "source": [ "l.append(2980)\n", "l" ], "id": "b8930ec0-c027-4335-8fac-a55b5bc11ffb" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tuple" ], "id": "9124e9bc-5aa2-4d48-9a96-aee6cd903a9a" }, { "cell_type": "code", "execution_count": 149, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "(2, 3, 4, 6)" ] } } ], "source": [ "t = (1, 2, 3, l)\n", "# t[0] = 20 # doesn't work\n", "t = (2, 3, 4, 6)\n", "t" ], "id": "dc30cd07-bc16-4727-a145-53ae67d92498" }, { "cell_type": "code", "execution_count": 150, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "{'a': 6, 'edward': 1.1, 'b': 'new variable'}" ] } } ], "source": [ "# another look at dictionaries\n", "d" ], "id": "0edfb4a4-130a-45b3-9f91-18eba1469f4d" }, { "cell_type": "code", "execution_count": 151, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "{'a': 6,\n", " 'edward': 1.1,\n", " 'b': 'new variable',\n", " 1: 'new',\n", " 1.1: 'cool',\n", " (2, 3, 4, 6): 'whoa'}" ] } } ], "source": [ "d[1] = \"new\"\n", "d[1.1] = \"cool\"\n", "d[t] = \"whoa\"\n", "# d[l] = \"nope\"\n", "d" ], "id": "7a8b8e35-169b-46d1-b927-6cf8b9ec5fbf" }, { "cell_type": "code", "execution_count": 152, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "{'a': 6,\n", " 'edward': 1.1,\n", " 'b': 'new variable',\n", " 1: 'new',\n", " 1.1: 'cool',\n", " (2, 3, 4, 6): 'new thing'}" ] } } ], "source": [ "d[t] = \"new thing\"\n", "d" ], "id": "ede5bc90-d184-49be-a48f-ea1780726efe" }, { "cell_type": "code", "execution_count": 153, "metadata": {}, "outputs": [], "source": [ "del d[t]" ], "id": "aeb3e3af-914e-4c14-b789-6b28aa01b191" }, { "cell_type": "code", "execution_count": 154, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "{'a': 6, 'edward': 1.1, 'b': 'new variable', 1: 'new', 1.1: 'cool'}" ] } } ], "source": [ "d" ], "id": "f804e939-440c-4e56-8df2-1fed7e025149" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions" ], "id": "29e32916-0086-4e35-a7ad-38722f626550" }, { "cell_type": "code", "execution_count": 155, "metadata": {}, "outputs": [], "source": [ "def f(x):\n", " return x + 3" ], "id": "5cf08e95-0998-4ae4-b09f-ed0fd5320b09" }, { "cell_type": "code", "execution_count": 156, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "8" ] } } ], "source": [ "f(5)" ], "id": "cd1256aa-87a4-4dee-9a8a-90a85203394f" }, { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "3" ] } } ], "source": [ "x" ], "id": "ad1ad80d-2a4d-403f-a376-91690df61bc4" }, { "cell_type": "code", "execution_count": 158, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "18" ] } } ], "source": [ "def g(x, y):\n", " return x * y + 3\n", "g(3, 5)" ], "id": "38fac7d7-570d-480a-96c0-dd3d873144a1" }, { "cell_type": "code", "execution_count": 159, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "9" ] } } ], "source": [ "def h(x, z = 10):\n", " return x * z\n", "h(3, z = 3)" ], "id": "b856faaa-4602-4901-a2c1-dba6b577a5a9" }, { "cell_type": "code", "execution_count": 160, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "9" ] } } ], "source": [ "def j(x, y = 10, z = 23):\n", " return x + y * z\n", "j(3, z = 2, y = 3)" ], "id": "a748a50f-fc9a-41b9-af8f-217a1d6c559c" }, { "cell_type": "code", "execution_count": 161, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "[1, 2, 3, 'mutable', 2980]" ] } } ], "source": [ "l" ], "id": "a50f57d2-ec4f-4eed-a929-788a056f1d70" }, { "cell_type": "code", "execution_count": 162, "metadata": {}, "outputs": [], "source": [ "def k(lst):\n", " lst[0] = \"new value\"" ], "id": "c9a738eb-46db-4cfd-97a2-2618c7a6225c" }, { "cell_type": "code", "execution_count": 163, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "['new value', 2, 3, 'mutable', 2980]" ] } } ], "source": [ "k(l)\n", "l" ], "id": "23bbad58-f382-42dc-9146-c06c84835f48" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## For loops" ], "id": "430453dc-583f-4fc4-82e8-cbea9d5b70bd" }, { "cell_type": "code", "execution_count": 164, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "0\n", "1\n", "2" ] } ], "source": [ "for i in range(3):\n", " print(i)" ], "id": "d7c3b4a0-0e75-4ded-a9b7-7cc06978836a" }, { "cell_type": "code", "execution_count": 165, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "something\n", "in\n", "your\n", "list" ] } ], "source": [ "l = [\"something\", \"in\", \"your\", \"list\"]\n", "for li in l:\n", " print(li)" ], "id": "f6099fdb-d76b-41cc-b4df-4dbe5825d31e" }, { "cell_type": "code", "execution_count": 166, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "0 something\n", "1 in\n", "2 your\n", "3 list" ] } ], "source": [ "for i, li in enumerate(l):\n", " print(i, li)" ], "id": "e6935ad2-5fe9-43df-95db-5d091a1be2aa" }, { "cell_type": "code", "execution_count": 167, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "'314'" ] } } ], "source": [ "t2 = (\"math\", \"314\")\n", "a, b = t2\n", "b" ], "id": "540bbae8-357d-49a9-a539-fedabe168232" }, { "cell_type": "code", "execution_count": 168, "metadata": {}, "outputs": [], "source": [ "def n(x):\n", " return x, x * 2\n", "a, b = n(3)" ], "id": "be69ef74-f441-4c59-8e23-616b34cf50d7" }, { "cell_type": "code", "execution_count": 169, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "6" ] } } ], "source": [ "b" ], "id": "0bd5a692-640d-43a5-816a-7a0ceccbbbc4" }, { "cell_type": "code", "execution_count": 170, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "2\n", "4\n", "6\n", "8\n", "10" ] } ], "source": [ "i = 0\n", "while True:\n", " i += 2\n", " if i > 10:\n", " break\n", " print(i)\n" ], "id": "721b7bcc-a72a-453b-98f8-bcbfec9d3601" }, { "cell_type": "code", "execution_count": 171, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "cool" ] } ], "source": [ "if l:\n", " print(\"cool\")" ], "id": "71932ff4-98de-46c8-a709-bbe5fbb33830" }, { "cell_type": "code", "execution_count": 172, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "hwhat" ] } ], "source": [ "if [[]]:\n", " print(\"hwhat\")" ], "id": "3e1267a0-6d15-451b-9d2a-660898330160" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## F-strings" ], "id": "4a38428a-6979-438e-95e0-b74827ff908f" }, { "cell_type": "code", "execution_count": 173, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "'math314'" ] } } ], "source": [ "s = \"math314\"\n", "s" ], "id": "bd983772-9c57-4995-a4d0-4ba74e249e7f" }, { "cell_type": "code", "execution_count": 174, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "'is a string with an interpolated variable like 3'" ] } } ], "source": [ "x = 3\n", "f = f\"is a string with an interpolated variable like {x}\"\n", "f" ], "id": "dda5081d-444e-49b2-99a8-4b05d2bcd67e" }, { "cell_type": "markdown", "metadata": {}, "source": [ "# List comprehension" ], "id": "e7a3aa87-971f-41c5-ba0e-fd7694cadde2" }, { "cell_type": "code", "execution_count": 176, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "[0, 10, 20]" ] } } ], "source": [ "l2 = [h(x) for x in range(3)]\n", "l2" ], "id": "8165aba5-9873-46b1-8a1f-23b225d130f0" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dict Comprehension" ], "id": "64c11e44-affc-4d0d-922d-f63694fe345f" }, { "cell_type": "code", "execution_count": 177, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "{0: 0, 1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60, 7: 70, 8: 80, 9: 90}" ] } } ], "source": [ "d2 = {x: h(x) for x in range(10)}\n", "d2" ], "id": "a63d6445-0239-4c8a-a73f-a0a1007622e7" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Classes" ], "id": "4c874116-38aa-4929-867b-39e01c62bf23" }, { "cell_type": "code", "execution_count": 181, "metadata": {}, "outputs": [], "source": [ "class Table():\n", " def __init__(self, legs = 10):\n", " self._legs = legs\n", "\n", " def num_legs(self, x = 2):\n", " return self._legs" ], "id": "8dbf8978-cc16-4b0f-9f89-79ef97492b95" }, { "cell_type": "code", "execution_count": 182, "metadata": {}, "outputs": [], "source": [ "tbl = Table()" ], "id": "4ddd02ed-79f4-40e4-917d-97047916d175" }, { "cell_type": "code", "execution_count": 183, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "10" ] } } ], "source": [ "tbl.num_legs()" ], "id": "a6113099-5242-43c1-a514-425abbb48886" }, { "cell_type": "code", "execution_count": 184, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "5" ] } } ], "source": [ "tbl2 = Table(legs = 5)\n", "tbl2.num_legs()" ], "id": "2f7ecf16-4c4f-4223-9509-4f76bc5a04fd" }, { "cell_type": "code", "execution_count": 186, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "int" ] } } ], "source": [ "x\n", "type(x)" ], "id": "e9fda151-63ce-452f-90a5-14fe676d85df" }, { "cell_type": "code", "execution_count": 187, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "['__abs__',\n", " '__add__',\n", " '__and__',\n", " '__bool__',\n", " '__ceil__',\n", " '__class__',\n", " '__delattr__',\n", " '__dir__',\n", " '__divmod__',\n", " '__doc__',\n", " '__eq__',\n", " '__float__',\n", " '__floor__',\n", " '__floordiv__',\n", " '__format__',\n", " '__ge__',\n", " '__getattribute__',\n", " '__getnewargs__',\n", " '__getstate__',\n", " '__gt__',\n", " '__hash__',\n", " '__index__',\n", " '__init__',\n", " '__init_subclass__',\n", " '__int__',\n", " '__invert__',\n", " '__le__',\n", " '__lshift__',\n", " '__lt__',\n", " '__mod__',\n", " '__mul__',\n", " '__ne__',\n", " '__neg__',\n", " '__new__',\n", " '__or__',\n", " '__pos__',\n", " '__pow__',\n", " '__radd__',\n", " '__rand__',\n", " '__rdivmod__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__rfloordiv__',\n", " '__rlshift__',\n", " '__rmod__',\n", " '__rmul__',\n", " '__ror__',\n", " '__round__',\n", " '__rpow__',\n", " '__rrshift__',\n", " '__rshift__',\n", " '__rsub__',\n", " '__rtruediv__',\n", " '__rxor__',\n", " '__setattr__',\n", " '__sizeof__',\n", " '__str__',\n", " '__sub__',\n", " '__subclasshook__',\n", " '__truediv__',\n", " '__trunc__',\n", " '__xor__',\n", " 'as_integer_ratio',\n", " 'bit_count',\n", " 'bit_length',\n", " 'conjugate',\n", " 'denominator',\n", " 'from_bytes',\n", " 'imag',\n", " 'is_integer',\n", " 'numerator',\n", " 'real',\n", " 'to_bytes']" ] } } ], "source": [ "dir(x)" ], "id": "d5baaedd-147f-43e1-9976-70bda794e83d" }, { "cell_type": "code", "execution_count": 188, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "True" ] } } ], "source": [ "x.is_integer()" ], "id": "e47b3630-4f33-4f43-af67-df0dfa280243" }, { "cell_type": "code", "execution_count": 189, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "['__abs__',\n", " '__add__',\n", " '__bool__',\n", " '__ceil__',\n", " '__class__',\n", " '__delattr__',\n", " '__dir__',\n", " '__divmod__',\n", " '__doc__',\n", " '__eq__',\n", " '__float__',\n", " '__floor__',\n", " '__floordiv__',\n", " '__format__',\n", " '__ge__',\n", " '__getattribute__',\n", " '__getformat__',\n", " '__getnewargs__',\n", " '__getstate__',\n", " '__gt__',\n", " '__hash__',\n", " '__init__',\n", " '__init_subclass__',\n", " '__int__',\n", " '__le__',\n", " '__lt__',\n", " '__mod__',\n", " '__mul__',\n", " '__ne__',\n", " '__neg__',\n", " '__new__',\n", " '__pos__',\n", " '__pow__',\n", " '__radd__',\n", " '__rdivmod__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__rfloordiv__',\n", " '__rmod__',\n", " '__rmul__',\n", " '__round__',\n", " '__rpow__',\n", " '__rsub__',\n", " '__rtruediv__',\n", " '__setattr__',\n", " '__sizeof__',\n", " '__str__',\n", " '__sub__',\n", " '__subclasshook__',\n", " '__truediv__',\n", " '__trunc__',\n", " 'as_integer_ratio',\n", " 'conjugate',\n", " 'fromhex',\n", " 'hex',\n", " 'imag',\n", " 'is_integer',\n", " 'real']" ] } } ], "source": [ "y = 2.1\n", "dir(y)" ], "id": "e381fa95-7db5-41bf-b204-e2e9a885effc" }, { "cell_type": "code", "execution_count": 195, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "False" ] } } ], "source": [ "y = 2.1\n", "y.is_integer()" ], "id": "5a987ded-9a85-4c52-9edd-57358ee2e89e" }, { "cell_type": "code", "execution_count": 196, "metadata": {}, "outputs": [], "source": [ "class Table2():\n", " def __init__(self):\n", " self._legs = 4\n", "\n", " def num_legs(self):\n", " return 10" ], "id": "15ea1815-3004-433a-b898-555b157e0ab1" }, { "cell_type": "code", "execution_count": 198, "metadata": {}, "outputs": [], "source": [ "tbl3 = Table2()" ], "id": "c574cea1-d127-41b1-b41d-9bb39cd085c4" }, { "cell_type": "code", "execution_count": 200, "metadata": {}, "outputs": [], "source": [ "# Definitely do not do this\n", "# we did it to be silly and to highlight the dangers of Python\n", "tbl3.__class__ = Table " ], "id": "2bc42db1-adf2-4ce4-b37e-a86c79cc6085" }, { "cell_type": "code", "execution_count": 201, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "4" ] } } ], "source": [ "tbl3.num_legs()" ], "id": "958ed76d-ffac-43f6-a72c-df7870279924" }, { "cell_type": "code", "execution_count": 202, "metadata": {}, "outputs": [], "source": [ "class Table3():\n", " def __init__(self):\n", " self._not_legs = 4" ], "id": "5909653f-b1a1-4d15-a8d2-40f10614fadc" }, { "cell_type": "code", "execution_count": 203, "metadata": {}, "outputs": [], "source": [ "tbl4 = Table()" ], "id": "3906371b-d704-4620-bc9b-4e2870839c94" }, { "cell_type": "code", "execution_count": 204, "metadata": {}, "outputs": [], "source": [ "tbl4.__class__ = Table3" ], "id": "0836258a-380f-4f17-a9cd-5e390150bbdd" }, { "cell_type": "code", "execution_count": 205, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "10" ] } } ], "source": [ "tbl4._legs" ], "id": "4214bd35-7346-483d-9f3b-3d453639f3be" } ], "nbformat": 4, "nbformat_minor": 5, "metadata": { "kernelspec": { "name": "python3", "display_name": "Python 3 (ipykernel)", "language": "python" }, "language_info": { "name": "python", "codemirror_mode": { "name": "ipython", "version": "3" }, "file_extension": ".py", "mimetype": "text/x-python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.12" } } }