diff --git a/04-list-dict/answer-questions.ipynb b/04-list-dict/answer-questions.ipynb index 43dd00d..68b4683 100644 --- a/04-list-dict/answer-questions.ipynb +++ b/04-list-dict/answer-questions.ipynb @@ -1,18 +1,226 @@ { "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", "```\n", " sales = [100,200,50,300,20,500,1000,10]\n", "```\n", "* 请对 sales 进行升序排序,并且打印出来\n", "* 请对 sales 进行降序排序,并且打印出来\n", "* 请用排序+切片的方式,找出 Top3 的销售额\n", - "* 请用排序+切片的方式,找出 最低的3个销售额" + "* 请用排序+切片的方式,找出 最低的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)" ] }, { @@ -44,21 +252,6 @@ } ], "source": [ - "sales = [100,200,50,300,20,500,1000,10]\n", - "print(\"进行升序排序\")\n", - "sales.sort() #\n", - "print(sales)\n", - "print(\"-\"*70)\n", - "\n", - "print(\"进行降序排序\")\n", - "sales.sort(reverse= True) #进行降序排序\n", - "print(sales)\n", - "print(\"-\"*70)\n", - "\n", - "sales.sort() #进行升序排序\n", - "print(\"查看最后3个数据(top3)\")\n", - "print(sales[-3:]) \n", - "print(\"-\"*70)\n", "\n", "print(\"查看最前3个数据( 最低的3个销售额)\")\n", "print(sales[:3]) \n" @@ -69,6 +262,9 @@ "metadata": {}, "source": [ "# 车位管理机器人\n", + "---\n", + "题目:\n", + "\n", "* 你现有管理一个停车场\n", "* 现有一组车位租用情况,结构如下:\n", "```\n", @@ -77,7 +273,35 @@ "* 现有新来了一辆车 'A0030', 请把它放在 car_nums 的最后面\n", "* 现有新来了一辆车 'A0000', 请把它放在 car_nums 的最前面\n", "* 'A00X9' 这辆车现在不租你的停车位上,请把它从 car_nums 中删掉\n", - "* 现在来了一个车队 ['B0001','B0002','B003'], 请用一行代码把它加到 car_nums 中" + "* 现在来了一个车队 ['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" ] }, { @@ -109,7 +333,6 @@ } ], "source": [ - "car_nums = ['A0001','A00X9','A0027']\n", "print(\"现有新来了一辆车 'A0030', 请把它放在 car_nums 的最后面\")\n", "car_nums.append('A0030')\n", "print(car_nums)\n", @@ -121,8 +344,12 @@ "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", @@ -136,6 +363,9 @@ "metadata": {}, "source": [ "# 自动询价机器人\n", + "---\n", + "题目:\n", + "\n", "* 你现在管理一个商务团队,每天有客户找问价格\n", "* 你有一个价格对应表\n", "```\n", @@ -149,40 +379,93 @@ "* 每次客户来问题,你都要找这个表,你现在很烦,想做一个自动机器人\n", "* 写一个 while 循环,提示用户输入 sku\n", "* 根据用户输入的sku查询 prices, 找印出对应价格\n", - "* 如果用户输入 ! 则退出循环(break)" + "* 如果用户输入 ! 则退出循环(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": 29, + "execution_count": 3, "metadata": { "ExecuteTime": { - "end_time": "2020-07-14T05:51:59.462459Z", - "start_time": "2020-07-14T05:51:57.412321Z" + "end_time": "2020-10-09T08:49:42.172498Z", + "start_time": "2020-10-09T08:49:42.169766Z" } }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "请输入SKU:!\n" - ] - } - ], + "outputs": [], "source": [ + "# 定义价格表\n", "prices = {\n", " \"SKU-A\": 100,\n", " \"SKU-B\": 120,\n", " \"SKU-C\": 190,\n", " \"SKU-D\": 200\n", - "}\n", - "\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", - " print(\"sku: %s, price: %s\" % (sku,prices.get(sku)) )" + " price = prices.get(sku)\n", + " print(\"sku: %s, price: %f\" % (sku,price) )" ] }, { @@ -191,8 +474,9 @@ "source": [ "# 仓库数据管理机器人\n", "\n", - "\n", "---\n", + "题目:\n", + "\n", "* 你现在是仓库的负责人\n", "* 仓库数格式是这样的\n", "```\n", @@ -210,8 +494,10 @@ "* 使用切片方法显示 total 中的最后一行记录\n", "\n", "---\n", + "题目解析:\n", + "\n", + "这是一道复合题,也是为了方便大家理解复杂的数据结构,为以后大家学 Pandas 处理得复杂数据打下伏笔,不过大家放心,后面学习 Pandas 后肯定比现在方便!\n", "\n", - "解题提示:这是一道复合题 \n", "* total 的最外层是 [], 表示它是一个list\n", "\n", " 可以用 print(type(total)) 来看它是什么类型\n", @@ -230,7 +516,69 @@ " \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" ] }, { @@ -275,7 +623,7 @@ "print(\"请统计仓库的物品总数量(quantity)\")\n", "total_quantity = 0\n", "for item in total:\n", - " total_quantity += item[\"quantity\"]\n", + " total_quantity = total_quantity + item[\"quantity\"]\n", "print(\"仓库的物品总数量:\",total_quantity)\n", "print(\"-\"*70)\n", "\n", @@ -289,7 +637,7 @@ "print(\"-\"*70)\n", "\n", "print(\"现在 SKU-E 新品上市,要入库300件商品,请在 total 中新增一条相应记录\")\n", - "sku_e = {\"sku\":\"SKU-E\", \"quituantity\":3000}\n", + "sku_e = {\"sku\":\"SKU-E\", \"quantity\":300}\n", "total.append(sku_e)\n", "print(\"更新后数据:\",total)\n", "print(\"-\"*70)\n", @@ -309,6 +657,15 @@ "print(\"使用切片方法显示 total 中的最后一行记录\")\n", "print(\"最后一行数据:\",total[-1])" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 致谢\n", + "\n", + "谢谢 Loren 对本章内容的评审,特此致谢!" + ] } ], "metadata": { diff --git a/04-list-dict/questions.ipynb b/04-list-dict/questions.ipynb index 088bc1d..01d2116 100644 --- a/04-list-dict/questions.ipynb +++ b/04-list-dict/questions.ipynb @@ -1,18 +1,259 @@ { "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", "```\n", " sales = [100,200,50,300,20,500,1000,10]\n", "```\n", "* 请对 sales 进行升序排序,并且打印出来\n", "* 请对 sales 进行降序排序,并且打印出来\n", "* 请用排序+切片的方式,找出 Top3 的销售额\n", - "* 请用排序+切片的方式,找出 最低的3个销售额" + "* 请用排序+切片的方式,找出 最低的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" ] }, { @@ -20,6 +261,9 @@ "metadata": {}, "source": [ "# 车位管理机器人\n", + "---\n", + "题目:\n", + "\n", "* 你现有管理一个停车场\n", "* 现有一组车位租用情况,结构如下:\n", "```\n", @@ -28,7 +272,71 @@ "* 现有新来了一辆车 'A0030', 请把它放在 car_nums 的最后面\n", "* 现有新来了一辆车 'A0000', 请把它放在 car_nums 的最前面\n", "* 'A00X9' 这辆车现在不租你的停车位上,请把它从 car_nums 中删掉\n", - "* 现在来了一个车队 ['B0001','B0002','B003'], 请用一行代码把它加到 car_nums 中" + "* 现在来了一个车队 ['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" ] }, { @@ -36,6 +344,9 @@ "metadata": {}, "source": [ "# 自动询价机器人\n", + "---\n", + "题目:\n", + "\n", "* 你现在管理一个商务团队,每天有客户找问价格\n", "* 你有一个价格对应表\n", "```\n", @@ -49,17 +360,97 @@ "* 每次客户来问题,你都要找这个表,你现在很烦,想做一个自动机器人\n", "* 写一个 while 循环,提示用户输入 sku\n", "* 根据用户输入的sku查询 prices, 找印出对应价格\n", - "* 如果用户输入 ! 则退出循环(break)" + "* 如果用户输入 ! 则退出循环(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", "* 仓库数格式是这样的\n", "```\n", @@ -77,8 +468,10 @@ "* 使用切片方法显示 total 中的最后一行记录\n", "\n", "---\n", + "题目解析:\n", + "\n", + "这是一道复合题,也是为了方便大家理解复杂的数据结构,为以后大家学 Pandas 处理得复杂数据打下伏笔,不过大家放心,后面学习 Pandas 后肯定比现在方便!\n", "\n", - "解题提示:这是一道复合题 \n", "* total 的最外层是 [], 表示它是一个list\n", "\n", " 可以用 print(type(total)) 来看它是什么类型\n", @@ -97,7 +490,137 @@ " \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 对本章内容的评审,特此致谢!" ] } ], @@ -130,7 +653,7 @@ "toc_cell": false, "toc_position": {}, "toc_section_display": true, - "toc_window_display": false + "toc_window_display": true }, "varInspector": { "cols": { diff --git a/05-function/answer-questions.ipynb b/05-function/answer-questions.ipynb index 57df358..ff66578 100644 --- a/05-function/answer-questions.ipynb +++ b/05-function/answer-questions.ipynb @@ -1,10 +1,97 @@ { "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", @@ -26,9 +113,10 @@ " \n", "* 遍历orders, 调用 deliver_mail 函数,生成3封邮件\n", "\n", - "---\n", + "--- \n", + "题目解析:\n", "\n", - "解题提示:这是一道复合题 \n", + "注意:这是一道复合题 \n", "\n", "* orders 的最外层是 [], 表示它是一个list\n", "\n", @@ -57,7 +145,46 @@ "for i in range(len(orders)):\n", " order = orders[i]\n", " print(order)\n", - "```\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)" ] }, { @@ -131,8 +258,43 @@ "metadata": {}, "source": [ "# 小小数学家1\n", - "* 定义一个函数autosum,输出任意指定数字内的数字的和,结果为一个数字\n", - "* 例如:执行autosum(3),得到 6\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" ] }, { @@ -156,26 +318,128 @@ } ], "source": [ - "def autosum(max_num):\n", + "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(\"autosum(3):\",autosum(3)) \n", - "print(\"autosum(10):\",autosum(10)) \n", - "print(\"autosum(100):\",autosum(100)) \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", - "* 定义一个函数outeven,输出任意指定数字内的全部偶数,结果为一个列表\n", - "* 例如:执行outeven(10),得到[2, 4, 6, 8, 10]" + "---\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" ] }, { @@ -199,26 +463,97 @@ } ], "source": [ - "def outeven(max_num):\n", + "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(\"outeven(3):\",outeven(3)) \n", - "print(\"outeven(10):\",outeven(10)) \n", - "print(\"outeven(100):\",outeven(100)) \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": null, + "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": {}, - "outputs": [], - "source": [] + "source": [ + "# 致谢\n", + "\n", + "谢谢 Loren 对本章内容的评审,特此致谢!" + ] } ], "metadata": { diff --git a/05-function/questions.ipynb b/05-function/questions.ipynb index d0a6b84..5d8a2f8 100644 --- a/05-function/questions.ipynb +++ b/05-function/questions.ipynb @@ -1,10 +1,97 @@ { "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", @@ -26,9 +113,10 @@ " \n", "* 遍历orders, 调用 deliver_mail 函数,生成3封邮件\n", "\n", - "---\n", + "--- \n", + "题目解析:\n", "\n", - "解题提示:这是一道复合题 \n", + "注意:这是一道复合题 \n", "\n", "* orders 的最外层是 [], 表示它是一个list\n", "\n", @@ -57,7 +145,91 @@ "for i in range(len(orders)):\n", " order = orders[i]\n", " print(order)\n", - "```\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" ] }, { @@ -65,8 +237,136 @@ "metadata": {}, "source": [ "# 小小数学家1\n", - "* 定义一个函数autosum,输出任意指定数字内的数字的和,结果为一个数字\n", - "* 例如:执行autosum(3),得到 6\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)) " ] }, { @@ -74,8 +374,161 @@ "metadata": {}, "source": [ "# 小小数学家2\n", - "* 定义一个函数outeven,输出任意指定数字内的全部偶数,结果为一个列表\n", - "* 例如:执行outeven(10),得到[2, 4, 6, 8, 10]" + "---\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 对本章内容的评审,特此致谢!" ] } ], @@ -108,7 +561,7 @@ "toc_cell": false, "toc_position": {}, "toc_section_display": true, - "toc_window_display": false + "toc_window_display": true }, "varInspector": { "cols": { diff --git a/71-jinjia/jinja2.ipynb b/71-jinjia/jinja2.ipynb index a33fc2a..f2af72c 100644 --- a/71-jinjia/jinja2.ipynb +++ b/71-jinjia/jinja2.ipynb @@ -76,11 +76,11 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 20, "metadata": { "ExecuteTime": { - "end_time": "2020-09-16T09:10:32.727740Z", - "start_time": "2020-09-16T09:10:32.706555Z" + "end_time": "2020-09-21T07:45:19.502597Z", + "start_time": "2020-09-21T07:45:19.499427Z" } }, "outputs": [], @@ -108,11 +108,11 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 21, "metadata": { "ExecuteTime": { - "end_time": "2020-09-16T09:10:32.732976Z", - "start_time": "2020-09-16T09:10:32.729950Z" + "end_time": "2020-09-21T07:45:20.117503Z", + "start_time": "2020-09-21T07:45:20.105782Z" } }, "outputs": [], @@ -144,11 +144,11 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 22, "metadata": { "ExecuteTime": { - "end_time": "2020-09-16T09:10:32.740663Z", - "start_time": "2020-09-16T09:10:32.734960Z" + "end_time": "2020-09-21T07:45:20.882233Z", + "start_time": "2020-09-21T07:45:20.858971Z" } }, "outputs": [ @@ -197,11 +197,11 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 23, "metadata": { "ExecuteTime": { - "end_time": "2020-09-16T09:10:32.746079Z", - "start_time": "2020-09-16T09:10:32.742194Z" + "end_time": "2020-09-21T07:45:22.974437Z", + "start_time": "2020-09-21T07:45:22.966299Z" } }, "outputs": [ @@ -209,13 +209,13 @@ "name": "stdout", "output_type": "stream", "text": [ - "Peter had a little lamb.\n" + "Mary had a little lamb.\n" ] } ], "source": [ "template = env.get_template('lamb.j2')\n", - "output = template.render(name = \"Peter\")\n", + "output = template.render(name = \"Mary\")\n", "print(output)" ] }, @@ -228,11 +228,11 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 24, "metadata": { "ExecuteTime": { - "end_time": "2020-09-16T09:10:32.754357Z", - "start_time": "2020-09-16T09:10:32.747807Z" + "end_time": "2020-09-21T07:45:25.764823Z", + "start_time": "2020-09-21T07:45:25.752895Z" } }, "outputs": [ @@ -250,10 +250,6 @@ "