提交 488f365a 编写于 作者: F feilong

init python skill tree

上级 8f379fef
.vscode
.idea
.DS_Store
__pycache__
*.pyc
*.zip
*.out
\ No newline at end of file
# skill_tree_python
Python 技能树开放编辑仓库
\ No newline at end of file
Python 技能树开放编辑仓库
## 初始化
```
pip install -r requirement.txt
```
## 目录结构说明
* 技能树`骨架文件`
* 位置:`data/tree.json`
* 说明:该文件是执行 `python main.py` 生成的,请勿人工编辑
* 技能树`根节点`配置文件:
* 位置:`data/config.json`
* 说明:可编辑配置关键词等字段,其中 `node_id` 字段是生成的,请勿编辑
* 技能树`难度节点`
* 位置:`data/xxx`,例如: `data/1.python初阶`
* 说明:
* 每个技能树有 3 个等级,目录前的序号是必要的,用来保持文件夹目录的顺序
* 每个目录下有一个 `config.json` 可配置关键词信息,其中 `node_id` 字段是生成的,请勿编辑
* 技能树`章节点`
* 位置:`data/xxx/xxx`,例如:`data/1.python初阶/1.预备知识`
* 说明:
* 每个技能树的每个难度等级有 n 个章节,目录前的序号是必要的,用来保持文件夹目录的顺序
* 每个目录下有一个 `config.json` 可配置关键词信息,其中 `node_id` 字段是生成的,请勿编辑
* 技能树`知识节点`
* 位置:`data/xxx/xxx`,例如:`data/1.python初阶/1.预备知识`
* 说明:
* 每个技能树的每章有 n 个知识节点,目录前的序号是必要的,用来保持文件夹目录的顺序
* 每个目录下有一个 `config.json`
* 其中 `node_id` 字段是生成的,请勿编辑
* 其中 `keywords` 可配置关键字字段
* 其中 `children` 可配置该`知识节点`下的子树结构信息,参考后面描述
* 其中 `export` 可配置该`知识节点`下的导出习题信息,参考后面描述
## `知识节点` 子树信息结构
例如 `data/1.python初阶/1.预备知识/1.Python简介/config.json` 里配置对该知识节点子树信息结构:
```json
{
// ...
"children": [
{
"Python都能做什么": {
"keywords": [],
"children": [],
"node_id": "python-4-0"
}
},
{
"python起源和发展": {
"keywords": [],
"children": [],
"node_id": "python-insert-2"
}
},
{
"python语言特点": {
"keywords": [],
"children": [],
"node_id": "python-insert-3"
}
}
],
}
```
## `知识节点` 的习题配置规则(一)
例如 `data/1.python初阶/1.预备知识/1.Python简介/config.json` 里配置对该知识节点导出的习题
```json
{
// ...
"export": [
"helloworld.json"
// ...
]
}
```
其中`"helloworld.json"`是习题的配置文件,文件如下:
```json
{
"exercise_id": 33,
"source": "helloworld.py",
"depends": [],
"multiline": [
{
"str1 = \"Hello,\"": "",
"str2 = \"World!\"": "str = \"Hello,World!\"",
"print('str1'+'str2')": "print(str)"
},
{
"str1 = \"Hello,\"": "",
"str2 = \"World!\"": "",
"print('str1'+'str2')": "print(\"Hello\" +\",\" +\"World!\")"
},
{
"print('str1'+'str2')": "print(str1+str2)"
}
]
}
```
其中:
* `"exercise_id"` 是脚本填充的字段,编辑的时候不用填写
* `"source"` 指定了习题的来源文件
* `"depends"`: 指定了习题依赖的同知识点下的其他源文件
* `"multiline"`: 是习题选项替换的规则之一
#### 习题源代码
`"helloworld.py"`的源代码如下:
```python
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Hello World
# 描述:输出 "Hello,World!" 字符串,找出错的那项。
if __name__ == '__main__':
str1 = "Hello,"
str2 = "World!"
print('str1'+'str2')
```
源代码的头部注释固定格式,配置`作者`,`标题`,`描述`,其中描述可以是MarkDown格式
#### 习题源代码替换规则
对于习题代码 `"helloworld.py"` 的代码变种替换规则配置在`"helloworld.json"` 里面。有两种替换规则:
**单行替换规则**
* 配置由`one_line`字段指定的单行替换字典
* 格式是:`"<源字符串>"`: [`"<替换字符串A>"`, `<替换字符串B>`,...],
* 其中每个 `"<源字符串>"` `/` `"<替换字符串A>"` 被生成为是一个替换选项
* 指定的配置应该能至少生成 `3+` 个替换选项
```json
{
"one_line": {
"printf": ["print"],
"return 0;": ["return 0"],
"(\"Hello,Wrold!\")": [" \"Hello,Wrold!\""]
}
}
```
上面的替换规则会将代码替换成 3 个变种的代码:
```c
// 变种代码1
#include <stdio.h>
int main(int argc, char** argv){
print("Hello,Wrold!");
return 0;
}
```
```c
// 变种代码2
#include <stdio.h>
int main(int argc, char** argv){
print("Hello,Wrold!");
return 0
}
```
```c
// 变种代码3
#include <stdio.h>
int main(int argc, char** argv){
print "Hello,Wrold!";
return 0
}
```
这些变种代码将会作为技能树该知识点该代码选择题的选项。
**多行替换规则**
* 配置由`multiline`字段指定的多行替换数组
* 数组的每个元素是一组替换规则,会整组被替换
例如:
```json
{
"multiline": [
{
"printf": "print"
},
{
"int main(int argc, char** argv){" : "int main(char** argv){",
"return 0;" : "return 0",
},
{
"#include <stdio.h>": ""
}
]
}
```
同样,该配置将支持将源代码生成3个变种代码
```c
// 变种代码1
#include <stdio.h>
int main(int argc, char** argv){
print("Hello,Wrold!");
return 0;
}
```
```c
// 变种代码2, 注意第2组替换规则,包含了两行替换
#include <stdio.h>
int main(char** argv){
print("Hello,Wrold!");
return 0
}
```
```c
// 变种代码3
int main(int argc, char** argv){
print("Hello,Wrold!");
return 0;
}
```
## `知识节点` 的习题配置规则(二)
另外一种更便利的配置习题的方式是直接基于 MarkDown 格式配置。还是以`"helloworld.json"` 为例子展开描述。
此时,`"helloworld.json"` 简化为:
```json
{
"type": "code_options",
"author": "幻灰龙",
"source": "helloworld.md",
}
```
其中 type 字段目前都固定是 `code_options`。根据具体情况写好其它字段,注意这里 `source` 的文件名,我们指定了一个 Markdown 文件。现在我们新建一个 `helloworld.md` 并编辑为:
````markdown
# Hello World
输出 "Hello,World!" 字符串,找出错的那项。
## 答案
```python
if __name__ == '__main__':
str1 = "Hello,"
str2 = "World!"
print('str1'+'str2')
```
## 选项
### 两字符串变量拼接
```python
if __name__ == '__main__':
str1 = "Hello,"
str2 = "World!"
print(str1+str2)
```
### 两字符串字面量拼接
```python
if __name__ == '__main__':
print("Hello,"+"World!")
```
### 两字符串字面量拼接成字符串变量
```python
if __name__ == '__main__':
str = "Hello," + "World!"
print(str)
```
````
这是一个最基本的习题结构,它包含标题、答案、选项,注意这几个一级和二级标题必须填写正确,解释器会读取这几个标题。而选项的标题会被直接忽略掉,在
最终生成的习题中不包含选项的三级标题,所以这个标题可以用来标注一些编辑信息,例如“此选项没有关闭文件连接”,“类型错误”等等。
## 习题配置规则的选择
我们介绍了两种习题编辑规则,目前Python技能树这里大量使用了规则(一),这是历史编辑遗留。新增习题建议使用规则(二),更简洁方便。
## 技能树合成
在根目录下执行 `python main.py` 会合成技能树文件,合成的技能树文件: `data/tree.json`
* 合成过程中,会自动检查每个目录下 `config.json` 里的 `node_id` 是否存在,不存在则生成
* 合成过程中,会自动检查每个知识点目录下 `config.json` 里的 `export` 里导出的习题配置,检查是否存在`exercise_id` 字段,如果不存在则生成
{
"export": [
"helloworld.json"
],
"keywords": [],
"children": [
{
"Python都能做什么": {
"keywords": [],
"children": [],
"node_id": "python-4-0"
}
},
{
"python起源和发展": {
"keywords": [],
"children": [],
"node_id": "python-insert-2"
}
},
{
"python语言特点": {
"keywords": [],
"children": [],
"node_id": "python-insert-3"
}
}
],
"node_id": "python-3-0"
}
\ No newline at end of file
{
"exercise_id": 33,
"source": "helloworld.py",
"depends": [],
"multiline": [
{
"str1 = \"Hello,\"": "",
"str2 = \"World!\"": "str = \"Hello,World!\"",
"print('str1'+'str2')": "print(str)"
},
{
"str1 = \"Hello,\"": "",
"str2 = \"World!\"": "",
"print('str1'+'str2')": "print(\"Hello\" +\",\" +\"World!\")"
},
{
"print('str1'+'str2')": "print(str1+str2)"
}
],
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Hello World
# 描述:输出 "Hello,World!" 字符串,找出错的那项。
if __name__ == '__main__':
str1 = "Hello,"
str2 = "World!"
print('str1'+'str2')
{
"one_line": {
"print(\"\".join(result))": [
"print(\",\".join(result))",
"print(result)"
],
"result.append(token[\"key\"])": [
"result.append(token)"
]
},
"source": "class.py",
"depends": [],
"exercise_id": 26,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:面向对象
# 描述:使用 Python Class 组织代码,输出“猴子在苹果树下打架”
class Monkey(object):
def __init__(self) -> None:
super().__init__()
self.tokens = []
def add_action_token(self, token):
self.tokens.append({"key": token})
def play(self):
result = []
for token in self.tokens:
result.append(token["key"])
print("".join(result))
def main():
m = Monkey()
m.add_action_token("猴子在")
m.add_action_token("苹果树下")
m.add_action_token("打架")
m.play()
if __name__ == '__main__':
main()
{
"export": [
"process.json",
"function.json",
"class.json"
],
"keywords": [],
"children": [],
"node_id": "python-3-1"
}
\ No newline at end of file
{
"one_line": {
"token[\"key\"] ": [
"token"
],
"print(\"\".join(result))": [
"print(result)",
"print(\"\".join(result))"
],
"dump_monkey(tokens):": [
"dumpmonkey(tokens):"
]
},
"source": "function.py",
"depends": [],
"exercise_id": 28,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:函数代码
# 描述:使用 Python 函数 组织代码,输出“猴子在苹果树下打架”
def dump_monkey(tokens):
result = []
for token in tokens:
result.append(token["key"])
print("".join(result))
if __name__ == '__main__':
tokens = [
{
"key": "猴子在"
},
{
"key": "苹果树下"
},
{
"key": "打架"
}
]
dump_monkey(tokens)
{
"one_line": {
"token[\"key\"] ": [
"token"
],
"print(\"\".join(result))": [
"print(result)"
],
"for token in tokens": [
"for t in tokens"
],
"result = []": [
"result = {}"
]
},
"source": "process.py",
"depends": [],
"exercise_id": 27,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:基本的 Python 处理
# 描述:Python 过程性代码,用循环合并“猴子在苹果树下打架”文本并打印
if __name__ == '__main__':
tokens = [
{
"key": "猴子在"
},
{
"key": "苹果树下"
},
{
"key": "打架"
}
]
result = []
for token in tokens:
result.append(token["key"])
print("".join(result))
{
"export": [
"install.json"
],
"keywords": [],
"children": [
{
"Windows安装Python": {
"keywords": [],
"children": [],
"node_id": "python-4-1"
}
},
{
"Linux安装Python": {
"keywords": [],
"children": [],
"node_id": "python-4-2"
}
},
{
"MacOS安装Python": {
"keywords": [],
"children": [],
"node_id": "python-4-3"
}
}
],
"node_id": "python-3-2"
}
\ No newline at end of file
{
"one_line": {
"while True:": [
"while:"
],
"target = install.get(ret)": [
"target = install[ret]"
],
"if target is None:": [
"if target is not None:"
],
"print(\"* {}: {} 系统\".format(p, install[p]['platform']))": [
"print(\"* {}: {} 系统\".format(p))"
]
},
"source": "install.py",
"depends": [],
"exercise_id": 31,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:安装Python
# 描述:交互式提示程序,用户选择对应平台后,能正确输出对应平台上的安装包下载连接
def test():
install = {
"w": {
"platform": "Window",
"desc": "请下载 Windows 安装包安装:https://www.python.org/downloads/windows/"
},
"l": {
"platform": "Linux",
"desc": "请下载 Linux 的 Python 源码安装:https://www.python.org/downloads/source/",
},
"m": {
"platform": "MacOS",
"desc": "请下载 Mac 的安装包:https://www.python.org/downloads/macos/,或者使用 brew install python 安装",
}
}
print("Python 可以在全平台上安装,例如:")
for p in install:
print("* {}: {} 系统".format(p, install[p]['platform']))
while True:
ret = input("请选择安装平台[w/l/m]:")
target = install.get(ret)
if target is None:
print("不支持的平台")
else:
print("安装平台:{}".format(target['platform']))
print("安装说明:{}".format(target['desc']))
break
if __name__ == '__main__':
test()
{
"export": [
"run.json"
],
"keywords": [],
"children": [
{
"以交互方式运行": {
"keywords": [],
"children": [],
"node_id": "python-4-4"
}
},
{
"以脚本方式运行": {
"keywords": [],
"children": [],
"node_id": "python-4-5"
}
}
],
"node_id": "python-3-3"
}
\ No newline at end of file
{
"one_line": {
"break": [
"continue"
],
"if has_learn_i and has_learn_f": [
"if has_learn_i or has_learn_f"
],
"elif": [
"else if"
],
"item = run.get(ret)": [
"item = run[ret]"
]
},
"source": "run.py",
"depends": [],
"exercise_id": 32,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 运行方式
# 描述:提示用户选择想要了解的Python 的两种运行方式,选择后输出对应方式的基本说明,最后输出用户学习了几种运行方式
def test():
run = {
"i": {
"title": "交互式编程( Interactive )",
"desc": [
"打开终端,输入 python 回车",
"进入 Python 交互式命令行",
"输入 print('monkey king is coding!')"
]
},
"f": {
"title": "Python 源代源文件( File )",
"desc": [
"使用你喜欢的编辑器拷贝本练习的代码, 保存为run.py",
"打开终端,cd 到 run.py 保存的目录",
"输入 python run.py"
]
}
}
print("有两种基本的方式运行 Python")
for s in run:
item = run.get(s)
print("* {}: {}".format(s, item['title']))
has_learn_i = False
has_learn_f = False
while True:
ret = input("请选择你想了解的 Python 运行方式(输入:i/f选择,输入 q 退出):")
if ret == 'q':
break
elif ret == 'i':
has_learn_i = True
elif ret == 'f':
has_learn_f = True
item = run.get(ret)
if item is None:
print("[错误] 不支持的运行方式")
else:
desc = item['desc']
for i in range(0, len(desc)):
print("{}. {}".format(i, desc[i]))
if has_learn_i and has_learn_f:
print("[2/2]您已完成两种 Python 运行方式的学习")
elif has_learn_f:
print("[1/2]您已完成 Python 源代码方式运行学习")
elif has_learn_i:
print("[1/2]您已完成 Python 交互式命令行运行学习")
else:
print("[0/2]您似乎跳过了运行方式的学习?期待下次光临!")
if __name__ == '__main__':
test()
{
"export": [
"ide.json"
],
"keywords": [],
"children": [
{
"typing --- 类型提示支持": {
"keywords": [],
"children": [],
"node_id": "python-4-6"
}
},
{
"pydoc --- 文档生成器和在线帮助系统": {
"keywords": [],
"children": [],
"node_id": "python-4-7"
}
},
{
"Python Development Mode": {
"keywords": [],
"children": [],
"node_id": "python-4-8"
}
},
{
"Effects of the Python Development Mode": {
"keywords": [],
"children": [],
"node_id": "python-4-9"
}
},
{
"ResourceWarning Example": {
"keywords": [],
"children": [],
"node_id": "python-4-10"
}
},
{
"Bad file descriptor error example": {
"keywords": [],
"children": [],
"node_id": "python-4-11"
}
},
{
"doctest --- 测试交互性的Python示例": {
"keywords": [],
"children": [],
"node_id": "python-4-12"
}
},
{
"unittest --- 单元测试框架": {
"keywords": [],
"children": [],
"node_id": "python-4-13"
}
},
{
"unittest.mock --- mock对象库": {
"keywords": [],
"children": [],
"node_id": "python-4-14"
}
},
{
"unittest.mock上手指南": {
"keywords": [],
"children": [],
"node_id": "python-4-15"
}
},
{
"2to3 - 自动将Python 2 代码转为Python 3 代码": {
"keywords": [],
"children": [],
"node_id": "python-4-16"
}
},
{
"test --- Python回归测试包": {
"keywords": [],
"children": [],
"node_id": "python-4-17"
}
},
{
"test.support --- Utilities for the Python test suite": {
"keywords": [],
"children": [],
"node_id": "python-4-18"
}
},
{
"test.support.socket_helper --- Utilities for socket tests": {
"keywords": [],
"children": [],
"node_id": "python-4-19"
}
},
{
"test.support.script_helper --- Utilities for the Python execution tests": {
"keywords": [],
"children": [],
"node_id": "python-4-20"
}
},
{
"test.support.bytecode_helper --- Support tools for testing correct bytecode generation": {
"keywords": [],
"children": [],
"node_id": "python-4-21"
}
},
{
"使用PyCharm开发Python": {
"keywords": [],
"children": [],
"node_id": "python-insert-0"
}
},
{
"使用VSCode开发Python": {
"keywords": [],
"children": [],
"node_id": "python-insert-1"
}
}
],
"node_id": "python-3-4"
}
\ No newline at end of file
{
"one_line": {
"break": [
"continue"
],
"ide = ide_dict.get(ret)": [
"ide = ide_dict[ret]"
],
"if ide is None": [
"if ide is not None"
],
"input": [
"print"
],
"format(ide['desc'])": [
"format(ide['link'])"
]
},
"source": "ide.py",
"depends": [],
"exercise_id": 30,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python IDE
# 描述:提示用户选择想要了解的IDE,输出对应 IDE 介绍
def test():
ide_list = [
{
"shutcut": 'i',
"name": "IDEL",
"link": "https://docs.python.org/3/library/idle.html",
"desc": "Python 内置的IDE,功能比较一般"
},
{
"shutcut": 'v',
"name": "VIM",
"link": "http://www.vim.org/",
"desc": "如果你是个VIM爱好者,可以用VIM编写Python,但是Python的缩进处理会比较麻烦。当然,你在 Linux 服务器上的时候有时候就只能用VI/VIM了。"
},
{
"shutcut": 'c',
"name": "VSCode",
"link": "https://code.visualstudio.com/",
"desc": "VSCode 对Python的支持非常友好,配合几个插件后几乎是对 Python 开发最友好的IDE了"
},
{
"shutcut": 'p',
"name": "PyCharm",
"link": "https://www.jetbrains.com/pycharm/",
"desc": "jetbrains 出品的 PyCharm 也是 Python 开发者常用的IDE"
}
]
ide_dict = {}
short_cuts = []
for ide in ide_list:
s = ide['shutcut']
ide_dict[s] = ide
short_cuts.append(s)
print("常见的 Python IDE 列表:")
for s in ide_dict:
print('* {}: {}'.format(s, ide_dict[s]['name']))
while True:
print('')
ret = input("请选择你喜欢的IDE({}, q退出):".format('/'.join(short_cuts)))
if ret == 'q':
break
ide = ide_dict.get(ret)
if ide is None:
print("[错误] 不支持的选项")
else:
print("* IDE: {}".format(ide['name']))
print("* 链接: {}".format(ide['link']))
print("* 描述: {}".format(ide['desc']))
if __name__ == '__main__':
test()
{
"export": [
"style.json"
],
"keywords": [],
"children": [],
"node_id": "python-3-5"
}
\ No newline at end of file
{
"one_line": {
"type(node) == type('')": [
"type(node) == type([])"
],
"dump_markdown_list(node, depth+1)": [
"dump_markdown_list(node, depth)"
],
"print('{}* {}'.format(' '*depth, node))": [
"print('{}* {}'.format(' '**depth))"
]
},
"source": "style.py",
"depends": [],
"exercise_id": 29,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 编码规范
# 描述:输出MarkDown风格列表结构的 “学习 Python 编码规范以及相关工具和插件” 的介绍信息
def dump_markdown_list(tree, depth):
for node in tree:
if type(node) == type(''):
print('{}* {}'.format(' '*depth, node))
else:
print('')
dump_markdown_list(node, depth+1)
print('')
if __name__ == '__main__':
dump_markdown_list([
'Python 编码风格',
[
'请阅读 Google Python 编码风格指南:',
'[google-python-styleguide_zh_cn](https://zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_style_rules /)'
],
'代码风格和自动完成工具链',
[
'推荐使用 VSCode 开发',
'请安装 VSCode 插件',
[
'Pylance',
'Python Path',
'请安装 pylint',
]
]
], 0)
{
"export": [
"import_json_example.json",
"import_other_module.json",
"import_with_from.json"
],
"keywords": [],
"children": [
{
"pip": {
"keywords": [],
"children": [],
"node_id": "python-4-22"
}
}
],
"node_id": "python-3-6"
}
\ No newline at end of file
{
"one_line": {
"import json": [
"#include <json>;",
"using json;",
"import json;"
]
},
"source": "import_json_example.py",
"depends": [],
"exercise_id": 163,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 模块导入1
# 描述:通过 import 导入Python内置库
import json
def dump_json(obj):
print(json.dumps(obj))
if __name__ == '__main__':
obj = {
'err': 'success',
'result': ['item1', 'item2']
}
dump_json(obj)
{
"multiline": [
{
"import import_json_example": "import import_json_example.dump_json",
"import_json_example.dump_json": "dump_json"
},
{
"import_json_example.dump_json": "dump_json"
},
{
"import import_json_example": "import import_json_example;"
}
],
"source": "import_other_module.py",
"depends": [
"import_json_example.py"
],
"exercise_id": 164,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 模块导入2
# 描述:通过 import 导入自己写的另外一个模块
import import_json_example
if __name__ == '__main__':
obj = {
'err': 'success',
'result': ['item1', 'item2']
}
import_json_example.dump_json(obj)
{
"multiline": [
{
"from import_json_example import dump_json": "from import_json_example import import_json_example",
"dump_json": "import_json_example.dump_json"
},
{
"from import_json_example import dump_json": "import dump_json from import_json_example"
},
{
"dump_json(obj)": "import_json_example.dump_json(obj)"
}
],
"source": "import_with_from.py",
"depends": [
"import_json_example.py"
],
"exercise_id": 165,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 模块导入3
# 描述:通过 from 语句导入模块
from import_json_example import dump_json
if __name__ == '__main__':
obj = {
'err': 'success',
'result': ['item1', 'item2']
}
dump_json(obj)
{
"keywords": [],
"node_id": "python-2-0"
}
\ No newline at end of file
{
"export": [
"step.json"
],
"keywords": [],
"children": [
{
"建议使用4个空格": {
"keywords": [],
"children": [],
"node_id": "python-4-23"
}
}
],
"node_id": "python-3-7"
}
\ No newline at end of file
{
"source": "step.py",
"depends": [],
"exercise_id": 83,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 缩进规则
# 描述:Python 的缩进在一个源文件里要保持一致,例如都是4个空格或者都是2个空格
# 全局代码
for i in range(0, 10):
print('global code: {}'.format(i))
if i == 5:
print('global code: *')
# 函数
def test():
for i in range(0, 10):
print('function code: {}'.format(i))
if i == 5:
print('function code: *')
# 类
class Test():
def __init__(self) -> None:
pass
def test(self):
for i in range(0, 10):
print('member function code: {}'.format(i))
if i == 5:
print('member function code: *')
if __name__ == '__main__':
test()
t = Test()
t.test()
{
"export": [
"util_builtin_01.json",
"util_builtin_02.json",
"input.json"
],
"keywords": [],
"children": [
{
"help": {
"keywords": [],
"children": [],
"node_id": "python-4-143"
}
},
{
"input": {
"keywords": [],
"children": [],
"node_id": "python-4-144"
}
},
{
"print": {
"keywords": [],
"children": [],
"node_id": "python-4-145"
}
},
{
"range": {
"keywords": [],
"children": [],
"node_id": "python-4-146"
}
},
{
"format": {
"keywords": [],
"children": [],
"node_id": "python-4-147"
}
},
{
"len": {
"keywords": [],
"children": [],
"node_id": "python-4-148"
}
},
{
"sorted": {
"keywords": [],
"children": [],
"node_id": "python-4-149"
}
},
{
"open": {
"keywords": [],
"children": [],
"node_id": "python-4-150"
}
},
{
"dir": {
"keywords": [],
"children": [],
"node_id": "python-4-151"
}
},
{
"enumerate/zip": {
"keywords": [],
"children": [],
"node_id": "python-4-152"
}
},
{
"type/isinstance": {
"keywords": [],
"children": [],
"node_id": "python-4-153"
}
},
{
"min/max/sum": {
"keywords": [],
"children": [],
"node_id": "python-4-154"
}
},
{
"abs/round/pow/divmod": {
"keywords": [],
"children": [],
"node_id": "python-4-155"
}
}
],
"node_id": "python-3-16"
}
\ No newline at end of file
{
"one_line": {
"random.randint": [
"random.rand"
],
"continue": [
"break"
],
"if n == v": [
"if n != v"
],
"elif n < v": [
"elif n > v"
],
"elif n > v": [
"elif n < v"
],
"input": [
"scanf",
"readline"
]
},
"source": "input.py",
"depends": [],
"exercise_id": 91,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 基本输入
# 描述:心里想一个0-100之间的数字,让用户猜,如果小了或者大了,就给予提示,直到用户猜到
import random
def gauss_num():
v = random.randint(0, 100)
while True:
n = input("请输入0-100之间的一个数字:")
try:
n = int(n)
except:
print("无效的数字")
continue
if n == v:
print("你猜对了!")
break
elif n < v:
print("你猜错了,我想到的数字比{}大".format(n))
elif n > v:
print("你猜错了,我想到的数字比{}小".format(n))
if __name__ == '__main__':
gauss_num()
{
"one_line": {
"abs(-1) == 1": [
"abs(-1) != 1"
],
"max(10, 100) == 100": [
"max(10, 100) == 10"
],
"min(10, 100) == 10": [
"min(10, 100) == 100"
],
"round(3.5) == 3": [
"round(3.5) == 4"
],
"pow(2, 4) == 16": [
"pow(2, 4) == 8"
],
"all([True, True]) == True": [
"all([True, False]) == True"
],
"any([True, False]) == True": [
"any([True, True]) == True"
],
"bin(2) == '0b10'": [
"bin(2) == '0b00'"
],
"bool({}) == False": [
"bool({}) == True"
],
"bytes([0, 1]) == b'\\x00\\x01'": [
"bytes([0, 1]) == b'\\x01\\x01'"
],
"str(1) == '1'": [
"str(1) == 1"
],
"callable(abs) == True": [
"callable(abs) == False"
],
"chr(97) == 'a'": [
"chr(97) == 'b'"
],
"'count' in dir([])": [
"'count' not in dir([])"
],
"float(1) == 1.0": [
"float(1) == 1.01"
],
"'a, b, c'": [
"'a.b.c'"
],
"hash(chr(97))": [
"hash(chr(98))"
],
"'0x1'": [
"'0x0'"
],
"type({}) ==": [
"type([]) =="
]
},
"source": "util_builtin_01.py",
"depends": [],
"exercise_id": 230,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 常用内置方法(1)
# 描述:内置函数基本用例,基本测试+执行,,每个 assert 结果都是 True
if __name__ == '__main__':
assert abs(-1) == 1
assert max(10, 100) == 100
assert min(10, 100) == 10
assert round(3.5) == 3
assert pow(2, 4) == 16
assert all([True, True]) == True
assert any([True, False]) == True
assert ascii('你') == "'\\u4f60'"
assert bin(2) == '0b10'
assert bool({}) == False
assert bytes([0, 1]) == b'\x00\x01'
assert str(1) == '1'
assert callable(abs) == True
assert chr(97) == 'a'
assert 'count' in dir([])
assert float(1) == 1.0
assert '{}, {}, {}'.format('a', 'b', 'c') == 'a, b, c'
assert hash("a") == hash(chr(97))
assert hex(1) == '0x1'
assert type({}) == type({'a': 1})
{
"one_line": {
"assert index == [0, 1, 2]": [
"assert index == [1, 2, 3]"
],
"assert sum([1, -1, 3] == 3)": [
"assert sum([1, -1, 3] == 5)"
],
"(1, 4), (2, 5), (3, 6)": [
"1, 4, 2, 5, 3, 6"
],
"assert len(seasons) == 4": [
"assert seasons.length == 4"
],
"assert shortcut == ['S', 'S', 'F', 'W']": [
"assert shortcut == ['S', 'F', 'W']"
],
"len(non_s_seasons) == 2": [
"len(non_s_seasons) == 3"
],
"assert len(s_seasons) == 2": [
"assert len(s_seasons) == 1"
]
},
"source": "util_builtin_02.py",
"depends": [],
"exercise_id": 236,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 常用内置方法(2)
# 描述:列表相关内置函数基本用例,基本测试+执行,每个 assert 结果都是 True
if __name__ == '__main__':
index = []
for i in range(0, 3):
index.append(i)
assert index == [0, 1, 2]
assert sum([1, -1, 3] == 3)
x = [1, 2, 3]
y = [4, 5, 6]
zipped = zip(x, y)
assert list(zipped) == [(1, 4), (2, 5), (3, 6)]
seasons = ['Spring', 'Summer', 'Fall', 'Winter']
assert len(seasons) == 4
shortcut = list(map(lambda s: s[0], seasons))
assert shortcut == ['S', 'S', 'F', 'W']
non_s_seasons = filter(lambda s: s[0] == 'S', seasons)
assert len(non_s_seasons) == 2
s_seasons = list(filter(lambda s: s[0] == 'S', seasons))
assert len(s_seasons) == 2
{
"export": [],
"keywords": [],
"children": [
{
"总体概念": {
"keywords": [],
"children": [
{
"迭代": {
"keywords": [],
"children": [],
"node_id": "python-5-1358"
}
},
{
"缓存": {
"keywords": [],
"children": [],
"node_id": "python-5-1359"
}
},
{
"透明度": {
"keywords": [],
"children": [],
"node_id": "python-5-1360"
}
}
],
"node_id": "python-4-1423"
}
},
{
"流程控制": {
"keywords": [],
"children": [
{
"捕获异常": {
"keywords": [],
"children": [],
"node_id": "python-5-1361"
}
},
{
"异常链": {
"keywords": [],
"children": [],
"node_id": "python-5-1362"
}
},
{
"当一切顺利的时候": {
"keywords": [],
"children": [],
"node_id": "python-5-1363"
}
},
{
"继续执行,无论异常如何": {
"keywords": [],
"children": [],
"node_id": "python-5-1364"
}
},
{
"优化循环": {
"keywords": [],
"children": [],
"node_id": "python-5-1365"
}
},
{
"with语句": {
"keywords": [],
"children": [],
"node_id": "python-5-1366"
}
},
{
"条件表达式": {
"keywords": [],
"children": [],
"node_id": "python-5-1367"
}
}
],
"node_id": "python-4-1424"
}
},
{
"迭代": {
"keywords": [],
"children": [
{
"序列解包": {
"keywords": [],
"children": [],
"node_id": "python-5-1368"
}
},
{
"列表解析式": {
"keywords": [],
"children": [],
"node_id": "python-5-1369"
}
},
{
"生成器表达式": {
"keywords": [],
"children": [],
"node_id": "python-5-1370"
}
},
{
"集合解析式": {
"keywords": [],
"children": [],
"node_id": "python-5-1371"
}
},
{
"字典解析式": {
"keywords": [],
"children": [],
"node_id": "python-5-1372"
}
},
{
"将迭代器链接在一起": {
"keywords": [],
"children": [],
"node_id": "python-5-1373"
}
},
{
"将迭代器压缩在一起": {
"keywords": [],
"children": [],
"node_id": "python-5-1374"
}
}
],
"node_id": "python-4-1425"
}
},
{
"容器数据类型": {
"keywords": [],
"children": [
{
"集合": {
"keywords": [],
"children": [],
"node_id": "python-5-1375"
}
},
{
"命名元组": {
"keywords": [],
"children": [],
"node_id": "python-5-1376"
}
},
{
"有序字典": {
"keywords": [],
"children": [],
"node_id": "python-5-1377"
}
},
{
"带有默认值的字典": {
"keywords": [],
"children": [],
"node_id": "python-5-1378"
}
}
],
"node_id": "python-4-1426"
}
},
{
"导入代码": {
"keywords": [],
"children": [
{
"回退(备用)导入": {
"keywords": [],
"children": [],
"node_id": "python-5-1379"
}
},
{
"从即将发布的版本中导入": {
"keywords": [],
"children": [],
"node_id": "python-5-1380"
}
},
{
"使用__all__进行自定义导入": {
"keywords": [],
"children": [],
"node_id": "python-5-1381"
}
},
{
"相对导入": {
"keywords": [],
"children": [],
"node_id": "python-5-1382"
}
},
{
"__import__函数": {
"keywords": [],
"children": [],
"node_id": "python-5-1383"
}
},
{
"importlib模块": {
"keywords": [],
"children": [],
"node_id": "python-5-1384"
}
}
],
"node_id": "python-4-1427"
}
},
{
"令人兴奋的Python扩展:NIST的随机数信标": {
"keywords": [],
"children": [
{
"如何安装NIST信标库": {
"keywords": [],
"children": [],
"node_id": "python-5-1385"
}
},
{
"示例:获取值": {
"keywords": [],
"children": [],
"node_id": "python-5-1386"
}
},
{
"示例:模拟抛硬币并记录每次正反面的结果": {
"keywords": [],
"children": [],
"node_id": "python-5-1387"
}
},
{
"安装Pandas和matplotlib": {
"keywords": [],
"children": [],
"node_id": "python-5-1388"
}
},
{
"创建文本文件": {
"keywords": [],
"children": [],
"node_id": "python-5-1389"
}
},
{
"使用Pandas显示数据": {
"keywords": [],
"children": [],
"node_id": "python-5-1390"
}
},
{
"进行一些数据分析": {
"keywords": [],
"children": [],
"node_id": "python-5-1391"
}
},
{
"使用matplotlib进行绘图": {
"keywords": [],
"children": [],
"node_id": "python-5-1392"
}
},
{
"图表的类型": {
"keywords": [],
"children": [],
"node_id": "python-5-1393"
}
},
{
"安装Scrapy": {
"keywords": [],
"children": [],
"node_id": "python-5-1394"
}
},
{
"运行Scrapy": {
"keywords": [],
"children": [],
"node_id": "python-5-1395"
}
},
{
"使用Scrapy获取Web数据": {
"keywords": [],
"children": [],
"node_id": "python-5-1396"
}
},
{
"通过Scrapy浏览网页": {
"keywords": [],
"children": [],
"node_id": "python-5-1397"
}
},
{
"shell选项": {
"keywords": [],
"children": [],
"node_id": "python-5-1398"
}
},
{
"安装Beautiful Soup": {
"keywords": [],
"children": [],
"node_id": "python-5-1399"
}
},
{
"使用Beautiful Soup": {
"keywords": [],
"children": [],
"node_id": "python-5-1400"
}
},
{
"如何安装feedparser库": {
"keywords": [],
"children": [],
"node_id": "python-5-1401"
}
},
{
"如何使用feedparser库": {
"keywords": [],
"children": [],
"node_id": "python-5-1402"
}
},
{
"安装NumPy": {
"keywords": [],
"children": [],
"node_id": "python-5-1403"
}
},
{
"使用NumPy": {
"keywords": [],
"children": [],
"node_id": "python-5-1404"
}
},
{
"使用NumPy数组": {
"keywords": [],
"children": [],
"node_id": "python-5-1405"
}
},
{
"统计度量": {
"keywords": [],
"children": [],
"node_id": "python-5-1406"
}
},
{
"如何安装Pillow库": {
"keywords": [],
"children": [],
"node_id": "python-5-1407"
}
},
{
"图像显示:确定文件的大小和类型并显示": {
"keywords": [],
"children": [],
"node_id": "python-5-1408"
}
},
{
"图像处理:裁剪图像的一部分": {
"keywords": [],
"children": [],
"node_id": "python-5-1409"
}
},
{
"随机数": {
"keywords": [],
"children": [],
"node_id": "python-5-1410"
}
},
{
"密码生成器": {
"keywords": [],
"children": [],
"node_id": "python-5-1411"
}
}
],
"node_id": "python-4-1428"
}
}
],
"node_id": "python-3-8"
}
\ No newline at end of file
{
"source": "call.py",
"depends": [],
"exercise_id": 197,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 函数调用
# 描述:使用函数设计一组 “容器API”:init/set/get/exist
def init():
return {}
def set(dict, key, value):
dict[key] = value
def get(dict, key):
return dict.get(key)
def exist(dict, key):
return dict.get(key) is not None
def test():
dict = init()
for i in range(10):
key = "key_{}".format(i)
value = i
set(dict, key, value)
test_key = "key_4"
if exist(dict, test_key):
test_value = get(dict, test_key)
print("key is: {}, value is: {}".format(test_key, test_value))
if __name__ == '__main__':
test()
{
"export": [
"helloworld2.json",
"fact1.json",
"fact2.json",
"fibonacci1.json",
"fibonacci2.json",
"call.json"
],
"keywords": [],
"children": [
{
"参数": {
"keywords": [],
"children": [
{
"规划时的灵活性": {
"keywords": [],
"children": [],
"node_id": "python-5-1412"
}
},
{
"可变位置参数": {
"keywords": [],
"children": [],
"node_id": "python-5-1413"
}
},
{
"可变关键字参数": {
"keywords": [],
"children": [],
"node_id": "python-5-1414"
}
},
{
"组合不同类型的参数": {
"keywords": [],
"children": [],
"node_id": "python-5-1415"
}
},
{
"调用具有可变参数的函数": {
"keywords": [],
"children": [],
"node_id": "python-5-1416"
}
},
{
"传递参数": {
"keywords": [],
"children": [],
"node_id": "python-5-1417"
}
},
{
"自省": {
"keywords": [],
"children": [],
"node_id": "python-5-1418"
}
}
],
"node_id": "python-4-1429"
}
},
{
"装饰器": {
"keywords": [],
"children": [
{
"闭包": {
"keywords": [],
"children": [],
"node_id": "python-5-1419"
}
},
{
"包装器": {
"keywords": [],
"children": [],
"node_id": "python-5-1420"
}
},
{
"带参数的装饰器": {
"keywords": [],
"children": [],
"node_id": "python-5-1421"
}
},
{
"带参数或不带参数的装饰器": {
"keywords": [],
"children": [],
"node_id": "python-5-1422"
}
}
],
"node_id": "python-4-1430"
}
},
{
"函数注解": {
"keywords": [],
"children": [
{
"提取样板": {
"keywords": [],
"children": [],
"node_id": "python-5-1423"
}
},
{
"用装饰器进行注解": {
"keywords": [],
"children": [],
"node_id": "python-5-1424"
}
}
],
"node_id": "python-4-1431"
}
},
{
"生成器": {
"keywords": [],
"children": [],
"node_id": "python-4-1432"
}
},
{
"lambda": {
"keywords": [],
"children": [],
"node_id": "python-4-1433"
}
},
{
"自省": {
"keywords": [],
"children": [
{
"标识对象类型": {
"keywords": [],
"children": [],
"node_id": "python-5-1425"
}
},
{
"模块和软件包": {
"keywords": [],
"children": [],
"node_id": "python-5-1426"
}
},
{
"文档字符串": {
"keywords": [],
"children": [
{
"描述函数的作用": {
"keywords": [],
"children": [],
"node_id": "python-6-222"
}
},
{
"返回值": {
"keywords": [],
"children": [],
"node_id": "python-6-223"
}
},
{
"包含预期的异常": {
"keywords": [],
"children": [],
"node_id": "python-6-224"
}
}
],
"node_id": "python-5-1427"
}
}
],
"node_id": "python-4-1434"
}
}
],
"node_id": "python-3-9"
}
\ No newline at end of file
{
"source": "fact1.py",
"depends": [],
"exercise_id": 190,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 函数
# 描述:循环计算阶乘函数
def fact(n):
sum = 1
for i in range(0, n):
sum *= (i+1)
return sum
if __name__ == '__main__':
print(fact(10))
{
"source": "fact2.py",
"depends": [],
"exercise_id": 244,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 函数递归
# 描述:递归计算阶乘函数
def fact(n):
if n == 1:
return 1
return n*fact(n-1)
if __name__ == '__main__':
print(fact(998))
{
"source": "fibonacci1.py",
"depends": [],
"exercise_id": 201,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 函数递归
# 描述:递归计算斐波那契函数
def fibonacci(n):
if n == 1 or n == 2:
return 1
return fibonacci(n-1) + fibonacci(n-2)
if __name__ == '__main__':
print(fibonacci(6))
{
"source": "fibonacci2.py",
"depends": [],
"exercise_id": 239,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 函数+缓存
# 描述:递归计算斐波那契函数, 带缓存
def fibonacci(n, cache):
if n == 1 or n == 2:
return 1
if cache.get(n) is not None:
return cache[n]
cache[n] = fibonacci(n-1, cache) + fibonacci(n-2, cache)
return cache[n]
if __name__ == '__main__':
print(fibonacci(6, {}))
{
"one_line": {
"circulate_print(str, count+1)": [
"circulate_print(str, count)"
],
"if count == 5": [
"if count > 5"
],
"count=0": [
"count"
]
},
"source": "helloworld2.py",
"depends": [],
"exercise_id": 166,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Hello World 进阶
# 描述:使用递归循环输出Hello World的每个字符,循环5次
def circulate_print(str, count=0):
if count == 5:
return
for char in str:
print(char)
circulate_print(str, count+1)
if __name__ == '__main__':
str = "Hello,World!"
circulate_print(str)
{
"export": [
"key_value_set.json",
"hash_key_value_set.json",
"hash_key_value_set_by_compose.json"
],
"keywords": [],
"children": [
{
"继承": {
"keywords": [],
"children": [
{
"多重继承": {
"keywords": [],
"children": [],
"node_id": "python-5-1428"
}
},
{
"方法解析顺序": {
"keywords": [],
"children": [],
"node_id": "python-5-1429"
}
},
{
"使用super函数将控制权传递给其他类": {
"keywords": [],
"children": [],
"node_id": "python-5-1430"
}
},
{
"自省": {
"keywords": [],
"children": [],
"node_id": "python-5-1431"
}
}
],
"node_id": "python-4-1435"
}
},
{
"如何创建类": {
"keywords": [],
"children": [
{
"在运行时创建类": {
"keywords": [],
"children": [],
"node_id": "python-5-1432"
}
},
{
"元类": {
"keywords": [],
"children": [],
"node_id": "python-5-1433"
}
},
{
"控制命名空间": {
"keywords": [],
"children": [],
"node_id": "python-5-1434"
}
}
],
"node_id": "python-4-1436"
}
},
{
"成员变量": {
"keywords": [],
"children": [
{
"属性": {
"keywords": [],
"children": [],
"node_id": "python-5-1435"
}
},
{
"描述器": {
"keywords": [],
"children": [],
"node_id": "python-5-1436"
}
}
],
"node_id": "python-4-1437"
}
},
{
"方法": {
"keywords": [],
"children": [
{
"非绑定方法": {
"keywords": [],
"children": [],
"node_id": "python-5-1437"
}
},
{
"绑定方法": {
"keywords": [],
"children": [],
"node_id": "python-5-1438"
}
}
],
"node_id": "python-4-1438"
}
},
{
"魔术方法": {
"keywords": [],
"children": [
{
"处理成员变量": {
"keywords": [],
"children": [],
"node_id": "python-5-1439"
}
},
{
"字符串表示": {
"keywords": [],
"children": [
{
"字节字符串": {
"keywords": [],
"children": [
{
"借助chr和ord进行简单的转换": {
"keywords": [],
"children": [],
"node_id": "python-7-34"
}
},
{
"借助struct模块进行复杂的转换": {
"keywords": [],
"children": [],
"node_id": "python-7-35"
}
}
],
"node_id": "python-6-225"
}
},
{
"文本": {
"keywords": [],
"children": [
{
"Unicode": {
"keywords": [],
"children": [],
"node_id": "python-7-36"
}
},
{
"编码": {
"keywords": [],
"children": [],
"node_id": "python-7-37"
}
}
],
"node_id": "python-6-226"
}
},
{
"简单的替换": {
"keywords": [],
"children": [],
"node_id": "python-6-227"
}
},
{
"格式化": {
"keywords": [],
"children": [
{
"在对象中查找值": {
"keywords": [],
"children": [],
"node_id": "python-7-38"
}
},
{
"区分字符串类型": {
"keywords": [],
"children": [],
"node_id": "python-7-39"
}
},
{
"标准格式规范": {
"keywords": [],
"children": [],
"node_id": "python-7-40"
}
},
{
"为文本文档制作目录": {
"keywords": [],
"children": [],
"node_id": "python-7-41"
}
},
{
"自定义格式规范": {
"keywords": [],
"children": [],
"node_id": "python-7-42"
}
}
],
"node_id": "python-6-228"
}
}
],
"node_id": "python-5-1440"
}
}
],
"node_id": "python-4-1439"
}
}
],
"node_id": "python-3-10"
}
\ No newline at end of file
{
"multiline": [
{
"hash_set = {}": "",
"hash_set[key] = value": "hash_set[key] = {}"
},
{
"return []": "return None"
},
{
"value = hashset.hget('puzzle', key)": "value = hashset.get('puzzle', key)"
},
{
"hashset.hset('puzzle', 'good', 'bye!')": "hashset.hset('good', 'bye!')"
}
],
"source": "hash_key_value_set.py",
"depends": [
"key_value_set.py"
],
"exercise_id": 169,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 类的基本使用(2)
# 描述:继承容器类,扩展功能,支持类似redis的 hset/hget/hkeys 扩展接口
from key_value_set import KeyValueSet
class HashKeyValueSet(KeyValueSet):
def __init__(self) -> None:
super().__init__()
def hset(self, hash_key, key, value):
hash_set = self.get(hash_key)
if hash_set is None:
hash_set = {}
hash_set[key] = value
self.set(hash_key, hash_set)
else:
hash_set[key] = value
self.set(hash_key, hash_set)
def hget(self, hash_key, key):
hash_set = self.get(hash_key)
if hash_set is None:
return None
else:
return hash_set.get(key)
def hkeys(self, hash_key):
hash_set = self.get(hash_key)
if hash_set is None:
return []
else:
return hash_set.keys()
def test():
hashset = HashKeyValueSet()
hashset.hset('puzzle', 'hello', 'world!')
hashset.hset('puzzle', 'monkey', 'king!')
hashset.hset('puzzle', 'tomorrow', 'is another day')
hashset.hset('puzzle', 'good', 'bye!')
keys = hashset.hkeys('puzzle')
for key in keys:
ret = input("猜一猜下半句是什么? {} -> :".format(key))
value = hashset.hget('puzzle', key)
if ret == value:
print('你太厉害了,这都能猜得到!')
else:
print('哈哈,肯定猜不到得啦:{}->{}'.format(key, value))
if __name__ == '__main__':
test()
{
"one_line": {
"self.kvset.set": [
"self.set"
],
"self.kvset.get": [
"self.get"
],
"KeyValueSet()": [
""
]
},
"source": "hash_key_value_set_by_compose.py",
"depends": [
"key_value_set.py"
],
"exercise_id": 168,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 类的基本使用(3)
# 描述:类的组合,使用组合而非继承实现类的扩展,支持类似redis的 hset/hget/hkeys 扩展接口
from key_value_set import KeyValueSet
class HashKeyValueSet:
def __init__(self, kvset) -> None:
super().__init__()
self.kvset = kvset
def hset(self, hash_key, key, value):
hash_set = self.kvset.get(hash_key)
if hash_set is None:
hash_set = {}
hash_set[key] = value
self.kvset.set(hash_key, hash_set)
else:
hash_set[key] = value
self.kvset.set(hash_key, hash_set)
def hget(self, hash_key, key):
hash_set = self.kvset.get(hash_key)
if hash_set is None:
return None
else:
return hash_set.get(key)
def hkeys(self, hash_key):
hash_set = self.kvset.get(hash_key)
if hash_set is None:
return []
else:
return hash_set.keys()
def test():
hashset = HashKeyValueSet(KeyValueSet())
hashset.hset('puzzle', 'hello', 'world!')
hashset.hset('puzzle', 'monkey', 'king!')
hashset.hset('puzzle', 'tomorrow', 'is another day')
hashset.hset('puzzle', 'good', 'bye!')
keys = hashset.hkeys('puzzle')
for key in keys:
ret = input("猜一猜下半句是什么? {} -> :".format(key))
value = hashset.hget('puzzle', key)
if ret == value:
print('你太厉害了,这都能猜得到!')
else:
print('哈哈,肯定猜不到得啦:{}->{}'.format(key, value))
if __name__ == '__main__':
test()
{
"one_line": {
"self.dict.get(key)": [
"self.dict[key]"
],
"is not None": [
"is None"
],
"len(self.dict.keys())": [
"self.dict.keys().length()"
],
"value = kv.get(key)": [
"value = kv[key]"
],
"if ret == value:": [
"if ret = value:",
"if ret != value:"
]
},
"source": "key_value_set.py",
"depends": [],
"exercise_id": 167,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 类的基本使用(1)
# 描述:编写一个kv容器类,支持 set/get/exist/keys/count 接口
class KeyValueSet:
def __init__(self) -> None:
self.dict = {}
def set(self, key, value):
self.dict[key] = value
def get(self, key):
return self.dict.get(key)
def exist(self, key):
return self.dict.get(key) is not None
def keys(self):
return self.dict.keys()
def count(self):
return len(self.dict.keys())
def test():
kv = KeyValueSet()
kv.set('hello', 'world!')
kv.set('monkey', 'king!')
kv.set('tomorrow', 'is another day')
kv.set('good', 'bye!')
for key in kv.keys():
ret = input("猜一猜下半句是什么? {} -> :".format(key))
value = kv.get(key)
if ret == value:
print('你太厉害了,这都能猜得到!')
else:
print('哈哈,肯定猜不到得啦:{}->{}'.format(key, value))
if __name__ == '__main__':
test()
{
"source": "calc.py",
"depends": [],
"exercise_id": 76,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 顺序语句
# 描述:一行一个语句,计算长方形面积
def test():
print("* 如果想计算一个长方形的面积")
print("* 那就先定义长宽,例如:x = 10, y=20")
x = 10
y = 20
print("* 紧接着,我们计算长方形的面积:s = x * y")
s = x * y
print("* 现在可以输出结果了,该长方形的面积是:{}".format(s))
if __name__ == '__main__':
test()
{
"export": [
"calc.json"
],
"keywords": [],
"children": [],
"node_id": "python-3-11"
}
\ No newline at end of file
{
"export": [
"if-else.json",
"none.json"
],
"keywords": [],
"children": [
{
"if": {
"keywords": [],
"children": [
{
"if-else语句": {
"keywords": [],
"children": [],
"node_id": "python-5-64"
}
},
{
"if-elif-else结构": {
"keywords": [],
"children": [],
"node_id": "python-5-65"
}
},
{
"省略else代码块": {
"keywords": [],
"children": [],
"node_id": "python-5-66"
}
},
{
"使用if语句处理列表": {
"keywords": [],
"children": [
{
"检查特殊元素": {
"keywords": [],
"children": [],
"node_id": "python-6-16"
}
},
{
"确定列表不是空的": {
"keywords": [],
"children": [],
"node_id": "python-6-17"
}
}
],
"node_id": "python-5-67"
}
},
{
"设置if语句的格式": {
"keywords": [],
"children": [],
"node_id": "python-5-68"
}
}
],
"node_id": "python-4-96"
}
},
{
"elif": {
"keywords": [],
"children": [],
"node_id": "python-4-97"
}
},
{
"else": {
"keywords": [],
"children": [],
"node_id": "python-4-98"
}
},
{
"条件判断": {
"keywords": [],
"children": [],
"node_id": "python-4-99"
}
},
{
"这正是布尔值的用武之地": {
"keywords": [],
"children": [],
"node_id": "python-4-100"
}
},
{
"代码块嵌套": {
"keywords": [],
"children": [
{
"字典列表": {
"keywords": [],
"children": [],
"node_id": "python-5-69"
}
},
{
"在字典中存储列表": {
"keywords": [],
"children": [],
"node_id": "python-5-70"
}
},
{
"在字典中存储字典": {
"keywords": [],
"children": [],
"node_id": "python-5-71"
}
}
],
"node_id": "python-4-101"
}
},
{
"检查是否相等": {
"keywords": [],
"children": [],
"node_id": "python-4-102"
}
},
{
"检查是否相等时忽略大小写": {
"keywords": [],
"children": [],
"node_id": "python-4-103"
}
},
{
"检查是否不相等": {
"keywords": [],
"children": [],
"node_id": "python-4-104"
}
},
{
"数值比较": {
"keywords": [],
"children": [],
"node_id": "python-4-105"
}
},
{
"检查多个条件": {
"keywords": [],
"children": [],
"node_id": "python-4-106"
}
},
{
"检查特定值是否包含在列表中": {
"keywords": [],
"children": [],
"node_id": "python-4-107"
}
},
{
"布尔表达式": {
"keywords": [],
"children": [],
"node_id": "python-4-108"
}
}
],
"node_id": "python-3-12"
}
\ No newline at end of file
{
"one_line": {
"ret ==": [
"ret !=",
"ret >=",
"ret <="
],
"\"{}\".format(test)": [
"test"
],
"if ret == ": [
"if int(ret) == "
]
},
"source": "if-else.py",
"depends": [],
"exercise_id": 97,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 条件和分支(1)
# 描述:编写一个程序,能正确提示用户输入测试数字,判断是否是除以2等于5的数字。
def test():
test = 10
ret = input("有一个数除以2是5,请问它是什么?:")
if ret == "{}".format(test):
print("恭喜你答对了!")
else:
print("哎,这么简单都答不对,回去重修吧!")
if __name__ == '__main__':
test()
{
"one_line": {
"is None": [
"is not None",
"not is None",
"None"
]
},
"source": "none.py",
"depends": [],
"exercise_id": 96,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 条件和分支(2)
# 描述:存在还是不存在,提示用户输入查询关键字,判断是否在字典的key中,输出查询结果。
def is_existed():
database = {
"monkey": "猴王说,我写的程序在五指山上的服务器上运行5000年了!"
}
key = input('请输入你要查询的关键字:')
print("* 猴王数据库正在努力检索中...")
if database.get(key) is None:
print("* 无法检索到该信息!")
else:
print("* 返回:{}".format(database[key]))
if __name__ == '__main__':
is_existed()
{
"export": [
"for.json",
"while.json"
],
"keywords": [],
"children": [
{
"for": {
"keywords": [],
"children": [],
"node_id": "python-4-109"
}
},
{
"while": {
"keywords": [],
"children": [],
"node_id": "python-4-110"
}
},
{
"break": {
"keywords": [],
"children": [],
"node_id": "python-4-111"
}
},
{
"continue": {
"keywords": [],
"children": [],
"node_id": "python-4-112"
}
},
{
"循环判断": {
"keywords": [],
"children": [],
"node_id": "python-4-113"
}
},
{
"迭代字典": {
"keywords": [],
"children": [],
"node_id": "python-4-114"
}
},
{
"一些迭代工具": {
"keywords": [],
"children": [],
"node_id": "python-4-115"
}
},
{
"跳出循环": {
"keywords": [],
"children": [],
"node_id": "python-4-116"
}
},
{
"循环中的else子句": {
"keywords": [],
"children": [],
"node_id": "python-4-117"
}
}
],
"node_id": "python-3-13"
}
\ No newline at end of file
{
"multiline": [
{
"for item in list:": "for item in list"
},
{
"i = 0": "",
"for item in list:": "for i, item in list:"
},
{
"for item in list:": "for(item in list):"
}
],
"source": "for.py",
"depends": [],
"exercise_id": 78,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 循环1
# 描述:使用 for 遍历打印列表信息
def test(list):
i = 0
for item in list:
print('')
print("## 第{}条信息".format(i))
print("* id: {}".format(item['id']))
print("* number: {}".format(item['number']))
print("* title: {}".format(item['title']))
print("* body: {}".format(item['body']))
i += 1
if __name__ == '__main__':
list = [
{
"id": 966024429,
"number": 2341,
"title": "Question about license.",
"body": "I would like to create a [winget](https://github.com/microsoft/winget-cli) package for jq. 🙏🏻"
},
{
"id": 962477084,
"number": 2340,
"title": "visibility of wiki pages",
"body": "The visibility of wiki pages to search engines is generally limited; for example, the search result for \"jq Cookbook\" looks like this:"
}
]
test(list)
{
"multiline": [
{
"while i < len(list):": "while(i < len(list)):"
},
{
"while i < len(list):": "while i in range(0,len(list):"
},
{
"while i < len(list):": "while i < len(list)"
}
],
"source": "while.py",
"depends": [],
"exercise_id": 77,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 循环1
# 描述:使用 while 遍历,打印列表信息
def test(list):
i = 0
while i < len(list):
item = list[i]
print('')
print("## 第{}条信息".format(i))
print("* id: {}".format(item['id']))
print("* number: {}".format(item['number']))
print("* title: {}".format(item['title']))
print("* body: {}".format(item['body']))
i += 1
if __name__ == '__main__':
list = [
{
"id": 955350543,
"number": 2337,
"title": "Release 1.6 does not have pre-autoreconf'ed configure script",
"body": "If you have a usage question, please ask us on either Stack Overflow (https://stackoverflow.com/questions/tagged/jq) or in the #jq channel (http://irc.lc/freenode/%23jq/) on Freenode (https://webchat.freenode.net/)."
},
{
"id": 954792209,
"number": 2336,
"title": "Fix typo",
"body": ""
}
]
test(list)
{
"export": [
"string.json",
"list.json",
"tuple.json",
"dict.json"
],
"keywords": [],
"children": [
{
"整型": {
"keywords": [],
"children": [],
"node_id": "python-4-118"
}
},
{
"浮点型": {
"keywords": [],
"children": [],
"node_id": "python-4-119"
}
},
{
"布尔型": {
"keywords": [],
"children": [],
"node_id": "python-4-120"
}
},
{
"字符串": {
"keywords": [],
"children": [
{
"单引号字符串以及对引号转义": {
"keywords": [],
"children": [],
"node_id": "python-5-72"
}
},
{
"拼接字符串": {
"keywords": [],
"children": [],
"node_id": "python-5-73"
}
},
{
"字符串表示str和repr": {
"keywords": [],
"children": [],
"node_id": "python-5-74"
}
},
{
"长字符串、原始字符串和字节": {
"keywords": [],
"children": [],
"node_id": "python-5-75"
}
},
{
"字符串基本操作": {
"keywords": [],
"children": [],
"node_id": "python-5-76"
}
},
{
"设置字符串的格式:精简版": {
"keywords": [],
"children": [
{
"替换字段名": {
"keywords": [],
"children": [],
"node_id": "python-6-18"
}
}
],
"node_id": "python-5-77"
}
},
{
"使用方法修改字符串的大小写": {
"keywords": [],
"children": [],
"node_id": "python-5-78"
}
},
{
"使用制表符或换行符来添加空白": {
"keywords": [],
"children": [],
"node_id": "python-5-79"
}
},
{
"删除空白": {
"keywords": [],
"children": [],
"node_id": "python-5-80"
}
},
{
"使用字符串时避免语法错误": {
"keywords": [],
"children": [],
"node_id": "python-5-81"
}
},
{
"字符串编码转换": {
"keywords": [],
"children": [
{
"使用encode方法编码": {
"keywords": [],
"children": [],
"node_id": "python-6-19"
}
},
{
"使用decode方法解码": {
"keywords": [],
"children": [],
"node_id": "python-6-20"
}
}
],
"node_id": "python-5-82"
}
}
],
"node_id": "python-4-121"
}
},
{
"datetime --- 基本的日期和时间类型": {
"keywords": [],
"children": [],
"node_id": "python-4-122"
}
},
{
"zoneinfo --- IANA时区支持": {
"keywords": [],
"children": [],
"node_id": "python-4-123"
}
},
{
"calendar --- 日历相关函数": {
"keywords": [],
"children": [],
"node_id": "python-4-124"
}
},
{
"collections --- 容器数据类型": {
"keywords": [],
"children": [],
"node_id": "python-4-125"
}
},
{
"collections.abc --- 容器的抽象基类": {
"keywords": [],
"children": [],
"node_id": "python-4-126"
}
},
{
"heapq --- 堆队列算法": {
"keywords": [],
"children": [],
"node_id": "python-4-127"
}
},
{
"bisect --- 数组二分查找算法": {
"keywords": [],
"children": [],
"node_id": "python-4-128"
}
},
{
"array --- 高效的数值数组": {
"keywords": [],
"children": [],
"node_id": "python-4-129"
}
},
{
"weakref --- 弱引用": {
"keywords": [],
"children": [],
"node_id": "python-4-130"
}
},
{
"types --- 动态类型创建和内置类型名称": {
"keywords": [],
"children": [],
"node_id": "python-4-131"
}
},
{
"copy --- 浅层 (shallow) 和深层 (deep) 复制操作": {
"keywords": [],
"children": [],
"node_id": "python-4-132"
}
},
{
"pprint --- 数据美化输出": {
"keywords": [],
"children": [],
"node_id": "python-4-133"
}
},
{
"reprlib --- 另一种repr 实现": {
"keywords": [],
"children": [],
"node_id": "python-4-134"
}
},
{
"enum --- 对枚举的支持": {
"keywords": [],
"children": [],
"node_id": "python-4-135"
}
},
{
"graphlib --- Functionality to operate with graph-like structures": {
"keywords": [],
"children": [],
"node_id": "python-4-136"
}
}
],
"node_id": "python-3-14"
}
\ No newline at end of file
{
"one_line": {
"map.get('tags')": [
"map.get('profile')"
],
"map['no'] = 10000": [
"map.set('no', 10000)"
],
"dict = {}": [
"dict = new dict{}"
]
},
"source": "dict.py",
"depends": [],
"exercise_id": 81,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 字典
# 描述:字典的基本使用,插入key-value,并打印结果
if __name__ == '__main__':
map = {}
print('* 初始化字典:', map)
print('* 使用下标插入数据到字典')
map['name'] = "monkey"
map['age'] = 25
map['no'] = 10000
for key in map:
value = map[key]
print("{}:{}".format(key, value))
print('* 字典里可以插入其他数据类型,例如插入列表')
map['tags'] = ['程序员', '户外达人']
print('* 打印 tags :{}', map.get('tags'))
print('* 字典里可以插入其他数据类型,例如插入字典')
map['profile'] = {'info1': "test", "info2": "test"}
print('* 打印 profile :{', map.get('profile'))
{
"one_line": {
"array.append": [
"array.push"
],
"array.pop(0)": [
"array.pop()"
],
"array = []": [
"array = new list()"
]
},
"source": "list.py",
"depends": [],
"exercise_id": 80,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 列表
# 描述:列表的基本使用,使用append/pop,打印结果
if __name__ == '__main__':
array = []
print('* 初始化列表 array:', array)
print('* 使用 append 追加数据到列表')
array.append(10)
array.append(20)
array.append(30)
array.append(40)
print('* 现在列表的值 array:', array)
print('* 使用 pop 弹出先插入的数据')
array.pop(0)
array.pop(0)
print('* 现在列表的值 array:', array)
print("* 开始遍历列表")
for item in array:
print(" ->{}".format(item))
{
"one_line": {
"\"双引号字符串里的单引号: 'hello world!'\"": [
"\"双引号字符串里的单引号: \"hello world!\"\""
],
"'单引号字符串里的双引号: \"hello world!\"'": [
"'单引号字符串里的双引号: 'hello world!''"
],
"* \"双引号\" ": [
"* '''双引号''' "
],
"* '单引号'": [
"* '''单引号'''"
],
"\"字符串加法\"+\"字符串加法\"": [
"\"字符串加法\"*\"字符串加法\""
],
"\"字符串乘法\"*3": [
"\"字符串乘法\"+3"
]
},
"source": "string.py",
"depends": [],
"exercise_id": 82,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 字符串
# 描述:使用字符串,几种不同引号的混合使用
def test():
print("双引号字符串")
print('单引号字符串')
print("双引号字符串里的单引号: 'hello world!'")
print('单引号字符串里的双引号: "hello world!"')
triple = '''
三引号字符串
'''
print(triple)
triple2 = '''
三引号字符串的便利:
* "双引号"
* '单引号'
'''
print(triple2)
print("字符串加法"+"字符串加法")
print("字符串乘法"*3)
print(",".join(["字符串数组的聚合", "字符串数组的聚合", "字符串数组的聚合"]))
if __name__ == '__main__':
test()
{
"one_line": {
"('红色',)": [
"('红色')"
],
"('红色', '绿色', '蓝色')": [
"('红色', ('绿色',), '蓝色')"
],
"0.6, 0.8, 0.3": [
"0.6, (0.8, 0.3)"
]
},
"source": "tuple.py",
"depends": [],
"exercise_id": 79,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 元组
# 描述:元组的基本使用,循环打印元组成员,如果只有一个元素,应该怎么表示<br/><img src="https://img-mid.csdnimg.cn/release/static/image/mid/ask/321539019236199.jpg?x-oss-process=image/auto-orient,1/resize,w_320,m_lfit"/>
import math
if __name__ == '__main__':
tuple1 = ('红色',)
for element in tuple1:
print(element)
tuple = ('红色', '绿色', '蓝色')
for element in tuple:
print(element)
print("不带括号的也是元组:")
r, g, b = 0.6, 0.8, 0.3
print("普通颜色[0-1]:({}, {}, {})".format(r, g, b))
hr, hg, hb = (math.pow(r, 3/2), math.pow(g, 4/5), math.pow(b, 3/2))
print("使用《黑客帝国》绿色滤镜算法计算后的颜色[0-1]:({}, {}, {})".format(hr, hg, hb))
{
"one_line": {
"list(set(lista))": [
"list(lista)",
"set(lista)"
],
"item >= bound": [
"item > bound"
],
"res = []": [
"res = {}"
]
},
"source": "builtin_class.py",
"depends": [],
"exercise_id": 240,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 内置类型
# 描述:内置类的使用,列表元素去重+过滤小于3的元素
def remove_duplicates(items):
res = list(set(items))
return res
def filter_element(items, bound):
res = []
for item in items:
if item >= bound:
res.append(item)
return res
if __name__ == '__main__':
a = [1, 2, 3, 4, 4, 5, 5]
print('输入: {}'.format(a))
res = remove_duplicates(a)
print('去重后的结果:{}'.format(res))
bound = 3
res = filter_element(a, bound)
print('过滤小于{}的元素:{}'.format(bound, res))
{
"export": [
"builtin_class.json"
],
"keywords": [],
"children": [
{
"列表:list": {
"keywords": [],
"children": [
{
"函数list": {
"keywords": [],
"children": [],
"node_id": "python-5-83"
}
},
{
"基本的列表操作": {
"keywords": [],
"children": [
{
"遍历整个列表": {
"keywords": [],
"children": [
{
"深入研究循环": {
"keywords": [],
"children": [],
"node_id": "python-7-0"
}
},
{
"在for循环中执行更多操作": {
"keywords": [],
"children": [],
"node_id": "python-7-1"
}
},
{
"在for循环结束后执行一些操作": {
"keywords": [],
"children": [],
"node_id": "python-7-2"
}
}
],
"node_id": "python-6-21"
}
},
{
"避免缩进错误": {
"keywords": [],
"children": [
{
"忘记缩进": {
"keywords": [],
"children": [],
"node_id": "python-7-3"
}
},
{
"忘记缩进额外的代码行": {
"keywords": [],
"children": [],
"node_id": "python-7-4"
}
},
{
"不必要的缩进": {
"keywords": [],
"children": [],
"node_id": "python-7-5"
}
},
{
"循环后不必要的缩进": {
"keywords": [],
"children": [],
"node_id": "python-7-6"
}
},
{
"遗漏了冒号": {
"keywords": [],
"children": [],
"node_id": "python-7-7"
}
}
],
"node_id": "python-6-22"
}
},
{
"创建数值列表": {
"keywords": [],
"children": [
{
"使用range创建数字列表": {
"keywords": [],
"children": [],
"node_id": "python-7-8"
}
},
{
"对数字列表执行简单的统计计算": {
"keywords": [],
"children": [],
"node_id": "python-7-9"
}
},
{
"列表解析": {
"keywords": [],
"children": [],
"node_id": "python-7-10"
}
}
],
"node_id": "python-6-23"
}
},
{
"设置代码格式": {
"keywords": [],
"children": [
{
"格式设置指南": {
"keywords": [],
"children": [],
"node_id": "python-7-11"
}
},
{
"行长": {
"keywords": [],
"children": [],
"node_id": "python-7-12"
}
},
{
"空行": {
"keywords": [],
"children": [],
"node_id": "python-7-13"
}
},
{
"其他格式设置指南": {
"keywords": [],
"children": [],
"node_id": "python-7-14"
}
}
],
"node_id": "python-6-24"
}
}
],
"node_id": "python-5-84"
}
},
{
"列表方法": {
"keywords": [],
"children": [],
"node_id": "python-5-85"
}
},
{
"列表数据类型": {
"keywords": [],
"children": [],
"node_id": "python-5-86"
}
},
{
"增强的赋值操作": {
"keywords": [],
"children": [],
"node_id": "python-5-87"
}
},
{
"序列数据类型": {
"keywords": [],
"children": [],
"node_id": "python-5-88"
}
},
{
"引用": {
"keywords": [],
"children": [],
"node_id": "python-5-89"
}
},
{
"访问列表元素": {
"keywords": [],
"children": [],
"node_id": "python-5-90"
}
},
{
"索引从0而不是1开始": {
"keywords": [],
"children": [],
"node_id": "python-5-91"
}
},
{
"使用列表中的各个值": {
"keywords": [],
"children": [],
"node_id": "python-5-92"
}
},
{
"遍历切片": {
"keywords": [],
"children": [],
"node_id": "python-5-93"
}
},
{
"复制列表": {
"keywords": [],
"children": [],
"node_id": "python-5-94"
}
},
{
"列表的创建和删除": {
"keywords": [],
"children": [],
"node_id": "python-5-95"
}
},
{
"对列表进行统计计算": {
"keywords": [],
"children": [],
"node_id": "python-5-96"
}
},
{
"对列表进行排序": {
"keywords": [],
"children": [],
"node_id": "python-5-97"
}
}
],
"node_id": "python-4-137"
}
},
{
"字典:dict": {
"keywords": [],
"children": [
{
"访问字典中的值": {
"keywords": [],
"children": [],
"node_id": "python-5-98"
}
},
{
"添加键值对": {
"keywords": [],
"children": [],
"node_id": "python-5-99"
}
},
{
"先创建一个空字典": {
"keywords": [],
"children": [],
"node_id": "python-5-100"
}
},
{
"修改字典中的值": {
"keywords": [],
"children": [],
"node_id": "python-5-101"
}
},
{
"删除键值对": {
"keywords": [],
"children": [],
"node_id": "python-5-102"
}
},
{
"由类似对象组成的字典": {
"keywords": [],
"children": [],
"node_id": "python-5-103"
}
},
{
"使用get来访问值": {
"keywords": [],
"children": [],
"node_id": "python-5-104"
}
},
{
"遍历字典": {
"keywords": [],
"children": [
{
"遍历所有键值对": {
"keywords": [],
"children": [],
"node_id": "python-6-25"
}
},
{
"遍历字典中的所有键": {
"keywords": [],
"children": [],
"node_id": "python-6-26"
}
},
{
"按特定顺序遍历字典中的所有键": {
"keywords": [],
"children": [],
"node_id": "python-6-27"
}
},
{
"遍历字典中的所有值": {
"keywords": [],
"children": [],
"node_id": "python-6-28"
}
}
],
"node_id": "python-5-105"
}
},
{
"访问字典": {
"keywords": [],
"children": [],
"node_id": "python-5-106"
}
},
{
"字典推导式": {
"keywords": [],
"children": [],
"node_id": "python-5-107"
}
}
],
"node_id": "python-4-138"
}
},
{
"元组:tuple": {
"keywords": [],
"children": [
{
"定义元组": {
"keywords": [],
"children": [],
"node_id": "python-5-108"
}
},
{
"遍历元组中的所有值": {
"keywords": [],
"children": [],
"node_id": "python-5-109"
}
},
{
"修改元组变量": {
"keywords": [],
"children": [],
"node_id": "python-5-110"
}
},
{
"访问元组元素": {
"keywords": [],
"children": [],
"node_id": "python-5-111"
}
},
{
"修改元组": {
"keywords": [],
"children": [],
"node_id": "python-5-112"
}
},
{
"元组推导式": {
"keywords": [],
"children": [],
"node_id": "python-5-113"
}
}
],
"node_id": "python-4-139"
}
},
{
"集合:set": {
"keywords": [],
"children": [
{
"创建集合": {
"keywords": [],
"children": [],
"node_id": "python-5-114"
}
},
{
"集合的交集、并集和差集运算": {
"keywords": [],
"children": [],
"node_id": "python-5-115"
}
}
],
"node_id": "python-4-140"
}
},
{
"整型:int": {
"keywords": [],
"children": [],
"node_id": "python-4-141"
}
},
{
"字符串:str": {
"keywords": [],
"children": [],
"node_id": "python-4-142"
}
}
],
"node_id": "python-3-15"
}
\ No newline at end of file
{
"node_id": "python-2-1",
"keywords": []
}
\ No newline at end of file
{
"export": [
"list01.json",
"list02.json",
"tuple.json",
"dict.json"
],
"keywords": [],
"children": [],
"node_id": "python-3-17"
}
\ No newline at end of file
{
"one_line": {
"if key != 'w'": [
"if key == 'w'",
""
],
"key: install[key]": [
"install[key]"
]
},
"source": "dict.py",
"depends": [],
"exercise_id": 207,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 字典推导式
# 描述:Python 独步天下的推导式表达式,使用字典推导式过滤,打印非windows系统的 Python 安装介绍
def test():
install = {
"w": {
"platform": "Window",
"desc": "请下载 Windows 安装包安装:https://www.python.org/downloads/windows/"
},
"l": {
"platform": "Linux",
"desc": "请下载 Linux 的 Python 源码安装:https://www.python.org/downloads/source/",
},
"m": {
"platform": "MacOS",
"desc": "请下载 Mac 的安装包:https://www.python.org/downloads/macos/,或者使用 brew install python 安装",
}
}
non_windows = {key: install[key] for key in install if key != 'w'}
print("打算最近只用非Windows系统安装Python了:")
print()
for key in non_windows:
target = non_windows[key]
print("安装平台:{}".format(target['platform']))
print("安装说明:{}".format(target['desc']))
print()
if __name__ == '__main__':
test()
{
"one_line": {
"if n % 2 == 0": [
"if n % 2 != 0",
"where n % 2 == 0"
],
"for n in list": [
"for n in list:"
]
},
"source": "list01.py",
"depends": [],
"exercise_id": 195,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 列表推导式(1)
# 描述:Python 独步天下的推导式表达式,使用列表推导式过滤出偶数列表
if __name__ == '__main__':
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print('')
print("# 使用列表推导式过滤出偶数")
even = [n for n in list if n % 2 == 0]
for n in even:
print(n)
print('')
{
"one_line": {
"if '不热' in d['tags']": [
"if '热' in d['tags']",
""
],
"d for d": [
"for d"
]
},
"source": "list02.py",
"depends": [],
"exercise_id": 214,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 列表推导式(2)
# 描述:Python 独步天下的推导式表达式,使用列表推导式过滤不热的天气
if __name__ == '__main__':
obj_list = [
{"key": "day1", "value": "大雨哗啦啦啦啦下", 'tags': ["不热"]},
{"key": "day2", "value": "很热很热,哥哥不在家,我一个人出去旅行", 'tags': ["热"]},
{"key": "day3", "value": "阴天有时下肉丸", 'tags': ["不热"]}
]
print('')
print("# 过滤出不热的日子")
non_hot_days = [d for d in obj_list if '不热' in d['tags']]
for day in non_hot_days:
print("* [{}]: {}".format(day['key'], day['value']))
print('')
{
"one_line": {
"if len(book) <= 4": [
"if len(book) < 4"
],
"for book in books": [
"for book in books:"
],
"book for book": [
"books[i] for book"
]
},
"source": "tuple.py",
"depends": [],
"exercise_id": 194,
"type": "code_options"
}
\ No newline at end of file
# -*- coding: UTF-8 -*-
# 作者:幻灰龙
# 标题:Python 元组推导式
# 描述:Python 独步天下的推导式表达式,使用元表推式过滤长度小于等于4的书籍
def test():
books = ('程序员修炼之道', '构建之法', '代码大全', 'TCP/IP协议详解')
reading = (book for book in books if len(book) <= 4)
print("太长的书就不看了,只读短的:")
for book in reading:
print(" ->《{}》".format(book))
print("可是发现书的名字短,内容也可能很长啊!")
if __name__ == '__main__':
test()
{
"export": [
"encode.json"
],
"keywords": [],
"children": [
{
"理解内码和字节码的概念": {
"keywords": [],
"children": [],
"node_id": "python-4-203"
}
}
],
"node_id": "python-3-26"
}
\ No newline at end of file
{
"multiline": [
{
"u'二": "'二"
},
{
"encode": "decode",
"decode": "encode"
},
{
"'utf-8'": "'utf8'"
}
],
"source": "encode.py",
"depends": [],
"exercise_id": 107,
"type": "code_options"
}
\ No newline at end of file
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册