diff --git a/.gitignore b/.gitignore index 9aeca0a..742f63f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .ipynb_checkpoints .pyc *#* -*~ \ No newline at end of file +*~ +video/* \ No newline at end of file diff --git a/01-variable/answer-questions.ipynb b/01-variable/answer-questions.ipynb index 3be1ca1..3bbedab 100644 --- a/01-variable/answer-questions.ipynb +++ b/01-variable/answer-questions.ipynb @@ -4,9 +4,59 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## 打招呼机器人\n", + "# 1. Python 学习开始寄语\n", + "\n", + "就像要和外国人沟通要学外语一样,要有效的和计算机沟能也要学习计算机的语言。\n", + "\n", + "很高兴能和大家一起学 Python, Python 是一门易学的计算机语言。\n", + "\n", + "## 怎么学 Python\n", + "\n", + "编程是一门实践者的科学,就如学习外语一样;只看书学不会一门外语,不动手也同样学不会 Python.\n", + "\n", + "我们会使用 Notebook/Colab 的开发环境来学习 Python, 大家可以像学习Excel一样,在Python格子里写代码。\n", + "\n", + "当大家按自己理解写下代码,再点击运行,这就是最好的学习方法之一。\n", + "\n", + "## 碰到不懂的问题\n", + "\n", + "学习 Python 比学习一问外语容易的地方是现在有了 Google/Baidu, 碰到问题可以查\n", + "\n", + "很多程序员每天的工作也是 Google/Baidu, 因为那么多的方法和公式没有人能记得住\n", + "\n", + "大家要记得一个事情:学习的目的是为了学习解决问题的方法和思路,而不是背诵知识,因为在背诵上人是永远不可能打败计算机的。\n", + "\n", + "在初始学习阶段,老师会帮大家整理很多参考资料链接,大家一定要注意看!\n", + "\n", + "## 写代码报错了怎么办\n", + "\n", + "* [新人必看: Zane 老师整理了检查错误的方法](https://www.yinxiang.com/everhub/note/1e44d717-75c3-413d-83ad-5716126ccaa6)\n", + "* [新人必看:17个新手常见Python运行时错误](https://www.oschina.net/question/89964_62779)\n", + "\n", + "## Python 各种基础知识\n", + "\n", + "* [Zane老师整理Python基础知识](https://www.yinxiang.com/everhub/note/414a0a33-7d28-4c27-af22-23f950b990fd)\n", + "* [Python3基础教程](https://www.runoob.com/python3/python3-tutorial.html)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 2. 打招呼机器人\n", + "---\n", + "题目:\n", + "\n", "* 提示用户输入姓名\n", - "* 然后输出 Hi + 姓名" + "* 然后输出 Hi + 姓名\n", + "---\n", + "参考:\n", + "\n", + "* [变量的创建和赋值](https://github.com/TwoWater/Python/blob/master/Article/PythonBasis/python2/Variable.md)\n", + "* [Python 变量的数据类型](https://www.runoob.com/python3/python3-data-type.html) \n", + "* [Python3 input() 函数](https://www.runoob.com/python3/python3-func-input.html)\n", + "\n", + "* 注意:在 input 输入框里输入完信息后一定要按回车,不然计算机不知道!" ] }, { @@ -42,7 +92,10 @@ } }, "source": [ - "## 邮件机器人\n", + "# 3. 邮件机器人\n", + "---\n", + "题目:\n", + "\n", "* 假设计你是要给自动给客户发邮件的运营人员\n", "* 提示运营人员输入姓名 \n", "* 提示运营人输入购买的商品 \n", @@ -50,10 +103,25 @@ "\n", "Dear [用户姓名]:\n", " \n", - " Your purchased product : [产品名] is delivered!\n", - " \n", - " Thanks for your choosing!\n", + "Your purchased product : [产品名] is delivered!\n", " \n", + "Thanks for your choosing!\n", + "\n", + "---\n", + "参考: \n", + "\n", + "* [使用三引号处理多行字符串](https://blog.csdn.net/sinat_41104353/article/details/79266048)\n", + "\n", + "\n", + "* [python3处理字符串](https://www.runoob.com/python3/python3-string.html)\n", + "\n", + " 请查看 Python三引号/Python字符串格式化 两节\n", + " \n", + " 大家可以把自己代入进去,想想有个机器人能帮你做事!\n", + "\n", + "\n", + "* [最全面的 python 字符串拼接总结](https://segmentfault.com/a/1190000015475309)\n", + "\n", " " ] }, @@ -102,8 +170,16 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## 截取字符串\n", - "从 \"You Rise Me Up\" 中截取 Rise" + "# 4. 截取字符串\n", + "---\n", + "题目:\n", + "\n", + "从 \"You Rise Me Up\" 中截取 Rise\n", + "\n", + "---\n", + "参考:\n", + "\n", + "* [截取字符串的方法详解](https://blog.csdn.net/weixin_42350212/article/details/80551677)" ] }, { @@ -133,8 +209,21 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## 字符串大小写转换\n", - "将 \"Python Basic\" 转成小写" + "# 5. 字符串大小写转换\n", + "---\n", + "问题:\n", + "* 将 \"Python Basic\" 转成小写\n", + "\n", + "---\n", + "参考:\n", + "\n", + "* [Python 字符串大小写转换](https://www.runoob.com/python3/python3-upper-lower.html)\n", + "\n", + "\n", + "\n", + "* [Python3 字符串](https://www.runoob.com/python3/python3-string.html)\n", + "\n", + " 请翻到 “Python 的字符串内建函数“,这里不但有大小写转换函数,也有其它Python字符串自带的函数的介绍\n" ] }, { @@ -164,9 +253,17 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## 数值计算\n", + "# 6. 数值计算\n", + "--- \n", + "题目:\n", + "\n", "* 计算 1000 乘以 2000,再减 1000000\n", - "* 打印出结果" + "* 打印出结果\n", + "\n", + "---\n", + "参考:\n", + "\n", + "* [Python 运算符](https://www.runoob.com/python/python-operators.html)" ] }, { @@ -204,10 +301,17 @@ } }, "source": [ - "## 变量类型 (附加题,不考)\n", + "# 7. 变量类型 (附加题,不考)\n", + "---\n", + "题目:\n", + "\n", "* 猜一下下面两行代码的执行结果:\n", "* print(type(100))\n", - "* print(type(\"Hello\"))" + "* print(type(\"Hello\"))\n", + "\n", + "---\n", + "参考:\n", + "* [Python type() 函数](https://www.runoob.com/python/python-func-type.html)" ] }, { @@ -238,12 +342,23 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Python 开发规范 (写最好代码,但不考)\n", + "# 8. Python 开发规范 (写最好代码,虽不考/但是非常重要)\n", "\n", "就像写邮件需要注意格式一样,写代码也需要注意格式和命名:\n", "\n", - "https://blog.csdn.net/xufive/article/details/84957425\n" + "https://blog.csdn.net/xufive/article/details/84957425\n", + "\n", + "* 变量名尽量小写, 如有多个单词,用下划线隔开\n", + "* 每个代码格里的代码行数最好不要超过10行\n", + "\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -275,7 +390,7 @@ "toc_cell": false, "toc_position": {}, "toc_section_display": true, - "toc_window_display": false + "toc_window_display": true }, "varInspector": { "cols": { diff --git a/02-logic/answer-questions.ipynb b/02-logic/answer-questions.ipynb index 04f737e..2c7479c 100644 --- a/02-logic/answer-questions.ipynb +++ b/02-logic/answer-questions.ipynb @@ -4,21 +4,118 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# 季节判断机器人\n", - "* 在中国 1~3月为春季,4~6个月为夏季,7~9月为秋季,10~12月为冬季\n", - "* 提示用户输入一个月份(数字),然后输出是什么季节\n", - "* 如果用户输入 1~12 之外的数字,提示 \"机器故障\"\n", + "# 1. 基础知识重复提醒\n", "\n", - " 备注:因为是学习的第二天,所以暂时让用户输入,以后可以直接读系统中的日期" + "有同学会问,不是上一章讲过这些吗?为什么要要重复提醒呢?\n", + "\n", + "是的,知识学习理解的过程是一个反复的过程,我们小时候第一次学习汉字时,基础字练了20次以上。\n", + "\n", + "对于 Python 这么一门语言的练习,基础知识的理解我建议要重复3次以上。\n", + "\n", + "老师把重要的基础知识重复出现3次,大家要把作业练习3次以上,老师推荐的各种链接多看一下。\n", + "\n", + "学习新知识时快就是慢,慢就是快!\n", + "\n", + "\n", + "\n", + "## 写代码报错了怎么办\n", + "\n", + "* [必看: Zane 老师整理了检查错误的方法](https://www.yinxiang.com/everhub/note/1e44d717-75c3-413d-83ad-5716126ccaa6)\n", + "* [新人必看:17个新手常见Python运行时错误](https://www.oschina.net/question/89964_62779)\n", + "\n", + "\n", + "## Python 各种基础知识\n", + "\n", + "* [Zane老师整理Python基础知识](https://www.yinxiang.com/everhub/note/414a0a33-7d28-4c27-af22-23f950b990fd)\n", + "* [Python3基础教程](https://www.runoob.com/python3/python3-tutorial.html)\n", + "* [Python 运算符](https://www.runoob.com/python/python-operators.html)\n", + "\n", + "## 新知识逻辑判断提醒\n", + "\n", + "* 逻辑判断对于没有程序基础的同学是一个难点;\n", + "\n", + "\n", + "* 大家要尝试着把自己代入进去来理解,想像一下自己要控制一个机器人,要指挥它来干活,\n", + "\n", + " 你现在写的逻辑判断代码就是命令,告诉这个机器人要怎么样工作;\n", + " \n", + " \n", + "* if/else/elif 这些指令要多练习,老师发的各种参考资料链接要多看,有问题要多问!\n", + "\n", + "## 什么是 Markdown(选学)\n", + "\n", + "学习了解 Markdown 可以让大家的代码也有漂亮的文字配合!\n", + "\n", + "* [Markdown 是什么](https://www.zhihu.com/question/19963642)\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 2. 季节判断机器人\n", + "---\n", + "题目:\n", + "\n", + "* 在中国 1~3月为春季,4~6个月为夏季,7~9月为秋季,10~12月为冬季;\n", + "* 提示用户输入一个月份(数字),然后输出是什么季节;\n", + "* 如果用户输入 1~12 之外的数字,提示 \"机器故障\"。\n", + "\n", + " 备注:因为是学习的第二天,所以暂时让用户输入,以后可以直接读系统中的日期\n", + "---\n", + "重要知识点:\n", + "\n", + "* 每个条件后面要使用冒号 :,表示接下来是满足条件后要执行的语句块;\n", + "* 使用缩进来划分语句块,相同缩进数的语句在一起组成一个语句块。\n", + "\n", + "---\n", + "参考:\n", + "* [Python3 input() 函数](https://www.runoob.com/python3/python3-func-input.html)\n", + "* [Python3 条件控制](https://www.runoob.com/python3/python3-conditional-statements.html)\n", + "* [Python 运算符](https://www.runoob.com/python/python-operators.html)\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 解法1\n", + "\n", + "\n", + "* 使用 input 函数提示用户输入现在是几月\n", + "\n", + " 注意:input 函数返回的的字符串\n", + " \n", + "* 将输入的值用 int 函数转成数字,把转换后的值放到 month;\n", + "\n", + "* 首先通 month<1 or month>12 来排除掉所有的异常值;\n", + "\n", + "* 通过 if/elif 来分别判断 month 是在春夏秋冬的哪一季;\n", + "\n", + "* 我们判断 month 时使用了 小于等于(<=), 同时把 month 放在中间;\n", + "\n", + " 1<=month<= 3, 意示着 month大于等于1,而且小于等于3,这就是春季\n", + "\n", + "\n", + "* 最后执行到 else 时,表示这一次用户输入的异常数据\n", + "\n", + " 则告诉用户 \"机器故障\"\n", + "\n", + "\n", + "* 把异常情况的处理放到最后面的 else 一种常见的写法,这样写起大家容易理解\n", + "\n", + " 但是不足的地方是代码会不够高效,要多写不少代码!" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 1, "metadata": { "ExecuteTime": { - "end_time": "2020-07-14T01:28:56.303344Z", - "start_time": "2020-07-14T01:28:54.859365Z" + "end_time": "2020-09-15T07:20:56.871197Z", + "start_time": "2020-09-15T07:20:52.153375Z" } }, "outputs": [ @@ -26,12 +123,143 @@ "name": "stdout", "output_type": "stream", "text": [ - "请入现在是几月:1000\n", - "机器故障\n" + "请入现在是几月:2\n", + "春季\n" ] } ], "source": [ + "month = int(input(\"请入现在是几月:\"))\n", + "if 1<=month<=3:\n", + " print(\"春季\")\n", + "elif 4<=month<=6:\n", + " print(\"夏季\")\n", + "elif 7<=month<=9:\n", + " print(\"秋季\")\n", + "elif 10<=month<=12:\n", + " print(\"冬季\")\n", + "else:\n", + " print(\"机器故障\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 解法2\n", + "\n", + "* 使用 input 函数提示用户输入现在是几月\n", + "\n", + " 注意:input 函数返回的的字符串\n", + " \n", + "* 将输入的值用 int 函数转成数字,把转换后的值放到 month\n", + "\n", + "* 首先通 month<1 or month>12 来排除掉所有的异常值\n", + "\n", + " or 是一个“或”条件,在这里表示 month 小于1,或者 month 大于12都是异常数据\n", + " \n", + " 如是异常数据,则告诉用户 \"机器故障\"\n", + " \n", + "* 再通过 if/elif 组合来判断用户输入的现在是几月,告诉用户现在是哪一季 \n", + "\n", + " 因为前面把异常数据排除了,\n", + " \n", + " 所以可以直接通过 month 小于等于 3 判断知道现在是春季, month 小于等于 6 判断知道现在是夏季,以此类推\n", + " \n", + " \n", + "* 先处理异常情况是程序老手的一种常见做法,也是我推荐的作法\n", + "\n", + " 先把异常数据全部处理完后,后面的条件逻辑判断会简单很多\n", + " \n", + " 大家可以把解法1/解法2进行对比,自己反复打几次来体会一下" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-15T07:20:58.738996Z", + "start_time": "2020-09-15T07:20:56.873725Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "请入现在是几月:2\n", + "春季\n" + ] + } + ], + "source": [ + "month = int(input(\"请入现在是几月:\"))\n", + "if month<1 or month>12:\n", + " print(\"机器故障\")\n", + "elif month<=3:\n", + " print(\"春季\")\n", + "elif month<=6:\n", + " print(\"夏季\")\n", + "elif month<=9:\n", + " print(\"秋季\")\n", + "elif month<=12:\n", + " print(\"冬季\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 解法3\n", + "\n", + "* 使用 input 函数提示用户输入现在是几月\n", + "\n", + " 注意:input 函数返回的的字符串\n", + " \n", + "* 将输入的值用 int 函数转成数字,把转换后的值放到 month\n", + "\n", + "* 通过 if/elif 来分别判断 month 是在春夏秋冬的哪一季\n", + "\n", + "* 我们判断 month 时使用了 大于等于(>=) 和 小于等于(<=), 同时还使用了 and 条件\n", + "\n", + " month>=1 and month<=3, 意示着 month大于等于1,而且小于等于3,这就是春季\n", + "\n", + "\n", + "* 最后执行到 else 时,表示,这一次用户输入的异常数据\n", + "\n", + " 则告诉用户 \"机器故障\"\n", + "\n", + "\n", + "* 把异常情况的处理放到最后面的 else 一种常见的写法,这样写起大家容易理解\n", + "\n", + " 但是不足的地方是代码会不够高效,要多写不少代码!\n", + "\n", + "\n", + "* 这里和解法1基本一致,但是新使用了 and, >= " + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-15T07:20:59.832765Z", + "start_time": "2020-09-15T07:20:58.741537Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "请入现在是几月:2\n", + "春季\n" + ] + } + ], + "source": [ + "\n", "month = int(input(\"请入现在是几月:\"))\n", "if month>=1 and month<=3:\n", " print(\"春季\")\n", @@ -49,50 +277,61 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# 价格预警机器人\n", + "# 3. 价格预警机器人\n", "* 假设你要监控 A,B,C 三个商品\n", - "* 如是果 A/B/C 分别降价 30/20/10 元上,就要报警\n", + "* 如果 A/B/C 分别降价 30/20/10 元上,就要报警\n", "* 提示用户输入商品名(A/B/C)\n", "* 提示用户输入降价值 (数字)\n", "* 如果需要报警则打印商品名 + 降价值\n" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 解法1\n", + "\n", + "\n", + "* 使用 input 函数提示用户输入商品名, 放到 product 中\n", + "\n", + " 注意 product 是我们设定的一个变量名,我们也可以起一个别的名字,比如叫 name\n", + " \n", + "* 使用 input 函数提示用户输入降价值, 再用 int 函数将其转换成数字后放到 lower_price 中\n", + "\n", + " 注意 lower_price 是我们设定的一个变量名,我们也可以起一个别的名字,比如叫 jiangjia (虽然这不是一个好名字)\n", + "\n", + "* 先判断是否 product==\"A\" 并且 lower_price > 30, 那么就提示 A 商品的降价幅度\n", + "\n", + "* 再判断是否 product==\"B\" 并且 lower_price > 20 , 那么就提示 B 商品的降价幅度\n", + "\n", + "* 再判断是否 product==\"C\" 并且 lower_price > 10, 那么就提示 C 商品的降价幅度\n", + "\n", + "* 最后如果前面的三个条件都不满足,就输出 \"No Alert!\"\n", + "\n", + "* 大家好好理解 if/elif/else 的层次递进关系" + ] + }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 4, "metadata": { "ExecuteTime": { - "end_time": "2020-07-14T01:35:14.721332Z", - "start_time": "2020-07-14T01:33:44.855245Z" + "end_time": "2020-09-15T07:21:05.254821Z", + "start_time": "2020-09-15T07:20:59.835292Z" } }, "outputs": [ { - "ename": "KeyboardInterrupt", - "evalue": "", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m~/opt/anaconda3/lib/python3.7/site-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 883\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 884\u001b[0;31m \u001b[0mident\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreply\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msession\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstdin_socket\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 885\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m~/opt/anaconda3/lib/python3.7/site-packages/jupyter_client/session.py\u001b[0m in \u001b[0;36mrecv\u001b[0;34m(self, socket, mode, content, copy)\u001b[0m\n\u001b[1;32m 802\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 803\u001b[0;31m \u001b[0mmsg_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msocket\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv_multipart\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 804\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mzmq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mZMQError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m~/opt/anaconda3/lib/python3.7/site-packages/zmq/sugar/socket.py\u001b[0m in \u001b[0;36mrecv_multipart\u001b[0;34m(self, flags, copy, track)\u001b[0m\n\u001b[1;32m 474\u001b[0m \"\"\"\n\u001b[0;32m--> 475\u001b[0;31m \u001b[0mparts\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mflags\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtrack\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtrack\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 476\u001b[0m \u001b[0;31m# have first part already, only loop while more to receive\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv\u001b[0;34m()\u001b[0m\n", - "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv\u001b[0;34m()\u001b[0m\n", - "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket._recv_copy\u001b[0;34m()\u001b[0m\n", - "\u001b[0;32m~/opt/anaconda3/lib/python3.7/site-packages/zmq/backend/cython/checkrc.pxd\u001b[0m in \u001b[0;36mzmq.backend.cython.checkrc._check_rc\u001b[0;34m()\u001b[0m\n", - "\u001b[0;31mKeyboardInterrupt\u001b[0m: ", - "\nDuring handling of the above exception, another exception occurred:\n", - "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mproduct\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"请输入商品名:\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mlower_price\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"请输入降价值:\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mproduct\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m\"A\"\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mlower_price\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m30\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"[Alert] Product %s, lower_price %d\"\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mproduct\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mlower_price\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mproduct\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m\"B\"\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mlower_price\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m20\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m~/opt/anaconda3/lib/python3.7/site-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 857\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 858\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 859\u001b[0;31m \u001b[0mpassword\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 860\u001b[0m )\n\u001b[1;32m 861\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m~/opt/anaconda3/lib/python3.7/site-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 887\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 888\u001b[0m \u001b[0;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 889\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 890\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 891\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mKeyboardInterrupt\u001b[0m: " + "name": "stdout", + "output_type": "stream", + "text": [ + "请输入商品名:A\n", + "请输入降价值:100\n", + "[Alert] Product A, lower_price 100\n" ] } ], "source": [ - "# 解法1\n", "product = input(\"请输入商品名:\")\n", "lower_price = int(input(\"请输入降价值:\"))\n", "if product == \"A\" and lower_price > 30:\n", @@ -105,13 +344,55 @@ " print(\"No Alert!\")" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 解法2\n", + "\n", + "---\n", + "改进点:\n", + "\n", + "大家是否发现下面这句话重复了3次:\n", + "\n", + " print(\"[Alert] Product %s, lower_price %d\" % (product,lower_price))\n", + "\n", + "所以们使用了 or 条件语句把对 A/B/C 三个商品的条件判断联接起来,最后一起调用了这条语言\n", + "\n", + "因为 print 输出的警告信息是使用 \"%\" 和变量进行自动拼接出来的,所不管是哪个商品警告都能复用\n", + "\n", + "大家写代码时一定要反复思考,不但是让写的代码能正常执行,也能很简洁优雅,让代码可以复用!\n", + "\n", + "---\n", + "\n", + "* 使用 input 函数提示用户输入商品名, 放到 product 中\n", + "\n", + " 注意 product 是我们设定的一个变量名,我们也可以起一个别的名字,比如叫 name\n", + " \n", + "* 使用 input 函数提示用户输入降价值, 再用 int 函数将其转换成数字后放到 lower_price 中\n", + "\n", + " 注意 lower_price 是我们设定的一个变量名,我们也可以起一个别的名字,比如叫 jiangjia (虽然这不是一个好名字)\n", + "\n", + "* 先判断是否 (product==\"A\" 并且 lower_price > 30) 或\n", + " (product==\"B\" 并且 lower_price > 20) 或\n", + " (product==\"C\" 并且 lower_price > 10) \n", + "\n", + " 如果3者中有一个条件满足了,就提示相应的商品及降价幅度\n", + " \n", + "\n", + "* 最后如果前面的三个条件都不满足,就输出 \"No Alert!\"\n", + "\n", + "\n", + "* 这里使用 if/else\n" + ] + }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 5, "metadata": { "ExecuteTime": { - "end_time": "2020-07-14T01:35:20.248871Z", - "start_time": "2020-07-14T01:35:17.006007Z" + "end_time": "2020-09-15T07:21:09.261330Z", + "start_time": "2020-09-15T07:21:05.256783Z" } }, "outputs": [ @@ -120,13 +401,12 @@ "output_type": "stream", "text": [ "请输入商品名:A\n", - "请输入降价值:31\n", - "[Alert] Product A, lower_price 31\n" + "请输入降价值:40\n", + "[Alert] Product A, lower_price 40\n" ] } ], "source": [ - "# 解法2\n", "product = input(\"请输入商品名:\")\n", "lower_price = int(input(\"请输入降价值:\"))\n", "if ((product == \"A\" and lower_price > 30) or \n", @@ -141,23 +421,36 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# 考试评语机器人\n", + "# 4. 考试评语机器人\n", + "\n", + "---\n", + "题目:\n", + "\n", "* 假设你现在是考试院的老师,在看试卷,要打评语\n", - "* 提示用户输出考试分数(数字)\n", + "* 提示用户输入考试分数(数字),分数范围是0 ~ 100 分\n", "* 如果输入值小于 60 分,则打印 \"要加油呀!\"\n", "* 如果输入值大于等于 60 分,且小于 80分,则打印 \"不错呀,认真学习了!\"\n", "* 如果输入值大于等于 80 分,则打印 \"考得不错,不要骄傲!\"\n", + "* 不在 0~100 分范围内的输入,我不需要打印任何信息\n", + "\n", + " 备注:因为是学习的第二天,所以暂时让用户输入,以后可以直接读取成绩表,直接出评语\n", + " \n", + "---\n", + "解题讲解:\n", "\n", - " 备注:因为是学习的第二天,所以暂时让用户输入,以后可以直接读取成绩表,直接出评语" + "此题为2,3题的变体,故不再专门解释!\n", + "\n", + "* 特别注意的是,本题不需要使用 else\n", + "* 在 if/elif 的组合里要考虑用户输入值不在 0~100 的情况" ] }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 6, "metadata": { "ExecuteTime": { - "end_time": "2020-07-14T01:37:42.534729Z", - "start_time": "2020-07-14T01:37:39.628546Z" + "end_time": "2020-09-15T07:21:11.450273Z", + "start_time": "2020-09-15T07:21:09.263112Z" } }, "outputs": [ @@ -165,18 +458,18 @@ "name": "stdout", "output_type": "stream", "text": [ - "请输入分数:82\n", + "请输入分数:100\n", "考得不错,不要骄傲!\n" ] } ], "source": [ "score = int(input(\"请输入分数:\"))\n", - "if score < 60:\n", + "if score < 60 and score>0:\n", " print(\"要加油呀!\")\n", "elif score >=60 and score <80:\n", " print(\"不错呀,认真学习了!\")\n", - "elif score >=80:\n", + "elif score >=80 and score <=100:\n", " print(\"考得不错,不要骄傲!\")" ] }, @@ -184,23 +477,35 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# 智能空调机器人\n", + "# 5. 智能空调机器人\n", + "\n", + "---\n", + "题目:\n", + "\n", "* 假设你现在设计一台智能空调\n", "* 提示用户输入季节(只能是 winter/summer)\n", "* 提示用户输入当前温度 (数字)\n", "* 如果季节是 winter, 且温度低于10度,则打印 \"太冷了,开热空调了\"\n", "* 如果季节是 summer, 且温度高于27度,则打印 \"太热了,开冷空调了\"\n", + "* 如果是不符合上面条件的用户输入,则不用打印任何东西\n", "\n", - " 备注:因为是学习的第二天,所以暂时让用户输入,以后可以接外部温度电子器直接读" + " 备注:因为是学习的第二天,所以暂时让用户输入,以后可以接外部温度电子器直接读\n", + " \n", + "---\n", + "解题讲解:\n", + "\n", + "此题为2,3题的变体,故不再专门解释!\n", + "\n", + "特别注意的是,本题不需要使用 else" ] }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 7, "metadata": { "ExecuteTime": { - "end_time": "2020-07-14T01:42:17.893109Z", - "start_time": "2020-07-14T01:42:12.964759Z" + "end_time": "2020-09-15T07:21:16.073203Z", + "start_time": "2020-09-15T07:21:11.452151Z" } }, "outputs": [ @@ -208,8 +513,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "请输入季节(输入 winter/summer) :wooo\n", - "请输入当前的温度(输入数字):100\n" + "请输入季节(输入 winter/summer) :winter\n", + "请输入当前的温度(输入数字):90\n" ] } ], @@ -221,6 +526,20 @@ "elif season == \"summer\" and temp > 27:\n", " print(\"太热了,开冷空调了\")\n" ] + }, + { + "cell_type": "markdown", + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-15T07:26:43.137544Z", + "start_time": "2020-09-15T07:26:43.134738Z" + } + }, + "source": [ + "# 6. 致谢!\n", + "\n", + "本章内容由 Miles/Zane 评审,在此致谢!" + ] } ], "metadata": { @@ -250,9 +569,14 @@ "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, - "toc_position": {}, + "toc_position": { + "height": "calc(100% - 180px)", + "left": "10px", + "top": "150px", + "width": "358.391px" + }, "toc_section_display": true, - "toc_window_display": false + "toc_window_display": true }, "varInspector": { "cols": { diff --git a/02-logic/questions.ipynb b/02-logic/questions.ipynb index 5b91e54..95aecb2 100644 --- a/02-logic/questions.ipynb +++ b/02-logic/questions.ipynb @@ -4,21 +4,222 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# 季节判断机器人\n", - "* 在中国 1~3月为春季,4~6个月为夏季,7~9月为秋季,10~12月为冬季\n", - "* 提示用户输入一个月份(数字),然后输出是什么季节\n", - "* 如果用户输入 1~12 之外的数字,提示 \"机器故障\"\n", + "# 1. 基础知识重复提醒\n", "\n", - " 备注:因为是学习的第二天,所以暂时让用户输入,以后可以直接读系统中的日期" + "有同学会问,不是上一章讲过这些吗?为什么要要重复提醒呢?\n", + "\n", + "是的,知识学习理解的过程是一个反复的过程,我们小时候第一次学习汉字时,基础字练了20次以上。\n", + "\n", + "对于 Python 这么一门语言的练习,基础知识的理解我建议要重复3次以上。\n", + "\n", + "老师把重要的基础知识重复出现3次,大家要把作业练习3次以上,老师推荐的各种链接多看一下。\n", + "\n", + "学习新知识时快就是慢,慢就是快!\n", + "\n", + "\n", + "\n", + "## 写代码报错了怎么办\n", + "\n", + "* [必看: Zane 老师整理了检查错误的方法](https://www.yinxiang.com/everhub/note/1e44d717-75c3-413d-83ad-5716126ccaa6)\n", + "* [新人必看:17个新手常见Python运行时错误](https://www.oschina.net/question/89964_62779)\n", + "\n", + "\n", + "## Python 各种基础知识\n", + "\n", + "* [Zane老师整理Python基础知识](https://www.yinxiang.com/everhub/note/414a0a33-7d28-4c27-af22-23f950b990fd)\n", + "* [Python3基础教程](https://www.runoob.com/python3/python3-tutorial.html)\n", + "* [Python 运算符](https://www.runoob.com/python/python-operators.html)\n", + "\n", + "## 新知识逻辑判断提醒\n", + "\n", + "* 逻辑判断对于没有程序基础的同学是一个难点;\n", + "\n", + "\n", + "* 大家要尝试着把自己代入进去来理解,想像一下自己要控制一个机器人,要指挥它来干活,\n", + "\n", + " 你现在写的逻辑判断代码就是命令,告诉这个机器人要怎么样工作;\n", + " \n", + " \n", + "* if/else/elif 这些指令要多练习,老师发的各种参考资料链接要多看,有问题要多问!\n", + "\n", + "## 什么是 Markdown(选学)\n", + "\n", + "学习了解 Markdown 可以让大家的代码也有漂亮的文字配合!\n", + "\n", + "* [Markdown 是什么](https://www.zhihu.com/question/19963642)\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 2. 季节判断机器人\n", + "---\n", + "题目:\n", + "\n", + "* 在中国 1~3月为春季,4~6个月为夏季,7~9月为秋季,10~12月为冬季;\n", + "* 提示用户输入一个月份(数字),然后输出是什么季节;\n", + "* 如果用户输入 1~12 之外的数字,提示 \"机器故障\"。\n", + "\n", + " 备注:因为是学习的第二天,所以暂时让用户输入,以后可以直接读系统中的日期\n", + "---\n", + "重要知识点:\n", + "\n", + "* 每个条件后面要使用冒号 :,表示接下来是满足条件后要执行的语句块;\n", + "* 使用缩进来划分语句块,相同缩进数的语句在一起组成一个语句块。\n", + "\n", + "---\n", + "参考:\n", + "* [Python3 input() 函数](https://www.runoob.com/python3/python3-func-input.html)\n", + "* [Python3 条件控制](https://www.runoob.com/python3/python3-conditional-statements.html)\n", + "* [Python 运算符](https://www.runoob.com/python/python-operators.html)\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 解法1\n", + "\n", + "\n", + "* 使用 input 函数提示用户输入现在是几月\n", + "\n", + " 注意:input 函数返回的的字符串\n", + " \n", + "* 将输入的值用 int 函数转成数字,把转换后的值放到 month;\n", + "\n", + "* 首先通 month<1 or month>12 来排除掉所有的异常值;\n", + "\n", + "* 通过 if/elif 来分别判断 month 是在春夏秋冬的哪一季;\n", + "\n", + "* 我们判断 month 时使用了 小于等于(<=), 同时把 month 放在中间;\n", + "\n", + " 1<=month<= 3, 意示着 month大于等于1,而且小于等于3,这就是春季\n", + "\n", + "\n", + "* 最后执行到 else 时,表示这一次用户输入的异常数据\n", + "\n", + " 则告诉用户 \"机器故障\"\n", + "\n", + "\n", + "* 把异常情况的处理放到最后面的 else 一种常见的写法,这样写起大家容易理解\n", + "\n", + " 但是不足的地方是代码会不够高效,要多写不少代码!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-15T07:20:56.871197Z", + "start_time": "2020-09-15T07:20:52.153375Z" + } + }, + "outputs": [], + "source": [ + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 解法2\n", + "\n", + "* 使用 input 函数提示用户输入现在是几月\n", + "\n", + " 注意:input 函数返回的的字符串\n", + " \n", + "* 将输入的值用 int 函数转成数字,把转换后的值放到 month\n", + "\n", + "* 首先通 month<1 or month>12 来排除掉所有的异常值\n", + "\n", + " or 是一个“或”条件,在这里表示 month 小于1,或者 month 大于12都是异常数据\n", + " \n", + " 如是异常数据,则告诉用户 \"机器故障\"\n", + " \n", + "* 再通过 if/elif 组合来判断用户输入的现在是几月,告诉用户现在是哪一季 \n", + "\n", + " 因为前面把异常数据排除了,\n", + " \n", + " 所以可以直接通过 month 小于等于 3 判断知道现在是春季, month 小于等于 6 判断知道现在是夏季,以此类推\n", + " \n", + " \n", + "* 先处理异常情况是程序老手的一种常见做法,也是我推荐的作法\n", + "\n", + " 先把异常数据全部处理完后,后面的条件逻辑判断会简单很多\n", + " \n", + " 大家可以把解法1/解法2进行对比,自己反复打几次来体会一下" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-15T07:20:58.738996Z", + "start_time": "2020-09-15T07:20:56.873725Z" + } + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 解法3\n", + "\n", + "* 使用 input 函数提示用户输入现在是几月\n", + "\n", + " 注意:input 函数返回的的字符串\n", + " \n", + "* 将输入的值用 int 函数转成数字,把转换后的值放到 month\n", + "\n", + "* 通过 if/elif 来分别判断 month 是在春夏秋冬的哪一季\n", + "\n", + "* 我们判断 month 时使用了 大于等于(>=) 和 小于等于(<=), 同时还使用了 and 条件\n", + "\n", + " month>=1 and month<=3, 意示着 month大于等于1,而且小于等于3,这就是春季\n", + "\n", + "\n", + "* 最后执行到 else 时,表示,这一次用户输入的异常数据\n", + "\n", + " 则告诉用户 \"机器故障\"\n", + "\n", + "\n", + "* 把异常情况的处理放到最后面的 else 一种常见的写法,这样写起大家容易理解\n", + "\n", + " 但是不足的地方是代码会不够高效,要多写不少代码!\n", + "\n", + "\n", + "* 这里和解法1基本一致,但是新使用了 and, >= " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-15T07:20:59.832765Z", + "start_time": "2020-09-15T07:20:58.741537Z" + } + }, + "outputs": [], + "source": [ + "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "# 价格预警机器人\n", + "# 3. 价格预警机器人\n", "* 假设你要监控 A,B,C 三个商品\n", - "* 如是果 A/B/C 分别降价 30/20/10 元上,就要报警\n", + "* 如果 A/B/C 分别降价 30/20/10 元上,就要报警\n", "* 提示用户输入商品名(A/B/C)\n", "* 提示用户输入降价值 (数字)\n", "* 如果需要报警则打印商品名 + 降价值\n" @@ -28,28 +229,185 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# 考试评语机器人\n", + "## 解法1\n", + "\n", + "\n", + "* 使用 input 函数提示用户输入商品名, 放到 product 中\n", + "\n", + " 注意 product 是我们设定的一个变量名,我们也可以起一个别的名字,比如叫 name\n", + " \n", + "* 使用 input 函数提示用户输入降价值, 再用 int 函数将其转换成数字后放到 lower_price 中\n", + "\n", + " 注意 lower_price 是我们设定的一个变量名,我们也可以起一个别的名字,比如叫 jiangjia (虽然这不是一个好名字)\n", + "\n", + "* 先判断是否 product==\"A\" 并且 lower_price > 30, 那么就提示 A 商品的降价幅度\n", + "\n", + "* 再判断是否 product==\"B\" 并且 lower_price > 20 , 那么就提示 B 商品的降价幅度\n", + "\n", + "* 再判断是否 product==\"C\" 并且 lower_price > 10, 那么就提示 C 商品的降价幅度\n", + "\n", + "* 最后如果前面的三个条件都不满足,就输出 \"No Alert!\"\n", + "\n", + "* 大家好好理解 if/elif/else 的层次递进关系" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-15T07:21:05.254821Z", + "start_time": "2020-09-15T07:20:59.835292Z" + } + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 解法2\n", + "\n", + "---\n", + "改进点:\n", + "\n", + "大家是否发现下面这句话重复了3次:\n", + "\n", + " print(\"[Alert] Product %s, lower_price %d\" % (product,lower_price))\n", + "\n", + "所以们使用了 or 条件语句把对 A/B/C 三个商品的条件判断联接起来,最后一起调用了这条语言\n", + "\n", + "因为 print 输出的警告信息是使用 \"%\" 和变量进行自动拼接出来的,所不管是哪个商品警告都能复用\n", + "\n", + "大家写代码时一定要反复思考,不但是让写的代码能正常执行,也能很简洁优雅,让代码可以复用!\n", + "\n", + "---\n", + "\n", + "* 使用 input 函数提示用户输入商品名, 放到 product 中\n", + "\n", + " 注意 product 是我们设定的一个变量名,我们也可以起一个别的名字,比如叫 name\n", + " \n", + "* 使用 input 函数提示用户输入降价值, 再用 int 函数将其转换成数字后放到 lower_price 中\n", + "\n", + " 注意 lower_price 是我们设定的一个变量名,我们也可以起一个别的名字,比如叫 jiangjia (虽然这不是一个好名字)\n", + "\n", + "* 先判断是否 (product==\"A\" 并且 lower_price > 30) 或\n", + " (product==\"B\" 并且 lower_price > 20) 或\n", + " (product==\"C\" 并且 lower_price > 10) \n", + "\n", + " 如果3者中有一个条件满足了,就提示相应的商品及降价幅度\n", + " \n", + "\n", + "* 最后如果前面的三个条件都不满足,就输出 \"No Alert!\"\n", + "\n", + "\n", + "* 这里使用 if/else\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-15T07:21:09.261330Z", + "start_time": "2020-09-15T07:21:05.256783Z" + } + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 4. 考试评语机器人\n", + "\n", + "---\n", + "题目:\n", + "\n", "* 假设你现在是考试院的老师,在看试卷,要打评语\n", - "* 提示用户输出考试分数(数字)\n", + "* 提示用户输出考试分数(数字),分数范围是0 ~ 100 分\n", "* 如果输入值小于 60 分,则打印 \"要加油呀!\"\n", "* 如果输入值大于等于 60 分,且小于 80分,则打印 \"不错呀,认真学习了!\"\n", "* 如果输入值大于等于 80 分,则打印 \"考得不错,不要骄傲!\"\n", + "* 不在 0~100 分范围内的输入,我不需要打印任何信息\n", "\n", - " 备注:因为是学习的第二天,所以暂时让用户输入,以后可以直接读取成绩表,直接出评语" + " 备注:因为是学习的第二天,所以暂时让用户输入,以后可以直接读取成绩表,直接出评语\n", + " \n", + "---\n", + "解题讲解:\n", + "\n", + "此题为2,3题的变体,故不再专门解释!\n", + "\n", + "* 特别注意的是,本题不需要使用 else\n", + "* 在 if/elif 的组合里要考虑用户输入值不在 0~100 的情况" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-15T07:21:11.450273Z", + "start_time": "2020-09-15T07:21:09.263112Z" + } + }, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, "source": [ - "# 智能空调机器人\n", + "# 5. 智能空调机器人\n", + "\n", + "---\n", + "题目:\n", + "\n", "* 假设你现在设计一台智能空调\n", "* 提示用户输入季节(只能是 winter/summer)\n", "* 提示用户输入当前温度 (数字)\n", "* 如果季节是 winter, 且温度低于10度,则打印 \"太冷了,开热空调了\"\n", "* 如果季节是 summer, 且温度高于27度,则打印 \"太热了,开冷空调了\"\n", + "* 如果是不符合上面条件的用户输入,则不用打印任何东西\n", + "\n", + " 备注:因为是学习的第二天,所以暂时让用户输入,以后可以接外部温度电子器直接读\n", + " \n", + "---\n", + "解题讲解:\n", + "\n", + "此题为2,3题的变体,故不再专门解释!\n", + "\n", + "特别注意的是,本题不需要使用 else" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-15T07:21:16.073203Z", + "start_time": "2020-09-15T07:21:11.452151Z" + } + }, + "outputs": [], + "source": [ + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-15T07:26:43.137544Z", + "start_time": "2020-09-15T07:26:43.134738Z" + } + }, + "source": [ + "# 6. 致谢!\n", "\n", - " 备注:因为是学习的第二天,所以暂时让用户输入,以后可以接外部温度电子器直接读" + "本章内容由 Miles/Zane 评审,在此致谢!" ] } ], @@ -80,9 +438,14 @@ "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, - "toc_position": {}, + "toc_position": { + "height": "calc(100% - 180px)", + "left": "10px", + "top": "150px", + "width": "358.391px" + }, "toc_section_display": true, - "toc_window_display": false + "toc_window_display": true }, "varInspector": { "cols": { diff --git a/03-loop/answer-questions.ipynb b/03-loop/answer-questions.ipynb index fb2aa24..aad427b 100644 --- a/03-loop/answer-questions.ipynb +++ b/03-loop/answer-questions.ipynb @@ -1,5 +1,60 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 1. 基础知识重复提醒\n", + "\n", + "有同学会问,不是上一章讲过这些吗?为什么要要重复提醒呢?\n", + "\n", + "是的,知识学习理解的过程是一个反复的过程,我们小时候第一次学习汉字时,基础字练了20次以上。\n", + "\n", + "对于 Python 这么一门语言的练习,基础知识的理解我建议要重复3次以上。\n", + "\n", + "老师把重要的基础知识重复出现3次,大家要把作业练习3次以上,老师推荐的各种链接多看一下。\n", + "\n", + "学习新知识时快就是慢,慢就是快!\n", + "\n", + "大家不但要做我们习题集上的题目,也要练习书上的例题,练习越多理解越深!\n", + "\n", + "## 新知识“循环遍历”的提醒\n", + "\n", + "如果想让重复工作自动化,必须要学好循环;\n", + "\n", + "你可以想像重复的工作,就像一个“列表”, 用循环处理1个任务和处理10000个任务本质上是一样的:\n", + "\n", + "\n", + "## 写代码报错了怎么办\n", + "\n", + "学习了怎么看错误信息,学会了怎么 Google/Baidu查找出错信息,就掌握了最重要的一门程序技能;\n", + "\n", + "有人说学会这两项,就等于半个初级程序员了;学习 Python不是为学习一个固定的知识,而是为了学习解决问题的方法。\n", + "\n", + "* [必看: Zane 老师整理了检查错误的方法](https://www.yinxiang.com/everhub/note/1e44d717-75c3-413d-83ad-5716126ccaa6)\n", + "* [新人必看:17个新手常见Python运行时错误](https://www.oschina.net/question/89964_62779)\n", + "\n", + "\n", + "## Python 各种基础知识\n", + "\n", + "* [Zane老师整理Python基础知识](https://www.yinxiang.com/everhub/note/414a0a33-7d28-4c27-af22-23f950b990fd)\n", + "* [Python3基础教程](https://www.runoob.com/python3/python3-tutorial.html)\n", + "* [Python 运算符](https://www.runoob.com/python/python-operators.html)\n", + "* [Python3 条件控制](https://www.runoob.com/python3/python3-conditional-statements.html)\n", + "* [Python3 循环语句](https://www.runoob.com/python3/python3-loop.html)\n", + "* [Python range() 函数用法](https://www.runoob.com/python/python-func-range.html)\n", + "* [Python continue 语句](https://www.runoob.com/python/python-continue-statement.html) 详解\n", + "* [Python While 循环语句](https://www.runoob.com/python/python-while-loop.html)\n", + "* [Python break 语句](https://www.runoob.com/python/python-break-statement.html)\n", + "* [Python for 循环语句](https://www.runoob.com/python/python-for-loop.html)\n", + "\n", + "## 什么是 Markdown(选学)\n", + "\n", + "学习了解 Markdown 可以让大家的代码也有漂亮的文字配合!\n", + "\n", + "* [Markdown 是什么](https://www.zhihu.com/question/19963642)" + ] + }, { "cell_type": "markdown", "metadata": { @@ -9,77 +64,97 @@ } }, "source": [ - "# 智能日历第一步,打印12个月\n", + "# 2. 智能日历第一步,打印12个月\n", + "---\n", + "题目:\n", + "\n", "* 你是智能日历的程序员\n", - "* 请打印出 1~12月,每月一行" + "* 请打印出 1~12月,每月一行\n", + "\n", + "---\n", + "参考:\n", + "\n", + "* [Python3 循环语句](https://www.runoob.com/python3/python3-loop.html)\n", + "* [python3——for循环](https://blog.csdn.net/brent_xy/article/details/79648622)\n", + "* [Python3 字符串](https://www.runoob.com/python3/python3-string.html)\n", + "* [Python str() 函数](https://www.w3school.com.cn/python/ref_func_str.asp)\n", + "* [Python range() 函数用法](https://www.runoob.com/python/python-func-range.html)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-15T09:44:25.819030Z", + "start_time": "2020-09-15T09:44:25.815128Z" + } + }, + "source": [ + "## 解法1\n", + "\n", + "* 使用 range() 生成 1~12 的数字集\n", + " \n", + " range(x,y) 表示生成大于等于x,小于y的所有数字,所以不包括y\n", + "\n", + " 大家可以执行: print(list(range(1,4)), 你会看到 [1,2,3]\n", + " \n", + " 补充:list函数 是让 range() 生成的数据集变成一个列表,可以被打印出来\n", + " \n", + " \n", + "* 使用 for 循环遍历数字集,将遍历的内容放到 i\n", + "\n", + " 使用 \"%\" 拼接符,联合 \"%d月\", i ; 拼出 \"x月\"这个格式,并输出\n", + "\n", + " %d 表示接收的是一个整数参数\n", + " \n", + " 如果大家记不熟可以参考 [Python3 字符串](https://www.runoob.com/python3/python3-string.html)" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-07-14T02:03:21.498180Z", "start_time": "2020-07-14T02:03:21.495062Z" } }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1月\n", - "2月\n", - "3月\n", - "4月\n", - "5月\n", - "6月\n", - "7月\n", - "8月\n", - "9月\n", - "10月\n", - "11月\n", - "12月\n" - ] - } - ], + "outputs": [], "source": [ - "# 解法1\n", + "\n", "for i in range(1,13):\n", " print(\"%d月\" % i)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 解法2\n", + "\n", + "* 使用 range() 生成 1~12 的数字集\n", + "\n", + " range(x,y) 表示生成大于等于x,小于y的所有数字,所以不包括y\n", + " \n", + "\n", + "* 使用 for 循环遍历数字集,将遍历的内容放到 i\n", + "\n", + " 用 str 函数将 i 转为字符串,并且用 \"+\" 和 \"月\" 进行拼接并输出 ,,并输出\n", + "\n", + " 如果大家记不熟可以参考 [Python str() 函数](https://www.w3school.com.cn/python/ref_func_str.asp)" + ] + }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-07-14T02:03:45.652068Z", "start_time": "2020-07-14T02:03:45.648374Z" } }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1月\n", - "2月\n", - "3月\n", - "4月\n", - "5月\n", - "6月\n", - "7月\n", - "8月\n", - "9月\n", - "10月\n", - "11月\n", - "12月\n" - ] - } - ], + "outputs": [], "source": [ - "# 解法2\n", "for i in range(1,13):\n", " print(str(i) + \"月\")" ] @@ -93,32 +168,65 @@ } }, "source": [ - "# for 循环计算 1 * 2 * 3 *..10" + "# 3. 用 for 循环计算1到4的连乘\n", + "---\n", + "题目解析:\n", + "\n", + "* 在 Python 中,乘法符号是 *\n", + "* 最笨的办法是 1 乘 2 乘 3 乘 4 \n", + "* 但是如果要 1 ~ 10000 的连乘你就傻眼了所以我们使 for 循环\n", + "\n", + "---\n", + "解题步骤:\n", + "\n", + "* 先将变量 total 赋值为 1: total = 1\n", + "\n", + " total 用来存连乘结果的变量,为了方便使用,我把 total初始化设为1\n", + " \n", + " \n", + "* 使用 range() 生成 1~4 的数字集\n", + "\n", + " range(x,y) 表示生成大于等于x,小于y的所有数字,所以不包括y\n", + "\n", + "\n", + "* 使用 for 循环遍历数字集,将遍历的内容放到 i\n", + "\n", + " 用 total 去乘 i, 然后再将结果放到 total\n", + "\n", + " 这样就中就可以实现连乘的效果\n", + " \n", + " \n", + "* 为了方便大家理解,我在每次循环的过程中,都用使用 print 输出 i, total 的值 \n", + "\n", + " 方便大家理解 for 循环\n", + "\n", + "---\n", + "备注:\n", + "\n", + "* 循环是非常重要的计算机概念,我一再反复强调,只有理解了它,你才把Python当机器人用\n", + "\n", + "\n", + "* 不能光看老师的讲解,外部的也要看:[Python for 循环语句](https://www.runoob.com/python/python-for-loop.html)\n" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": { "ExecuteTime": { - "end_time": "2020-07-14T02:04:58.939782Z", - "start_time": "2020-07-14T02:04:58.936529Z" + "end_time": "2020-09-16T02:54:43.089949Z", + "start_time": "2020-09-16T02:54:43.086482Z" } }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3628800\n" - ] - } - ], + "outputs": [], "source": [ "total = 1\n", - "for i in range(1,11):\n", + "for i in range(1,5):\n", + " #print(\">> For循环运算前: i:%d, total:%d\" % (i,total))\n", " total = total * i\n", - "print(total)" + " print(\">> For循环运算后: i:%d, total:%d\" % (i,total))\n", + "\n", + "print(\"连乘结果:\",total)" ] }, { @@ -130,35 +238,42 @@ } }, "source": [ - "# for 循环打印 \"Good Day!\" 里的每个字符" + "# 4. 用 for 循环打印 \"Good Day!\" 里的每个字符\n", + "---\n", + "题目解析:\n", + "\n", + "* \"Good Day!\" 是一个字符串\n", + "* 我们可以用 for 循环来遍历访问它\n", + "\n", + "---\n", + "解题步骤:\n", + "\n", + "\n", + "\n", + "* 使用 for 循环遍历 title,将遍历的内容放到 char\n", + "\n", + " 再在循环体中输出 char\n", + " \n", + "---\n", + "备注:\n", + "\n", + "* 循环是非常重要的计算机概念,我一再反复强调,只有理解了它,你才把Python当机器人用\n", + "\n", + "* 不能光看老师的讲解,外部的也要看:[Python for 循环语句](https://www.runoob.com/python/python-for-loop.html)\n", + "\n", + " 这里面有很多例子,大家可以拿出来运行,举一反三" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-07-14T02:05:46.643010Z", "start_time": "2020-07-14T02:05:46.629372Z" } }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "G\n", - "o\n", - "o\n", - "d\n", - " \n", - "D\n", - "a\n", - "y\n", - "!\n" - ] - } - ], + "outputs": [], "source": [ "title = \"Good Day!\"\n", "for char in title:\n", @@ -174,53 +289,61 @@ } }, "source": [ - "# for 循环打印出所有 1~50 之间的所有偶数" + "# 5. for 循环打印出所有 1~50 之间的所有偶数\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 解法1\n", + "---\n", + "题目解析:\n", + "\n", + "* 在 Python 中,% 表示取模 - 返回除法的余数, \n", + "\n", + " i % 2 == 0 表示 i除2的余数为0,也就是表明i为偶数\n", + "\n", + "---\n", + "解题步骤:\n", + "\n", + "\n", + "* 使用 range() 生成 0~50 的数字集\n", + "\n", + " range(x,y) 表示生成大于等于x,小于y的所有数字,所以不包括y\n", + "\n", + " 大家可以执行: print(list(range(1,4)), 你会看到 [1,2,3]\n", + " \n", + " 补充:list函数 是让 range() 生成的数据集变成一个列表,可以被打印出来 \n", + "\n", + "* 使用 for 循环遍历数字集,将遍历的内容放到 i\n", + "\n", + " 使用 % 取模操作符,判断i是否为偶数,如果是就用 print 打印出来\n", + "\n", + "---\n", + "参考备注:\n", + "\n", + "* 这道题综合了 for 循环和 if 逻辑判断,是一道综合应用题,方便大家加深对历史知识的理解!\n", + "\n", + "\n", + "* 不能光看老师的讲解,外部的也要看:[Python for 循环语句](https://www.runoob.com/python/python-for-loop.html)\n", + "\n", + "\n", + "* [Python算术运算符](https://www.runoob.com/python/python-operators.html) 详解" ] }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-07-14T02:06:20.403775Z", "start_time": "2020-07-14T02:06:20.400623Z" } }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2\n", - "4\n", - "6\n", - "8\n", - "10\n", - "12\n", - "14\n", - "16\n", - "18\n", - "20\n", - "22\n", - "24\n", - "26\n", - "28\n", - "30\n", - "32\n", - "34\n", - "36\n", - "38\n", - "40\n", - "42\n", - "44\n", - "46\n", - "48\n", - "50\n" - ] - } - ], + "outputs": [], "source": [ - "for i in range(1,51):\n", + "for i in range(0,51):\n", " if i%2 == 0:\n", " print(i)" ] @@ -229,31 +352,128 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# PM找差评\n", - "* 程序员把产器星级放入一个列表 [3,5,4,2,1,5,5]\n", - "* 请把所有小于4的评分打印出来(用 continue)" + "## 解法2\n", + "\n", + "---\n", + "题目解析:\n", + "\n", + "* 在 Python 中,range 的第3个参数表示 \"步长\",默认为1\n", + "\n", + " range(0,5) 会生成 [0,1,2,3,4]\n", + " \n", + " range(0,5,2) 则会生成 [0,2,4], 因为它的步长变成了2\n", + " \n", + " 这样也方便我们自动生成某一段范围内的偶数\n", + "\n", + "---\n", + "\n", + "解题步骤:\n", + "\n", + "\n", + "* 使用 range() 生成 0~50 的偶数\n", + "\n", + " range(0,51,2) 表示从0开始,到50为止,步长为2生成数据集\n", + " \n", + " 因为步长为2,所以生成的数据集都是偶数\n", + "\n", + "* 再使用 for 循环遍历数据集,并打印输出 \n", + "\n", + "---\n", + "参考备注:\n", + "\n", + "* 这道题综合了 for 循环和 if 逻辑判断,是一道综合应用题,方便大家加深对历史知识的理解!\n", + "\n", + "* 不能光看老师的讲解,外部的也要看:[Python for 循环语句](https://www.runoob.com/python/python-for-loop.html)\n", + "\n", + "* 再温习一下 [Python range() 函数用法](https://www.runoob.com/python/python-func-range.html)\n" ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "metadata": { "ExecuteTime": { - "end_time": "2020-07-14T02:07:12.968023Z", - "start_time": "2020-07-14T02:07:12.962565Z" + "end_time": "2020-09-16T03:24:19.203031Z", + "start_time": "2020-09-16T03:24:19.199317Z" } }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3\n", - "2\n", - "1\n" - ] + "outputs": [], + "source": [ + "for i in range(0,51,2):\n", + " print(i)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-16T03:33:03.268983Z", + "start_time": "2020-09-16T03:33:03.265051Z" } - ], + }, + "outputs": [], + "source": [ + "list(range(0,5,2))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 6. PM自动找差评\n", + "---\n", + "题目:\n", + "\n", + "* 程序员把产品星级放入一个列表 [3,5,4,2,1,5,5]\n", + "* 请把所有小于4的评分打印出来\n", + "\n", + " 请在 for 循环里跳过所有大于等于4星的星级(用 continue)\n", + "\n", + "---\n", + "题目解析:\n", + "\n", + "* [3,5,4,2,1,5,5] 是一个列表,可以用 for 循环遍历\n", + "\n", + "* continue 是用来跳过一次 for/whie 循环\n", + "\n", + "* star 星级大于等于4,就不是差评,可以跳过不处理\n", + "\n", + "---\n", + "解题步骤:\n", + "\n", + "* stars 是一个列表(你可以理解为一个袋子),里面存了所有产品的星级\n", + "\n", + " \n", + "* 使用 for 循环遍历 stars,将遍历的内容放到变量中 star\n", + "\n", + " 注意: stars/star 是我们设定的一个变量名,我们也可以起一个别的名字,比如叫 xingji(虽然这个名字并不好)\n", + "\n", + " 使用 if 检查 star 是否大于等 4,如果条件满足就使用 continue 跳过\n", + " \n", + " 没有跳过的情况,就打印输出 star\n", + "\n", + "---\n", + "参考备注:\n", + "\n", + "* 这道题综合了 for 循环和 if 逻辑判断和 continue跳过,是一道综合应用题,方便大家加深对历史知识的理解!\n", + "\n", + "* [Python continue 语句](https://www.runoob.com/python/python-continue-statement.html) 详解\n", + "\n", + "* 再温习一下:[Python for 循环语句](https://www.runoob.com/python/python-for-loop.html)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-16T04:02:22.463395Z", + "start_time": "2020-09-16T04:02:22.459880Z" + } + }, + "outputs": [], "source": [ "stars = [3,5,4,2,1,5,5]\n", "for star in stars:\n", @@ -262,22 +482,137 @@ " print(star)" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-16T04:13:47.930011Z", + "start_time": "2020-09-16T04:13:47.926934Z" + } + }, + "outputs": [], + "source": [ + "# 不用continue,理解即可\n", + "for star in stars:\n", + " if star < 4:\n", + " print(star)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-16T04:02:23.260137Z", + "start_time": "2020-09-16T04:02:23.256707Z" + } + }, + "outputs": [], + "source": [ + "# 使用lamda表达式\n", + "# 这是高级用法,对于普通学员不要求掌握\n", + "for star in list(filter(lambda x: x<4 , stars)):\n", + " print(star)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-16T04:02:23.798009Z", + "start_time": "2020-09-16T04:02:23.794689Z" + } + }, + "outputs": [], + "source": [ + "# 使用iterable\n", + "# 这是高级用法,对于普通学员不要求掌握\n", + "for star in [star for star in stars if star<4]:\n", + " print(star)" + ] + }, { "cell_type": "markdown", "metadata": {}, "source": [ - "# 自动邮件机器人\n", - "* 假设计你是要给自动给客户发邮件的运营人员\n", - "* 提示运营人员输入姓名 \n", - "* 提示运营人输入购买的商品 \n", - "* 输出邮件格式如下:\n", + "# 7. 自动邮件机器人\n", + "---\n", + "题目:\n", + "\n", + "* 假设你是负责给客户发邮件的运营人员\n", + "* 你现在学了 Python想开发一个发邮件机器人\n", + "\n", + "* 程序可以一直运行\n", + " \n", + " 提示运营人员输入姓名 \n", + " \n", + " 提示运营人输入购买的商品 \n", + "\n", + " 然后根据输入的姓名/商品名拼接输出邮件\n", + " \n", + " 输出邮件格式如下:\n", "\n", "Dear [用户姓名]:\n", " \n", " Your purchased product : [产品名] is delivered!\n", " \n", " Thanks for your choosing!\n", - "* 使用 while 来循环提示用户输入,直到用户在姓名中输入 ! 才退出(用break)" + " \n", + "* 使用 while 来循环提示用户输入,直到用户在姓名中输入 ! 才退出(用break)\n", + "\n", + "---\n", + "题目解析:\n", + "\n", + "* while True 表示无限循环,让这个循环一直执行下去\n", + "\n", + " 只要whilie 条件满足 就会执行里面的代码块 除非执行后导致这个条件不满足 循环才会停止\n", + " \n", + " 这while 接受的条件是 True (这是恒等式,就是条件一直成立), 只要循环体中间的代码不 break, 它就会一直运行下去\n", + " \n", + " \n", + "* break 是用来终止循环语句(for/while 循环)\n", + "\n", + "\n", + "* 请大家把自己代入进去,想像一下这个机器人能自动给客户发邮件会怎么样?\n", + "\n", + "---\n", + "解题步骤:\n", + "\n", + "* 使用三引号定义了多行邮件模板,中间的客户姓名,商品名用 %s 代替,方便自动填充\n", + "\n", + "* 使用 whiel True 声明一个无限循环\n", + "\n", + " 使用 input 提示输入 客户姓名,放到 name 中\n", + " \n", + " 用 if 判断 name == \"!\", 则调用 break 终止整个循环\n", + " \n", + " 使用 input 提示输入 商品名,放到 product 中\n", + " \n", + " 使用 % 将 (name,product) 拼接到模板 email_template中,把结果放到 email_body\n", + " \n", + " 打印输出 email_body\n", + " \n", + " 为了方便调试时区分每次循环时的输出,在循环的最后打印输出 \"----------Mail Delivered!--------------------\"\n", + "---\n", + "参考: \n", + "\n", + "* 必看:[Python While 循环语句](https://www.runoob.com/python/python-while-loop.html)\n", + "\n", + "* 必看:[Python break 语句](https://www.runoob.com/python/python-break-statement.html)\n", + "\n", + "* [使用三引号处理多行字符串](https://blog.csdn.net/sinat_41104353/article/details/79266048)\n", + "\n", + "\n", + "* [python3处理字符串](https://www.runoob.com/python3/python3-string.html)\n", + "\n", + " 请查看 Python三引号/Python字符串格式化 两节\n", + " \n", + " 大家可以把自己代入进去,想想有个机器人能帮你做事!\n", + "\n", + "\n", + "* [最全面的 python 字符串拼接总结](https://segmentfault.com/a/1190000015475309)\n" ] }, { @@ -285,27 +620,11 @@ "execution_count": null, "metadata": { "ExecuteTime": { - "start_time": "2020-07-14T02:17:26.449Z" + "end_time": "2020-09-16T07:42:46.446010Z", + "start_time": "2020-09-16T06:41:42.597043Z" } }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "请输入客户姓名:ABC\n", - "请输入商品名:DDD\n", - "\n", - "Dear ABC:\n", - "\n", - "Your purchased product : DDD is delivered!\n", - "\n", - "Thanks for your choosing!\n", - "\n", - "------------------------------\n" - ] - } - ], + "outputs": [], "source": [ "email_template = \"\"\"\n", "Dear %s:\n", @@ -314,6 +633,7 @@ "\n", "Thanks for your choosing!\n", "\"\"\"\n", + "#只要whilie 条件满足 就会执行里面的代码块 除非执行后导致这个条件不满足 循环才会停止\n", "while True:\n", " name = input(\"请输入客户姓名:\")\n", " if name == \"!\":\n", @@ -326,11 +646,13 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], - "source": [] + "source": [ + "# 致谢\n", + "\n", + "谢谢 Miles/Zack/Zane 对本章内容的评审,特此致谢!" + ] } ], "metadata": { @@ -360,9 +682,14 @@ "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, - "toc_position": {}, + "toc_position": { + "height": "calc(100% - 180px)", + "left": "10px", + "top": "150px", + "width": "358.391px" + }, "toc_section_display": true, - "toc_window_display": false + "toc_window_display": true }, "varInspector": { "cols": { diff --git a/03-loop/questions.ipynb b/03-loop/questions.ipynb index 916c217..b113c38 100644 --- a/03-loop/questions.ipynb +++ b/03-loop/questions.ipynb @@ -1,5 +1,60 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 1. 基础知识重复提醒\n", + "\n", + "有同学会问,不是上一章讲过这些吗?为什么要要重复提醒呢?\n", + "\n", + "是的,知识学习理解的过程是一个反复的过程,我们小时候第一次学习汉字时,基础字练了20次以上。\n", + "\n", + "对于 Python 这么一门语言的练习,基础知识的理解我建议要重复3次以上。\n", + "\n", + "老师把重要的基础知识重复出现3次,大家要把作业练习3次以上,老师推荐的各种链接多看一下。\n", + "\n", + "学习新知识时快就是慢,慢就是快!\n", + "\n", + "大家不但要做我们习题集上的题目,也要练习书上的例题,练习越多理解越深!\n", + "\n", + "## 新知识“循环遍历”的提醒\n", + "\n", + "如果想让重复工作自动化,必须要学好循环;\n", + "\n", + "你可以想像重复的工作,就像一个“列表”, 用循环处理1个任务和处理10000个任务本质上是一样的:\n", + "\n", + "\n", + "## 写代码报错了怎么办\n", + "\n", + "学习了怎么看错误信息,学会了怎么 Google/Baidu查找出错信息,就掌握了最重要的一门程序技能;\n", + "\n", + "有人说学会这两项,就等于半个初级程序员了;学习 Python不是为学习一个固定的知识,而是为了学习解决问题的方法。\n", + "\n", + "* [必看: Zane 老师整理了检查错误的方法](https://www.yinxiang.com/everhub/note/1e44d717-75c3-413d-83ad-5716126ccaa6)\n", + "* [新人必看:17个新手常见Python运行时错误](https://www.oschina.net/question/89964_62779)\n", + "\n", + "\n", + "## Python 各种基础知识\n", + "\n", + "* [Zane老师整理Python基础知识](https://www.yinxiang.com/everhub/note/414a0a33-7d28-4c27-af22-23f950b990fd)\n", + "* [Python3基础教程](https://www.runoob.com/python3/python3-tutorial.html)\n", + "* [Python 运算符](https://www.runoob.com/python/python-operators.html)\n", + "* [Python3 条件控制](https://www.runoob.com/python3/python3-conditional-statements.html)\n", + "* [Python3 循环语句](https://www.runoob.com/python3/python3-loop.html)\n", + "* [Python range() 函数用法](https://www.runoob.com/python/python-func-range.html)\n", + "* [Python continue 语句](https://www.runoob.com/python/python-continue-statement.html) 详解\n", + "* [Python While 循环语句](https://www.runoob.com/python/python-while-loop.html)\n", + "* [Python break 语句](https://www.runoob.com/python/python-break-statement.html)\n", + "* [Python for 循环语句](https://www.runoob.com/python/python-for-loop.html)\n", + "\n", + "## 什么是 Markdown(选学)\n", + "\n", + "学习了解 Markdown 可以让大家的代码也有漂亮的文字配合!\n", + "\n", + "* [Markdown 是什么](https://www.zhihu.com/question/19963642)" + ] + }, { "cell_type": "markdown", "metadata": { @@ -9,11 +64,94 @@ } }, "source": [ - "# 智能日历第一步,打印12个月\n", + "# 2. 智能日历第一步,打印12个月\n", + "---\n", + "题目:\n", + "\n", "* 你是智能日历的程序员\n", - "* 请打印出 1~12月,每月一行" + "* 请打印出 1~12月,每月一行\n", + "\n", + "---\n", + "参考:\n", + "\n", + "* [Python3 循环语句](https://www.runoob.com/python3/python3-loop.html)\n", + "* [python3——for循环](https://blog.csdn.net/brent_xy/article/details/79648622)\n", + "* [Python3 字符串](https://www.runoob.com/python3/python3-string.html)\n", + "* [Python str() 函数](https://www.w3school.com.cn/python/ref_func_str.asp)\n", + "* [Python range() 函数用法](https://www.runoob.com/python/python-func-range.html)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-15T09:44:25.819030Z", + "start_time": "2020-09-15T09:44:25.815128Z" + } + }, + "source": [ + "## 解法1\n", + "\n", + "* 使用 range() 生成 1~12 的数字集\n", + " \n", + " range(x,y) 表示生成大于等于x,小于y的所有数字,所以不包括y\n", + "\n", + " 大家可以执行: print(list(range(1,4)), 你会看到 [1,2,3]\n", + " \n", + " 补充:list函数 是让 range() 生成的数据集变成一个列表,可以被打印出来\n", + " \n", + " \n", + "* 使用 for 循环遍历数字集,将遍历的内容放到 i\n", + "\n", + " 使用 \"%\" 拼接符,联合 \"%d月\", i ; 拼出 \"x月\"这个格式,并输出\n", + "\n", + " %d 表示接收的是一个整数参数\n", + " \n", + " 如果大家记不熟可以参考 [Python3 字符串](https://www.runoob.com/python3/python3-string.html)" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2020-07-14T02:03:21.498180Z", + "start_time": "2020-07-14T02:03:21.495062Z" + } + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 解法2\n", + "\n", + "* 使用 range() 生成 1~12 的数字集\n", + "\n", + " range(x,y) 表示生成大于等于x,小于y的所有数字,所以不包括y\n", + " \n", + "\n", + "* 使用 for 循环遍历数字集,将遍历的内容放到 i\n", + "\n", + " 用 str 函数将 i 转为字符串,并且用 \"+\" 和 \"月\" 进行拼接并输出 ,,并输出\n", + "\n", + " 如果大家记不熟可以参考 [Python str() 函数](https://www.w3school.com.cn/python/ref_func_str.asp)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2020-07-14T02:03:45.652068Z", + "start_time": "2020-07-14T02:03:45.648374Z" + } + }, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": { @@ -23,9 +161,59 @@ } }, "source": [ - "# for 循环计算 1 * 2 * 3 *..10" + "# 3. 用 for 循环计算1到4的连乘\n", + "---\n", + "题目解析:\n", + "\n", + "* 在 Python 中,乘法符号是 *\n", + "* 最笨的办法是 1 乘 2 乘 3 乘 4 \n", + "* 但是如果要 1 ~ 10000 的连乘你就傻眼了所以我们使 for 循环\n", + "\n", + "---\n", + "解题步骤:\n", + "\n", + "* 先将变量 total 赋值为 1: total = 1\n", + "\n", + " total 用来存连乘结果的变量,为了方便使用,我把 total初始化设为1\n", + " \n", + " \n", + "* 使用 range() 生成 1~4 的数字集\n", + "\n", + " range(x,y) 表示生成大于等于x,小于y的所有数字,所以不包括y\n", + "\n", + "\n", + "* 使用 for 循环遍历数字集,将遍历的内容放到 i\n", + "\n", + " 用 total 去乘 i, 然后再将结果放到 total\n", + "\n", + " 这样就中就可以实现连乘的效果\n", + " \n", + " \n", + "* 为了方便大家理解,我在每次循环的过程中,都用使用 print 输出 i, total 的值 \n", + "\n", + " 方便大家理解 for 循环\n", + "\n", + "---\n", + "备注:\n", + "\n", + "* 循环是非常重要的计算机概念,我一再反复强调,只有理解了它,你才把Python当机器人用\n", + "\n", + "\n", + "* 不能光看老师的讲解,外部的也要看:[Python for 循环语句](https://www.runoob.com/python/python-for-loop.html)\n" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-16T02:54:43.089949Z", + "start_time": "2020-09-16T02:54:43.086482Z" + } + }, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": { @@ -35,9 +223,44 @@ } }, "source": [ - "# for 循环打印 \"Good Day!\" 里的每个字符" + "# 4. 用 for 循环打印 \"Good Day!\" 里的每个字符\n", + "---\n", + "题目解析:\n", + "\n", + "* \"Good Day!\" 是一个字符串\n", + "* 我们可以用 for 循环来遍历访问它\n", + "\n", + "---\n", + "解题步骤:\n", + "\n", + "\n", + "\n", + "* 使用 for 循环遍历 title,将遍历的内容放到 char\n", + "\n", + " 再在循环体中输出 char\n", + " \n", + "---\n", + "备注:\n", + "\n", + "* 循环是非常重要的计算机概念,我一再反复强调,只有理解了它,你才把Python当机器人用\n", + "\n", + "* 不能光看老师的讲解,外部的也要看:[Python for 循环语句](https://www.runoob.com/python/python-for-loop.html)\n", + "\n", + " 这里面有很多例子,大家可以拿出来运行,举一反三" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2020-07-14T02:05:46.643010Z", + "start_time": "2020-07-14T02:05:46.629372Z" + } + }, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": { @@ -47,42 +270,339 @@ } }, "source": [ - "# for 循环打印出所有 1~50 之间的所有偶数" + "# 5. for 循环打印出所有 1~50 之间的所有偶数\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "# PM找差评\n", - "* 程序员把产器星级放入一个列表 [3,5,4,2,1,5,5]\n", - "* 请把所有小于4的评分打印出来(用 continue)" + "## 解法1\n", + "---\n", + "题目解析:\n", + "\n", + "* 在 Python 中,% 表示取模 - 返回除法的余数, \n", + "\n", + " i % 2 == 0 表示 i除2的余数为0,也就是表明i为偶数\n", + "\n", + "---\n", + "解题步骤:\n", + "\n", + "\n", + "* 使用 range() 生成 0~50 的数字集\n", + "\n", + " range(x,y) 表示生成大于等于x,小于y的所有数字,所以不包括y\n", + "\n", + " 大家可以执行: print(list(range(1,4)), 你会看到 [1,2,3]\n", + " \n", + " 补充:list函数 是让 range() 生成的数据集变成一个列表,可以被打印出来 \n", + "\n", + "* 使用 for 循环遍历数字集,将遍历的内容放到 i\n", + "\n", + " 使用 % 取模操作符,判断i是否为偶数,如果是就用 print 打印出来\n", + "\n", + "---\n", + "参考备注:\n", + "\n", + "* 这道题综合了 for 循环和 if 逻辑判断,是一道综合应用题,方便大家加深对历史知识的理解!\n", + "\n", + "\n", + "* 不能光看老师的讲解,外部的也要看:[Python for 循环语句](https://www.runoob.com/python/python-for-loop.html)\n", + "\n", + "\n", + "* [Python算术运算符](https://www.runoob.com/python/python-operators.html) 详解" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2020-07-14T02:06:20.403775Z", + "start_time": "2020-07-14T02:06:20.400623Z" + } + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 解法2\n", + "\n", + "---\n", + "题目解析:\n", + "\n", + "* 在 Python 中,range 的第3个参数表示 \"步长\",默认为1\n", + "\n", + " range(0,5) 会生成 [0,1,2,3,4]\n", + " \n", + " range(0,5,2) 则会生成 [0,2,4], 因为它的步长变成了2\n", + " \n", + " 这样也方便我们自动生成某一段范围内的偶数\n", + "\n", + "---\n", + "\n", + "解题步骤:\n", + "\n", + "\n", + "* 使用 range() 生成 0~50 的偶数\n", + "\n", + " range(0,51,2) 表示从0开始,到50为止,步长为2生成数据集\n", + " \n", + " 因为步长为2,所以生成的数据集都是偶数\n", + "\n", + "* 再使用 for 循环遍历数据集,并打印输出 \n", + "\n", + "---\n", + "参考备注:\n", + "\n", + "* 这道题综合了 for 循环和 if 逻辑判断,是一道综合应用题,方便大家加深对历史知识的理解!\n", + "\n", + "* 不能光看老师的讲解,外部的也要看:[Python for 循环语句](https://www.runoob.com/python/python-for-loop.html)\n", + "\n", + "* 再温习一下 [Python range() 函数用法](https://www.runoob.com/python/python-func-range.html)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-16T03:24:19.203031Z", + "start_time": "2020-09-16T03:24:19.199317Z" + } + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-16T03:33:03.268983Z", + "start_time": "2020-09-16T03:33:03.265051Z" + } + }, + "outputs": [], + "source": [ + "list(range(0,5,2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "# 自动邮件机器人\n", - "* 假设计你是要给自动给客户发邮件的运营人员\n", - "* 提示运营人员输入姓名 \n", - "* 提示运营人输入购买的商品 \n", - "* 输出邮件格式如下:\n", + "# 6. PM自动找差评\n", + "---\n", + "题目:\n", + "\n", + "* 程序员把产品星级放入一个列表 [3,5,4,2,1,5,5]\n", + "* 请把所有小于4的评分打印出来\n", + "\n", + " 请在 for 循环里跳过所有大于等于4星的星级(用 continue)\n", + "\n", + "---\n", + "题目解析:\n", + "\n", + "* [3,5,4,2,1,5,5] 是一个列表,可以用 for 循环遍历\n", + "\n", + "* continue 是用来跳过一次 for/whie 循环\n", + "\n", + "* star 星级大于等于4,就不是差评,可以跳过不处理\n", + "\n", + "---\n", + "解题步骤:\n", + "\n", + "* stars 是一个列表(你可以理解为一个袋子),里面存了所有产品的星级\n", + "\n", + " \n", + "* 使用 for 循环遍历 stars,将遍历的内容放到变量中 star\n", + "\n", + " 注意: stars/star 是我们设定的一个变量名,我们也可以起一个别的名字,比如叫 xingji(虽然这个名字并不好)\n", + "\n", + " 使用 if 检查 star 是否大于等 4,如果条件满足就使用 continue 跳过\n", + " \n", + " 没有跳过的情况,就打印输出 star\n", + "\n", + "---\n", + "参考备注:\n", + "\n", + "* 这道题综合了 for 循环和 if 逻辑判断和 continue跳过,是一道综合应用题,方便大家加深对历史知识的理解!\n", + "\n", + "* [Python continue 语句](https://www.runoob.com/python/python-continue-statement.html) 详解\n", + "\n", + "* 再温习一下:[Python for 循环语句](https://www.runoob.com/python/python-for-loop.html)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-16T04:02:22.463395Z", + "start_time": "2020-09-16T04:02:22.459880Z" + } + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-16T04:13:47.930011Z", + "start_time": "2020-09-16T04:13:47.926934Z" + } + }, + "outputs": [], + "source": [ + "# 不用continue,理解即可\n", + "for star in stars:\n", + " if star < 4:\n", + " print(star)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-16T04:02:23.260137Z", + "start_time": "2020-09-16T04:02:23.256707Z" + } + }, + "outputs": [], + "source": [ + "# 使用lamda表达式\n", + "# 这是高级用法,对于普通学员不要求掌握\n", + "for star in list(filter(lambda x: x<4 , stars)):\n", + " print(star)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-16T04:02:23.798009Z", + "start_time": "2020-09-16T04:02:23.794689Z" + } + }, + "outputs": [], + "source": [ + "# 使用iterable\n", + "# 这是高级用法,对于普通学员不要求掌握\n", + "for star in [star for star in stars if star<4]:\n", + " print(star)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 7. 自动邮件机器人\n", + "---\n", + "题目:\n", + "\n", + "* 假设你是负责给客户发邮件的运营人员\n", + "* 你现在学了 Python想开发一个发邮件机器人\n", + "\n", + "* 程序可以一直运行\n", + " \n", + " 提示运营人员输入姓名 \n", + " \n", + " 提示运营人输入购买的商品 \n", + "\n", + " 然后根据输入的姓名/商品名拼接输出邮件\n", + " \n", + " 输出邮件格式如下:\n", "\n", "Dear [用户姓名]:\n", " \n", " Your purchased product : [产品名] is delivered!\n", " \n", " Thanks for your choosing!\n", - "* 使用 while 来循环提示用户输入,直到用户在姓名中输入 ! 才退出(用break)" + " \n", + "* 使用 while 来循环提示用户输入,直到用户在姓名中输入 ! 才退出(用break)\n", + "\n", + "---\n", + "题目解析:\n", + "\n", + "* while True 表示无限循环,让这个循环一直执行下去\n", + "\n", + " 只要whilie 条件满足 就会执行里面的代码块 除非执行后导致这个条件不满足 循环才会停止\n", + " \n", + " 这while 接受的条件是 True (这是恒等式,就是条件一直成立), 只要循环体中间的代码不 break, 它就会一直运行下去\n", + " \n", + " \n", + "* break 是用来终止循环语句(for/while 循环)\n", + "\n", + "\n", + "* 请大家把自己代入进去,想像一下这个机器人能自动给客户发邮件会怎么样?\n", + "\n", + "---\n", + "解题步骤:\n", + "\n", + "* 使用三引号定义了多行邮件模板,中间的客户姓名,商品名用 %s 代替,方便自动填充\n", + "\n", + "* 使用 whiel True 声明一个无限循环\n", + "\n", + " 使用 input 提示输入 客户姓名,放到 name 中\n", + " \n", + " 用 if 判断 name == \"!\", 则调用 break 终止整个循环\n", + " \n", + " 使用 input 提示输入 商品名,放到 product 中\n", + " \n", + " 使用 % 将 (name,product) 拼接到模板 email_template中,把结果放到 email_body\n", + " \n", + " 打印输出 email_body\n", + " \n", + " 为了方便调试时区分每次循环时的输出,在循环的最后打印输出 \"----------Mail Delivered!--------------------\"\n", + "---\n", + "参考: \n", + "\n", + "* 必看:[Python While 循环语句](https://www.runoob.com/python/python-while-loop.html)\n", + "\n", + "* 必看:[Python break 语句](https://www.runoob.com/python/python-break-statement.html)\n", + "\n", + "* [使用三引号处理多行字符串](https://blog.csdn.net/sinat_41104353/article/details/79266048)\n", + "\n", + "\n", + "* [python3处理字符串](https://www.runoob.com/python3/python3-string.html)\n", + "\n", + " 请查看 Python三引号/Python字符串格式化 两节\n", + " \n", + " 大家可以把自己代入进去,想想有个机器人能帮你做事!\n", + "\n", + "\n", + "* [最全面的 python 字符串拼接总结](https://segmentfault.com/a/1190000015475309)\n" ] }, { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-16T07:42:46.446010Z", + "start_time": "2020-09-16T06:41:42.597043Z" + } + }, "outputs": [], "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 致谢\n", + "\n", + "谢谢 Miles/Zack/Zane 对本章内容的评审,特此致谢!" + ] } ], "metadata": { @@ -112,9 +632,14 @@ "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, - "toc_position": {}, + "toc_position": { + "height": "calc(100% - 180px)", + "left": "10px", + "top": "150px", + "width": "358.391px" + }, "toc_section_display": true, - "toc_window_display": false + "toc_window_display": true }, "varInspector": { "cols": { diff --git a/04-list-dict/answer-questions.ipynb b/04-list-dict/answer-questions.ipynb new file mode 100644 index 0000000..68b4683 --- /dev/null +++ b/04-list-dict/answer-questions.ipynb @@ -0,0 +1,734 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 1. 基础知识重复提醒\n", + "\n", + "有同学会问,不是上一章讲过这些吗?为什么要要重复提醒呢?\n", + "\n", + "是的,知识学习理解的过程是一个反复的过程,我们小时候第一次学习汉字时,基础字练了20次以上。\n", + "\n", + "对于 Python 这么一门语言的练习,基础知识的理解我建议要重复3次以上。\n", + "\n", + "老师把重要的基础知识重复出现3次,大家要把作业练习3次以上,老师推荐的各种链接多看一下。\n", + "\n", + "学习新知识时快就是慢,慢就是快!\n", + "\n", + "大家不但要做我们习题集上的题目,也要练习书上的例题,练习越多理解越深!\n", + "\n", + "## 新知识“列表/字典/元组”的提醒\n", + "\n", + "机器人的要处理的重复任务,必须要有一个地方进行存储;\n", + "\n", + "列表/字典/元组等数据结构就是用来存储各种复杂任务与数据的地方\n", + "\n", + "数据结构 + 算法(计算逻辑)= 程序\n", + "\n", + "* [Python 列表(List)](https://www.runoob.com/python/python-lists.html)\n", + "* [Python 字典(Dictionary)](https://www.runoob.com/python/python-dictionary.html)\n", + "* [Python 元组](https://www.runoob.com/python/python-tuples.html)\n", + "\n", + "## 写代码报错了怎么办\n", + "\n", + "学习了怎么看错误信息,学会了怎么 Google/Baidu查找出错信息,就掌握了最重要的一门程序技能;\n", + "\n", + "有人说学会这两项,就等于半个初级程序员了;学习 Python不是为学习一个固定的知识,而是为了学习解决问题的方法。\n", + "\n", + "* [必看: Zane 老师整理了检查错误的方法](https://www.yinxiang.com/everhub/note/1e44d717-75c3-413d-83ad-5716126ccaa6)\n", + "* [新人必看:17个新手常见Python运行时错误](https://www.oschina.net/question/89964_62779)\n", + "\n", + "\n", + "## Python 各种基础知识\n", + "---\n", + "本章\n", + "* [Python 列表(List)](https://www.runoob.com/python/python-lists.html)\n", + "* [Python 字典(Dictionary)](https://www.runoob.com/python/python-dictionary.html)\n", + "* [Python 元组](https://www.runoob.com/python/python-tuples.html)\n", + "* [Python 字典(Dictionary) get()方法](https://www.runoob.com/python/att-dictionary-get.html)\n", + "* [Python List sort()方法](https://www.runoob.com/python/att-list-sort.html)\n", + "* [Python 切片介绍](https://www.liaoxuefeng.com/wiki/1016959663602400/1017269965565856)\n", + "* [Python基础学习-Python中最常见括号()、[]、{}的区别](https://www.cnblogs.com/xuxiaolu/p/4726064.html)\n", + "* [Python List append()方法](https://www.runoob.com/python/att-list-append.html)\n", + "* [Python List insert()方法](https://www.runoob.com/python/att-list-insert.html)\n", + "* [Python List index()方法](https://www.runoob.com/python/att-list-index.html)\n", + "* [python,list中删除的三种方法del、remove、pop](https://blog.csdn.net/weixin_42814873/article/details/83377431)\n", + "* [Python List sort()方法](https://www.runoob.com/python/att-list-sort.html)\n", + "\n", + "---\n", + "历史\n", + "\n", + "* [Zane老师整理Python基础知识](https://www.yinxiang.com/everhub/note/414a0a33-7d28-4c27-af22-23f950b990fd)\n", + "* [Python3基础教程](https://www.runoob.com/python3/python3-tutorial.html)\n", + "* [Python 运算符](https://www.runoob.com/python/python-operators.html)\n", + "* [Python3 条件控制](https://www.runoob.com/python3/python3-conditional-statements.html)\n", + "* [Python3 循环语句](https://www.runoob.com/python3/python3-loop.html)\n", + "* [Python range() 函数用法](https://www.runoob.com/python/python-func-range.html)\n", + "* [Python continue 语句](https://www.runoob.com/python/python-continue-statement.html) 详解\n", + "* [Python While 循环语句](https://www.runoob.com/python/python-while-loop.html)\n", + "* [Python break 语句](https://www.runoob.com/python/python-break-statement.html)\n", + "* [Python for 循环语句](https://www.runoob.com/python/python-for-loop.html)\n", + "\n", + "\n", + "## 什么是 Markdown(选学)\n", + "\n", + "学习了解 Markdown 可以让大家的代码也有漂亮的文字配合!\n", + "\n", + "* [Markdown 是什么](https://www.zhihu.com/question/19963642)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 智能排序机器人\n", + "---\n", + "题目:\n", + "* 你现在公司的销售经理,要对一些销售数据进行分析\n", + "\n", + "* 公司有百亿条销售数据,抽出一些销售额信息让你练手\n", + "\n", + "```\n", + " sales = [100,200,50,300,20,500,1000,10]\n", + "```\n", + "* 请对 sales 进行升序排序,并且打印出来\n", + "* 请对 sales 进行降序排序,并且打印出来\n", + "* 请用排序+切片的方式,找出 Top3 的销售额\n", + "* 请用排序+切片的方式,找出 最低的3个销售额\n", + "\n", + "--- \n", + "题目解析:\n", + "\n", + "* sales 是一个列表类型的对象(变量),里面有非常多对象方法\n", + "\n", + "* 其中就包括了 sort 方法\n", + "\n", + " sort() 用于对原列表进行排序\n", + " \n", + " 我们可以指定 reverse = False/True 对来对列表进行升降序排序\n", + " \n", + " 更多用法可以参考:[Python List sort()方法](https://www.runoob.com/python/att-list-sort.html)\n", + "\n", + "\n", + "* Python提供了切片(Slice)操作符,能大大简化取某一段范围内的数据\n", + "\n", + " 它提供了各种你想像不到的灵活方式来操作选取数据,它不但现在列表有用,以后在操作 Excel等数据时也有用\n", + " \n", + " 大家不但要看书上的内容,也可再看看外部: [Python 切片介绍](https://www.liaoxuefeng.com/wiki/1016959663602400/1017269965565856)\n", + "\n", + "\n", + "* 很多同学会被 \"()\", \"[]\" 这两个搞混,\n", + "\n", + " () 一般是接在函数名后,() 里面放若干个参数,比如 print(\"Hello,world\")\n", + " () 还有可以用来定义元组(tuple), 类似列表,但是里面的值是不可修改的\n", + " \n", + " [] 一般是接在 列表对象/字典对象后,这是用有来选择数据的,比如 sales[0] \n", + " 如果大家后面学习 Pandas, 会发现 [] 的更多用处 [Pandas的切片处理](https://blog.csdn.net/yoonhee/article/details/76168253)\n", + "\n", + "\n", + " 更详细的解释可以看这里:[Python基础学习-Python中最常见括号()、[]、{}的区别](https://www.cnblogs.com/xuxiaolu/p/4726064.html)\n", + " \n", + " \n", + "* 字符串 乘 一个数字,表示将这个字符串重多少次(是不是很好玩)\n", + "\n", + " \">\"* 10 == \">>>>>>>>>>\"\n", + " \n", + " 方便我们输出很长的分隔符\n", + " \n", + "---\n", + "\n", + "解题步骤:\n", + "\n", + "* 直接调用 sales 的 sort() 方法, 因为默认是升序\n", + "\n", + " 再打印输出排序后的 sales\n", + " \n", + "\n", + "* 调用 sales 的 sort() 方法,传入 reverse 参数\n", + "\n", + " 再打印输出排序后的 sales\n", + " \n", + "\n", + "* 直接调用 sales 的 sort() 方法, 这时 sales 是升序的\n", + "\n", + "\n", + "* 使用 [-3:] 切片选择最后3个数据并打印输出\n", + " \n", + " 切片里参数以的冒号 \":\" 来分隔,第1参数表示从哪哪里开始选择\n", + " \n", + " -3 表示从倒数第3个数开始选择,\n", + " \n", + " 冒号 \":\" 后为空表示选择到最后为止\n", + " \n", + " \n", + "* 使用 [:3] 切片选择前3个数据并打印输出\n", + "\n", + " 切片里参数以的冒号 \":\" 来分隔,第1参数表示从哪哪里开始选择\n", + " \n", + " 冒号 \":\" 前为空表示从列表最开始的地方进行选择\n", + " \n", + " 3 表示选到第列表的第3数据并为止\n", + " \n", + "---\n", + "参考:\n", + "* [Python List sort()方法](https://www.runoob.com/python/att-list-sort.html)\n", + "* [Python 切片介绍](https://www.liaoxuefeng.com/wiki/1016959663602400/1017269965565856)\n", + "* [Python基础学习-Python中最常见括号()、[]、{}的区别](https://www.cnblogs.com/xuxiaolu/p/4726064.html)\n", + "* [Python 列表(List)](https://www.runoob.com/python/python-lists.html)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sales = [100,200,50,300,20,500,1000,10]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(\"进行升序排序\")\n", + "sales.sort() #\n", + "print(sales)\n", + "print(\"-\"*70)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(\"进行降序排序\")\n", + "sales.sort(reverse= True) #进行降序排序\n", + "print(sales)\n", + "print(\"-\"*70)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sales.sort() #进行升序排序\n", + "print(\"查看最后3个数据(top3)\")\n", + "print(sales[-3:]) \n", + "print(\"-\"*70)" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "ExecuteTime": { + "end_time": "2020-07-16T10:18:33.737309Z", + "start_time": "2020-07-16T10:18:33.730414Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "进行升序排序\n", + "[10, 20, 50, 100, 200, 300, 500, 1000]\n", + "----------------------------------------------------------------------\n", + "进行降序排序\n", + "[1000, 500, 300, 200, 100, 50, 20, 10]\n", + "----------------------------------------------------------------------\n", + "查看最后3个数据(top3)\n", + "[300, 500, 1000]\n", + "----------------------------------------------------------------------\n", + "查看最前3个数据( 最低的3个销售额)\n", + "[10, 20, 50]\n" + ] + } + ], + "source": [ + "\n", + "print(\"查看最前3个数据( 最低的3个销售额)\")\n", + "print(sales[:3]) \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 车位管理机器人\n", + "---\n", + "题目:\n", + "\n", + "* 你现有管理一个停车场\n", + "* 现有一组车位租用情况,结构如下:\n", + "```\n", + " car_nums = ['A0001','A00X9','A0027'] \n", + "```\n", + "* 现有新来了一辆车 'A0030', 请把它放在 car_nums 的最后面\n", + "* 现有新来了一辆车 'A0000', 请把它放在 car_nums 的最前面\n", + "* 'A00X9' 这辆车现在不租你的停车位上,请把它从 car_nums 中删掉\n", + "* 现在来了一个车队 ['B0001','B0002','B003'], 请用一行代码把它加到 car_nums 中\n", + "\n", + "--- \n", + "题目解析:\n", + "\n", + "* 使用 list.append 函数向 car_nums 最尾部添加 'A0030'\n", + "\n", + "* 使用 list.insert 函数向 car_nums 最前面插入 'A0000'\n", + "\n", + " insert 函数有两个参数,第1个参数表示插入数据的位置,因为 Python 的列表数据下标是从 0 开始,所以我们填的是0.\n", + " \n", + " \n", + "* 使用 list.index 函数从 car_nums 中找出 'A00X9' 所在的位置,放到 del_index 中\n", + "\n", + " 再调用 del 来删除 'A00X9' \n", + " \n", + " \n", + "* 使用 list.extend 将 ['B0001','B0002','B003'] 放到 car_nums 的最尾部 \n", + "\n", + "---\n", + "参考:\n", + "* [Python List append()方法](https://www.runoob.com/python/att-list-append.html)\n", + "* [Python List insert()方法](https://www.runoob.com/python/att-list-insert.html)\n", + "* [Python List index()方法](https://www.runoob.com/python/att-list-index.html)\n", + "* [python,list中删除的三种方法del、remove、pop](https://blog.csdn.net/weixin_42814873/article/details/83377431)\n", + "* [Python List sort()方法](https://www.runoob.com/python/att-list-sort.html)\n", + "* [Python 切片介绍](https://www.liaoxuefeng.com/wiki/1016959663602400/1017269965565856)\n", + "* [Python基础学习-Python中最常见括号()、[]、{}的区别](https://www.cnblogs.com/xuxiaolu/p/4726064.html)\n", + "* [Python 列表(List)](https://www.runoob.com/python/python-lists.html)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": { + "ExecuteTime": { + "end_time": "2020-07-14T06:24:57.511394Z", + "start_time": "2020-07-14T06:24:57.505613Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "现有新来了一辆车 'A0030', 请把它放在 car_nums 的最后面\n", + "['A0001', 'A00X9', 'A0027', 'A0030']\n", + "----------------------------------------------------------------------\n", + "现有新来了一辆车 'A0000', 请把它放在 car_nums 的最前面\n", + "['A0000', 'A0001', 'A00X9', 'A0027', 'A0030']\n", + "----------------------------------------------------------------------\n", + "'A00X9' 这辆车现在不租你的停车位上,请把它从 car_nums 中删掉\n", + "['A0000', 'A0001', 'A0027', 'A0030']\n", + "----------------------------------------------------------------------\n", + "现在来了一个车队 ['B0001','B0002','B003'], 请用一行代码把它加到 car_nums 中\n", + "['A0000', 'A0001', 'A0027', 'A0030', 'B0001', 'B0002', 'B003']\n" + ] + } + ], + "source": [ + "print(\"现有新来了一辆车 'A0030', 请把它放在 car_nums 的最后面\")\n", + "car_nums.append('A0030')\n", + "print(car_nums)\n", + "print(\"-\"*70)\n", + "\n", + "print(\"现有新来了一辆车 'A0000', 请把它放在 car_nums 的最前面\")\n", + "car_nums.insert(0,'A0000')\n", + "print(car_nums)\n", + "print(\"-\"*70)\n", + "\n", + "print(\"'A00X9' 这辆车现在不租你的停车位上,请把它从 car_nums 中删掉\")\n", + "\n", + "del_index = car_nums.index('A00X9')\n", + "del car_nums[del_index]\n", + "# 第二种方法\n", + "# car_nums.remove('A00X9')\n", + "\n", + "print(car_nums)\n", + "print(\"-\"*70)\n", + "\n", + "print(\"现在来了一个车队 ['B0001','B0002','B003'], 请用一行代码把它加到 car_nums 中\")\n", + "car_nums.extend(['B0001','B0002','B003'])\n", + "print(car_nums)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 自动询价机器人\n", + "---\n", + "题目:\n", + "\n", + "* 你现在管理一个商务团队,每天有客户找问价格\n", + "* 你有一个价格对应表\n", + "```\n", + " prices = {\n", + " \"SKU-A\": 100,\n", + " \"SKU-B\": 120,\n", + " \"SKU-C\": 190,\n", + " \"SKU-D\": 200\n", + " }\n", + "``` \n", + "* 每次客户来问题,你都要找这个表,你现在很烦,想做一个自动机器人\n", + "* 写一个 while 循环,提示用户输入 sku\n", + "* 根据用户输入的sku查询 prices, 找印出对应价格\n", + "* 如果用户输入 ! 则退出循环(break)\n", + "\n", + "---\n", + "题目解析:\n", + "\n", + "* while True 表示无限循环,让这个循环一直执行下去\n", + "\n", + " 只要whilie 条件满足 就会执行里面的代码块 除非执行后导致这个条件不满足 循环才会停止\n", + " \n", + " 这while 接受的条件是 True (这是恒等式,就是条件一直成立), 只要循环体中间的代码不 break, 它就会一直运行下去\n", + " \n", + " \n", + "* break 是用来终止循环语句(for/while 循环)\n", + "\n", + "\n", + "* 请大家把自己代入进去,想像一下这个机器人能样回复客户询价?\n", + "\n", + "---\n", + "解题步骤:\n", + "\n", + "* 使用 {} 定义了一个字典类型的价格,key 是 SKU, value 是价格\n", + "\n", + " 在这里我们使用的是字典来存储数据的对应的关系,以后大家学习 Pandas 时候会有更简单更方便的办法来存数据。\n", + " \n", + " \n", + "* 使用 while True 声明一个无限循环\n", + "\n", + " 使用 input 提示输入 SKU,放到 sku 中\n", + " \n", + " 用 if 判断 sku == \"!\", 则调用 break 终止整个循环\n", + " \n", + " 以 sku 为参数调用 dict.get 函数,从 prices 中取得价格放到 price 中\n", + " \n", + " 使用 % 将 (sku,price) 拼接到模板 \"sku: %s, price: %f\" 中,并打印输出\n", + " (备注:%s 表示接收字符串,%f 表示接收浮点数)\n", + " \n", + "---\n", + "参考: \n", + "\n", + "* 必看:[Python while 循环语句](https://www.runoob.com/python/python-while-loop.html)\n", + "\n", + "* 必看:[Python break 语句](https://www.runoob.com/python/python-break-statement.html)\n", + "\n", + "* [python3处理字符串](https://www.runoob.com/python3/python3-string.html)\n", + "\n", + "* [Python 字典(Dictionary) get()方法](https://www.runoob.com/python/att-dictionary-get.html)\n", + "\n", + "* [最全面的 python 字符串拼接总结](https://segmentfault.com/a/1190000015475309)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "ExecuteTime": { + "end_time": "2020-10-09T08:49:42.172498Z", + "start_time": "2020-10-09T08:49:42.169766Z" + } + }, + "outputs": [], + "source": [ + "# 定义价格表\n", + "prices = {\n", + " \"SKU-A\": 100,\n", + " \"SKU-B\": 120,\n", + " \"SKU-C\": 190,\n", + " \"SKU-D\": 200\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "start_time": "2020-10-09T08:54:48.298Z" + } + }, + "outputs": [], + "source": [ + "# 开始写 while True 循环\n", + "while True:\n", + " sku = input(\"请输入SKU:\")\n", + " if sku == \"!\":\n", + " break\n", + " price = prices.get(sku)\n", + " print(\"sku: %s, price: %f\" % (sku,price) )" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 仓库数据管理机器人\n", + "\n", + "---\n", + "题目:\n", + "\n", + "* 你现在是仓库的负责人\n", + "* 仓库数格式是这样的\n", + "```\n", + " total = [\n", + " {\"sku\": \"SKU-A\", \"quantity\": 100},\n", + " {\"sku\": \"SKU-B\", \"quantity\": 200},\n", + " {\"sku\": \"SKU-C\", \"quantity\": 400},\n", + " {\"sku\": \"SKU-D\", \"quantity\": 300},\n", + " ]\n", + "``` \n", + "* 请统计仓库的物品总数量(quantity)\n", + "* 现在 SKU-A 要入库 100件商品,请更新 SKU-A 的库存记录\n", + "* 现在 SKU-E 新品上市,要入库300件商品,请在 total 中新增一条相应记录\n", + "* 现在 SKU-B 要退市,请将 SKU-B 这行记录删掉 #此题可选\n", + "* 使用切片方法显示 total 中的最后一行记录\n", + "\n", + "---\n", + "题目解析:\n", + "\n", + "这是一道复合题,也是为了方便大家理解复杂的数据结构,为以后大家学 Pandas 处理得复杂数据打下伏笔,不过大家放心,后面学习 Pandas 后肯定比现在方便!\n", + "\n", + "* total 的最外层是 [], 表示它是一个list\n", + "\n", + " 可以用 print(type(total)) 来看它是什么类型\n", + " \n", + " \n", + "* total 的里层是 {}, 表示里面放的是 dict; \n", + "\n", + " 那么total 是一个里面成员都是 dict的 list,\n", + " \n", + " total[0] 表示是 total 的第一个元素,print(type(total[0]))\n", + "\n", + "\n", + "* 可以使用 len(total) 得到列表的长度\n", + "\n", + " 使用 range(len(total)) 就可以生一个 total 列表下标的列表\n", + " \n", + " 如果上面那句话很绕, 你就认为: range(len(total)) == [0,1,2,3] 就好了\n", + " \n", + "* 如果大家能独立的把道题解好理解透,就达到半个初级程序水平了\n", + "\n", + "---\n", + "解题步骤:\n", + "\n", + "* 定义 total 变量,里面存储库存信息!\n", + "\n", + "* 声明变量 total_quantity = 0, 这个变量是用来存储仓库总库存量\n", + "\n", + " 使用 for 循环遍历 total, 将遍历的数据放到 item 中; total是一个列表,遍历 total 得到的 item, 就其中的一个元素,它是字典类型\n", + " \n", + " 通过 [] 从item 取出 key为 \"quantity\" 值(就是取出每个商品的库存量)\n", + " \n", + " 把 total_quanity 与 item[\"quantity\"] 相加,再放到 total_quanity中\n", + " \n", + " 当 这个 for 循环结束时,total_quanity中最是仓库的库存总量了\n", + " \n", + " \n", + "* 使用 for 循环遍历 total, 将遍历的数据放到 item 中; total是一个列表,遍历 total 得到的 item, 就其中的一个元素,它是字典类型\n", + "\n", + " 使用 if 语句判断 item 中 key为 \"sku\" 的值是否等于 \"SKU-A\"\n", + " \n", + " 如果相等,则更将 item 中 key为 \"quantity\" 的值增加 100\n", + " \n", + " 提示一下,大家可以开始学习使用 \"+=\" 运算符,减少代码量\n", + "\n", + "\n", + "* 定义一个字典类型的变量sku_e,里面有 \"sku\",\"quantity\"两个key 相应的 value\n", + "\n", + " 使用 list.append 函数将 sku_e 添加到 total 里面\n", + "\n", + "\n", + "* 声名变量 sku_b_index = -1,这个变量用来表示 SKU-B 在 total 里的位置,最开始将它设为 -1, 就是为了方便标识是否找到了 SKU-B, 如果找到了 sku_b_index 就肯定都不等于-1, 只会是0或更大的值\n", + "\n", + " len(total) 返回的是 total里元素的数量,假设返回了3\n", + " \n", + " 那么 range(len(total)) 就相当于 0,1,2\n", + " \n", + " 使用 for 循环遍历 total, 因为我们想要找到 SKU-B 在列表 total 中的位置,所以我们使用了:for index in range(len(total)),这样就把遍历列表 total的下标位置放到了 index 中\n", + " \n", + " 使用 [] + index 从 total 取出相应位置的数据放到 item 中,item 是一个字典类型的数据,表示一条库存记录\n", + " \n", + " 使用 if 语句判断 item 中 key 为 \"sku\" 的值是否等于 \"SKU-B\", 如果相等就表示找到了 SKU-B 的库存记录,这时把它在 total的位置记录下来保存到 sku_b_index\n", + " \n", + " 当 for 循环结束时,判断 sku_b_index 是否不等于 -1, 如果不等于,就使用 del 来删掉 total 里 sku_b_index 位置的记录\n", + " \n", + " \n", + "* total 是一个列表,[-1] 表示取最后一行的记录\n", + "\n", + "---\n", + "\n", + "* [Python 运算符](https://www.runoob.com/python/python-operators.html)\n", + "\n", + "* [Python List append()方法](https://www.runoob.com/python/att-list-append.html)\n", + "\n", + "* [Python for 循环语句](https://www.runoob.com/python/python-for-loop.html)\n", + "\n", + "* [Python range() 函数用法](https://www.runoob.com/python/python-func-range.html)\n", + "\n", + "* [python,list中删除的三种方法del、remove、pop](https://blog.csdn.net/weixin_42814873/article/details/83377431)\n", + "\n", + "* [Python 切片介绍](https://www.liaoxuefeng.com/wiki/1016959663602400/1017269965565856)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": { + "ExecuteTime": { + "end_time": "2020-07-14T06:31:32.424430Z", + "start_time": "2020-07-14T06:31:32.414878Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "请统计仓库的物品总数量(quantity)\n", + "仓库的物品总数量: 1000\n", + "----------------------------------------------------------------------\n", + "现在 SKU-A 要入库 100件商品,请更新 SKU-A 的库存记录\n", + "更新后数据: [{'sku': 'SKU-A', 'quantity': 200}, {'sku': 'SKU-B', 'quantity': 200}, {'sku': 'SKU-C', 'quantity': 400}, {'sku': 'SKU-D', 'quantity': 300}]\n", + "----------------------------------------------------------------------\n", + "现在 SKU-E 新品上市,要入库300件商品,请在 total 中新增一条相应记录\n", + "更新后数据: [{'sku': 'SKU-A', 'quantity': 200}, {'sku': 'SKU-B', 'quantity': 200}, {'sku': 'SKU-C', 'quantity': 400}, {'sku': 'SKU-D', 'quantity': 300}, {'sku': 'SKU-E', 'quituantity': 3000}]\n", + "----------------------------------------------------------------------\n", + "现在 SKU-B 要退市,请将 SKU-B 这行记录删掉\n", + "更新后数据: [{'sku': 'SKU-A', 'quantity': 200}, {'sku': 'SKU-C', 'quantity': 400}, {'sku': 'SKU-D', 'quantity': 300}, {'sku': 'SKU-E', 'quituantity': 3000}]\n", + "----------------------------------------------------------------------\n", + "使用切片方法显示 total 中的最后一行记录\n", + "最后一行数据: {'sku': 'SKU-E', 'quituantity': 3000}\n" + ] + } + ], + "source": [ + "total = [\n", + " {\"sku\": \"SKU-A\", \"quantity\": 100},\n", + " {\"sku\": \"SKU-B\", \"quantity\": 200},\n", + " {\"sku\": \"SKU-C\", \"quantity\": 400},\n", + " {\"sku\": \"SKU-D\", \"quantity\": 300},\n", + "]\n", + "\n", + "print(\"请统计仓库的物品总数量(quantity)\")\n", + "total_quantity = 0\n", + "for item in total:\n", + " total_quantity = total_quantity + item[\"quantity\"]\n", + "print(\"仓库的物品总数量:\",total_quantity)\n", + "print(\"-\"*70)\n", + "\n", + "\n", + "print(\"现在 SKU-A 要入库 100件商品,请更新 SKU-A 的库存记录\")\n", + "for item in total:\n", + " # 找到SKU-A,更新它的记录\n", + " if item['sku'] == \"SKU-A\":\n", + " item['quantity'] += 100\n", + "print(\"更新后数据:\",total)\n", + "print(\"-\"*70)\n", + "\n", + "print(\"现在 SKU-E 新品上市,要入库300件商品,请在 total 中新增一条相应记录\")\n", + "sku_e = {\"sku\":\"SKU-E\", \"quantity\":300}\n", + "total.append(sku_e)\n", + "print(\"更新后数据:\",total)\n", + "print(\"-\"*70)\n", + "\n", + "print(\"现在 SKU-B 要退市,请将 SKU-B 这行记录删掉\")\n", + "sku_b_index = -1\n", + "for index in range(len(total)):\n", + " # 找到SKU-A,更新它的记录\n", + " item = total[index]\n", + " if item['sku'] == \"SKU-B\":\n", + " sku_b_index = index\n", + "if(sku_b_index != -1 ):\n", + " del total[sku_b_index]\n", + "print(\"更新后数据:\",total)\n", + "print(\"-\"*70)\n", + "\n", + "print(\"使用切片方法显示 total 中的最后一行记录\")\n", + "print(\"最后一行数据:\",total[-1])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 致谢\n", + "\n", + "谢谢 Loren 对本章内容的评审,特此致谢!" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.6" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": true + }, + "varInspector": { + "cols": { + "lenName": 16, + "lenType": 16, + "lenVar": 40 + }, + "kernels_config": { + "python": { + "delete_cmd_postfix": "", + "delete_cmd_prefix": "del ", + "library": "var_list.py", + "varRefreshCmd": "print(var_dic_list())" + }, + "r": { + "delete_cmd_postfix": ") ", + "delete_cmd_prefix": "rm(", + "library": "var_list.r", + "varRefreshCmd": "cat(var_dic_list()) " + } + }, + "types_to_exclude": [ + "module", + "function", + "builtin_function_or_method", + "instance", + "_Feature" + ], + "window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/04-list-dict/questions.ipynb b/04-list-dict/questions.ipynb new file mode 100644 index 0000000..01d2116 --- /dev/null +++ b/04-list-dict/questions.ipynb @@ -0,0 +1,690 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 1. 基础知识重复提醒\n", + "\n", + "有同学会问,不是上一章讲过这些吗?为什么要要重复提醒呢?\n", + "\n", + "是的,知识学习理解的过程是一个反复的过程,我们小时候第一次学习汉字时,基础字练了20次以上。\n", + "\n", + "对于 Python 这么一门语言的练习,基础知识的理解我建议要重复3次以上。\n", + "\n", + "老师把重要的基础知识重复出现3次,大家要把作业练习3次以上,老师推荐的各种链接多看一下。\n", + "\n", + "学习新知识时快就是慢,慢就是快!\n", + "\n", + "大家不但要做我们习题集上的题目,也要练习书上的例题,练习越多理解越深!\n", + "\n", + "## 新知识“列表/字典/元组”的提醒\n", + "\n", + "机器人的要处理的重复任务,必须要有一个地方进行存储;\n", + "\n", + "列表/字典/元组等数据结构就是用来存储各种复杂任务与数据的地方\n", + "\n", + "数据结构 + 算法(计算逻辑)= 程序\n", + "\n", + "* [Python 列表(List)](https://www.runoob.com/python/python-lists.html)\n", + "* [Python 字典(Dictionary)](https://www.runoob.com/python/python-dictionary.html)\n", + "* [Python 元组](https://www.runoob.com/python/python-tuples.html)\n", + "\n", + "## 写代码报错了怎么办\n", + "\n", + "学习了怎么看错误信息,学会了怎么 Google/Baidu查找出错信息,就掌握了最重要的一门程序技能;\n", + "\n", + "有人说学会这两项,就等于半个初级程序员了;学习 Python不是为学习一个固定的知识,而是为了学习解决问题的方法。\n", + "\n", + "* [必看: Zane 老师整理了检查错误的方法](https://www.yinxiang.com/everhub/note/1e44d717-75c3-413d-83ad-5716126ccaa6)\n", + "* [新人必看:17个新手常见Python运行时错误](https://www.oschina.net/question/89964_62779)\n", + "\n", + "\n", + "## Python 各种基础知识\n", + "---\n", + "本章\n", + "* [Python 列表(List)](https://www.runoob.com/python/python-lists.html)\n", + "* [Python 字典(Dictionary)](https://www.runoob.com/python/python-dictionary.html)\n", + "* [Python 元组](https://www.runoob.com/python/python-tuples.html)\n", + "* [Python 字典(Dictionary) get()方法](https://www.runoob.com/python/att-dictionary-get.html)\n", + "* [Python List sort()方法](https://www.runoob.com/python/att-list-sort.html)\n", + "* [Python 切片介绍](https://www.liaoxuefeng.com/wiki/1016959663602400/1017269965565856)\n", + "* [Python基础学习-Python中最常见括号()、[]、{}的区别](https://www.cnblogs.com/xuxiaolu/p/4726064.html)\n", + "* [Python List append()方法](https://www.runoob.com/python/att-list-append.html)\n", + "* [Python List insert()方法](https://www.runoob.com/python/att-list-insert.html)\n", + "* [Python List index()方法](https://www.runoob.com/python/att-list-index.html)\n", + "* [python,list中删除的三种方法del、remove、pop](https://blog.csdn.net/weixin_42814873/article/details/83377431)\n", + "* [Python List sort()方法](https://www.runoob.com/python/att-list-sort.html)\n", + "\n", + "---\n", + "历史\n", + "\n", + "* [Zane老师整理Python基础知识](https://www.yinxiang.com/everhub/note/414a0a33-7d28-4c27-af22-23f950b990fd)\n", + "* [Python3基础教程](https://www.runoob.com/python3/python3-tutorial.html)\n", + "* [Python 运算符](https://www.runoob.com/python/python-operators.html)\n", + "* [Python3 条件控制](https://www.runoob.com/python3/python3-conditional-statements.html)\n", + "* [Python3 循环语句](https://www.runoob.com/python3/python3-loop.html)\n", + "* [Python range() 函数用法](https://www.runoob.com/python/python-func-range.html)\n", + "* [Python continue 语句](https://www.runoob.com/python/python-continue-statement.html) 详解\n", + "* [Python While 循环语句](https://www.runoob.com/python/python-while-loop.html)\n", + "* [Python break 语句](https://www.runoob.com/python/python-break-statement.html)\n", + "* [Python for 循环语句](https://www.runoob.com/python/python-for-loop.html)\n", + "\n", + "\n", + "## 什么是 Markdown(选学)\n", + "\n", + "学习了解 Markdown 可以让大家的代码也有漂亮的文字配合!\n", + "\n", + "* [Markdown 是什么](https://www.zhihu.com/question/19963642)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 智能排序机器人\n", + "---\n", + "题目:\n", + "* 你现在公司的销售经理,要对一些销售数据进行分析\n", + "\n", + "* 公司有百亿条销售数据,抽出一些销售额信息让你练手\n", + "\n", + "```\n", + " sales = [100,200,50,300,20,500,1000,10]\n", + "```\n", + "* 请对 sales 进行升序排序,并且打印出来\n", + "* 请对 sales 进行降序排序,并且打印出来\n", + "* 请用排序+切片的方式,找出 Top3 的销售额\n", + "* 请用排序+切片的方式,找出 最低的3个销售额\n", + "\n", + "--- \n", + "题目解析:\n", + "\n", + "* sales 是一个列表类型的对象(变量),里面有非常多对象方法\n", + "\n", + "* 其中就包括了 sort 方法\n", + "\n", + " sort() 用于对原列表进行排序\n", + " \n", + " 我们可以指定 reverse = False/True 对来对列表进行升降序排序\n", + " \n", + " 更多用法可以参考:[Python List sort()方法](https://www.runoob.com/python/att-list-sort.html)\n", + "\n", + "\n", + "* Python提供了切片(Slice)操作符,能大大简化取某一段范围内的数据\n", + "\n", + " 它提供了各种你想像不到的灵活方式来操作选取数据,它不但现在列表有用,以后在操作 Excel等数据时也有用\n", + " \n", + " 大家不但要看书上的内容,也可再看看外部: [Python 切片介绍](https://www.liaoxuefeng.com/wiki/1016959663602400/1017269965565856)\n", + "\n", + "\n", + "* 很多同学会被 \"()\", \"[]\" 这两个搞混,\n", + "\n", + " () 一般是接在函数名后,() 里面放若干个参数,比如 print(\"Hello,world\")\n", + " () 还有可以用来定义元组(tuple), 类似列表,但是里面的值是不可修改的\n", + " \n", + " [] 一般是接在 列表对象/字典对象后,这是用有来选择数据的,比如 sales[0] \n", + " 如果大家后面学习 Pandas, 会发现 [] 的更多用处 [Pandas的切片处理](https://blog.csdn.net/yoonhee/article/details/76168253)\n", + "\n", + "\n", + " 更详细的解释可以看这里:[Python基础学习-Python中最常见括号()、[]、{}的区别](https://www.cnblogs.com/xuxiaolu/p/4726064.html)\n", + " \n", + " \n", + "* 字符串 乘 一个数字,表示将这个字符串重多少次(是不是很好玩)\n", + "\n", + " \">\"* 10 == \">>>>>>>>>>\"\n", + " \n", + " 方便我们输出很长的分隔符\n", + " \n", + "---\n", + "\n", + "解题步骤:\n", + "\n", + "* 直接调用 sales 的 sort() 方法, 因为默认是升序\n", + "\n", + " 再打印输出排序后的 sales\n", + " \n", + "\n", + "* 调用 sales 的 sort() 方法,传入 reverse 参数\n", + "\n", + " 再打印输出排序后的 sales\n", + " \n", + "\n", + "* 直接调用 sales 的 sort() 方法, 这时 sales 是升序的\n", + "\n", + "\n", + "* 使用 [-3:] 切片选择最后3个数据并打印输出\n", + " \n", + " 切片里参数以的冒号 \":\" 来分隔,第1参数表示从哪哪里开始选择\n", + " \n", + " -3 表示从倒数第3个数开始选择,\n", + " \n", + " 冒号 \":\" 后为空表示选择到最后为止\n", + " \n", + " \n", + "* 使用 [:3] 切片选择前3个数据并打印输出\n", + "\n", + " 切片里参数以的冒号 \":\" 来分隔,第1参数表示从哪哪里开始选择\n", + " \n", + " 冒号 \":\" 前为空表示从列表最开始的地方进行选择\n", + " \n", + " 3 表示选到第列表的第3数据并为止\n", + " \n", + "---\n", + "参考:\n", + "* [Python List sort()方法](https://www.runoob.com/python/att-list-sort.html)\n", + "* [Python 切片介绍](https://www.liaoxuefeng.com/wiki/1016959663602400/1017269965565856)\n", + "* [Python基础学习-Python中最常见括号()、[]、{}的区别](https://www.cnblogs.com/xuxiaolu/p/4726064.html)\n", + "* [Python 列表(List)](https://www.runoob.com/python/python-lists.html)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sales = [100,200,50,300,20,500,1000,10]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(\"进行升序排序\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(\"进行降序排序\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#进行升序排序\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(\"查看最后3个数据(top3)\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "ExecuteTime": { + "end_time": "2020-07-16T10:18:33.737309Z", + "start_time": "2020-07-16T10:18:33.730414Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "进行升序排序\n", + "[10, 20, 50, 100, 200, 300, 500, 1000]\n", + "----------------------------------------------------------------------\n", + "进行降序排序\n", + "[1000, 500, 300, 200, 100, 50, 20, 10]\n", + "----------------------------------------------------------------------\n", + "查看最后3个数据(top3)\n", + "[300, 500, 1000]\n", + "----------------------------------------------------------------------\n", + "查看最前3个数据( 最低的3个销售额)\n", + "[10, 20, 50]\n" + ] + } + ], + "source": [ + "print(\"查看最前3个数据( 最低的3个销售额)\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 车位管理机器人\n", + "---\n", + "题目:\n", + "\n", + "* 你现有管理一个停车场\n", + "* 现有一组车位租用情况,结构如下:\n", + "```\n", + " car_nums = ['A0001','A00X9','A0027'] \n", + "```\n", + "* 现有新来了一辆车 'A0030', 请把它放在 car_nums 的最后面\n", + "* 现有新来了一辆车 'A0000', 请把它放在 car_nums 的最前面\n", + "* 'A00X9' 这辆车现在不租你的停车位上,请把它从 car_nums 中删掉\n", + "* 现在来了一个车队 ['B0001','B0002','B003'], 请用一行代码把它加到 car_nums 中\n", + "\n", + "--- \n", + "题目解析:\n", + "\n", + "* 使用 list.append 函数向 car_nums 最尾部添加 'A0030'\n", + "\n", + "* 使用 list.insert 函数向 car_nums 最前面插入 'A0000'\n", + "\n", + " insert 函数有两个参数,第1个参数表示插入数据的位置,因为 Python 的列表数据下标是从 0 开始,所以我们填的是0.\n", + " \n", + " \n", + "* 使用 list.index 函数从 car_nums 中找出 'A00X9' 所在的位置,放到 del_index 中\n", + "\n", + " 再调用 del 来删除 'A00X9' \n", + " \n", + " \n", + "* 使用 list.extend 将 ['B0001','B0002','B003'] 放到 car_nums 的最尾部 \n", + "\n", + "---\n", + "参考:\n", + "* [Python List append()方法](https://www.runoob.com/python/att-list-append.html)\n", + "* [Python List insert()方法](https://www.runoob.com/python/att-list-insert.html)\n", + "* [Python List index()方法](https://www.runoob.com/python/att-list-index.html)\n", + "* [python,list中删除的三种方法del、remove、pop](https://blog.csdn.net/weixin_42814873/article/details/83377431)\n", + "* [Python List sort()方法](https://www.runoob.com/python/att-list-sort.html)\n", + "* [Python 切片介绍](https://www.liaoxuefeng.com/wiki/1016959663602400/1017269965565856)\n", + "* [Python基础学习-Python中最常见括号()、[]、{}的区别](https://www.cnblogs.com/xuxiaolu/p/4726064.html)\n", + "* [Python 列表(List)](https://www.runoob.com/python/python-lists.html)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(\"现有新来了一辆车 'A0030', 请把它放在 car_nums 的最后面\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(\"现有新来了一辆车 'A0000', 请把它放在 car_nums 的最前面\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(\"'A00X9' 这辆车现在不租你的停车位上,请把它从 car_nums 中删掉\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(\"现在来了一个车队 ['B0001','B0002','B003'], 请用一行代码把它加到 car_nums 中\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 自动询价机器人\n", + "---\n", + "题目:\n", + "\n", + "* 你现在管理一个商务团队,每天有客户找问价格\n", + "* 你有一个价格对应表\n", + "```\n", + " prices = {\n", + " \"SKU-A\": 100,\n", + " \"SKU-B\": 120,\n", + " \"SKU-C\": 190,\n", + " \"SKU-D\": 200\n", + " }\n", + "``` \n", + "* 每次客户来问题,你都要找这个表,你现在很烦,想做一个自动机器人\n", + "* 写一个 while 循环,提示用户输入 sku\n", + "* 根据用户输入的sku查询 prices, 找印出对应价格\n", + "* 如果用户输入 ! 则退出循环(break)\n", + "\n", + "---\n", + "题目解析:\n", + "\n", + "* while True 表示无限循环,让这个循环一直执行下去\n", + "\n", + " 只要whilie 条件满足 就会执行里面的代码块 除非执行后导致这个条件不满足 循环才会停止\n", + " \n", + " 这while 接受的条件是 True (这是恒等式,就是条件一直成立), 只要循环体中间的代码不 break, 它就会一直运行下去\n", + " \n", + " \n", + "* break 是用来终止循环语句(for/while 循环)\n", + "\n", + "\n", + "* 请大家把自己代入进去,想像一下这个机器人能样回复客户询价?\n", + "\n", + "---\n", + "解题步骤:\n", + "\n", + "* 使用 {} 定义了一个字典类型的价格,key 是 SKU, value 是价格\n", + "\n", + " 在这里我们使用的是字典来存储数据的对应的关系,以后大家学习 Pandas 时候会有更简单更方便的办法来存数据。\n", + " \n", + "* 使用 whiel True 声明一个无限循环\n", + "\n", + " 使用 input 提示输入 SKU,放到 sku 中\n", + " \n", + " 用 if 判断 sku == \"!\", 则调用 break 终止整个循环\n", + " \n", + " 以 sku 为参数调用 dict.get 函数,从 prices 中取得价格放到 price 中\n", + " \n", + " 使用 % 将 (sku,price) 拼接到模板 \"sku: %s, price: %f\" 中,并打印输出\n", + " (备注:%s 表示接收字符串,%f 表示接收浮点数)\n", + " \n", + "---\n", + "参考: \n", + "\n", + "* 必看:[Python while 循环语句](https://www.runoob.com/python/python-while-loop.html)\n", + "\n", + "* 必看:[Python break 语句](https://www.runoob.com/python/python-break-statement.html)\n", + "\n", + "* [python3处理字符串](https://www.runoob.com/python3/python3-string.html)\n", + "\n", + "* [Python 字典(Dictionary) get()方法](https://www.runoob.com/python/att-dictionary-get.html)\n", + "\n", + "* [最全面的 python 字符串拼接总结](https://segmentfault.com/a/1190000015475309)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "ExecuteTime": { + "end_time": "2020-10-09T08:49:42.172498Z", + "start_time": "2020-10-09T08:49:42.169766Z" + } + }, + "outputs": [], + "source": [ + "# 定义价格表\n", + "prices = {\n", + " \"SKU-A\": 100,\n", + " \"SKU-B\": 120,\n", + " \"SKU-C\": 190,\n", + " \"SKU-D\": 200\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "ExecuteTime": { + "start_time": "2020-10-09T08:54:48.298Z" + } + }, + "outputs": [], + "source": [ + "# 开始写 while True 循环\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 仓库数据管理机器人\n", + "\n", + "---\n", + "题目:\n", + "\n", + "* 你现在是仓库的负责人\n", + "* 仓库数格式是这样的\n", + "```\n", + " total = [\n", + " {\"sku\": \"SKU-A\", \"quantity\": 100},\n", + " {\"sku\": \"SKU-B\", \"quantity\": 200},\n", + " {\"sku\": \"SKU-C\", \"quantity\": 400},\n", + " {\"sku\": \"SKU-D\", \"quantity\": 300},\n", + " ]\n", + "``` \n", + "* 请统计仓库的物品总数量(quantity)\n", + "* 现在 SKU-A 要入库 100件商品,请更新 SKU-A 的库存记录\n", + "* 现在 SKU-E 新品上市,要入库300件商品,请在 total 中新增一条相应记录\n", + "* 现在 SKU-B 要退市,请将 SKU-B 这行记录删掉 #此题可选\n", + "* 使用切片方法显示 total 中的最后一行记录\n", + "\n", + "---\n", + "题目解析:\n", + "\n", + "这是一道复合题,也是为了方便大家理解复杂的数据结构,为以后大家学 Pandas 处理得复杂数据打下伏笔,不过大家放心,后面学习 Pandas 后肯定比现在方便!\n", + "\n", + "* total 的最外层是 [], 表示它是一个list\n", + "\n", + " 可以用 print(type(total)) 来看它是什么类型\n", + " \n", + " \n", + "* total 的里层是 {}, 表示里面放的是 dict; \n", + "\n", + " 那么total 是一个里面成员都是 dict的 list,\n", + " \n", + " total[0] 表示是 total 的第一个元素,print(type(total[0]))\n", + "\n", + "\n", + "* 可以使用 len(total) 得到列表的长度\n", + "\n", + " 使用 range(len(total)) 就可以生一个 total 列表下标的列表\n", + " \n", + " 如果上面那句话很绕, 你就认为: range(len(total)) == [0,1,2,3] 就好了\n", + " \n", + "* 如果大家能独立的把道题解好理解透,就达到半个初级程序水平了\n", + "\n", + "---\n", + "解题步骤:\n", + "\n", + "* 定义 total 变量\n", + "\n", + "* 声明变量 total_quantity = 0, 这个变量是用来存储仓库总库存量\n", + "\n", + " 使用 for 循环遍历 total, 将遍历的数据放到 item 中; total是一个列表,遍历 total 得到的 item, 就其中的一个元素,它是字典类型\n", + " \n", + " 通过 [] 从item 取出 key为 \"quantity\" 值(就是取出每个商品的库存量)\n", + " \n", + " 把 total_quanity 与 item[\"quantity\"] 相加,再放到 total_quanity中\n", + " \n", + " 当 这个 for 循环结束时,total_quanity中最是仓库的库存总量了\n", + " \n", + " \n", + "* 使用 for 循环遍历 total, 将遍历的数据放到 item 中; total是一个列表,遍历 total 得到的 item, 就其中的一个元素,它是字典类型\n", + "\n", + " 使用 if 语句判断 item 中 key为 \"sku\" 的值是否等于 \"SKU-A\"\n", + " \n", + " 如果相等,则更将 item 中 key为 \"quantity\" 的值增加 100\n", + " \n", + " 提示一下,大家可以开始学习使用 \"+=\" 运算符,减少代码量\n", + "\n", + "\n", + "* 定义一个字典类型的变量sku_e,里面有 \"sku\",\"quantity\"两个key 相应的 value\n", + "\n", + " 使用 list.append 函数将 sku_e 添加到 total 里面\n", + "\n", + "\n", + "* 声名变量 sku_b_index = -1,这个变量用来表示 SKU-B 在 total 里的位置,最开始将它设为 -1, 就是为了方便标识是否找到了 SKU-B, 如果找到了 sku_b_index 就肯定都不等于-1, 只会是0或更大的值\n", + "\n", + " len(total) 返回的是 total里元素的数量,假设返回了3\n", + " \n", + " 那么 range(len(total)) 就相当于 0,1,2\n", + " \n", + " 使用 for 循环遍历 total, 因为我们想要找到 SKU-B 在列表 total 中的位置,所以我们使用了:for index in range(len(total)),这样就把遍历列表 total的下标位置放到了 index 中\n", + " \n", + " 使用 [] + index 从 total 取出相应位置的数据放到 item 中,item 是一个字典类型的数据,表示一条库存记录\n", + " \n", + " 使用 if 语句判断 item 中 key 为 \"sku\" 的值是否等于 \"SKU-B\", 如果相等就表示找到了 SKU-B 的库存记录,这时把它在 total的位置记录下来保存到 sku_b_index\n", + " \n", + " 当 for 循环结束时,判断 sku_b_index 是否不等于 -1, 如果不等于,就使用 del 来删掉 total 里 sku_b_index 位置的记录\n", + " \n", + " \n", + "* total 是一个列表,[-1] 表示取最后一行的记录\n", + "\n", + "---\n", + "\n", + "* [Python 运算符](https://www.runoob.com/python/python-operators.html)\n", + "\n", + "* [Python List append()方法](https://www.runoob.com/python/att-list-append.html)\n", + "\n", + "* [Python for 循环语句](https://www.runoob.com/python/python-for-loop.html)\n", + "\n", + "* [Python range() 函数用法](https://www.runoob.com/python/python-func-range.html)\n", + "\n", + "* [python,list中删除的三种方法del、remove、pop](https://blog.csdn.net/weixin_42814873/article/details/83377431)\n", + "\n", + "* [Python 切片介绍](https://www.liaoxuefeng.com/wiki/1016959663602400/1017269965565856)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "total = [\n", + " {\"sku\": \"SKU-A\", \"quantity\": 100},\n", + " {\"sku\": \"SKU-B\", \"quantity\": 200},\n", + " {\"sku\": \"SKU-C\", \"quantity\": 400},\n", + " {\"sku\": \"SKU-D\", \"quantity\": 300},\n", + "]\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(\"请统计仓库的物品总数量(quantity)\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(\"现在 SKU-A 要入库 100件商品,请更新 SKU-A 的库存记录\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(\"现在 SKU-E 新品上市,要入库300件商品,请在 total 中新增一条相应记录\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(\"现在 SKU-B 要退市,请将 SKU-B 这行记录删掉\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(\"使用切片方法显示 total 中的最后一行记录\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 致谢\n", + "\n", + "谢谢 Loren 对本章内容的评审,特此致谢!" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.6" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": true + }, + "varInspector": { + "cols": { + "lenName": 16, + "lenType": 16, + "lenVar": 40 + }, + "kernels_config": { + "python": { + "delete_cmd_postfix": "", + "delete_cmd_prefix": "del ", + "library": "var_list.py", + "varRefreshCmd": "print(var_dic_list())" + }, + "r": { + "delete_cmd_postfix": ") ", + "delete_cmd_prefix": "rm(", + "library": "var_list.r", + "varRefreshCmd": "cat(var_dic_list()) " + } + }, + "types_to_exclude": [ + "module", + "function", + "builtin_function_or_method", + "instance", + "_Feature" + ], + "window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/05-function/answer-questions.ipynb b/05-function/answer-questions.ipynb new file mode 100644 index 0000000..ff66578 --- /dev/null +++ b/05-function/answer-questions.ipynb @@ -0,0 +1,622 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 1. 基础知识重复提醒\n", + "\n", + "有同学会问,不是上一章讲过这些吗?为什么要要重复提醒呢?\n", + "\n", + "是的,知识学习理解的过程是一个反复的过程,我们小时候第一次学习汉字时,基础字练了20次以上。\n", + "\n", + "对于 Python 这么一门语言的练习,基础知识的理解我建议要重复3次以上。\n", + "\n", + "老师把重要的基础知识重复出现3次,大家要把作业练习3次以上,老师推荐的各种链接多看一下。\n", + "\n", + "学习新知识时快就是慢,慢就是快!\n", + "\n", + "大家不但要做我们习题集上的题目,也要练习书上的例题,练习越多理解越深!\n", + "\n", + "## 新知识函数的提醒\n", + "\n", + "函数是代码复用的基础单元,我们把常用的代码封装一个个函数,减少大家的工作量\n", + "\n", + "我们前使用的 print, range, len 都是函数,现在我们不但要学习怎么用函数,也要学习怎么自己写一个个函数\n", + "\n", + "函数的目的不是为了增加了大家的工作量,而是为了降低大家的工作量;如果大家把自己常用代码封装成一个个函数,要用的时候,只要传个参数调用就好了!\n", + "\n", + "前面变量/逻辑判断/循环/数据结构是日后大家每天用 Python 的基础;自己写函数是在前面基础的拔高,是 Excel 思维和程序思维的一个分水岭!\n", + "\n", + "大家放心,从这往后学习,大家会越学越\"容易\"。\n", + "\n", + "## 写代码报错了怎么办\n", + "\n", + "学习了怎么看错误信息,学会了怎么 Google/Baidu查找出错信息,就掌握了最重要的一门程序技能;\n", + "\n", + "有人说学会这两项,就等于半个初级程序员了;学习 Python不是为学习一个固定的知识,而是为了学习解决问题的方法。\n", + "\n", + "* [必看: Zane 老师整理了检查错误的方法](https://www.yinxiang.com/everhub/note/1e44d717-75c3-413d-83ad-5716126ccaa6)\n", + "* [新人必看:17个新手常见Python运行时错误](https://www.oschina.net/question/89964_62779)\n", + "\n", + "\n", + "## Python 各种基础知识\n", + "---\n", + "本章\n", + "\n", + "* [Python3 函数](https://www.runoob.com/python3/python3-function.html)\n", + "* [Python range() 函数用法](https://www.runoob.com/python/python-func-range.html)\n", + "* [B站视频-Python函数讲解](https://search.bilibili.com/all?keyword=python%E5%87%BD%E6%95%B0%E8%AE%B2%E8%A7%A3)\n", + "* [Youtube](https://www.youtube.com/results?search_query=Python+%E5%AD%A6%E4%B9%A0)/[B站](https://search.bilibili.com/all?keyword=Python%20%E5%AD%A6%E4%B9%A0)上有很非常多的 Python 学习视频,大家可以根据自己口味去选择\n", + "\n", + "---\n", + "历史\n", + "\n", + "* [Zane老师整理Python基础知识](https://www.yinxiang.com/everhub/note/414a0a33-7d28-4c27-af22-23f950b990fd)\n", + "* [Python3基础教程](https://www.runoob.com/python3/python3-tutorial.html)\n", + "* [Python 运算符](https://www.runoob.com/python/python-operators.html)\n", + "* [Python3 条件控制](https://www.runoob.com/python3/python3-conditional-statements.html)\n", + "* [Python3 循环语句](https://www.runoob.com/python3/python3-loop.html)\n", + "* [Python range() 函数用法](https://www.runoob.com/python/python-func-range.html)\n", + "* [Python continue 语句](https://www.runoob.com/python/python-continue-statement.html) 详解\n", + "* [Python While 循环语句](https://www.runoob.com/python/python-while-loop.html)\n", + "* [Python break 语句](https://www.runoob.com/python/python-break-statement.html)\n", + "* [Python for 循环语句](https://www.runoob.com/python/python-for-loop.html)\n", + "* [Python 列表(List)](https://www.runoob.com/python/python-lists.html)\n", + "* [Python 字典(Dictionary)](https://www.runoob.com/python/python-dictionary.html)\n", + "* [Python 元组](https://www.runoob.com/python/python-tuples.html)\n", + "* [Python 字典(Dictionary) get()方法](https://www.runoob.com/python/att-dictionary-get.html)\n", + "* [Python List sort()方法](https://www.runoob.com/python/att-list-sort.html)\n", + "* [Python 切片介绍](https://www.liaoxuefeng.com/wiki/1016959663602400/1017269965565856)\n", + "* [Python基础学习-Python中最常见括号()、[]、{}的区别](https://www.cnblogs.com/xuxiaolu/p/4726064.html)\n", + "* [Python List append()方法](https://www.runoob.com/python/att-list-append.html)\n", + "* [Python List insert()方法](https://www.runoob.com/python/att-list-insert.html)\n", + "* [Python List index()方法](https://www.runoob.com/python/att-list-index.html)\n", + "* [python,list中删除的三种方法del、remove、pop](https://blog.csdn.net/weixin_42814873/article/details/83377431)\n", + "* [Python List sort()方法](https://www.runoob.com/python/att-list-sort.html)\n", + "\n", + "\n", + "## 什么是 Markdown(选学)\n", + "\n", + "学习了解 Markdown 可以让大家的代码也有漂亮的文字配合!\n", + "\n", + "* [Markdown 是什么](https://www.zhihu.com/question/19963642)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 高级自动邮件机器人1\n", + "\n", + "---\n", + "题目:\n", + "\n", + "* 假设计你是要给自动给客户发邮件的运营\n", + "\n", + "下面的是客户的订单数据:\n", + "\n", + "```\n", + "orders = [\n", + " {\"buyer_name\": \"Pony\",\"product\": \"MacBook\"},\n", + " {\"buyer_name\": \"Bill\",\"product\": \"AirPods\"},\n", + " {\"buyer_name\": \"Hank\",\"product\": \"iPhone\"}\n", + "]\n", + "```\n", + "* 写一个函数 deliver_mail 接收客户姓名,产品名,然后输出下面格式的邮件:\n", + "\n", + "Dear [用户姓名]:\n", + " \n", + "Your purchased product : [产品名] is delivered!\n", + " \n", + "Thanks for your choosing!\n", + " \n", + "* 遍历orders, 调用 deliver_mail 函数,生成3封邮件\n", + "\n", + "--- \n", + "题目解析:\n", + "\n", + "注意:这是一道复合题 \n", + "\n", + "* orders 的最外层是 [], 表示它是一个list\n", + "\n", + " 可以用 print(type(orders)) 来看它是什么类型\n", + "\n", + "\n", + "* orders 的里层是 {}, 表示里面放的是 dict; \n", + "\n", + " orders 是一个里面成员都是 dict的 list,\n", + " \n", + " orders[0] 表示是 orders 的第一个元素,print(type(orders[0]))\n", + "\n", + "\n", + "* 如果你想知识 orders (订单表)里有多个 order(订单), 可以使用 len(orders) 查看里面有多少订单\n", + "\n", + "* 如果要遍历订单表有两种方法:\n", + "\n", + "```\n", + "# 第一种方法更为简单\n", + "for order in orders:\n", + " print(order)\n", + "```\n", + "\n", + "```\n", + "#第二种方法知道是第几个订单\n", + "for i in range(len(orders)):\n", + " order = orders[i]\n", + " print(order)\n", + "```\n", + "---\n", + "解题步骤:\n", + "\n", + "* 定义 orders 变量,里面存储了所有的订单信息\n", + "\n", + " btw: 同学们现在不要急,现在学习的是 Python 基础,相当于学开车时的倒车入库和S路;如果你现在就和 Excel 去对比,是不大合适的;等到下周大家开始学习 Pandas 时,就相当于学科目三,开车上了公路;慢慢的你就可以体会到里面的区别。\n", + " \n", + " 只有现在基础打好了,日后真的开车上路了就不会慌;如果在高速上,你连方向盘都不好,也很危险的!\n", + " \n", + " \n", + "* 使用 def 关键字定一个邮件发送函数, 名字叫 deliver_mail,接收 buyer_name, product 两个参数\n", + "\n", + " 函数名可以叫任何你想叫的名字,比较 fayoujian (用拼音不是一个好主意,这里举一个例子)\n", + " \n", + " 参数名也可以按你的喜好来,但是通常建议要起的有意义,函数名了变量名都要小写,单词之前用 \"_\" 来连接\n", + "\n", + " 因为字符串拼接以前讲过很多次,这里就不再展开,有不熟的同学可以看一下参考资料\n", + " \n", + " 特别注意的是我现在用 print 打印代替真实的邮件发送,后面的课程我会教大家来发送邮件。\n", + " \n", + " \n", + "* 我们使用 for 循环遍历 orders (列表类型), 将遍历的内容放到 order 中\n", + "\n", + " order是一个字典类型的变量,我们通过 [] + key 分别取出了 buyer_name, product\n", + " \n", + " 然后再在循环里,以 buyer_name, product 为参数调用 deliver_mail 函数。\n", + " \n", + "\n", + "* 大家可以想像一下如果你一天8小手工发500封邮件,现在用循环 + 函数就10分钟搞定了\n", + "\n", + "---\n", + "参考:\n", + "* [Python3 函数](https://www.runoob.com/python3/python3-function.html)\n", + "\n", + "* [Python3 循环语句](https://www.runoob.com/python3/python3-loop.html)\n", + "\n", + "* [python3处理字符串](https://www.runoob.com/python3/python3-string.html)\n", + "\n", + "* [最全面的 python 字符串拼接总结](https://segmentfault.com/a/1190000015475309)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "ExecuteTime": { + "end_time": "2020-07-14T09:36:17.418139Z", + "start_time": "2020-07-14T09:36:17.413010Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " Dear Pony:\n", + "\n", + " Your purchased product : MacBook is delivered!\n", + "\n", + " Thanks for your choosing!\n", + " \n", + "----------Mail Delivered!--------------------\n", + "\n", + " Dear Bill:\n", + "\n", + " Your purchased product : AirPods is delivered!\n", + "\n", + " Thanks for your choosing!\n", + " \n", + "----------Mail Delivered!--------------------\n", + "\n", + " Dear Hank:\n", + "\n", + " Your purchased product : iPhone is delivered!\n", + "\n", + " Thanks for your choosing!\n", + " \n", + "----------Mail Delivered!--------------------\n" + ] + } + ], + "source": [ + "orders = [\n", + " {\"buyer_name\": \"Pony\",\"product\": \"MacBook\"},\n", + " {\"buyer_name\": \"Bill\",\"product\": \"AirPods\"},\n", + " {\"buyer_name\": \"Hank\",\"product\": \"iPhone\"}\n", + "]\n", + "\n", + "def deliver_mail(buyer_name,product):\n", + " email_template = \"\"\"\n", + " Dear %s:\n", + "\n", + " Your purchased product : %s is delivered!\n", + "\n", + " Thanks for your choosing!\n", + " \"\"\"\n", + " email_body = email_template % (buyer_name,product)\n", + " print(email_body)\n", + " print(\"----------Mail Delivered!--------------------\")\n", + "\n", + "for order in orders:\n", + " buyer_name = order['buyer_name']\n", + " product = order['product']\n", + " deliver_mail(buyer_name,product)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 小小数学家1\n", + "---\n", + "题目:\n", + "\n", + "* 定义一个函数 auto_sum,接收一个参数 max_num,\n", + "\n", + " 计算 1~ max_num 内所有的整数的和,并将结果返回\n", + "\n", + "* 当我们调用 auto_sum 函数时,例如:auto_sum(3),得到 6\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 解法1\n", + "---\n", + "题目解析:\n", + "\n", + "* 这个题目主要练习大家对历史知识的掌控\n", + "\n", + "* 加深对 for 循环的理解,再一次了解怎么定义初始变量,理解如何加总求\n", + " \n", + "* 理解什么叫 return\n", + "\n", + "---\n", + "解题步骤:\n", + "\n", + "* 使用 def 定义一个名叫 auto_sum 的函数,接收一个名叫 max_num 的参数\n", + "\n", + " 定义一个初始变量 total, 将其赋值为0,用来存储汇总求和值\n", + " \n", + " 使用 for 循环遍历 1 ~ max_num 间的数值(可以使用range函数,记得要考虑range的右区间要加1), 把数值放到变量 i 中\n", + " \n", + " 在循环体中,把 i 的值加到 total 里\n", + " \n", + " 在循环体外把 total 返回(return)\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "ExecuteTime": { + "end_time": "2020-07-14T09:44:35.784968Z", + "start_time": "2020-07-14T09:44:35.780761Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "autosum(3): 6\n", + "autosum(10): 55\n", + "autosum(100): 5050\n" + ] + } + ], + "source": [ + "def auto_sum(max_num):\n", + " total = 0\n", + " for i in range(1,max_num+1):\n", + " total += i\n", + " return total\n", + "\n", + "print(\"auto_sum(3):\",auto_sum(3)) \n", + "print(\"auto_sum(10):\",auto_sum(10)) \n", + "print(\"auto_sum(100):\",auto_sum(100)) \n", + "\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 解法2\n", + "---\n", + "题目解析:\n", + "\n", + "* 这个题目主要练习大家对历史知识的掌控\n", + "\n", + "* 理解怎么使用 range 函数\n", + " \n", + "* 理解怎么使用 sum 函数,它不但可以接收单个参数,也可以接收多个参数\n", + "\n", + "---\n", + "解题步骤:\n", + "\n", + "* 使用 def 定义一个名叫 auto_sum 的函数,接收一个名叫 max_num 的参数\n", + "\n", + " 使用 range 生成 1~max_num 间的数字(记得要考虑range的右区间要加1)\n", + " \n", + " 使用 sum 对 range 进行求和,并将数值返回(return)\n", + " \n", + "---\n", + "参考:\n", + "\n", + "* [Python range() 函数用法](https://www.runoob.com/python/python-func-range.html)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "ExecuteTime": { + "end_time": "2020-10-12T04:32:05.217681Z", + "start_time": "2020-10-12T04:32:05.214021Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "auto_sum(3): 6\n", + "auto_sum(10): 55\n", + "auto_sum(100): 5050\n" + ] + } + ], + "source": [ + "def auto_sum(max_num):\n", + " return sum(range(1,max_num+1))\n", + "\n", + "print(\"auto_sum(3):\",auto_sum(3)) \n", + "print(\"auto_sum(10):\",auto_sum(10)) \n", + "print(\"auto_sum(100):\",auto_sum(100)) " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 小小数学家2\n", + "---\n", + "题目:\n", + "\n", + "* 定义一个函数 out_even,接收一个参数 max_num,\n", + "\n", + " 将 0 ~ max_num 内所有的偶数找出来,放到一个列表里,并返回\n", + "\n", + "* 例如:out_even(10),得到[0, 2, 4, 6, 8, 10]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 解法1\n", + "---\n", + "题目解析:\n", + "\n", + "* 这个题目主要练习大家对历史知识的掌控\n", + "\n", + "* 加深对 for 循环的理解,加深为运算符的理解\n", + " \n", + "* 加深对于列表操作的理解\n", + "\n", + "---\n", + "\n", + "解题步骤:\n", + "\n", + "* 使用 def 定义一个名叫 out_even 的函数,接收一个名叫 max_num 的参数\n", + "\n", + " 定义一个初始变量 ret_list, 将其赋值为 [],用来存储找到的偶数\n", + " \n", + " 使用 for 循环遍历 0 ~ max_num 间的数值(可以使用range函数,记得要考虑range的右区间要加1), 把数值放到变量 i 中\n", + " \n", + " 判断 i%2 是否为零,如果为零则将 i 加到 ret_list 中 ( % 是取模运算符 - 返回除法的余数)\n", + " \n", + " 在循环体外把 ret_list 返回(return)\n", + " \n", + "--- \n", + "资料:\n", + "\n", + "* [Python 运算符](https://www.runoob.com/python/python-operators.html)\n", + "\n", + "* [Python range() 函数用法](https://www.runoob.com/python/python-func-range.html)\n", + "\n", + "* [Python3 List append()方法](https://www.runoob.com/python3/python3-att-list-append.html)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "ExecuteTime": { + "end_time": "2020-07-18T00:06:52.395838Z", + "start_time": "2020-07-18T00:06:52.390937Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "outeven(3): [0, 2]\n", + "outeven(10): [0, 2, 4, 6, 8, 10]\n", + "outeven(100): [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]\n" + ] + } + ], + "source": [ + "def out_even(max_num):\n", + " ret_list = []\n", + " for i in range(0,max_num+1):\n", + " if(i % 2 == 0):\n", + " ret_list.append(i)\n", + " return ret_list\n", + "\n", + "print(\"out_even(3):\",out_even(3)) \n", + "print(\"out_even(10):\",out_even(10)) \n", + "print(\"out_even(100):\",out_even(100)) \n", + "\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 解法2\n", + "\n", + "---\n", + "题目解析:\n", + "\n", + "* 这个题目主要练习大家对历史知识的掌控\n", + "\n", + "* 加深对 range 函数的理解,特别是 步长(step) 参数的理解\n", + " \n", + "* 理解如何使用 list() 将 range 类型的数据转成列表\n", + "\n", + "---\n", + "\n", + "解题步骤:\n", + "\n", + "* 使用 def 定义一个名叫 out_even 的函数,接收一个名叫 max_num 的参数\n", + "\n", + " 可以使用range函数生成 0~max_num 之间的偶数集,记得要考虑range的右区间要加1\n", + " \n", + " 同时为了生成偶数集,记得把第3个参数(step) 设为2,这样只生成 0~max_num之间的偶数\n", + "\n", + " range 函数返回的是一个 range 类型的数据 (大家可以 print(range(1,3)), 看一下到底是什么类型的数据)\n", + " \n", + " 使用 list() 把 range 类型的数据转成 列表\n", + " \n", + " 最后使用 retrun 把运算结果返回。\n", + "\n", + "---\n", + "资料:\n", + "\n", + "* [Python range() 函数用法](https://www.runoob.com/python/python-func-range.html)\n", + "\n", + "* [python:range函数的使用,以及将结果转列表list](https://blog.csdn.net/yeziand01/article/details/85938548)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "ExecuteTime": { + "end_time": "2020-10-12T04:34:22.835659Z", + "start_time": "2020-10-12T04:34:22.831715Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "out_even(3): [0, 2]\n", + "out_even(10): [0, 2, 4, 6, 8, 10]\n", + "out_even(100): [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]\n" + ] + } + ], + "source": [ + "\n", + "def out_even(max_num):\n", + " return list(range(0,max_num+1,2))\n", + "\n", + "print(\"out_even(3):\",out_even(3)) \n", + "print(\"out_even(10):\",out_even(10)) \n", + "print(\"out_even(100):\",out_even(100)) \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 致谢\n", + "\n", + "谢谢 Loren 对本章内容的评审,特此致谢!" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.6" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": true + }, + "varInspector": { + "cols": { + "lenName": 16, + "lenType": 16, + "lenVar": 40 + }, + "kernels_config": { + "python": { + "delete_cmd_postfix": "", + "delete_cmd_prefix": "del ", + "library": "var_list.py", + "varRefreshCmd": "print(var_dic_list())" + }, + "r": { + "delete_cmd_postfix": ") ", + "delete_cmd_prefix": "rm(", + "library": "var_list.r", + "varRefreshCmd": "cat(var_dic_list()) " + } + }, + "types_to_exclude": [ + "module", + "function", + "builtin_function_or_method", + "instance", + "_Feature" + ], + "window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/05-function/questions.ipynb b/05-function/questions.ipynb new file mode 100644 index 0000000..5d8a2f8 --- /dev/null +++ b/05-function/questions.ipynb @@ -0,0 +1,598 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 1. 基础知识重复提醒\n", + "\n", + "有同学会问,不是上一章讲过这些吗?为什么要要重复提醒呢?\n", + "\n", + "是的,知识学习理解的过程是一个反复的过程,我们小时候第一次学习汉字时,基础字练了20次以上。\n", + "\n", + "对于 Python 这么一门语言的练习,基础知识的理解我建议要重复3次以上。\n", + "\n", + "老师把重要的基础知识重复出现3次,大家要把作业练习3次以上,老师推荐的各种链接多看一下。\n", + "\n", + "学习新知识时快就是慢,慢就是快!\n", + "\n", + "大家不但要做我们习题集上的题目,也要练习书上的例题,练习越多理解越深!\n", + "\n", + "## 新知识函数的提醒\n", + "\n", + "函数是代码复用的基础单元,我们把常用的代码封装一个个函数,减少大家的工作量\n", + "\n", + "我们前使用的 print, range, len 都是函数,现在我们不但要学习怎么用函数,也要学习怎么自己写一个个函数\n", + "\n", + "函数的目的不是为了增加了大家的工作量,而是为了降低大家的工作量;如果大家把自己常用代码封装成一个个函数,要用的时候,只要传个参数调用就好了!\n", + "\n", + "前面变量/逻辑判断/循环/数据结构是日后大家每天用 Python 的基础;自己写函数是在前面基础的拔高,是 Excel 思维和程序思维的一个分水岭!\n", + "\n", + "大家放心,从这往后学习,大家会越学越\"容易\"。\n", + "\n", + "## 写代码报错了怎么办\n", + "\n", + "学习了怎么看错误信息,学会了怎么 Google/Baidu查找出错信息,就掌握了最重要的一门程序技能;\n", + "\n", + "有人说学会这两项,就等于半个初级程序员了;学习 Python不是为学习一个固定的知识,而是为了学习解决问题的方法。\n", + "\n", + "* [必看: Zane 老师整理了检查错误的方法](https://www.yinxiang.com/everhub/note/1e44d717-75c3-413d-83ad-5716126ccaa6)\n", + "* [新人必看:17个新手常见Python运行时错误](https://www.oschina.net/question/89964_62779)\n", + "\n", + "\n", + "## Python 各种基础知识\n", + "---\n", + "本章\n", + "\n", + "* [Python3 函数](https://www.runoob.com/python3/python3-function.html)\n", + "* [Python range() 函数用法](https://www.runoob.com/python/python-func-range.html)\n", + "* [B站视频-Python函数讲解](https://search.bilibili.com/all?keyword=python%E5%87%BD%E6%95%B0%E8%AE%B2%E8%A7%A3)\n", + "* [Youtube](https://www.youtube.com/results?search_query=Python+%E5%AD%A6%E4%B9%A0)/[B站](https://search.bilibili.com/all?keyword=Python%20%E5%AD%A6%E4%B9%A0)上有很非常多的 Python 学习视频,大家可以根据自己口味去选择\n", + "\n", + "---\n", + "历史\n", + "\n", + "* [Zane老师整理Python基础知识](https://www.yinxiang.com/everhub/note/414a0a33-7d28-4c27-af22-23f950b990fd)\n", + "* [Python3基础教程](https://www.runoob.com/python3/python3-tutorial.html)\n", + "* [Python 运算符](https://www.runoob.com/python/python-operators.html)\n", + "* [Python3 条件控制](https://www.runoob.com/python3/python3-conditional-statements.html)\n", + "* [Python3 循环语句](https://www.runoob.com/python3/python3-loop.html)\n", + "* [Python range() 函数用法](https://www.runoob.com/python/python-func-range.html)\n", + "* [Python continue 语句](https://www.runoob.com/python/python-continue-statement.html) 详解\n", + "* [Python While 循环语句](https://www.runoob.com/python/python-while-loop.html)\n", + "* [Python break 语句](https://www.runoob.com/python/python-break-statement.html)\n", + "* [Python for 循环语句](https://www.runoob.com/python/python-for-loop.html)\n", + "* [Python 列表(List)](https://www.runoob.com/python/python-lists.html)\n", + "* [Python 字典(Dictionary)](https://www.runoob.com/python/python-dictionary.html)\n", + "* [Python 元组](https://www.runoob.com/python/python-tuples.html)\n", + "* [Python 字典(Dictionary) get()方法](https://www.runoob.com/python/att-dictionary-get.html)\n", + "* [Python List sort()方法](https://www.runoob.com/python/att-list-sort.html)\n", + "* [Python 切片介绍](https://www.liaoxuefeng.com/wiki/1016959663602400/1017269965565856)\n", + "* [Python基础学习-Python中最常见括号()、[]、{}的区别](https://www.cnblogs.com/xuxiaolu/p/4726064.html)\n", + "* [Python List append()方法](https://www.runoob.com/python/att-list-append.html)\n", + "* [Python List insert()方法](https://www.runoob.com/python/att-list-insert.html)\n", + "* [Python List index()方法](https://www.runoob.com/python/att-list-index.html)\n", + "* [python,list中删除的三种方法del、remove、pop](https://blog.csdn.net/weixin_42814873/article/details/83377431)\n", + "* [Python List sort()方法](https://www.runoob.com/python/att-list-sort.html)\n", + "\n", + "\n", + "## 什么是 Markdown(选学)\n", + "\n", + "学习了解 Markdown 可以让大家的代码也有漂亮的文字配合!\n", + "\n", + "* [Markdown 是什么](https://www.zhihu.com/question/19963642)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 高级自动邮件机器人1\n", + "\n", + "---\n", + "题目:\n", + "\n", + "* 假设计你是要给自动给客户发邮件的运营\n", + "\n", + "下面的是客户的订单数据:\n", + "\n", + "```\n", + "orders = [\n", + " {\"buyer_name\": \"Pony\",\"product\": \"MacBook\"},\n", + " {\"buyer_name\": \"Bill\",\"product\": \"AirPods\"},\n", + " {\"buyer_name\": \"Hank\",\"product\": \"iPhone\"}\n", + "]\n", + "```\n", + "* 写一个函数 deliver_mail 接收客户姓名,产品名,然后输出下面格式的邮件:\n", + "\n", + "Dear [用户姓名]:\n", + " \n", + "Your purchased product : [产品名] is delivered!\n", + " \n", + "Thanks for your choosing!\n", + " \n", + "* 遍历orders, 调用 deliver_mail 函数,生成3封邮件\n", + "\n", + "--- \n", + "题目解析:\n", + "\n", + "注意:这是一道复合题 \n", + "\n", + "* orders 的最外层是 [], 表示它是一个list\n", + "\n", + " 可以用 print(type(orders)) 来看它是什么类型\n", + "\n", + "\n", + "* orders 的里层是 {}, 表示里面放的是 dict; \n", + "\n", + " orders 是一个里面成员都是 dict的 list,\n", + " \n", + " orders[0] 表示是 orders 的第一个元素,print(type(orders[0]))\n", + "\n", + "\n", + "* 如果你想知识 orders (订单表)里有多个 order(订单), 可以使用 len(orders) 查看里面有多少订单\n", + "\n", + "* 如果要遍历订单表有两种方法:\n", + "\n", + "```\n", + "# 第一种方法更为简单\n", + "for order in orders:\n", + " print(order)\n", + "```\n", + "\n", + "```\n", + "#第二种方法知道是第几个订单\n", + "for i in range(len(orders)):\n", + " order = orders[i]\n", + " print(order)\n", + "```\n", + "---\n", + "解题步骤:\n", + "\n", + "* 定义 orders 变量,里面存储了所有的订单信息\n", + "\n", + " btw: 同学们现在不要急,现在学习的是 Python 基础,相当于学开车时的倒车入库和S路;如果你现在就和 Excel 去对比,是不大合适的;等到下周大家开始学习 Pandas 时,就相当于学科目三,开车上了公路;慢慢的你就可以体会到里面的区别。\n", + " \n", + " 只有现在基础打好了,日后真的开车上路了就不会慌;如果在高速上,你连方向盘都不好,也很危险的!\n", + " \n", + " \n", + "* 使用 def 关键字定一个邮件发送函数, 名字叫 deliver_mail,接收 buyer_name, product 两个参数\n", + "\n", + " 函数名可以叫任何你想叫的名字,比较 fayoujian (用拼音不是一个好主意,这里举一个例子)\n", + " \n", + " 参数名也可以按你的喜好来,但是通常建议要起的有意义,函数名了变量名都要小写,单词之前用 \"_\" 来连接\n", + "\n", + " 因为字符串拼接以前讲过很多次,这里就不再展开,有不熟的同学可以看一下参考资料\n", + " \n", + " 特别注意的是我现在用 print 打印代替真实的邮件发送,后面的课程我会教大家来发送邮件。\n", + " \n", + " \n", + "* 我们使用 for 循环遍历 orders (列表类型), 将遍历的内容放到 order 中\n", + "\n", + " order是一个字典类型的变量,我们通过 [] + key 分别取出了 buyer_name, product\n", + " \n", + " 然后再在循环里,以 buyer_name, product 为参数调用 deliver_mail 函数。\n", + " \n", + "\n", + "* 大家可以想像一下如果你一天8小手工发500封邮件,现在用循环 + 函数就10分钟搞定了\n", + "\n", + "---\n", + "参考:\n", + "* [Python3 函数](https://www.runoob.com/python3/python3-function.html)\n", + "\n", + "* [Python3 循环语句](https://www.runoob.com/python3/python3-loop.html)\n", + "\n", + "* [python3处理字符串](https://www.runoob.com/python3/python3-string.html)\n", + "\n", + "* [最全面的 python 字符串拼接总结](https://segmentfault.com/a/1190000015475309)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "ExecuteTime": { + "end_time": "2020-07-14T09:36:17.418139Z", + "start_time": "2020-07-14T09:36:17.413010Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " Dear Pony:\n", + "\n", + " Your purchased product : MacBook is delivered!\n", + "\n", + " Thanks for your choosing!\n", + " \n", + "----------Mail Delivered!--------------------\n", + "\n", + " Dear Bill:\n", + "\n", + " Your purchased product : AirPods is delivered!\n", + "\n", + " Thanks for your choosing!\n", + " \n", + "----------Mail Delivered!--------------------\n", + "\n", + " Dear Hank:\n", + "\n", + " Your purchased product : iPhone is delivered!\n", + "\n", + " Thanks for your choosing!\n", + " \n", + "----------Mail Delivered!--------------------\n" + ] + } + ], + "source": [ + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 小小数学家1\n", + "---\n", + "题目:\n", + "\n", + "* 定义一个函数 auto_sum,接收一个参数 max_num,\n", + "\n", + " 计算 1~ max_num 内所有的整数的和,并将结果返回\n", + "\n", + "* 当我们调用 auto_sum 函数时,例如:auto_sum(3),得到 6\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 解法1\n", + "---\n", + "题目解析:\n", + "\n", + "* 这个题目主要练习大家对历史知识的掌控\n", + "\n", + "* 加深对 for 循环的理解,再一次了解怎么定义初始变量,理解如何加总求\n", + " \n", + "* 理解什么叫 return\n", + "\n", + "---\n", + "解题步骤:\n", + "\n", + "* 使用 def 定义一个名叫 auto_sum 的函数,接收一个名叫 max_num 的参数\n", + "\n", + " 定义一个初始变量 total, 将其赋值为0,用来存储汇总求和值\n", + " \n", + " 使用 for 循环遍历 1 ~ max_num 间的数值(可以使用range函数,记得要考虑range的右区间要加1), 把数值放到变量 i 中\n", + " \n", + " 在循环体中,把 i 的值加到 total 里\n", + " \n", + " 在循环体外把 total 返回(return)\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "ExecuteTime": { + "end_time": "2020-07-14T09:44:35.784968Z", + "start_time": "2020-07-14T09:44:35.780761Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "autosum(3): 6\n", + "autosum(10): 55\n", + "autosum(100): 5050\n" + ] + } + ], + "source": [ + "# 请在这里做题\n", + "\n", + "\n", + "#\n", + "\n", + "\n", + "print(\"auto_sum(3):\",auto_sum(3)) \n", + "print(\"auto_sum(10):\",auto_sum(10)) \n", + "print(\"auto_sum(100):\",auto_sum(100)) \n", + "\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 解法2\n", + "---\n", + "题目解析:\n", + "\n", + "* 这个题目主要练习大家对历史知识的掌控\n", + "\n", + "* 理解怎么使用 range 函数\n", + " \n", + "* 理解怎么使用 sum 函数,它不但可以接收单个参数,也可以接收多个参数\n", + "\n", + "---\n", + "解题步骤:\n", + "\n", + "* 使用 def 定义一个名叫 auto_sum 的函数,接收一个名叫 max_num 的参数\n", + "\n", + " 使用 range 生成 1~max_num 间的数字(记得要考虑range的右区间要加1)\n", + " \n", + " 使用 sum 对 range 进行求和,并将数值返回(return)\n", + " \n", + "---\n", + "参考:\n", + "\n", + "* [Python range() 函数用法](https://www.runoob.com/python/python-func-range.html)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "ExecuteTime": { + "end_time": "2020-10-12T04:32:05.217681Z", + "start_time": "2020-10-12T04:32:05.214021Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "auto_sum(3): 6\n", + "auto_sum(10): 55\n", + "auto_sum(100): 5050\n" + ] + } + ], + "source": [ + "# 请在这里做题\n", + "\n", + "#\n", + "\n", + "print(\"auto_sum(3):\",auto_sum(3)) \n", + "print(\"auto_sum(10):\",auto_sum(10)) \n", + "print(\"auto_sum(100):\",auto_sum(100)) " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 小小数学家2\n", + "---\n", + "题目:\n", + "\n", + "* 定义一个函数 out_even,接收一个参数 max_num,\n", + "\n", + " 将 0 ~ max_num 内所有的偶数找出来,放到一个列表里,并返回\n", + "\n", + "* 例如:out_even(10),得到[0, 2, 4, 6, 8, 10]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 解法1\n", + "---\n", + "题目解析:\n", + "\n", + "* 这个题目主要练习大家对历史知识的掌控\n", + "\n", + "* 加深对 for 循环的理解,加深为运算符的理解\n", + " \n", + "* 加深对于列表操作的理解\n", + "\n", + "---\n", + "\n", + "解题步骤:\n", + "\n", + "* 使用 def 定义一个名叫 out_even 的函数,接收一个名叫 max_num 的参数\n", + "\n", + " 定义一个初始变量 ret_list, 将其赋值为 [],用来存储找到的偶数\n", + " \n", + " 使用 for 循环遍历 0 ~ max_num 间的数值(可以使用range函数,记得要考虑range的右区间要加1), 把数值放到变量 i 中\n", + " \n", + " 判断 i%2 是否为零,如果为零则将 i 加到 ret_list 中 ( % 是取模运算符 - 返回除法的余数)\n", + " \n", + " 在循环体外把 ret_list 返回(return)\n", + " \n", + "--- \n", + "资料:\n", + "\n", + "* [Python 运算符](https://www.runoob.com/python/python-operators.html)\n", + "\n", + "* [Python range() 函数用法](https://www.runoob.com/python/python-func-range.html)\n", + "\n", + "* [Python3 List append()方法](https://www.runoob.com/python3/python3-att-list-append.html)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "ExecuteTime": { + "end_time": "2020-07-18T00:06:52.395838Z", + "start_time": "2020-07-18T00:06:52.390937Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "outeven(3): [0, 2]\n", + "outeven(10): [0, 2, 4, 6, 8, 10]\n", + "outeven(100): [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]\n" + ] + } + ], + "source": [ + "# 请在这里做题\n", + "\n", + "#\n", + "print(\"out_even(3):\",out_even(3)) \n", + "print(\"out_even(10):\",out_even(10)) \n", + "print(\"out_even(100):\",out_even(100)) \n", + "\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 解法2\n", + "\n", + "---\n", + "题目解析:\n", + "\n", + "* 这个题目主要练习大家对历史知识的掌控\n", + "\n", + "* 加深对 range 函数的理解,特别是 步长(step) 参数的理解\n", + " \n", + "* 理解如何使用 list() 将 range 类型的数据转成列表\n", + "\n", + "---\n", + "\n", + "解题步骤:\n", + "\n", + "* 使用 def 定义一个名叫 out_even 的函数,接收一个名叫 max_num 的参数\n", + "\n", + " 可以使用range函数生成 0~max_num 之间的偶数集,记得要考虑range的右区间要加1\n", + " \n", + " 同时为了生成偶数集,记得把第3个参数(step) 设为2,这样只生成 0~max_num之间的偶数\n", + "\n", + " range 函数返回的是一个 range 类型的数据 (大家可以 print(range(1,3)), 看一下到底是什么类型的数据)\n", + " \n", + " 使用 list() 把 range 类型的数据转成 列表\n", + " \n", + " 最后使用 retrun 把运算结果返回。\n", + "\n", + "---\n", + "资料:\n", + "\n", + "* [Python range() 函数用法](https://www.runoob.com/python/python-func-range.html)\n", + "\n", + "* [python:range函数的使用,以及将结果转列表list](https://blog.csdn.net/yeziand01/article/details/85938548)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "ExecuteTime": { + "end_time": "2020-10-12T04:34:22.835659Z", + "start_time": "2020-10-12T04:34:22.831715Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "out_even(3): [0, 2]\n", + "out_even(10): [0, 2, 4, 6, 8, 10]\n", + "out_even(100): [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]\n" + ] + } + ], + "source": [ + "# 请在这里做题\n", + "\n", + "#\n", + "\n", + "print(\"out_even(3):\",out_even(3)) \n", + "print(\"out_even(10):\",out_even(10)) \n", + "print(\"out_even(100):\",out_even(100)) \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 致谢\n", + "\n", + "谢谢 Loren 对本章内容的评审,特此致谢!" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.6" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": true + }, + "varInspector": { + "cols": { + "lenName": 16, + "lenType": 16, + "lenVar": 40 + }, + "kernels_config": { + "python": { + "delete_cmd_postfix": "", + "delete_cmd_prefix": "del ", + "library": "var_list.py", + "varRefreshCmd": "print(var_dic_list())" + }, + "r": { + "delete_cmd_postfix": ") ", + "delete_cmd_prefix": "rm(", + "library": "var_list.r", + "varRefreshCmd": "cat(var_dic_list()) " + } + }, + "types_to_exclude": [ + "module", + "function", + "builtin_function_or_method", + "instance", + "_Feature" + ], + "window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/06-class-object/answer-questions.ipynb b/06-class-object/answer-questions.ipynb new file mode 100644 index 0000000..8bfccbe --- /dev/null +++ b/06-class-object/answer-questions.ipynb @@ -0,0 +1,303 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 高级订单自动邮件/短信机器人\n", + "\n", + "---\n", + "\n", + "友情提示:本练习题型(或者本章[类与对象]内容)*不会出现在考试*中\n", + "\n", + "实在不会的可以抄答案三次,谢谢!\n", + "\n", + "做过后,对后面的 Pandas 理解会很有帮助!\n", + "\n", + "---\n", + "\n", + "* 假设计你是要给自动给客户发邮件的运营\n", + "```\n", + "orders = [\n", + " {\"buyer_name\": \"Pony\",\"product\": \"MacBook\",\"email\": \"pony@qq.com\",\"phone\": 13812341234},\n", + " {\"buyer_name\": \"Bill\",\"product\": \"AirPods\",\"email\": \"bill@ms.com\",\"phone\": 13812341235},\n", + " {\"buyer_name\": \"Hank\",\"product\": \"iPhone\",\"email\": \"hank@xyz.com\",\"phone\": 13812341239},\n", + "]\n", + "```\n", + "* 写一个订单类 Order,构造函数接收4个参数:客户姓名,产品名,邮箱,手机号\n", + "* 为 Order 写一个函数 deliver_mail 如果调用就输出下面格式的邮件:\n", + "\n", + "to [email]\n", + "\n", + "Dear [用户姓名]:\n", + " \n", + " Your purchased product : [产品名] is delivered!\n", + " \n", + " Thanks for your choosing!\n", + " \n", + "* 为 Order 写一个函数 deliver_sms 如果调用就输出下面格式的短信:\n", + "\n", + "to [phone]\n", + "\n", + "Dear [用户姓名]:\n", + " \n", + " Your purchased product : [产品名] is delivered!\n", + " \n", + " Thanks for your choosing!\n", + "\n", + "* 遍历orders, 创建 Order类的对象实例,对每个对象调用deliver_mail/deliver_sms 方法\n", + "\n", + "---\n", + "\n", + "解题提示:这是一道复合题 \n", + "\n", + "* orders 的最外层是 [], 表示它是一个list\n", + "\n", + " 可以用 print(type(orders)) 来看它是什么类型\n", + "\n", + "\n", + "* orders 的里层是 {}, 表示里面放的是 dict; \n", + "\n", + " orders 是一个里面成员都是 dict的 list,\n", + " \n", + " orders[0] 表示是 orders 的第一个元素,print(type(orders[0]))\n", + "\n", + "\n", + "* 如果你想知识 orders (订单表)里有多个 order(订单), 可以使用 len(orders) 查看里面有多少订单\n", + "\n", + "* 如果要遍历订单表有两种方法:\n", + "\n", + "```\n", + "# 第一种方法更为简单\n", + "for order in orders:\n", + " print(order)\n", + "```\n", + "\n", + "```\n", + "#第二种方法知道是第几个订单\n", + "for i in range(len(orders)):\n", + " order = orders[i]\n", + " print(order)\n", + "```\n", + "* 假设大家按书上的说明,定义了一个 Order (订单)类\n", + "\n", + "> 定义初始化函数 __init__, 有4个参数: buyer_name, product, email, phone\n", + " \n", + "> 两个函数 deliver_mail/deliver_sms 记增加 self 参数,因为这类里的方法\n", + "\n", + "* 然后在每次循环中, 创建一个类型为 Order 的实例对象,\n", + "\n", + " 初始化函数里传入 buyer_name, product, email, phone\n", + " \n", + " 最后生成了 order_obj, 然后再分调用两个函数\n", + " \n", + "```\n", + " order_obj.deliver_mail()\n", + " order_obj.deliver_mail()\n", + "```\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "ExecuteTime": { + "end_time": "2020-07-14T10:13:53.984058Z", + "start_time": "2020-07-14T10:13:53.975701Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " To : pony@qq.com\n", + "\n", + " Dear Pony:\n", + "\n", + " Your purchased product : MacBook is delivered!\n", + "\n", + " Thanks for your choosing!\n", + " \n", + "----------Mail Delivered!--------------------\n", + "\n", + " To : 13812341234\n", + "\n", + " Dear Pony:\n", + "\n", + " Your purchased product : MacBook is delivered!\n", + "\n", + " Thanks for your choosing!\n", + " \n", + "----------SMS Delivered!--------------------\n", + "\n", + " To : bill@ms.com\n", + "\n", + " Dear Bill:\n", + "\n", + " Your purchased product : AirPods is delivered!\n", + "\n", + " Thanks for your choosing!\n", + " \n", + "----------Mail Delivered!--------------------\n", + "\n", + " To : 13812341235\n", + "\n", + " Dear Bill:\n", + "\n", + " Your purchased product : AirPods is delivered!\n", + "\n", + " Thanks for your choosing!\n", + " \n", + "----------SMS Delivered!--------------------\n", + "\n", + " To : hank@xyz.com\n", + "\n", + " Dear Hank:\n", + "\n", + " Your purchased product : iPhone is delivered!\n", + "\n", + " Thanks for your choosing!\n", + " \n", + "----------Mail Delivered!--------------------\n", + "\n", + " To : 13812341239\n", + "\n", + " Dear Hank:\n", + "\n", + " Your purchased product : iPhone is delivered!\n", + "\n", + " Thanks for your choosing!\n", + " \n", + "----------SMS Delivered!--------------------\n" + ] + } + ], + "source": [ + "class Order:\n", + " def __init__(self,buyer_name,product,email,phone):\n", + " self.buyer_name = buyer_name\n", + " self.product = product\n", + " self.email = email\n", + " self.phone = phone\n", + " \n", + " def deliver_mail(self):\n", + " email_template = \"\"\"\n", + " To : %s\n", + "\n", + " Dear %s:\n", + "\n", + " Your purchased product : %s is delivered!\n", + "\n", + " Thanks for your choosing!\n", + " \"\"\"\n", + " email_body = email_template % (self.email,self.buyer_name,self.product)\n", + " print(email_body)\n", + " print(\"----------Mail Delivered!--------------------\")\n", + " \n", + " def deliver_sms(self):\n", + " sms_template = \"\"\"\n", + " To : %s\n", + "\n", + " Dear %s:\n", + "\n", + " Your purchased product : %s is delivered!\n", + "\n", + " Thanks for your choosing!\n", + " \"\"\"\n", + " sms_body = sms_template % (self.phone,self.buyer_name,self.product)\n", + " print(sms_body)\n", + " print(\"----------SMS Delivered!--------------------\") \n", + "\n", + "orders = [\n", + " {\"buyer_name\": \"Pony\",\"product\": \"MacBook\",\"email\": \"pony@qq.com\",\"phone\": 13812341234},\n", + " {\"buyer_name\": \"Bill\",\"product\": \"AirPods\",\"email\": \"bill@ms.com\",\"phone\": 13812341235},\n", + " {\"buyer_name\": \"Hank\",\"product\": \"iPhone\",\"email\": \"hank@xyz.com\",\"phone\": 13812341239},\n", + "]\n", + "\n", + "for order in orders:\n", + " buyer_name = order['buyer_name']\n", + " product = order['product']\n", + " email = order['email']\n", + " phone = order['phone']\n", + " \n", + " order_obj = Order(buyer_name,product,email,phone)\n", + " order_obj.deliver_mail()\n", + " order_obj.deliver_sms()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.6" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false + }, + "varInspector": { + "cols": { + "lenName": 16, + "lenType": 16, + "lenVar": 40 + }, + "kernels_config": { + "python": { + "delete_cmd_postfix": "", + "delete_cmd_prefix": "del ", + "library": "var_list.py", + "varRefreshCmd": "print(var_dic_list())" + }, + "r": { + "delete_cmd_postfix": ") ", + "delete_cmd_prefix": "rm(", + "library": "var_list.r", + "varRefreshCmd": "cat(var_dic_list()) " + } + }, + "types_to_exclude": [ + "module", + "function", + "builtin_function_or_method", + "instance", + "_Feature" + ], + "window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/06-class-object/questions.ipynb b/06-class-object/questions.ipynb new file mode 100644 index 0000000..538a2f3 --- /dev/null +++ b/06-class-object/questions.ipynb @@ -0,0 +1,173 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 高级订单自动邮件/短信机器人\n", + "\n", + "---\n", + "\n", + "友情提示:本练习题型(或者本章[类与对象]内容)*不会出现在考试*中\n", + "\n", + "实在不会的可以抄答案三次,谢谢!\n", + "\n", + "做过后,对后面的 Pandas 理解会很有帮助!\n", + "\n", + "---\n", + "\n", + "* 假设计你是要给自动给客户发邮件的运营\n", + "```\n", + "orders = [\n", + " {\"buyer_name\": \"Pony\",\"product\": \"MacBook\",\"email\": \"pony@qq.com\",\"phone\": 13812341234},\n", + " {\"buyer_name\": \"Bill\",\"product\": \"AirPods\",\"email\": \"bill@ms.com\",\"phone\": 13812341235},\n", + " {\"buyer_name\": \"Hank\",\"product\": \"iPhone\",\"email\": \"hank@xyz.com\",\"phone\": 13812341239},\n", + "]\n", + "```\n", + "* 写一个订单类 Order,构造函数接收4个参数:客户姓名,产品名,邮箱,手机号\n", + "* 为 Order 写一个函数 deliver_mail 如果调用就输出下面格式的邮件:\n", + "\n", + "to [email]\n", + "\n", + "Dear [用户姓名]:\n", + " \n", + " Your purchased product : [产品名] is delivered!\n", + " \n", + " Thanks for your choosing!\n", + " \n", + "* 为 Order 写一个函数 deliver_sms 如果调用就输出下面格式的短信:\n", + "\n", + "to [phone]\n", + "\n", + "Dear [用户姓名]:\n", + " \n", + " Your purchased product : [产品名] is delivered!\n", + " \n", + " Thanks for your choosing!\n", + "\n", + "* 遍历orders, 创建 Order类的对象实例,对每个对象调用deliver_mail/deliver_sms 方法\n", + "\n", + "---\n", + "\n", + "解题提示:这是一道复合题 \n", + "\n", + "* orders 的最外层是 [], 表示它是一个list\n", + "\n", + " 可以用 print(type(orders)) 来看它是什么类型\n", + "\n", + "\n", + "* orders 的里层是 {}, 表示里面放的是 dict; \n", + "\n", + " orders 是一个里面成员都是 dict的 list,\n", + " \n", + " orders[0] 表示是 orders 的第一个元素,print(type(orders[0]))\n", + "\n", + "\n", + "* 如果你想知识 orders (订单表)里有多个 order(订单), 可以使用 len(orders) 查看里面有多少订单\n", + "\n", + "* 如果要遍历订单表有两种方法:\n", + "\n", + "```\n", + "# 第一种方法更为简单\n", + "for order in orders:\n", + " print(order)\n", + "```\n", + "\n", + "```\n", + "#第二种方法知道是第几个订单\n", + "for i in range(len(orders)):\n", + " order = orders[i]\n", + " print(order)\n", + "```\n", + "* 假设大家按书上的说明,定义了一个 Order (订单)类\n", + "\n", + "> 定义初始化函数 __init__, 有4个参数: buyer_name, product, email, phone\n", + " \n", + "> 两个函数 deliver_mail/deliver_sms 记增加 self 参数,因为这类里的方法\n", + "\n", + "* 然后在每次循环中, 创建一个类型为 Order 的实例对象,\n", + "\n", + " 初始化函数里传入 buyer_name, product, email, phone\n", + " \n", + " 最后生成了 order_obj, 然后再分调用两个函数\n", + " \n", + "```\n", + " order_obj.deliver_mail()\n", + " order_obj.deliver_mail()\n", + "```\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.6" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false + }, + "varInspector": { + "cols": { + "lenName": 16, + "lenType": 16, + "lenVar": 40 + }, + "kernels_config": { + "python": { + "delete_cmd_postfix": "", + "delete_cmd_prefix": "del ", + "library": "var_list.py", + "varRefreshCmd": "print(var_dic_list())" + }, + "r": { + "delete_cmd_postfix": ") ", + "delete_cmd_prefix": "rm(", + "library": "var_list.r", + "varRefreshCmd": "cat(var_dic_list()) " + } + }, + "types_to_exclude": [ + "module", + "function", + "builtin_function_or_method", + "instance", + "_Feature" + ], + "window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/71-jinjia/complex_html.j2 b/71-jinjia/complex_html.j2 new file mode 100644 index 0000000..2f1e3cc --- /dev/null +++ b/71-jinjia/complex_html.j2 @@ -0,0 +1,15 @@ +

{{ title }}

+ + diff --git a/71-jinjia/foot.html b/71-jinjia/foot.html new file mode 100644 index 0000000..e69de29 diff --git a/71-jinjia/helloworld.txt b/71-jinjia/helloworld.txt new file mode 100644 index 0000000..d39825c --- /dev/null +++ b/71-jinjia/helloworld.txt @@ -0,0 +1 @@ +Just Hello World! \ No newline at end of file diff --git a/71-jinjia/jinja2.ipynb b/71-jinjia/jinja2.ipynb new file mode 100644 index 0000000..f2af72c --- /dev/null +++ b/71-jinjia/jinja2.ipynb @@ -0,0 +1,408 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 学习用 jinja2 来拼接复杂的 html\n", + "\n", + "---\n", + "背景介绍\n", + "\n", + "Jinja2是为python提供的一个功能齐全的模板引擎,它可以帮你轻松的拼出你想要文本或HTML\n", + "\n", + "[本章配套资料与代码下载](https://downgit.github.io/#/home?url=https:%2F%2Fgithub.com%2Fliuhui998%2Fpython-basic%2Ftree%2Fmaster%2F71-jinjia)\n", + "\n", + "---\n", + "参考资料:\n", + "* [Jinja2 Templating Engine Tutorial](https://medium.com/@jasonrigden/jinja2-templating-engine-tutorial-4bd31fb4aea3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 安装 jinja2 Python 包\n", + "\n", + "如果已安装,请使用\"#\" 将下面这一行注释" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-16T09:10:32.703101Z", + "start_time": "2020-09-16T09:10:30.888098Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: jinja2 in /Users/peter/opt/anaconda3/lib/python3.7/site-packages (2.11.1)\r\n", + "Requirement already satisfied: MarkupSafe>=0.23 in /Users/peter/opt/anaconda3/lib/python3.7/site-packages (from jinja2) (1.1.1)\r\n" + ] + } + ], + "source": [ + "!pip install jinja2" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-16T09:33:32.852789Z", + "start_time": "2020-09-16T09:33:32.834849Z" + } + }, + "source": [ + "# 初始化相关" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-16T09:35:11.049435Z", + "start_time": "2020-09-16T09:35:11.047145Z" + } + }, + "source": [ + "## 导入 jinja2 相关的包" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-21T07:45:19.502597Z", + "start_time": "2020-09-21T07:45:19.499427Z" + } + }, + "outputs": [], + "source": [ + "\n", + "from jinja2 import Environment, FileSystemLoader" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 初始化 jinja2 相关变量\n", + "\n", + "* FileSystemLoader 是一个文件加载器,参数要指定模板文件的路径\n", + " \n", + " 因为模板文件就在当前目录,所以我们这里用了 \".\", \n", + " \n", + " 构建好放到 file_loader 中\n", + " \n", + "* 然后再以 file_loader 为参数构建一个 Environment 类型的对象\n", + "\n", + " 放到 env 中" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-21T07:45:20.117503Z", + "start_time": "2020-09-21T07:45:20.105782Z" + } + }, + "outputs": [], + "source": [ + "file_loader = FileSystemLoader('.')\n", + "env = Environment(loader=file_loader)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 最简单的模板\n", + "\n", + "* 我有一个 helloworld.txt\n", + "\n", + " 里面的内容是:\n", + " \n", + " Just Hello World!\n", + " \n", + "\n", + "* 我们使用 env.get_template 加载了 helloworld.txt\n", + "\n", + " 放到 template 中\n", + "\n", + "\n", + "* 再调用 template 的 render() 方法进行渲染,将返回值放到 output" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-21T07:45:20.882233Z", + "start_time": "2020-09-21T07:45:20.858971Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Just Hello World!\n" + ] + } + ], + "source": [ + "\n", + "template = env.get_template('helloworld.txt')\n", + "output = template.render()\n", + "print(output)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 简单变量的模板\n", + "\n", + "* 我有一个 lamb.j2\n", + "\n", + " 里面的内容是:\n", + " \n", + " {{ name }} had a little lamb.\n", + " \n", + "\n", + "* 我们使用 env.get_template 加载了 lamb.j2\n", + "\n", + " 放到 template 中\n", + "\n", + "\n", + "* 再调用 template 的 render() 方法进行渲染,\n", + "\n", + " 在 render 函数参数中指定 name 变量的值为 \"Peter\"\n", + " \n", + " 将render函数的返回值放到 output\n", + " \n", + " \n", + "* 最后打印输出 " + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-21T07:45:22.974437Z", + "start_time": "2020-09-21T07:45:22.966299Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Mary had a little lamb.\n" + ] + } + ], + "source": [ + "template = env.get_template('lamb.j2')\n", + "output = template.render(name = \"Mary\")\n", + "print(output)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 支持 For 循环的模板" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-21T07:45:25.764823Z", + "start_time": "2020-09-21T07:45:25.752895Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "

美国的前三位总统

\n", + "\n", + "\n" + ] + } + ], + "source": [ + "names = [\"George Washington\",\"John Adams\",\"Thomas Jefferson\"]\n", + "title = \"美国的前三位总统\"\n", + "\n", + "template = env.get_template('simple_html.j2')\n", + "output = template.render(title = title,names = names)\n", + "print(output)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-16T09:50:41.143075Z", + "start_time": "2020-09-16T09:50:41.141013Z" + } + }, + "source": [ + "# 支持 For 循环 + 条件判断的模板" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "ExecuteTime": { + "end_time": "2020-09-16T09:27:42.431065Z", + "start_time": "2020-09-16T09:27:42.424285Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "

美国的前三位总统

\n", + "\n", + "\n" + ] + } + ], + "source": [ + "presidents = [\n", + " {\"name\": \"George Washington\", \"live\":67},\n", + " {\"name\": \"John Adams\", \"live\":91},\n", + " {\"name\": \"Thomas Jefferson\", \"live\":83}, \n", + "]\n", + "title = \"美国的前三位总统\"\n", + "\n", + "template = env.get_template('complex_html.j2')\n", + "output = template.render(title = title,presidents = presidents)\n", + "print(output)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.6" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false + }, + "varInspector": { + "cols": { + "lenName": 16, + "lenType": 16, + "lenVar": 40 + }, + "kernels_config": { + "python": { + "delete_cmd_postfix": "", + "delete_cmd_prefix": "del ", + "library": "var_list.py", + "varRefreshCmd": "print(var_dic_list())" + }, + "r": { + "delete_cmd_postfix": ") ", + "delete_cmd_prefix": "rm(", + "library": "var_list.r", + "varRefreshCmd": "cat(var_dic_list()) " + } + }, + "types_to_exclude": [ + "module", + "function", + "builtin_function_or_method", + "instance", + "_Feature" + ], + "window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/71-jinjia/lamb.j2 b/71-jinjia/lamb.j2 new file mode 100644 index 0000000..47d25c1 --- /dev/null +++ b/71-jinjia/lamb.j2 @@ -0,0 +1 @@ +{{ name }} had a little lamb. \ No newline at end of file diff --git a/71-jinjia/simple_html.j2 b/71-jinjia/simple_html.j2 new file mode 100644 index 0000000..3bab8e5 --- /dev/null +++ b/71-jinjia/simple_html.j2 @@ -0,0 +1,7 @@ +

{{ title }}

+ + diff --git a/README.md b/README.md index 258aa8a..7b0bace 100644 --- a/README.md +++ b/README.md @@ -1,30 +1,114 @@ -# Python Basic 习题集 -## 前言 + +# Python 学习寄语 为了大家有更多的 Python 练习机会,我整理了这本习题集 +就像要和外国人沟通要学外语一样,要有效的和计算机沟能也要学习计算机的语言。 + +很高兴能和大家一起学 Python, Python 是一门易学的计算机语言。 + +## 怎么学 Python + +编程是一门实践者的科学,就如学习外语一样;只看书学不会一门外语,不动手也同样学不会 Python. + +我们会使用 Notebook/Colab 的开发环境来学习 Python, 大家可以像学习Excel一样,在Python格子里写代码。 + +当大家按自己理解写下代码,再点击运行,这就是最好的学习方法之一。 + +## 碰到不懂的问题怎么办 + +学习 Python 比学习一问外语容易的地方是现在有了 Google/Baidu, 碰到问题可以查 + +很多程序员每天的工作也是 Google/Baidu, 因为那么多的方法和公式没有人能记得住 + +大家要记得一个事情:学习的目的是为了学习解决问题的方法和思路,而不是背诵知识,因为在背诵上人是永远不可能打败计算机的。 + +在初始学习阶段,老师会帮大家整理很多参考资料链接,大家一定要注意看! + +# Python Basic 习题集 + +前面的基础题目大家可能会觉得很简单很傻,离大家最开始学 Python 梦想有一点差距 + +但是饭是一口口吃的,路是一步步走的;Peter 把复杂的问题一步步的拆解,方便大家可以啃下来。 + +如果大家能把小的问题都解决了,就可以去解更复杂的问题! + ## 变量 -[习题](01-variable/questions.ipynb), [答案](01-variable/answer-questions.ipynb) +* 学习了解什么是变量 +* 怎么做一个简单的邮件机器人 +* 怎么进行字符串截取操作 +* 有很多字符串的基础知识 + +[知识讲解与答案](01-variable/answer-questions.ipynb),[空白练习题](01-variable/questions.ipynb) ## 逻辑判断 -[习题](02-logic/questions.ipynb), [答案](02-logic/answer-questions.ipynb) +一个处理重复工作的自动机器人里肯定有大量的逻辑判断,学习写逻辑判断是开发自动化机器人脚本的第一步: + +* 开发季节判断机器人 +* 开发价格预警机器人 +* 开发考试评语机器人 +* 开发智能空调机器人 -## 循环 +[知识讲解与答案](02-logic/answer-questions.ipynb),[空白练习题](02-logic/questions.ipynb) + + +## 循环遍历 + +如果想让重复工作自动化,必须要学好循环; + +你可以想像重复的工作,就像一个“列表”, 用循环处理1个任务和处理10000个任务本质上是一样的: + +* 智能日历第一步,打印12个月 +* for 循环计算1到4的连乘 +* for 循环打印 "Good Day!" 里的每个字符 +* for 循环打印出所有 1~50 之间的所有偶数 +* PM自动找差评 +* 自动邮件机器人 + +[知识讲解与答案](03-loop/answer-questions.ipynb),[空白练习题](03-loop/questions.ipynb) -[习题](03-loop/questions.ipynb), [答案](03-loop/answer-questions.ipynb) ## 列表/字典/元组 +机器人的要处理的重复任务,必须要有一个地方进行存储; + +列表/字典/元组等数据结构就是用来存储各种复杂任务与数据的地方 + +数据结构 + 算法(计算逻辑)= 程序 + +* 智能排序机器人 +* 车位管理机器人 +* 自动询价机器人 +* 仓库数据管理机器人 + +[知识讲解与答案](04-list-dict/answer-questions.ipynb),[空白练习题](04-list-dict/questions.ipynb) + ## 函数 +如果开发一个机器人时,没有别人写的功能函数,什么都要自己开发就会非常低效 + +前面我们学会用函数,如 print 等,函数是对程序的一种封装,这里我们要学习怎么来写函数 + +* 高级自动邮件机器人1 +* 小小数学家1 +* 小小数学家2 + +[知识讲解与答案](05-function/answer-questions.ipynb),[空白练习题](05-function/questions.ipynb) + + ## 类与对象 -## 注释 +类是对更复杂功能的一种封装,你可以把 str 想像为一个类,lower() 是类里方法,对象可以类的一实例。 + +使用别人开发的类,可以大大降低我们的开发工作量,在真实的工作中,我们很少自己来写类,但是要了解怎么写一个类。 + +* 高级订单自动邮件/短信机器人 + +[知识讲解与答案](06-class-object/answer-questions.ipynb),[空白练习题](06-class-object/questions.ipynb) -## 异常与调试 ## 模块功能库 -## 用dingding发消息 +