Skip to content

Commit b9a8ec5

Browse files
add content
1 parent 0c475e1 commit b9a8ec5

36 files changed

+11433
-678
lines changed

Chapter1/function.ipynb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"id": "516aab5c",
6565
"metadata": {},
6666
"source": [
67-
"### When to Use and Not to Use Lambda Functions"
67+
"### Lambda vs Named Functions: When to Use Each"
6868
]
6969
},
7070
{
@@ -73,7 +73,9 @@
7373
"id": "5d4d1401",
7474
"metadata": {},
7575
"source": [
76-
"Lambda functions are helpful when defining a function that is used only once and does not require a name."
76+
"Lambda functions are ideal for situations where a function is used only once and does not require a name. They provide a concise way to define small, one-time use functions.\n",
77+
"\n",
78+
"For example:"
7779
]
7880
},
7981
{
@@ -89,13 +91,22 @@
8991
"even_numbers = filter(lambda num: num % 2 == 0, numbers)"
9092
]
9193
},
94+
{
95+
"cell_type": "markdown",
96+
"id": "3e53c4c5-8270-46b4-b94a-59d20c03ce80",
97+
"metadata": {},
98+
"source": [
99+
"In this example, the lambda function is used to filter out even numbers from the list. Since it's only used once, a lambda function is a suitable choice."
100+
]
101+
},
92102
{
93103
"attachments": {},
94104
"cell_type": "markdown",
95105
"id": "e68f8ef3",
96106
"metadata": {},
97107
"source": [
98-
"However, if you need to reuse a function in various parts of your code, use a named function to avoid repeating the same code. "
108+
"However, if you need to reuse a function in various parts of your code, it's better to define a named function.\n",
109+
"\n"
99110
]
100111
},
101112
{
@@ -124,6 +135,14 @@
124135
"any(is_even(num) for num in numbers)"
125136
]
126137
},
138+
{
139+
"cell_type": "markdown",
140+
"id": "9b774fdb-662d-4ef6-9f2a-08c29cc7ff8c",
141+
"metadata": {},
142+
"source": [
143+
"In this example, the `is_even` function is defined by a name and is used multiple times. This approach avoids repeating the same code and makes your code more maintainable."
144+
]
145+
},
127146
{
128147
"attachments": {},
129148
"cell_type": "markdown",
@@ -524,7 +543,7 @@
524543
"name": "python",
525544
"nbconvert_exporter": "python",
526545
"pygments_lexer": "ipython3",
527-
"version": "3.11.4"
546+
"version": "3.11.6"
528547
},
529548
"toc": {
530549
"base_numbering": 1,

Chapter1/python_new_features.ipynb

Lines changed: 96 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@
513513
"id": "9838891c",
514514
"metadata": {},
515515
"source": [
516-
"### Walrus Operator: Assign a Variable in an Expression"
516+
"### Write Cleaner Python with the Walrus Operatorn"
517517
]
518518
},
519519
{
@@ -522,27 +522,44 @@
522522
"id": "b2477b9e",
523523
"metadata": {},
524524
"source": [
525-
"The walrus operator (`:=`) in Python 3.8 and above allows you to assign a variable in an expression. The walrus operator is useful when you want to:\n",
526-
"- Debug the components in an expression\n",
527-
"- Avoid repeated computations\n",
528-
"- Assign a meaningful name to an expression"
525+
"The walrus operator (`:=`) in Python 3.8+ allows you to assign a variable in an expression, making your code more readable and efficient. It's useful in two main scenarios:\n",
526+
"\n",
527+
"1. Giving a meaningful name to a complex expression for better readability.\n",
528+
"2. Avoiding repeated computations by reusing a variable instead of recomputing the expression."
529+
]
530+
},
531+
{
532+
"cell_type": "markdown",
533+
"id": "9739695b-7ddb-4997-9408-56bd7e53dd30",
534+
"metadata": {},
535+
"source": [
536+
"Let's consider an example where we want to calculate the radius, area, and volume of a circle given its diameter and height:"
529537
]
530538
},
531539
{
532540
"cell_type": "code",
533-
"execution_count": null,
541+
"execution_count": 10,
534542
"id": "68b0e1a3",
535543
"metadata": {},
536544
"outputs": [],
537545
"source": [
538546
"from math import pi\n",
539547
"\n",
540-
"diameter = 4\n"
548+
"diameter = 4\n",
549+
"height = 2"
550+
]
551+
},
552+
{
553+
"cell_type": "markdown",
554+
"id": "d43f5da9-89e1-4e8a-b29d-f48ad247a9d0",
555+
"metadata": {},
556+
"source": [
557+
"Without the walrus operator, we might compute the radius and area multiple times:"
541558
]
542559
},
543560
{
544561
"cell_type": "code",
545-
"execution_count": 16,
562+
"execution_count": 12,
546563
"id": "14cfc887",
547564
"metadata": {},
548565
"outputs": [
@@ -552,26 +569,31 @@
552569
"2.0"
553570
]
554571
},
555-
"execution_count": 16,
572+
"execution_count": 12,
556573
"metadata": {},
557574
"output_type": "execute_result"
558575
}
559576
],
560577
"source": [
561-
"# without Walrus operator\n",
562-
"\n",
563578
"circle = {\n",
564579
" \"radius\": diameter / 2, # computed twice\n",
565-
" \"area\": pi * (diameter / 2)**2,\n",
566-
"}\n",
567-
"\n",
568-
"diameter / 2\n"
580+
" \"area\": pi * (diameter / 2)**2, # computed twice\n",
581+
" \"volume\": pi * (diameter / 2)**2 * height,\n",
582+
"}"
583+
]
584+
},
585+
{
586+
"cell_type": "markdown",
587+
"id": "4c9997c8-5069-4723-8fc1-16f823304760",
588+
"metadata": {},
589+
"source": [
590+
"To avoid repeated computations, we can assign the radius and area to variables before creating the dictionary:"
569591
]
570592
},
571593
{
572594
"cell_type": "code",
573-
"execution_count": 15,
574-
"id": "805e31ea",
595+
"execution_count": 13,
596+
"id": "b18fb3eb-45b2-43bb-93d5-7a0cd6ded503",
575597
"metadata": {},
576598
"outputs": [
577599
{
@@ -580,20 +602,70 @@
580602
"2.0"
581603
]
582604
},
583-
"execution_count": 15,
605+
"execution_count": 13,
584606
"metadata": {},
585607
"output_type": "execute_result"
586608
}
587609
],
588610
"source": [
589-
"# with Walrus operator\n",
611+
"radius = diameter / 2\n",
612+
"area = pi * radius**2\n",
590613
"\n",
614+
"circle = {\n",
615+
" \"radius\": radius,\n",
616+
" \"area\": area,\n",
617+
" \"volume\": area * height,\n",
618+
"}"
619+
]
620+
},
621+
{
622+
"cell_type": "markdown",
623+
"id": "d220fa6a-6ca4-41c2-a1bb-e0b458fb03dc",
624+
"metadata": {},
625+
"source": [
626+
"To make the code more concise, we can use the walrus operator to assign the radius and area to variables while creating the dictionary."
627+
]
628+
},
629+
{
630+
"cell_type": "code",
631+
"execution_count": 16,
632+
"id": "805e31ea",
633+
"metadata": {},
634+
"outputs": [],
635+
"source": [
591636
"circle = {\n",
592637
" \"radius\": (radius := diameter / 2),\n",
593-
" \"area\": pi * radius**2,\n",
594-
"}\n",
595-
"\n",
596-
"radius"
638+
" \"area\": (area := pi * radius**2),\n",
639+
" \"volume\": area * height,\n",
640+
"}"
641+
]
642+
},
643+
{
644+
"cell_type": "markdown",
645+
"id": "0719cf42-6f2b-4f38-b5a2-12e4765111f7",
646+
"metadata": {},
647+
"source": [
648+
"After executing the code with the walrus operator, we can access the assigned variables:"
649+
]
650+
},
651+
{
652+
"cell_type": "code",
653+
"execution_count": 17,
654+
"id": "91908a69-8f78-42c8-b56b-cc12d6ac28d4",
655+
"metadata": {},
656+
"outputs": [
657+
{
658+
"name": "stdout",
659+
"output_type": "stream",
660+
"text": [
661+
"2.0\n",
662+
"12.566370614359172\n"
663+
]
664+
}
665+
],
666+
"source": [
667+
"print(radius)\n",
668+
"print(area)"
597669
]
598670
},
599671
{
@@ -713,7 +785,7 @@
713785
],
714786
"metadata": {
715787
"kernelspec": {
716-
"display_name": "Python 3",
788+
"display_name": "Python 3 (ipykernel)",
717789
"language": "python",
718790
"name": "python3"
719791
},

Chapter2/dataclasses.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@
503503
],
504504
"metadata": {
505505
"kernelspec": {
506-
"display_name": "Python 3 (ipykernel)",
506+
"display_name": "venv",
507507
"language": "python",
508508
"name": "python3"
509509
},

Chapter5/machine_learning.ipynb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2639,7 +2639,7 @@
26392639
"celltoolbar": "Tags",
26402640
"hide_input": false,
26412641
"kernelspec": {
2642-
"display_name": "Python 3 (ipykernel)",
2642+
"display_name": "venv",
26432643
"language": "python",
26442644
"name": "python3"
26452645
},
@@ -2667,11 +2667,6 @@
26672667
"toc_position": {},
26682668
"toc_section_display": true,
26692669
"toc_window_display": false
2670-
},
2671-
"vscode": {
2672-
"interpreter": {
2673-
"hash": "c3bc044b9863ed6dec4c55e7ad5af27f030f7d27aed3f39d7a4886a926c4e2c1"
2674-
}
26752670
}
26762671
},
26772672
"nbformat": 4,

Chapter5/render.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
<!DOCTYPE html>
3+
<html lang="en">
4+
<head>
5+
<meta charset="UTF-8">
6+
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/chart.xkcd@1.1/dist/chart.xkcd.min.js"></script>
7+
</head>
8+
<body>
9+
<div id="5262ec43a4b74316a579ac0216eb636b" class="chart-container" style="width: 800px">
10+
<svg id="chart_5262ec43a4b74316a579ac0216eb636b"></svg>
11+
</div>
12+
<script>
13+
const svg_5262ec43a4b74316a579ac0216eb636b = document.querySelector('#chart_5262ec43a4b74316a579ac0216eb636b')
14+
const chart_5262ec43a4b74316a579ac0216eb636b = new chartXkcd.Line(svg_5262ec43a4b74316a579ac0216eb636b, {"title": "\u67d0\u5546\u573a\u9500\u552e\u60c5\u51b5", "data": {"datasets": [{"label": "series-A", "data": [57, 134, 137, 129, 145, 60, 49]}, {"label": "series-B", "data": [114, 55, 27, 101, 125, 27, 105]}], "labels": ["\u886c\u886b", "\u6bdb\u8863", "\u9886\u5e26", "\u88e4\u5b50", "\u98ce\u8863", "\u9ad8\u8ddf\u978b", "\u889c\u5b50"]}, "xLabel": "I'm xlabel", "yLabel": "I'm ylabel", "options": {"yTickCount": 3, "legendPosition": 1}});
15+
</script>
16+
</body>
17+
</html>

0 commit comments

Comments
 (0)