Merge pull request #10 from anine09/main

1. Revise the contents
2. Fix import student module issue
This commit is contained in:
Chuanyu Xue
2022-07-12 21:13:17 -04:00
committed by GitHub
4 changed files with 297 additions and 279 deletions

View File

@@ -497,14 +497,14 @@
" - `**` 表示乘方,`1 ** 2` 的结果是 1\n",
" - `/` 表示除法,`1 / 2` 的结果是 0.5 \n",
" - `//` 表示整除,`1 // 2` 的结果是 0 (相当于除法结果向下取整)\n",
" - `%` 表示取余 `1 % 2` 的结果是 1\n",
" - `%` 表示取余`1 % 2` 的结果是 1\n",
" \n",
" \n",
"- 逻辑运算符\n",
" - `>` 表示大于,`1 > 2` 的结果是 False\n",
" - `>=` 表示大于等于, `1 >= 2` 的结果是 False\n",
" - `<=` 表示小于,`1 > 2` 的结果是 True\n",
" - `<` 表示小于等于, `1 >= 2` 的结果是 True\n",
" - `<=` 表示小于,`1 <= 2` 的结果是 True\n",
" - `<` 表示小于等于, `1 < 2` 的结果是 True\n",
" - `==` 表示等于, `1 == 2` 的结果是 False\n",
" - `!=` 表示不等于, `1 != 2` 的结果是 True\n",
" - `and` 表示逻辑\"与\"`True and False` 的结果是 False\n",
@@ -517,11 +517,18 @@
" - `<<` 表示左移操作\n",
" - `&` 表示按位与\n",
" - `|` 表示按位或\n",
" - `^` 表示按位或\n",
" - `^` 表示按位或\n",
" - `~` 表示按位取反\n",
" \n",
"其中最常用的是算数运算符与逻辑运算符,位运算符在 集合 操作中经常使用。\n",
" "
"\n",
"附:逻辑运算参照表\n",
"| X | Y | X and Y | X or Y | not X | not Y |\n",
"| ----- | ----- | ------- | ------ | ----- | ----- |\n",
"| True | True | True | True | False | False |\n",
"| True | False | False | True | False | True |\n",
"| False | False | False | False | True | True |\n",
"| False | True | False | True | True | False |\n"
]
},
{
@@ -1334,11 +1341,8 @@
}
],
"metadata": {
"interpreter": {
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3.7.1 ('DataScience')",
"language": "python",
"name": "python3"
},
@@ -1352,7 +1356,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.2"
"version": "3.7.1"
},
"toc": {
"base_numbering": 1,
@@ -1366,6 +1370,11 @@
"toc_position": {},
"toc_section_display": true,
"toc_window_display": true
},
"vscode": {
"interpreter": {
"hash": "4951620600495571d210fcc70149573e34741e5378bdea11097277e6fa3349d5"
}
}
},
"nbformat": 4,

View File

@@ -15,11 +15,11 @@
"source": [
"顾名思义,数据结构是能够将数据组合在一起的一种结构。\n",
"\n",
"在数据科学领域,很多情况下需要将数据进行有序排列。例如我们统计了大学某班 50 人的数学成绩,那么创建 50 个变量例如 XiaoMing = 99, XiaoHu = 86 .... 无疑是非常繁琐的。这时我们可以通过数据结构整合这些数据,例如在上一节中以方括号标识的列 `[ 99, 86, 77 .... ]`,这将会使我们的程序大大简化。\n",
"在数据科学领域,很多情况下需要将数据进行有序排列。例如我们统计了大学某班 50 人的数学成绩,那么创建 50 个变量例如 XiaoMing = 99, XiaoHu = 86 .... 无疑是非常繁琐的。这时我们可以通过数据结构整合这些数据,例如在上一节中以方括号标识的列 `[ 99, 86, 77 .... ]`,这将会使我们的程序大大简化。\n",
"\n",
"Python 中常用的数据结构有:\n",
"\n",
"- 列 List: 用于保存有序项集合的变量,以方括号标识。\n",
"- 列 List: 用于保存有序项集合的变量,以方括号标识。\n",
"- 元组 Tuple: 用于保存有序项集合的常量,以圆括号标识。\n",
"- 字典 Dict: 用于保存无序(键,值)项集合的变量,以花括号标识。\n",
"- 集合 Set: 用于保存无序项集合的变量,以花括号标识。\n",
@@ -40,7 +40,7 @@
"id": "according-admission",
"metadata": {},
"source": [
"## 1.2.1 列"
"## 1.2.1 列"
]
},
{
@@ -48,7 +48,7 @@
"id": "union-medication",
"metadata": {},
"source": [
"列是用于保存有序项集合的变量,通过方括号创建。列是最容易理解的数据结构,它就像一个任务清单,任务清单的每一项都是一个单独的任务。\n",
"列是用于保存有序项集合的变量,通过方括号创建。列是最容易理解的数据结构,它就像一个任务清单,任务清单的每一项都是一个单独的任务。\n",
"\n",
"下面我们创建一个含有四个整数的列表。"
]
@@ -69,14 +69,14 @@
"metadata": {},
"source": [
"\n",
"列支持以下操作:\n",
"列支持以下操作:\n",
"\n",
"- 增:通过函数 `append` 可以向列内增加元素\n",
"- 删:通过关键字 `del` 可以删除列内元素\n",
"- 查:通过关键字 `[ ]` 可以查找列某个位置元素\n",
"- 增:通过函数 `append` 可以向列内增加元素\n",
"- 删:通过关键字 `del` 可以删除列内元素\n",
"- 查:通过关键字 `[ ]` 可以查找列某个位置元素\n",
"- 改:通过赋值符号 `=` 可以修改某个位置的元素\n",
"\n",
"列的优点是:\n",
"列的优点是:\n",
"\n",
"- 快速向尾部添加元素\n",
"- 快速遍历所有元素\n",
@@ -96,9 +96,9 @@
"id": "stretch-newsletter",
"metadata": {},
"source": [
"我们通过 `[ ]` 关键字查找列中某个位置的元素。\n",
"我们通过 `[ ]` 关键字查找列中某个位置的元素。\n",
"\n",
"例如 `l[0]` 可以获取列中首个元素,`l[1]` 可以获取列中第 2 个元素。同时它还支持倒序查找,例如 `l[-1]` 表示倒数第一个元素(末尾的元素)。"
"例如 `l[0]` 可以获取列中首个元素,`l[1]` 可以获取列中第 2 个元素。同时它还支持倒序查找,例如 `l[-1]` 表示倒数第一个元素(末尾的元素)。"
]
},
{
@@ -134,9 +134,9 @@
"id": "photographic-worker",
"metadata": {},
"source": [
"`[ ]` 关键字也可以通过 “切片” 的形式获取含有多个元素的子列。\n",
"`[ ]` 关键字也可以通过 “切片” 的形式获取含有多个元素的子列。\n",
"\n",
"例如 `l[0:2]` 代表列从中第 0 个元素 到 第 2 个元素(前开后闭,不包括第 2 个元素)"
"例如 `l[0:2]` 代表列从中第 0 个元素 到 第 2 个元素(左闭右开 `[ ) `,不包括第 2 个元素)"
]
},
{
@@ -155,9 +155,9 @@
}
],
"source": [
"## 查找第 0 到 第 2 的元素子列\n",
"## 查找第 0 到 第 2 的元素子列\n",
"print(l[0:2])\n",
"## 查找第 1 到 最后 的元素子列\n",
"## 查找第 1 到 最后 的元素子列\n",
"print(l[1:-1])"
]
},
@@ -174,7 +174,7 @@
"id": "alien-appeal",
"metadata": {},
"source": [
"通过 `[ ]` 关键字同样可以修改列中某个位置的元素,类似的它也支持倒序以及切片的形式。"
"通过 `[ ]` 关键字同样可以修改列中某个位置的元素,类似的它也支持倒序以及切片的形式。"
]
},
{
@@ -212,7 +212,7 @@
}
],
"source": [
"## 修改从第 0 到第 2 的元素子列的值为 [-1, -2]\n",
"## 修改从第 0 到第 2 的元素子列的值为 [-1, -2]\n",
"l[0:2] = [-1, -2]\n",
"print(l)"
]
@@ -230,7 +230,7 @@
"id": "political-astrology",
"metadata": {},
"source": [
"通过 `append` 函数可以实现向列尾部添加新的元素。"
"通过 `append` 函数可以实现向列尾部添加新的元素。"
]
},
{
@@ -270,7 +270,7 @@
"id": "similar-delhi",
"metadata": {},
"source": [
"通过 `del` 关键字可以删除列某个位置的元素。"
"通过 `del` 关键字可以删除列某个位置的元素。"
]
},
{
@@ -289,10 +289,10 @@
}
],
"source": [
"## 删除列 首个 元素\n",
"## 删除列 首个 元素\n",
"del l[0]\n",
"print(l)\n",
"## 删除列 最后一个 元素\n",
"## 删除列 最后一个 元素\n",
"del l[-1]\n",
"print(l)"
]
@@ -310,7 +310,7 @@
"id": "correct-coalition",
"metadata": {},
"source": [
"在熟悉了列的增删查找功能后,我们就可以尝试以此为基础搭建我们的学生成绩管理系统。"
"在熟悉了列的增删查找功能后,我们就可以尝试以此为基础搭建我们的学生成绩管理系统。"
]
},
{
@@ -342,12 +342,12 @@
"source": [
"## Task 1 建立学生信息管理系统\n",
"\n",
"## 首先建立一个 “名字” 列记录哪个学生在列的哪个位置。\n",
"## 首先建立一个 “名字” 列记录哪个学生在列的哪个位置。\n",
"names = ['XiaoHu', 'XiaoMing', 'XiaoWei']\n",
"\n",
"## 根据名字列的位置分别建立 “语文成绩” “数学成绩序列” 序列。\n",
"math_scores = [65, 80, 95]\n",
"language_scores = [55, 92, 98]"
"## 根据名字列的位置分别建立 “语文成绩” “数学成绩列表” 列表。\n",
"Math_scores = [65, 80, 95]\n",
"Chinese_scores = [55, 92, 98]"
]
},
{
@@ -361,35 +361,35 @@
"\n",
"## 首先找到 \"XiaoHu\" 在哪个位置,更新该位置的成绩\n",
"## 通过 for-in 循环遍历名字元素\n",
"position = -1\n",
"position = None\n",
"count = 0\n",
"for name in names:\n",
" ## 找到 XiaoHu 在列中的位置\n",
" ## 找到 XiaoHu 在列中的位置\n",
" if name == \"XiaoHu\":\n",
" position = count\n",
" count = count + 1\n",
"## 根据 XiaoHu 在列中的位置更新成绩\n",
"math_scores[position] = 95\n",
"language_scores[position] = 85\n",
"## 根据 XiaoHu 在列中的位置更新成绩\n",
"Math_scores[position] = 95\n",
"Chinese_scores[position] = 85\n",
"\n",
"## 以同样方法更新 XiaoMing 与 XiaoWei 的成绩\n",
"position = -1\n",
"position = None\n",
"count = 0\n",
"for name in names:\n",
" if name == \"XiaoMing\":\n",
" position = count\n",
" count = count + 1\n",
"math_scores[position] = 75\n",
"language_scores[position] = 71\n",
"Math_scores[position] = 75\n",
"Chinese_scores[position] = 71\n",
"\n",
"position = -1\n",
"position = None\n",
"count = 0\n",
"for name in names:\n",
" if name == \"XiaoWei\":\n",
" position = count\n",
" count = count + 1\n",
"math_scores[position] = 92\n",
"language_scores[position] = 93"
"Math_scores[position] = 92\n",
"Chinese_scores[position] = 93"
]
},
{
@@ -410,8 +410,8 @@
],
"source": [
"print(names)\n",
"print(math_scores)\n",
"print(language_scores)"
"print(Math_scores)\n",
"print(Chinese_scores)"
]
},
{
@@ -425,17 +425,17 @@
"\n",
"## 首先找到 \"XiaoMing\" 在哪个位置\n",
"## 通过 for-in 循环遍历名字元素\n",
"position = -1\n",
"position = None\n",
"count = 0\n",
"for name in names:\n",
" ## 找到 XiaoMing 在列中的位置\n",
" ## 找到 XiaoMing 在列中的位置\n",
" if name == \"XiaoMing\":\n",
" position = count\n",
" count = count + 1\n",
"## 根据 XiaoMing 在列中的位置删除\n",
"## 根据 XiaoMing 在列中的位置删除\n",
"del names[position]\n",
"del math_scores[position]\n",
"del language_scores[position]"
"del Math_scores[position]\n",
"del Chinese_scores[position]"
]
},
{
@@ -456,13 +456,13 @@
],
"source": [
"print(names)\n",
"print(math_scores)\n",
"print(language_scores)"
"print(Math_scores)\n",
"print(Chinese_scores)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 14,
"id": "homeless-bangladesh",
"metadata": {},
"outputs": [],
@@ -470,13 +470,13 @@
"## Task 4 录入 Cryin 的信息\n",
"\n",
"names.append('Cryin')\n",
"math_scores.append(87)\n",
"language_scores.append(88)"
"Math_scores.append(87)\n",
"Chinese_scores.append(88)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 13,
"id": "european-encounter",
"metadata": {},
"outputs": [
@@ -484,16 +484,16 @@
"name": "stdout",
"output_type": "stream",
"text": [
"['XiaoHu', 'XiaoWei', 'Cryin']\n",
"[95, 92, 87]\n",
"[85, 93, 88]\n"
"['XiaoHu', 'XiaoWei']\n",
"[95, 92]\n",
"[85, 93]\n"
]
}
],
"source": [
"print(names)\n",
"print(math_scores)\n",
"print(language_scores)"
"print(Math_scores)\n",
"print(Chinese_scores)"
]
},
{
@@ -501,7 +501,7 @@
"id": "confident-utility",
"metadata": {},
"source": [
"以上我们就初步实现了学生成绩管理系统,并实现了用户的一些简单需求。可以发现列的增删改操作相对轻松,但最困难的部分是需要遍历整个列寻找元素在列中的位置,这种困难是由列在计算机中的存储形式决定的。后面我们介绍的数据结构 “字典” 可以用来解决这个问题。"
"以上我们就初步实现了学生成绩管理系统,并实现了用户的一些简单需求。可以发现列的增删改操作相对轻松,但最困难的部分是需要遍历整个列寻找元素在列中的位置,这种困难是由列在计算机中的存储形式决定的。后面我们介绍的数据结构 “字典” 可以用来解决这个问题。"
]
},
{
@@ -660,9 +660,9 @@
"metadata": {},
"source": [
"字典支持以下操作:\n",
"- 增:通过关键字 `[ ]` 可以向列内增加元素\n",
"- 删:通过关键字 `del` 可以删除列内元素\n",
"- 查:通过关键字 `[ ]` 可以查找列某个位置元素\n",
"- 增:通过关键字 `[ ]` 可以向列内增加元素\n",
"- 删:通过关键字 `del` 可以删除列内元素\n",
"- 查:通过关键字 `[ ]` 可以查找列某个位置元素\n",
"- 改:通过赋值符号 `=` 可以修改某个位置的元素\n",
"\n",
"字典的优点是:\n",
@@ -840,18 +840,18 @@
"source": [
"## Task 1 建立学生信息管理系统\n",
"\n",
"math_scores = {}\n",
"math_scores['XiaoHu'] = 65\n",
"math_scores['XiaoMing'] = 80\n",
"math_scores['XiaoWei'] = 95\n",
"Math_scores = {}\n",
"Math_scores['XiaoHu'] = 65\n",
"Math_scores['XiaoMing'] = 80\n",
"Math_scores['XiaoWei'] = 95\n",
"\n",
"language_scores = {}\n",
"language_scores['XiaoHu'] = 55\n",
"language_scores['XiaoMing'] = 92\n",
"language_scores['XiaoWei'] = 98\n",
"Chinese_scores = {}\n",
"Chinese_scores['XiaoHu'] = 55\n",
"Chinese_scores['XiaoMing'] = 92\n",
"Chinese_scores['XiaoWei'] = 98\n",
"\n",
"print(math_scores)\n",
"print(language_scores)"
"print(Math_scores)\n",
"print(Chinese_scores)"
]
},
{
@@ -872,17 +872,17 @@
"source": [
"## Task 2 根据本次期末考试的成绩更新系统\n",
"\n",
"math_scores['XiaoHu'] = 95\n",
"math_scores['XiaoMing'] = 75\n",
"math_scores['XiaoWei'] = 92\n",
"Math_scores['XiaoHu'] = 95\n",
"Math_scores['XiaoMing'] = 75\n",
"Math_scores['XiaoWei'] = 92\n",
"\n",
"language_scores = {}\n",
"language_scores['XiaoHu'] = 85\n",
"language_scores['XiaoMing'] = 71\n",
"language_scores['XiaoWei'] = 93\n",
"Chinese_scores = {}\n",
"Chinese_scores['XiaoHu'] = 85\n",
"Chinese_scores['XiaoMing'] = 71\n",
"Chinese_scores['XiaoWei'] = 93\n",
"\n",
"print(math_scores)\n",
"print(language_scores)"
"print(Math_scores)\n",
"print(Chinese_scores)"
]
},
{
@@ -903,11 +903,11 @@
"source": [
"## Task 3 删除 XiaoMing 的信息\n",
"\n",
"del math_scores['XiaoMing']\n",
"del language_scores['XiaoMing']\n",
"del Math_scores['XiaoMing']\n",
"del Chinese_scores['XiaoMing']\n",
"\n",
"print(math_scores)\n",
"print(language_scores)"
"print(Math_scores)\n",
"print(Chinese_scores)"
]
},
{
@@ -928,11 +928,11 @@
"source": [
"## Task 4 录入 Cryin 的信息\n",
"\n",
"math_scores['Cryin'] = 87\n",
"language_scores['Cryin'] = 88\n",
"Math_scores['Cryin'] = 87\n",
"Chinese_scores['Cryin'] = 88\n",
"\n",
"print(math_scores)\n",
"print(language_scores)"
"print(Math_scores)\n",
"print(Chinese_scores)"
]
},
{
@@ -1160,7 +1160,7 @@
}
],
"source": [
"## 集合亦或\n",
"## 集合异或,即不属于交集的部分\n",
"print(s ^ s2)"
]
},
@@ -1177,7 +1177,7 @@
"id": "static-framework",
"metadata": {},
"source": [
"### 1.2.5.1 列练习"
"### 1.2.5.1 列练习"
]
},
{
@@ -1186,7 +1186,7 @@
"metadata": {},
"source": [
"<blockquote>\n",
"给定两个大小分别为 m 和 n 的升序(从小到大)列 nums1 和 nums2。请你找出并返回这两个升序列的 中位数 。\n",
"给定两个大小分别为 m 和 n 的升序(从小到大)列 nums1 和 nums2。请你找出并返回这两个升序列的 中位数 。\n",
"\n",
"例子:\n",
" \n",
@@ -1201,7 +1201,7 @@
"id": "instrumental-melbourne",
"metadata": {},
"source": [
"找到两个列的中位数的思路分两步,首先合并两个升序列为一个升序列,然后找到升序列中中间的数即为中位数。"
"找到两个列的中位数的思路分两步,首先合并两个升序列为一个升序列,然后找到升序列中中间的数即为中位数。"
]
},
{
@@ -1209,7 +1209,7 @@
"id": "express-insulin",
"metadata": {},
"source": [
"合并两个升序列的思路很简单,因为两个列都是升序,所以只需要判断两个列首个元素的大小,将最小的依次放入一个新的数组中。如果其中一个数组空了,说明另一个数组中的所有元素都比之前的大,将它们合并到新数组的尾部即可。"
"合并两个升序列的思路很简单,因为两个列都是升序,所以只需要判断两个列首个元素的大小,将最小的依次放入一个新的数组中。如果其中一个数组空了,说明另一个数组中的所有元素都比之前的大,将它们合并到新数组的尾部即可。"
]
},
{
@@ -1219,22 +1219,22 @@
"metadata": {},
"outputs": [],
"source": [
"## 定义合并两个升序列的函数\n",
"## 定义合并两个升序列的函数\n",
"def merge(nums1, nums2):\n",
" result = []\n",
" ## 如果两个列都不是空的,依次比较首个元素大小\n",
" ## 如果两个列都不是空的,依次比较首个元素大小\n",
" while len(nums1) > 0 and len(nums2) > 0:\n",
" ## 如果第一个列首个元素更小\n",
" ## 如果第一个列首个元素更小\n",
" if nums1[0] < nums2[0]:\n",
" ## 将 nums1 列的首个元素放入 result 列\n",
" ## 将 nums1 列的首个元素放入 result 列\n",
" result.append(nums1[0])\n",
" nums1 = nums1[1:]\n",
" ## 如果第 nums2 列首个元素更小\n",
" ## 如果第 nums2 列首个元素更小\n",
" else:\n",
" ## 将 nums2 列的首个元素放入 result 列\n",
" ## 将 nums2 列的首个元素放入 result 列\n",
" result.append(nums2[0])\n",
" nums2 = nums2[1:]\n",
" ## 如果某个列空了,将非空的数组合并到 result 尾部\n",
" ## 如果某个列空了,将非空的数组合并到 result 尾部\n",
" result = result + nums1\n",
" result = result + nums2\n",
" return result"
@@ -1247,13 +1247,13 @@
"metadata": {},
"outputs": [],
"source": [
"## 在合并后的列中寻找中位数\n",
"## 在合并后的列中寻找中位数\n",
"def find_medium(nums):\n",
" half = len(nums) // 2\n",
" ## 如果列长度为偶数,则取中间两个数的平均值\n",
" ## 如果列长度为偶数,则取中间两个数的平均值\n",
" if len(nums) % 2 == 0:\n",
" return (nums[half - 1] + nums[half]) / 2\n",
" ## 如果列长度为奇数,则取最中间数\n",
" ## 如果列长度为奇数,则取最中间数\n",
" else:\n",
" return nums[half]"
]
@@ -1436,7 +1436,7 @@
"id": "brief-oakland",
"metadata": {},
"source": [
"那么在上述程序中,输入列的大小为 4在最坏情况下 twosum 函数中的两个 for-in 循环一共执行 16 次 check_sum 操作,因此该算法的复杂度是$O(n^2)$。下面我们用 字典 优化代码复杂度为 $O(n)$,思路是遍历一次列并将遍历过的元素值存储到字典中,若有字典中的元素值与列中的元素值求和为target则返回这两个值。"
"那么在上述程序中,输入列的大小为 4在最坏情况下 twosum 函数中的两个 for-in 循环一共执行 16 次 check_sum 操作,因此该算法的复杂度是$O(n^2)$。下面我们用 字典 优化代码复杂度为 $O(n)$,思路是遍历一次列并将遍历过的元素值存储到字典中,若有字典中的元素值与列中的元素值求和为target则返回这两个值。"
]
},
{
@@ -1477,11 +1477,8 @@
}
],
"metadata": {
"interpreter": {
"hash": "b658b93575a16dafdf00f5313d36bbc5ac0ec49ea379e64c9fa6db2ffc1a77f9"
},
"kernelspec": {
"display_name": "Python 3.9.2 64-bit",
"display_name": "Python 3.7.1 ('DataScience')",
"language": "python",
"name": "python3"
},
@@ -1495,7 +1492,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.2"
"version": "3.7.1"
},
"notify_time": "10",
"toc": {
@@ -1515,6 +1512,11 @@
},
"toc_section_display": true,
"toc_window_display": true
},
"vscode": {
"interpreter": {
"hash": "4951620600495571d210fcc70149573e34741e5378bdea11097277e6fa3349d5"
}
}
},
"nbformat": 4,

View File

@@ -198,7 +198,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "whole-translation",
"metadata": {},
"outputs": [],
@@ -218,19 +218,10 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "marked-hello",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class '__main__.student'>\n",
"<class '__main__.student'>\n"
]
}
],
"outputs": [],
"source": [
"## 使用 类的名字 student 与 括号() 可以创建类的对象 xiaohu\n",
"xiaohu = student()\n",
@@ -253,12 +244,12 @@
"id": "injured-difference",
"metadata": {},
"source": [
"我们提到类与对象都有包含一组属性与方法,在 Python 中类的变量用于表示类的属性。`student` 表示学生类,在我们的系统中我们只关注学生的姓名、数学成绩和语文成绩而不考虑其他,分别用类变量 `name`, `math_score`, `language_score` 表示。这体现了面向对象编程的”抽象性“。"
"我们提到类与对象都有包含一组属性与方法,在 Python 中类的变量用于表示类的属性。`student` 表示学生类,在我们的系统中我们只关注学生的姓名、数学成绩和语文成绩而不考虑其他,分别用类变量 `name`, `Math_score`, `Chinese_score` 表示。这体现了面向对象编程的”抽象性“。"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"id": "severe-reset",
"metadata": {},
"outputs": [],
@@ -266,9 +257,9 @@
"## 创建一个 student 类\n",
"class student():\n",
" ## 类的内部定义三个内部变量,并定义他们的初始值\n",
" name = 'Undifined'\n",
" math_score = -1\n",
" language_score = -1"
" name = 'Undefined'\n",
" Math_score = None\n",
" Chinese_score = None"
]
},
{
@@ -281,27 +272,17 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"id": "manual-montreal",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Undifined\n",
"-1\n",
"-1\n"
]
}
],
"outputs": [],
"source": [
"## 使用 类的名字 student 与 括号() 可以创建类的对象 xiaohu\n",
"xiaohu = student()\n",
"## 使用 . 符号引用对象xiaohu的变量name、math_score、language_score\n",
"## 使用 . 符号引用对象xiaohu的变量name、Math_score、Chinese_score\n",
"print(xiaohu.name)\n",
"print(xiaohu.math_score)\n",
"print(xiaohu.language_score)"
"print(xiaohu.Math_score)\n",
"print(xiaohu.Chinese_score)"
]
},
{
@@ -314,29 +295,19 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"id": "black-pakistan",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"XiaoHu\n",
"65\n",
"55\n"
]
}
],
"outputs": [],
"source": [
"## 通过赋值符号改变对象内变量的值\n",
"xiaohu.name = 'XiaoHu'\n",
"xiaohu.math_score = 65\n",
"xiaohu.language_score = 55\n",
"xiaohu.Math_score = 65\n",
"xiaohu.Chinese_score = 55\n",
"\n",
"print(xiaohu.name)\n",
"print(xiaohu.math_score)\n",
"print(xiaohu.language_score)"
"print(xiaohu.Math_score)\n",
"print(xiaohu.Chinese_score)"
]
},
{
@@ -352,19 +323,10 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"id": "south-tongue",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"XiaoMing\n",
"XiaoHu\n"
]
}
],
"outputs": [],
"source": [
"## 更改对象变量只影响该对象的属性。\n",
"xiaoming = student()\n",
@@ -376,19 +338,10 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"id": "loose-albuquerque",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Undifined\n",
"Change Name\n"
]
}
],
"outputs": [],
"source": [
"## 更改类变量会影响所有后续由该类创建对象的属性。\n",
"xiaoming = student()\n",
@@ -420,7 +373,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"id": "heavy-jewel",
"metadata": {},
"outputs": [],
@@ -430,25 +383,17 @@
" def print_name(self,):\n",
" print(self.name)\n",
" \n",
" name = 'Undifined'\n",
" math_score = -1\n",
" language_score = -1"
" name = 'Undefined'\n",
" Math_score = None\n",
" Chinese_score = None"
]
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"id": "occasional-paint",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Xiaohu\n"
]
}
],
"outputs": [],
"source": [
"xiaohu = student()\n",
"xiaohu.name = 'Xiaohu'\n",
@@ -475,7 +420,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": null,
"id": "colonial-craps",
"metadata": {},
"outputs": [],
@@ -483,41 +428,34 @@
"class student():\n",
" ## 定义一个修改对象数据值的函数\n",
" def change_score(self, course_name, score):\n",
" if course_name == 'math':\n",
" self.math_score = score\n",
" if course_name == 'language':\n",
" self.language_score = score\n",
" ## 如果输入的 course_name 不是 math 或者 language则输出错误信息\n",
" print(course_name, \" course is still not in current system\")\n",
" if course_name == 'Math':\n",
" self.Math_score = score\n",
" elif course_name == 'Chinese':\n",
" self.Chinese_score = score\n",
" ## 如果输入的 course_name 不是 Math 或者 Chinese则输出错误信息\n",
" else:\n",
" print(course_name, \" course is still not in current system\")\n",
" \n",
" def print_name(self,):\n",
" print(self.name)\n",
" \n",
" name = 'Undifined'\n",
" math_score = -1\n",
" language_score = -1"
" name = 'Undefined'\n",
" Math_score = None\n",
" Chinese_score = None"
]
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": null,
"id": "split-burner",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"language course is still not in current system\n"
]
}
],
"outputs": [],
"source": [
"xiaohu = student()\n",
"xiaohu.name = 'Xiaohu'\n",
"xiaohu.change_score('math', 65)\n",
"xiaohu.change_score('language',55)\n",
"xiaohu.change_score('physics',48)"
"xiaohu.change_score('Math', 65)\n",
"xiaohu.change_score('Chinese',55)\n",
"xiaohu.change_score('Physics',48)"
]
},
{
@@ -544,8 +482,8 @@
"在之前的代码中,我们采用赋值符号来为对象产生初始值:\n",
"\n",
" xiaohu.name = 'XiaoHu'\n",
" xiaohu.math_score = 65\n",
" xiaohu.language_score = 55\n",
" xiaohu.Math_score = 65\n",
" xiaohu.Chinese_score = 55\n",
" \n",
"然而这种方式或许有些麻烦,其实 Python 类中有一种 `__init__` 方法专门用于初始化对象。这个方法在通过类创建对象时被自动调用,并按照你的想法初始化该对象。下面我们通过 `__init__` 方法重新定义 student 类:\n",
"\n"
@@ -560,26 +498,26 @@
"source": [
"class student():\n",
" ## 定义 __init__ 方法,通过该方法的参数输入初始值,在该方法内部为变量赋值\n",
" def __init__(self, name, math_score, language_score):\n",
" def __init__(self, name, Math_score, Chinese_score):\n",
" self.name = name\n",
" self.math_score = math_score\n",
" self.language_score = language_score\n",
" self.Math_score = Math_score\n",
" self.Chinese_score = Chinese_score\n",
" \n",
" def change_score(self, course_name, score):\n",
" if course_name == 'math':\n",
" self.math_score = score\n",
" elif course_name == 'language':\n",
" self.language_score = score\n",
" ## 如果输入的 course_name 不是 math 或者 language则输出错误信息\n",
" if course_name == 'Math':\n",
" self.Math_score = score\n",
" elif course_name == 'Chinese':\n",
" self.Chinese_score = score\n",
" ## 如果输入的 course_name 不是 Math 或者 Chinese则输出错误信息\n",
" else:\n",
" print(course_name, \" course is still not in current system\")\n",
" \n",
" def print_name(self,):\n",
" print(self.name)\n",
" \n",
" name = 'Undifined'\n",
" math_score = -1\n",
" language_score = -1"
" name = 'Undefined'\n",
" Math_score = None\n",
" Chinese_score = None"
]
},
{
@@ -1078,29 +1016,29 @@
"outputs": [],
"source": [
"class student():\n",
" def __init__(self, name, math_score, language_score):\n",
" def __init__(self, name, Math_score, Chinese_score):\n",
" self.name = name\n",
" self.math_score = math_score\n",
" self.language_score = language_score\n",
" self.Math_score = Math_score\n",
" self.Chinese_score = Chinese_score\n",
" \n",
" ## repr 函数用于定义对象被输出时的输出结果\n",
" def __repr__(self):\n",
" return str((self.name, self.math_score, self.language_score))\n",
" return str((self.name, self.Math_score, self.Chinese_score))\n",
" \n",
" def change_score(self, course_name, score):\n",
" if course_name == 'math':\n",
" self.math_score = score\n",
" elif course_name == 'language':\n",
" self.language_score = score\n",
" if course_name == 'Math':\n",
" self.Math_score = score\n",
" elif course_name == 'Chinese':\n",
" self.Chinese_score = score\n",
" else:\n",
" print(course_name, \" course is still not in current system\")\n",
" \n",
" def print_name(self,):\n",
" print(self.name)\n",
" \n",
" name = 'Undifined'\n",
" math_score = -1\n",
" language_score = -1"
" name = 'Undefined'\n",
" Math_score = None\n",
" Chinese_score = None"
]
},
{
@@ -1137,13 +1075,13 @@
"print(ab)\n",
"\n",
"## Task2\n",
"ab['XiaoHu'].change_score('math', 95)\n",
"ab['XiaoMing'].change_score('math', 75)\n",
"ab['XiaoWei'].change_score('math', 92)\n",
"ab['XiaoHu'].change_score('Math', 95)\n",
"ab['XiaoMing'].change_score('Math', 75)\n",
"ab['XiaoWei'].change_score('Math', 92)\n",
"\n",
"ab['XiaoHu'].change_score('language', 85)\n",
"ab['XiaoMing'].change_score('language', 71)\n",
"ab['XiaoWei'].change_score('language', 93)\n",
"ab['XiaoHu'].change_score('Chinese', 85)\n",
"ab['XiaoMing'].change_score('Chinese', 71)\n",
"ab['XiaoWei'].change_score('Chinese', 93)\n",
"print(ab)\n",
"\n",
"## Task3\n",
@@ -1157,11 +1095,8 @@
}
],
"metadata": {
"interpreter": {
"hash": "6e6b1a68f12b475eafe78cfc17c6f5d94c7a04c458bc4083bc2db350380231c7"
},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3.7.1 ('DataScience')",
"language": "python",
"name": "python3"
},
@@ -1175,7 +1110,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.2"
"version": "3.7.1"
},
"toc": {
"base_numbering": 1,
@@ -1194,6 +1129,11 @@
},
"toc_section_display": true,
"toc_window_display": true
},
"vscode": {
"interpreter": {
"hash": "4951620600495571d210fcc70149573e34741e5378bdea11097277e6fa3349d5"
}
}
},
"nbformat": 4,

View File

@@ -45,11 +45,33 @@
"id": "508c28c5",
"metadata": {},
"source": [
"> 为了测试 python 中的文件操作,我们先通过命令行在当前目录保存一个内容为 “hello world!” 的 test.txt 文件。\n",
"> 为了测试 Python 中的文件操作,我们先通过命令行在当前目录保存一个内容为 “hello world!” 的 test.txt 文件。\n",
"\n",
"<img style=\"float: center;\" src=\"./src/fig41.png\" width=\"50%\"> "
]
},
{
"cell_type": "markdown",
"id": "9acf44f7",
"metadata": {},
"source": [
"> 或者通过以下代码来生成 `test.txt` 文件"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "161166fc",
"metadata": {},
"outputs": [],
"source": [
"file = open('test.txt', 'w')\n",
"file.write(\"\"\"Hello world!\n",
"Hello Python!!\n",
"Hello smart way!!!\"\"\")\n",
"file.close()"
]
},
{
"cell_type": "markdown",
"id": "fd7d8214",
@@ -67,7 +89,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 7,
"id": "85581df5",
"metadata": {},
"outputs": [
@@ -119,7 +141,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 8,
"id": "c42d7de4",
"metadata": {},
"outputs": [
@@ -141,7 +163,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 9,
"id": "0604c0d2",
"metadata": {},
"outputs": [
@@ -171,7 +193,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 10,
"id": "7d180532",
"metadata": {},
"outputs": [
@@ -203,7 +225,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 11,
"id": "8388fe2d",
"metadata": {},
"outputs": [],
@@ -236,7 +258,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 12,
"id": "f3352cfd",
"metadata": {},
"outputs": [],
@@ -293,6 +315,49 @@
"<img style=\"float: center;\" src=\"./src/fig44.png\" width=\"50%\"> "
]
},
{
"cell_type": "markdown",
"id": "575e38db",
"metadata": {},
"source": [
"> 为了接下来的模块引入更方便,我们也可以使用下面的代码来直接在当前目录生成 `student.py` 文件。"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "bc4a2658",
"metadata": {},
"outputs": [],
"source": [
"file = open('student.py', 'w',encoding=\"utf-8\")\n",
"file.write(\"\"\"class student():\n",
" def __init__(self, name, Math_score, Chinese_score):\n",
" self.name = name\n",
" self.Math_score = Math_score\n",
" self.Chinese_score = Chinese_score\n",
" \n",
" ## repr 函数用于定义对象被输出时的输出结果\n",
" def __repr__(self):\n",
" return str((self.name, self.Math_score, self.Chinese_score))\n",
" \n",
" def change_score(self, course_name, score):\n",
" if course_name == 'Math':\n",
" self.Math_score = score\n",
" elif course_name == 'Chinese':\n",
" self.Chinese_score = score\n",
" else:\n",
" print(course_name, \" course is still not in current system\")\n",
" \n",
" def print_name(self,):\n",
" print(self.name)\n",
" \n",
" name = 'Undefined'\n",
" Math_score = None\n",
" Chinese_score = None\"\"\")\n",
"file.close()"
]
},
{
"cell_type": "markdown",
"id": "ef0e6af7",
@@ -303,7 +368,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 3,
"id": "8de02c29",
"metadata": {},
"outputs": [],
@@ -313,7 +378,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 4,
"id": "4c9f7a60",
"metadata": {},
"outputs": [
@@ -348,7 +413,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 16,
"id": "abf9e578",
"metadata": {},
"outputs": [
@@ -378,7 +443,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 17,
"id": "323741ef",
"metadata": {},
"outputs": [
@@ -393,12 +458,12 @@
"source": [
"import math\n",
"## 通过 math.log 计算数值的对数\n",
"print(math.log(xiaohu.math_score))"
"print(math.log(xiaohu.Math_score))"
]
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 18,
"id": "67f56bad",
"metadata": {},
"outputs": [
@@ -406,7 +471,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Thu Jul 1 09:51:16 2021\n"
"Tue Jul 12 18:44:42 2022\n"
]
}
],
@@ -456,7 +521,7 @@
"id": "f0673d08",
"metadata": {},
"source": [
"### 1.4.3.2 Python 之"
"### 1.4.3.2 Python 之"
]
},
{
@@ -517,11 +582,8 @@
}
],
"metadata": {
"interpreter": {
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3.7.1 ('DataScience')",
"language": "python",
"name": "python3"
},
@@ -535,7 +597,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.2"
"version": "3.7.1"
},
"toc": {
"base_numbering": 1,
@@ -549,6 +611,11 @@
"toc_position": {},
"toc_section_display": true,
"toc_window_display": true
},
"vscode": {
"interpreter": {
"hash": "4951620600495571d210fcc70149573e34741e5378bdea11097277e6fa3349d5"
}
}
},
"nbformat": 4,