updates 03-06
This commit is contained in:
parent
a1ed6175de
commit
15ac4124f2
@ -300,7 +300,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 59,
|
||||
"execution_count": 8,
|
||||
"id": "ec197a99-5b09-480e-b7aa-8129474e03d2",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@ -308,26 +308,37 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"4576794160\n"
|
||||
"4384777776\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"x = \"MPCS\"\n",
|
||||
"x = \"MPCS\" \n",
|
||||
"print(id(x)) # Unique integer-value for the object pointed by x"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 9,
|
||||
"id": "68e3ade8-d9a1-4d9c-b2f6-382fb90e745a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"4384777776\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"y = \"MPCS\" \n",
|
||||
"print(id(y)) "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 60,
|
||||
"execution_count": 10,
|
||||
"id": "93b42c37-41d5-41ff-a92e-863bcf2ee78e",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@ -335,9 +346,9 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Fruit1 id = 4576869760 \n",
|
||||
" Fruit2 id = 4576746816\n",
|
||||
"Fruit3 id= 4576746816\n"
|
||||
"Fruit1 id = 4384803008 \n",
|
||||
" Fruit2 id = 4384782464\n",
|
||||
"Fruit3 id= 4384782464\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@ -349,6 +360,27 @@
|
||||
"print(f\"Fruit3 id= {id(fruit3)}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"id": "198a8eea-56d0-4402-baeb-0efe54486630",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"True"
|
||||
]
|
||||
},
|
||||
"execution_count": 25,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"fruit is fruit2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f36d4ec8-eefa-4474-84b1-b8be365329bc",
|
||||
@ -370,7 +402,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 61,
|
||||
"execution_count": 12,
|
||||
"id": "67ee08d9-96b4-426f-bc05-4dc7bfecdf96",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
@ -383,8 +415,8 @@
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"a == b True\n",
|
||||
"4576799552\n",
|
||||
"4576826368\n",
|
||||
"4384871424\n",
|
||||
"4384871168\n",
|
||||
"a is b False\n"
|
||||
]
|
||||
}
|
||||
@ -399,6 +431,47 @@
|
||||
"print(\"a is b\", a is b) # The id values are different"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"id": "1479f1f1-3d10-41c5-ac1b-30f5b9c4cef1",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"4366718832\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(id(None))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"id": "0c9d9d98-2bd2-41b9-aee8-d5cf68f76c21",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"4366718832"
|
||||
]
|
||||
},
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def f():\n",
|
||||
" pass\n",
|
||||
"id(f())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "37ad8b3a-2b9b-4879-afb2-a05cc3ab659e",
|
||||
@ -419,10 +492,20 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 16,
|
||||
"id": "3f65348f-9118-4840-ab30-6ef05e7dbf93",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"4384869248\n",
|
||||
"[1, 2, 3, 4]\n",
|
||||
"4384869248\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# list d\n",
|
||||
"d = [1, 2, 3]\n",
|
||||
@ -434,10 +517,20 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 17,
|
||||
"id": "7d1b2659-239c-4333-ac62-8533406bb025",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"4384746736\n",
|
||||
"Hello World\n",
|
||||
"4384771632\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# str D\n",
|
||||
"s = \"Hello\"\n",
|
||||
@ -470,21 +563,148 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "153d1f65-9d24-4f09-bb97-173c119e3528",
|
||||
"execution_count": 20,
|
||||
"id": "f01f68c9-927c-4076-a967-3b6fbbf90033",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "subslide"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"True\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"a = 1000\n",
|
||||
"b = 1000\n",
|
||||
"a = 100\n",
|
||||
"b = 100\n",
|
||||
"\n",
|
||||
"# Two different objects, two different ids.\n",
|
||||
"print(a is b)\n",
|
||||
"\n",
|
||||
"print(a is b)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"id": "150da8f9-213f-4fc7-9f47-78c33b38d169",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "subslide"
|
||||
}
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"500 True\n",
|
||||
"501 True\n",
|
||||
"502 True\n",
|
||||
"503 True\n",
|
||||
"504 True\n",
|
||||
"505 True\n",
|
||||
"506 True\n",
|
||||
"507 True\n",
|
||||
"508 True\n",
|
||||
"509 True\n",
|
||||
"510 True\n",
|
||||
"511 True\n",
|
||||
"512 True\n",
|
||||
"513 True\n",
|
||||
"514 True\n",
|
||||
"515 True\n",
|
||||
"516 True\n",
|
||||
"517 True\n",
|
||||
"518 True\n",
|
||||
"519 True\n",
|
||||
"520 True\n",
|
||||
"521 True\n",
|
||||
"522 True\n",
|
||||
"523 True\n",
|
||||
"524 True\n",
|
||||
"525 True\n",
|
||||
"526 True\n",
|
||||
"527 True\n",
|
||||
"528 True\n",
|
||||
"529 True\n",
|
||||
"530 True\n",
|
||||
"531 True\n",
|
||||
"532 True\n",
|
||||
"533 True\n",
|
||||
"534 True\n",
|
||||
"535 True\n",
|
||||
"536 True\n",
|
||||
"537 True\n",
|
||||
"538 True\n",
|
||||
"539 True\n",
|
||||
"540 True\n",
|
||||
"541 True\n",
|
||||
"542 True\n",
|
||||
"543 True\n",
|
||||
"544 True\n",
|
||||
"545 True\n",
|
||||
"546 True\n",
|
||||
"547 True\n",
|
||||
"548 True\n",
|
||||
"549 True\n",
|
||||
"550 True\n",
|
||||
"551 True\n",
|
||||
"552 True\n",
|
||||
"553 True\n",
|
||||
"554 True\n",
|
||||
"555 True\n",
|
||||
"556 True\n",
|
||||
"557 True\n",
|
||||
"558 True\n",
|
||||
"559 True\n",
|
||||
"560 True\n",
|
||||
"561 True\n",
|
||||
"562 True\n",
|
||||
"563 True\n",
|
||||
"564 True\n",
|
||||
"565 True\n",
|
||||
"566 True\n",
|
||||
"567 True\n",
|
||||
"568 True\n",
|
||||
"569 True\n",
|
||||
"570 True\n",
|
||||
"571 True\n",
|
||||
"572 True\n",
|
||||
"573 True\n",
|
||||
"574 True\n",
|
||||
"575 True\n",
|
||||
"576 True\n",
|
||||
"577 True\n",
|
||||
"578 True\n",
|
||||
"579 True\n",
|
||||
"580 True\n",
|
||||
"581 True\n",
|
||||
"582 True\n",
|
||||
"583 True\n",
|
||||
"584 True\n",
|
||||
"585 True\n",
|
||||
"586 True\n",
|
||||
"587 True\n",
|
||||
"588 True\n",
|
||||
"589 True\n",
|
||||
"590 True\n",
|
||||
"591 True\n",
|
||||
"592 True\n",
|
||||
"593 True\n",
|
||||
"594 True\n",
|
||||
"595 True\n",
|
||||
"596 True\n",
|
||||
"597 True\n",
|
||||
"598 True\n",
|
||||
"599 True\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# a = 100\n",
|
||||
"# b = 100\n",
|
||||
"\n",
|
||||
@ -492,7 +712,7 @@
|
||||
"# this means that a and b point to the same object\n",
|
||||
"# print(a is b)\n",
|
||||
"\n",
|
||||
"for i in range(200, 300):\n",
|
||||
"for i in range(500, 600):\n",
|
||||
" print(i, i is i)"
|
||||
]
|
||||
},
|
||||
@ -534,10 +754,20 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 26,
|
||||
"id": "c7e81136-78ee-4503-bc66-443dfcbfbccf",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"4384496640\n",
|
||||
"4384370048\n",
|
||||
"[1, 2, 3, 4] [1, 2, 3]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"x = [1, 2, 3]\n",
|
||||
"y = x.copy()\n",
|
||||
@ -551,7 +781,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": 27,
|
||||
"id": "10cafa1b-b1fe-4954-b005-4f6f953f36b5",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
@ -565,6 +795,7 @@
|
||||
"text": [
|
||||
"x is y False\n",
|
||||
"x[0] is y[0] True\n",
|
||||
"x[1] is y[1] True\n",
|
||||
"[[1, 2, 5], [3, 4]] \n",
|
||||
" [[1, 2, 5], [3, 4]]\n"
|
||||
]
|
||||
@ -578,6 +809,7 @@
|
||||
"\n",
|
||||
"print(\"x is y\", x is y)\n",
|
||||
"print(\"x[0] is y[0]\", x[0] is y[0])\n",
|
||||
"print(\"x[1] is y[1]\", x[1] is y[1])\n",
|
||||
"\n",
|
||||
"# print(x, y)\n",
|
||||
"x[0].append(5)\n",
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -9,15 +9,17 @@
|
||||
"\n",
|
||||
"The style of programming we've been doing is called **imperative** or **procedural**. Statements run in sequence and change a program's state.\n",
|
||||
"\n",
|
||||
"As we said early on, Python is multi-paradigm. \n",
|
||||
"**state** can be thought of as the status of all variables at a given time. Imperative programming relies heavily on functions updating state.\n",
|
||||
"\n",
|
||||
"As we said early on, Python is multi-paradigm. \n",
|
||||
"\n",
|
||||
"> \"[...] practicality beats purity.\"\n",
|
||||
"> \n",
|
||||
"> - The Zen of Python\n",
|
||||
"\n",
|
||||
"Languages like LISP, Haskell, Racket are purely functional & differ significantly from procedural & object-oriented languages.\n",
|
||||
"Languages like LISP, Haskell, and Racket are purely functional & differ significantly from procedural & object-oriented languages.\n",
|
||||
"\n",
|
||||
"Functional programming uses a more mathematical definition of functions. Instead of the recipe model of procedural programming, mathematical functions take input(s) and return an output. \n",
|
||||
"Functional programming uses a definition of functions more compatible with the mathematical definition. Instead of the recipe model of procedural programming, mathematical functions take input(s) and return an output. \n",
|
||||
"\n",
|
||||
"These functions do not have the concept of \"state\", that is, calling a function in math creates a mapping from inputs to outputs.\n",
|
||||
"\n",
|
||||
@ -42,12 +44,21 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 1,
|
||||
"id": "72bee260-b4d1-4c61-a689-8799d4dfb7c6",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"echo = <function echo at 0x10760c820>\n",
|
||||
"type(echo) = <class 'function'>\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def echo(message):\n",
|
||||
" print(message)\n",
|
||||
@ -59,12 +70,21 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 2,
|
||||
"id": "39ee1708-c3ec-47ed-9c87-2124fa55b57f",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"hello\n",
|
||||
"hello\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# we can assign other names to objects, including functions\n",
|
||||
"\n",
|
||||
@ -75,24 +95,48 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 3,
|
||||
"id": "5a175699-fd00-4e69-a534-2d25085550d2",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"(4418750496, 4418750496)"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"id(x), id(echo)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 4,
|
||||
"id": "52de62a9-af2f-4fbe-becf-2c315ddb9c77",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"0\n",
|
||||
"1\n",
|
||||
"1\n",
|
||||
"2\n",
|
||||
"3\n",
|
||||
"3\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# we can also store functions in other types\n",
|
||||
"\n",
|
||||
@ -103,12 +147,22 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 5,
|
||||
"id": "6cad8105-8a71-4b8a-926c-dfa0f635e7ad",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"twice\n",
|
||||
"twice\n",
|
||||
"once\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# dictionaries too\n",
|
||||
"func_mapping = {False: print, True: echo}\n",
|
||||
@ -130,12 +184,21 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 20,
|
||||
"id": "d89416fb-e0e7-48ca-9ca7-9fea5525b273",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"add, 3, 4 = 7\n",
|
||||
"sub, 3, 4 = -1\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# we can pass functions into other functions\n",
|
||||
"\n",
|
||||
@ -188,18 +251,93 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "9039813a-a943-40e1-802f-f230547ffa2d",
|
||||
"execution_count": 17,
|
||||
"id": "c898e181-1fcd-477e-b6bf-573544ed6170",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"x = [(\"Nick\", 1), (\"Yusong\", 9), (\"Emma\", 100)]\n",
|
||||
"x = [(\"Nick\", 1), (\"Yusong\", 9000), (\"Emma\", 100)]\n",
|
||||
"\n",
|
||||
"def negate(y):\n",
|
||||
" return -y\n",
|
||||
"\n",
|
||||
"def second_key(item):\n",
|
||||
" return item[1]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"id": "ff9cfd44-8a22-4c83-98a6-4420ce651238",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[('Nick', 1), ('Emma', 100), ('Yusong', 9000)]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"x.sort(key=second_key)\n",
|
||||
"print(x)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"id": "73f8aee0-8a13-43cf-b565-96285891dd4f",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"1"
|
||||
]
|
||||
},
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"second_key(x[0])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"id": "8c3c8074-4696-483a-9a16-238caec889f6",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[('Emma', 100), ('Nick', 1), ('Yusong', 9)]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"x.sort()\n",
|
||||
"print(x)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "3b13f2e4-f269-482a-b0b4-ed1831fd6e1b",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def second_key(item):\n",
|
||||
" return item[1]\n",
|
||||
" \n",
|
||||
"x.sort(key=lambda item: item[1])\n",
|
||||
"print(x)\n",
|
||||
"\n",
|
||||
@ -302,12 +440,23 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 21,
|
||||
"id": "68f51efb-9a62-4303-afe7-3639be9cc395",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"30"
|
||||
]
|
||||
},
|
||||
"execution_count": 21,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# can fit places a function definition can't\n",
|
||||
"# such as being used as a parameter\n",
|
||||
@ -326,7 +475,28 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 23,
|
||||
"id": "9b6748b0-9fb0-4664-86c6-bc35e4c5d9e2",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['abc', 'Abc', 'ABC', 'AbC']"
|
||||
]
|
||||
},
|
||||
"execution_count": 23,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"sorted([\"abc\", \"Abc\", \"ABC\", \"AbC\"], key=lambda s: s.upper())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"id": "39bf652e-f85d-4a66-96bf-0ae21054be26",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@ -342,12 +512,23 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 25,
|
||||
"id": "1eb1c524-30e1-4b20-b2a9-7b078e906c61",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"(function, function)"
|
||||
]
|
||||
},
|
||||
"execution_count": 25,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"type(mul), type(mul2)"
|
||||
]
|
||||
@ -414,12 +595,22 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 1,
|
||||
"id": "3e2768f5-8817-4dfc-8ff5-72a774f499fa",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"3\n",
|
||||
"4\n",
|
||||
"5\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def add_two(x):\n",
|
||||
" return x + 2\n",
|
||||
@ -430,24 +621,67 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 2,
|
||||
"id": "cd940ce1-c28a-4646-8846-f199f586cad8",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Help on class map in module builtins:\n",
|
||||
"\n",
|
||||
"class map(object)\n",
|
||||
" | map(func, *iterables) --> map object\n",
|
||||
" | \n",
|
||||
" | Make an iterator that computes the function using arguments from\n",
|
||||
" | each of the iterables. Stops when the shortest iterable is exhausted.\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",
|
||||
" | __next__(self, /)\n",
|
||||
" | Implement next(self).\n",
|
||||
" | \n",
|
||||
" | __reduce__(...)\n",
|
||||
" | Return state information for pickling.\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(map)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 3,
|
||||
"id": "04106e30-95df-432a-b6b0-40fcc58f51e5",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[3, 4, 5]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"x = list(map(add_two, [1, 2, 3]))\n",
|
||||
"print(x)"
|
||||
@ -455,59 +689,123 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 11,
|
||||
"id": "fba295ed-9dcb-4cbb-8b7e-3b0ff2965639",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"A!\n",
|
||||
"B?\n",
|
||||
"C.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# commonly used with lambdas\n",
|
||||
"\n",
|
||||
"for x in map(lambda x, y, z: x * y + z, (\"A\", \"B\", \"C\"), [\"!\", \"?\", \".\"]):\n",
|
||||
"for x in map(lambda x, y: x+y, (\"A\", \"B\", \"C\"), [\"!\", \"?\", \".\"]):\n",
|
||||
" print(x)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c4e1d7f9-c111-44ff-8525-de147f90aa56",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"execution_count": 12,
|
||||
"id": "18f5223a-b85a-44c6-9f49-40183a1e3208",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"A!!\n",
|
||||
"B???\n",
|
||||
"C....\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# operator module contains all of the common operators\n",
|
||||
"import operator\n",
|
||||
"\n",
|
||||
"operator.sub(20, 5)\n"
|
||||
"# number of parameters must match number of iterables\n",
|
||||
"for x in map(lambda x, y, z: x+y*z, (\"A\", \"B\", \"C\"), [\"!\", \"?\", \".\"], [2, 3, 4]):\n",
|
||||
" print(x)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 14,
|
||||
"id": "c4e1d7f9-c111-44ff-8525-de147f90aa56",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"15"
|
||||
]
|
||||
},
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# operator module contains all of the common operators in function form\n",
|
||||
"import operator\n",
|
||||
"operator.sub(20, 5)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"id": "e6383ce1-6e06-44a4-beb2-1e946b81c483",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[10, 10]"
|
||||
]
|
||||
},
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"\n",
|
||||
"list(map(operator.sub, [20, 19], [10, 9]))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 20,
|
||||
"id": "b58441e4-ba95-43e4-8151-dd25e1c7efd1",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{'AAA', 'BBB', 'CCC'}"
|
||||
]
|
||||
},
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# the result of `map` is an iterable\n",
|
||||
"# the result of `map` is a new kind of iterable (in fact a generator, which we'll cover next)\n",
|
||||
"\n",
|
||||
"# possible to pass into set or list \n",
|
||||
"# or anywhere you can pass an iterable\n",
|
||||
"# or use anywhere you can use an iterable\n",
|
||||
"set(map(lambda x: x * 3, (\"A\", \"B\", \"C\")))"
|
||||
]
|
||||
},
|
||||
@ -523,46 +821,90 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 21,
|
||||
"id": "7eaab05d-2b8e-45fa-ad97-565c2edac0f6",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['Abc', 'Xyz']"
|
||||
]
|
||||
},
|
||||
"execution_count": 21,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"list(map(lambda s: s.title(), filter(str.isupper, [\"a\", \"ABC\", \"AbCdeF\", \"XYZ\"])))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 22,
|
||||
"id": "c4a69097-c30c-4096-b69b-f3fa89a1bc7f",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['ABC', 'XYZ']"
|
||||
]
|
||||
},
|
||||
"execution_count": 22,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"list(filter(lambda s: s.isupper(), [\"a\", \"ABC\", \"AbCdeF\", \"XYZ\"]))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 23,
|
||||
"id": "6852f961-f20d-4c82-8da4-c7bdcfc1ae4b",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['abc', 'xyz']"
|
||||
]
|
||||
},
|
||||
"execution_count": 23,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"list(map(lambda s: s.lower(), filter(lambda s: s.isupper(), [\"a\", \"ABC\", \"AbCdeF\", \"XYZ\"])))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 24,
|
||||
"id": "a195957c-0e89-4d3e-9b85-ace9e1f84e5d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[1, 9, 25, 49, 81, 121, 169, 225, 289, 361]"
|
||||
]
|
||||
},
|
||||
"execution_count": 24,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"g = (x**2 for x in filter(lambda x: x % 2 != 0, range(20)))\n",
|
||||
"list(g)"
|
||||
@ -672,10 +1014,21 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 25,
|
||||
"id": "5a23eb2c-8324-4ff1-beb2-bca7ff5dbbe1",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"20"
|
||||
]
|
||||
},
|
||||
"execution_count": 25,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import operator\n",
|
||||
"operator.mul(2, 10)"
|
||||
@ -683,10 +1036,21 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 26,
|
||||
"id": "70988dc5-1a32-4316-bb9a-d4be646dd2c2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"-5"
|
||||
]
|
||||
},
|
||||
"execution_count": 26,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import functools\n",
|
||||
"negate = functools.partial(operator.mul, -1)\n",
|
||||
@ -695,10 +1059,19 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 27,
|
||||
"id": "7b2d87c2-903a-4b48-a012-3e3b0b03584f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"16\n",
|
||||
"16\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def calls_twice(f):\n",
|
||||
" print(f())\n",
|
||||
@ -713,10 +1086,19 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 28,
|
||||
"id": "0e2ffbc6-e05f-401e-b569-273c61f6d514",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"x!a!b!c\n",
|
||||
"x!a!b!c\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print_ex = functools.partial(print, \"x\", sep=\"!\")\n",
|
||||
"print_ex(\"a\", \"b\", \"c\")\n",
|
||||
@ -725,35 +1107,49 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 32,
|
||||
"id": "6e9be6b5-0a68-4de6-8289-31fb5095d31f",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# parameters must be valid\n",
|
||||
"print_foo = functools.partial(print, foo=\"x\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 30,
|
||||
"id": "10c4769b-7b1f-4fbc-924d-8b3e5de7fb93",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"ename": "TypeError",
|
||||
"evalue": "'foo' is an invalid keyword argument for print()",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
|
||||
"Cell \u001b[0;32mIn[30], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mprint_foo\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mhello\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n",
|
||||
"\u001b[0;31mTypeError\u001b[0m: 'foo' is an invalid keyword argument for print()"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print_foo(\"hello\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 34,
|
||||
"id": "318556fb-eafa-44af-bcfd-0848b87b6e18",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# another way to deal with functions we're calling with the same args repeatedly\n",
|
||||
"def request_page(url, verify, cache=True, send_cookies=False, https_only=True):\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
@ -762,15 +1158,14 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 38,
|
||||
"id": "9b4c5678-2525-4822-93e3-77dd0f40213b",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"secure_request(\"\", verify=False)\n",
|
||||
"sec"
|
||||
"secure_request(\"\", verify=False)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
541
06.generators-comprehensions.ipynb
Normal file
541
06.generators-comprehensions.ipynb
Normal file
@ -0,0 +1,541 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "2663b868-8c7e-4776-9c3b-0cc03d581ee5",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Generators & Comprehensions\n",
|
||||
"\n",
|
||||
"### Generator Functions\n",
|
||||
"\n",
|
||||
"A generator function works differently from all of the functions we've seen before. It allows the function to return (using the `yield` statement) and resume where it left off, with internal state intact.\n",
|
||||
"\n",
|
||||
"Between calls to the generator function, state is suspended."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "f6f883d1-88fd-4488-8af7-17c15a47e933",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def simple_generator_func():\n",
|
||||
" print(\"start\")\n",
|
||||
" yield 1\n",
|
||||
" print(\"still running\")\n",
|
||||
" yield 2\n",
|
||||
" print(\"one more\")\n",
|
||||
" yield 3"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "2260b250-1ae6-4d3b-ab05-314ddad19256",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"<generator object simple_generator_func at 0x1064afd80>"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# return type of a yielding function is a generator\n",
|
||||
"simple_generator_func()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "c051b45d-9a37-4086-b90c-2090110c8cc6",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"g = simple_generator_func()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "427fe881-459e-4a20-acfa-dae65a1b906c",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"source": [
|
||||
"##"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "6a87d7d2-9eb5-4415-9770-79d9594aafa5",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"start\n",
|
||||
"yielded value= 1\n",
|
||||
"still running\n",
|
||||
"yielded value= 2\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"for x in simple_generator_func():\n",
|
||||
" print(\"yielded value=\", x)\n",
|
||||
" if x == 2:\n",
|
||||
" break"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "544d5a37-a1c0-47f6-b270-429ea2215386",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def evens_up_to(n): \n",
|
||||
" for i in range(2, n + 1):\n",
|
||||
" if i % 2 == 0:\n",
|
||||
" yield i"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "7fbc6cf5-0995-4d20-b47d-6b5454899168",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"2\n",
|
||||
"4\n",
|
||||
"6\n",
|
||||
"8\n",
|
||||
"10\n",
|
||||
"12\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"for evens in evens_up_to(10000000000000):\n",
|
||||
" print(evens)\n",
|
||||
" if evens > 10:\n",
|
||||
" break"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "2fbf706b-6f6c-4a24-890e-10121d709582",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Generators do not have to ever exit, here is an infinite generator\n",
|
||||
"def powers_of_two():\n",
|
||||
" n = 2\n",
|
||||
" while True:\n",
|
||||
" yield n\n",
|
||||
" n *= 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"id": "32920b91-7b00-4b2a-8927-07bd5cbc6fa2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# list(powers_of_two())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"id": "27a8bd75-3912-4f97-9655-763279f07f9b",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"2\n",
|
||||
"4\n",
|
||||
"8\n",
|
||||
"16\n",
|
||||
"32\n",
|
||||
"64\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"for x in powers_of_two():\n",
|
||||
" if x > 100:\n",
|
||||
" break\n",
|
||||
" print(x)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"id": "dd4f4d70-e74a-43d3-88bc-7dfe04add2a2",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"range(0, 1000000000000000000)"
|
||||
]
|
||||
},
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"range(1000000000000000000)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"id": "9be584c1-4cf9-4ad3-88ab-4b5da18e42da",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"g = powers_of_two()\n",
|
||||
"h = powers_of_two()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"id": "81095f30-547f-4624-b2a4-9cc5997c4a8b",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"True"
|
||||
]
|
||||
},
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"g is not h"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"id": "e34470cd-efdd-45d0-a1b5-58e478260e4c",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"2"
|
||||
]
|
||||
},
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"next(g)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"id": "2e656696-8e3e-4dfd-86f9-9b87a08debe3",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"2"
|
||||
]
|
||||
},
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"next(h)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"id": "39b39657-243c-4641-9859-90fdb7adb3fc",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"range(0, 100)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"r = range(100)\n",
|
||||
"print(r)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "308fc5a9-78ac-40cd-afac-970614e9460e",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Discussion: Benefits of Generators"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "4fefa3b8-0727-4586-97df-698b91d91c6f",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"- Avoids creating entire collections up front.\n",
|
||||
"\n",
|
||||
"- Can result in drastic memory savings.\n",
|
||||
"\n",
|
||||
" - How much memory does `range(100000)` need?\n",
|
||||
"\n",
|
||||
"- Avoids doing expensive computations until necessary."
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "229fa10f-331e-4b72-959e-d28b952760ee",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Generator Expressions / Comprehensions\n",
|
||||
"\n",
|
||||
"Exact same syntax as a list comprehension, but results in a generator object.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"```python\n",
|
||||
"g = (expression for var in iterable)\n",
|
||||
"\n",
|
||||
"# or \n",
|
||||
"\n",
|
||||
"g = (expression for var in iterable if condition)\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"Creates a generator that yields `expression` for each iteration of the for loop. (Optionally only if the `condition` is satisfied)\n",
|
||||
"\n",
|
||||
"Equivalent to:\n",
|
||||
"\n",
|
||||
"```python\n",
|
||||
"def g():\n",
|
||||
" for var in iterable:\n",
|
||||
" if condition:\n",
|
||||
" yield expression\n",
|
||||
"```"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 37,
|
||||
"id": "aa9b4c18-3d73-4d9c-8e78-3dba56b4a717",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"<generator object <genexpr> at 0x107f1cf90>\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"pow_generator = (i + 1 for i in powers_of_two())\n",
|
||||
"print(pow_generator)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "2e69c9d6-11ec-4e01-9116-95f55c8c2c7e",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 38,
|
||||
"id": "729f6476-491a-4daf-b460-23f602ef18fa",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"3\n",
|
||||
"5\n",
|
||||
"9\n",
|
||||
"17\n",
|
||||
"33\n",
|
||||
"65\n",
|
||||
"129\n",
|
||||
"257\n",
|
||||
"513\n",
|
||||
"1025\n",
|
||||
"2049\n",
|
||||
"4097\n",
|
||||
"8193\n",
|
||||
"16385\n",
|
||||
"32769\n",
|
||||
"65537\n",
|
||||
"131073\n",
|
||||
"262145\n",
|
||||
"524289\n",
|
||||
"1048577\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# can be used just like other generators we've seen (as an iterable)\n",
|
||||
"for i in pow_generator:\n",
|
||||
" print(i)\n",
|
||||
" # will run forever without this\n",
|
||||
" if i > 1000000:\n",
|
||||
" break"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 39,
|
||||
"id": "cfed52c0-0467-4b50-ac6c-66d6fa694ec6",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"2097153\n",
|
||||
"4194305\n",
|
||||
"8388609\n",
|
||||
"16777217\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"for x in pow_generator:\n",
|
||||
" print(x) # can resume the generator\n",
|
||||
" if x > 10000000:\n",
|
||||
" break"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 49,
|
||||
"id": "ac1be105-edc6-4b52-8715-e54c186861d2",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[0, 4, 16, 36, 64]"
|
||||
]
|
||||
},
|
||||
"execution_count": 49,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# also possible to specify conditionals in generator expression, same as comprehension\n",
|
||||
"gen2 = (i**2 for i in range(10) if i % 2 == 0)\n",
|
||||
"list(gen2)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 52,
|
||||
"id": "c721ff55-86ac-46b1-826f-4613905162c8",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[0, 4, 16, 36, 64]"
|
||||
]
|
||||
},
|
||||
"execution_count": 52,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# equivalent with map & filter\n",
|
||||
"ll = range(10)\n",
|
||||
"list(map(lambda x: x**2, filter(lambda x: x % 2 == 0, ll)))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a5aba588-3823-4763-b1ab-5a6217f72f8e",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"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.10.15"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
Loading…
Reference in New Issue
Block a user