Browse Source

Update python tutorial

pull/5/head
bushuhui 3 years ago
parent
commit
dc77309487
10 changed files with 106 additions and 263 deletions
  1. +58
    -154
      0_python/0-ipython_notebook.ipynb
  2. +0
    -0
      0_python/0-ipython_notebook_EN.ipynb
  3. +1
    -31
      0_python/1_Basics.ipynb
  4. +1
    -21
      0_python/3_Data_Structure_1.ipynb
  5. +2
    -2
      0_python/5_Control_Flow.ipynb
  6. +27
    -37
      0_python/6_Function.ipynb
  7. +9
    -11
      0_python/7_Class.ipynb
  8. +8
    -7
      0_python/README.md
  9. BIN
      0_python/images/jupyter_shortcuts.png
  10. +0
    -0
      0_python/test.txt

1_numpy_matplotlib_scipy_sympy/3-ipython_notebook.ipynb → 0_python/0-ipython_notebook.ipynb View File

@@ -4,7 +4,52 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# 介绍IPython和Jupyter笔记本"
"# IPython和Jupyter笔记本"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"本在线讲义使用Jupyter Notebook编写,因此首先介绍Jupter Notebook的使用方法。使用Notebook,可以方便的将理论描述、程序、数据可视化等集成在一个多媒体页面,方便大家的学习。\n",
"\n",
"Jupyter notebook 是一种 Web 应用,它能让用户将说明文本、数学方程、代码和可视化内容全部组合到一个易于共享的文档中,非常方便研究和教学,让使用者一目了然。Jupyter notebook特别适合做数据处理,其用途可以包括数据清理和探索、可视化、机器学习和大数据分析。其具有以下一些特点:\n",
"* 编程时具有语法高亮、缩进、tab补全的功能。\n",
"* 可直接通过浏览器运行代码,同时在代码块下方展示运行结果。\n",
"* 以富媒体格式展示计算结果。富媒体格式包括:HTML,LaTeX,PNG,SVG等。\n",
"* 对代码编写说明文档或语句时,支持Markdown语法。\n",
"* 支持使用LaTeX编写数学性说明。\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Jupyter 安装\n",
"安装Jupyter最简单的方法就是使用 Anaconda,其发行版附带了 Jupyter Notebook。在 conda 环境下安装 Jupyter Notebook 可以使用 \n",
"\n",
"```\n",
"conda install jupyter\n",
"```\n",
"\n",
"当然,也可以通过 `pip` 来安装 \n",
"```\n",
"pip install jupyter。\n",
"```\n",
"\n",
"安装后便可在终端中输入以下命令启动:\n",
"```\n",
"# jupyter notebook\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 常用操作\n",
"\n",
"![shortcut](images/jupyter_shortcuts.png)"
]
},
{
@@ -70,24 +115,20 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1-numpy_tutorial_EN.ipynb\texample.png\r\n",
"1-numpy_tutorial.ipynb\t\tmatplotlib_ani.ipynb\r\n",
"2-matplotlib_tutorial_EN.ipynb\tmatplotlib_full.ipynb\r\n",
"2-matplotlib_tutorial.ipynb\trandom-matrix.csv\r\n",
"3-ipython_notebook_EN.ipynb\trandom-matrix.npy\r\n",
"3-ipython_notebook.ipynb\tREADME.md\r\n",
"4-scipy_tutorial_EN.ipynb\tstockholm_td_adj.dat\r\n",
"4-scipy_tutorial.ipynb\t\tutils_git_advanced.ipynb\r\n",
"5-sympy_tutorial_EN.ipynb\tutils_git.ipynb\r\n",
"5-sympy_tutorial.ipynb\t\tutils_shell.ipynb\r\n",
"bokeh_tutorial.ipynb\r\n"
"0-ipython_notebook_EN.ipynb 3_Data_Structure_1.ipynb\t 7_Class_EN.ipynb\r\n",
"0-ipython_notebook.ipynb 4_Data_Structure_2_EN.ipynb 7_Class.ipynb\r\n",
"1_Basics_EN.ipynb\t 4_Data_Structure_2.ipynb\t images\r\n",
"1_Basics.ipynb\t\t 5_Control_Flow_EN.ipynb\t Python.pdf\r\n",
"2_Print_Statement_EN.ipynb 5_Control_Flow.ipynb\t README_EN.md\r\n",
"2_Print_Statement.ipynb 6_Function_EN.ipynb\t README.md\r\n",
"3_Data_Structure_1_EN.ipynb 6_Function.ipynb\t\t test.txt\r\n"
]
}
],
@@ -272,7 +313,9 @@
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
@@ -301,145 +344,6 @@
"source": [
"%run?"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"from IPython.display import HTML, SVG, YouTubeVideo"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"podoc": {
"output_text": "<IPython.core.display.HTML object>"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"<table style=\"border: 2px solid black;\">\n",
"<tr><td>0,0</td><td>0,1</td><td>0,2</td><td>0,3</td><td>0,4</td></tr><tr><td>1,0</td><td>1,1</td><td>1,2</td><td>1,3</td><td>1,4</td></tr><tr><td>2,0</td><td>2,1</td><td>2,2</td><td>2,3</td><td>2,4</td></tr><tr><td>3,0</td><td>3,1</td><td>3,2</td><td>3,3</td><td>3,4</td></tr><tr><td>4,0</td><td>4,1</td><td>4,2</td><td>4,3</td><td>4,4</td></tr>\n",
"</table>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"HTML('''\n",
"<table style=\"border: 2px solid black;\">\n",
"''' +\n",
" ''.join(['<tr>' +\n",
" ''.join([f'<td>{row},{col}</td>'\n",
" for col in range(5)]) +\n",
" '</tr>' for row in range(5)]) +\n",
" '''\n",
"</table>\n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"podoc": {
"output_text": "<IPython.core.display.SVG object>"
}
},
"outputs": [
{
"data": {
"image/svg+xml": [
"<svg height=\"80\" width=\"600\"><circle cx=\"300\" cy=\"30\" fill=\"red\" r=\"0.0\" stroke=\"black\" stroke-width=\"2\">\n",
" </circle><circle cx=\"297\" cy=\"30\" fill=\"red\" r=\"3.0\" stroke=\"black\" stroke-width=\"2\">\n",
" </circle><circle cx=\"288\" cy=\"30\" fill=\"red\" r=\"6.0\" stroke=\"black\" stroke-width=\"2\">\n",
" </circle><circle cx=\"273\" cy=\"30\" fill=\"red\" r=\"9.0\" stroke=\"black\" stroke-width=\"2\">\n",
" </circle><circle cx=\"252\" cy=\"30\" fill=\"red\" r=\"12.0\" stroke=\"black\" stroke-width=\"2\">\n",
" </circle><circle cx=\"225\" cy=\"30\" fill=\"red\" r=\"15.0\" stroke=\"black\" stroke-width=\"2\">\n",
" </circle><circle cx=\"192\" cy=\"30\" fill=\"red\" r=\"18.0\" stroke=\"black\" stroke-width=\"2\">\n",
" </circle><circle cx=\"153\" cy=\"30\" fill=\"red\" r=\"21.0\" stroke=\"black\" stroke-width=\"2\">\n",
" </circle><circle cx=\"108\" cy=\"30\" fill=\"red\" r=\"24.0\" stroke=\"black\" stroke-width=\"2\">\n",
" </circle><circle cx=\"57\" cy=\"30\" fill=\"red\" r=\"27.0\" stroke=\"black\" stroke-width=\"2\">\n",
" </circle></svg>"
],
"text/plain": [
"<IPython.core.display.SVG object>"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"SVG('''<svg width=\"600\" height=\"80\">''' +\n",
" ''.join([f'''<circle\n",
" cx=\"{(30 + 3*i) * (10 - i)}\"\n",
" cy=\"30\"\n",
" r=\"{3. * float(i)}\"\n",
" fill=\"red\"\n",
" stroke-width=\"2\"\n",
" stroke=\"black\">\n",
" </circle>''' for i in range(10)]) +\n",
" '''</svg>''')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"podoc": {
"output_text": "<IPython.lib.display.YouTubeVideo at 0x7fc0000b64a8>"
}
},
"outputs": [],
"source": [
"YouTubeVideo('VQBZ2MqWBZI')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```json\n",
"{\n",
" \"cells\": [\n",
" {\n",
" \"cell_type\": \"code\",\n",
" \"execution_count\": 1,\n",
" \"metadata\": {},\n",
" \"outputs\": [\n",
" {\n",
" \"name\": \"stdout\",\n",
" \"output_type\": \"stream\",\n",
" \"text\": [\n",
" \"Hello world!\\n\"\n",
" ]\n",
" }\n",
" ],\n",
" \"source\": [\n",
" \"print(\\\"Hello world!\\\")\"\n",
" ]\n",
" }\n",
" ],\n",
" \"metadata\": {},\n",
" \"nbformat\": 4,\n",
" \"nbformat_minor\": 2\n",
"}\n",
"```"
]
}
],
"metadata": {
@@ -458,7 +362,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
"version": "3.7.9"
}
},
"nbformat": 4,

1_numpy_matplotlib_scipy_sympy/3-ipython_notebook_EN.ipynb → 0_python/0-ipython_notebook_EN.ipynb View File


+ 1
- 31
0_python/1_Basics.ipynb View File

@@ -882,36 +882,6 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**cmp(x,y)**\n",
"\n",
"|x ? y|Output|\n",
"|---|---|\n",
"| x < y | -1 |\n",
"| x == y | 0 |\n",
"| x > y | 1 |"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"print(1<2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**pow(x,y,z)** 可以被用来找到幂函数$x^y$也可以找到第三个指定数字的mod值,即:($x^y$ % z)。"
]
},
@@ -1030,7 +1000,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
"version": "3.7.9"
}
},
"nbformat": 4,


+ 1
- 21
0_python/3_Data_Structure_1.ipynb View File

@@ -578,26 +578,6 @@
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'Rajath' in names"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -2145,7 +2125,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
"version": "3.7.9"
}
},
"nbformat": 4,


+ 2
- 2
0_python/5_Control_Flow.ipynb View File

@@ -511,7 +511,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"就是这样!只记得用方括号括起来。"
"将表达方式用方括号括起来。"
]
},
{
@@ -685,7 +685,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
"version": "3.7.9"
}
},
"nbformat": 4,


+ 27
- 37
0_python/6_Function.ipynb View File

@@ -34,7 +34,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"将上面的语法理解为,定义了一个名为“funcname”的函数,它接受“arg1,arg2,…argN”的参数。函数在执行语句后返回一个“值”。"
"将上面的语法理解为,定义了一个名为`funcname`的函数,它接受`arg1,arg2,…argN`的参数。函数在执行语句后返回一个`<value>`。"
]
},
{
@@ -62,16 +62,16 @@
"source": [
"不需要每次都写上面的两个语句,可以通过定义一个函数来替换它,这个函数只需一行就能完成任务。\n",
"\n",
"定义一个函数 firstfunc()."
"定义一个函数 `first_func()`."
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def firstfunc():\n",
"def first_func():\n",
" print(\"Hey Rajath!\")\n",
" print(\"Rajath, How do you do?\")"
]
@@ -93,8 +93,8 @@
}
],
"source": [
"firstfunc()\n",
"funca=firstfunc\n",
"first_func()\n",
"funca=first_func\n",
"funca()"
]
},
@@ -102,30 +102,30 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**firstfunc()** 每一次只打印一个人的消息。我们可以让我们的函数 **firstfunc()** 接受参数,该参数将存储名称然后打印相应地接受字符串。为了这样做我们需要像所示的那样在函数内添加一个参数。"
"**first_func()** 每一次只打印一个人的消息。我们可以让我们的函数 **first_func()** 接受参数,该参数将存储名称然后打印相应地接受字符串。为了这样做我们需要像所示的那样在函数内添加一个参数。"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def firstfunc(username):\n",
"def first_func(username):\n",
" print(\"Hey\", username + '!')\n",
" print(username + ',' ,\"How do you do?\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter your name : Jack\n"
"Please enter your name : hello\n"
]
}
],
@@ -142,60 +142,50 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hey Jack!\n",
"Jack, How do you do?\n"
"Hey hello!\n",
"hello, How do you do?\n"
]
}
],
"source": [
"firstfunc(name1)"
"first_func(name1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"让我们通过定义另一个函数**secondfunc()** 来进一步简化它,该函数接受名称并将其存储在一个变量中,然后从函数本身内部调用**firstfunc()**。"
"让我们通过定义另一个函数**second_func()** 来进一步简化它,该函数接受名称并将其存储在一个变量中,然后从函数本身内部调用**first_func()**。"
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"def firstfunc(username):\n",
"def first_func(username):\n",
" print(\"Hey\", username + '!')\n",
" print(username + ',' ,\"How do you do?\")\n",
"def secondfunc():\n",
"def second_func():\n",
" name = input(\"Please enter your name : \")\n",
" firstfunc(name)"
" first_func(name)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter your name : Tom\n",
"Hey Tom!\n",
"Tom, How do you do?\n"
]
}
],
"outputs": [],
"source": [
"secondfunc()"
"second_func()"
]
},
{
@@ -415,16 +405,16 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"当一个函数的参数在大多数情况下是常见的或者它是“隐式的”时,使用这个概念。"
"当一个函数的参数在大多数情况下是常见的或者它是`隐式的`时,使用这个概念。"
]
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def implicitadd(x,addnumber=3):\n",
"def implicitadd(x, addnumber=3):\n",
" return x+addnumber"
]
},
@@ -1060,7 +1050,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
"version": "3.7.9"
}
},
"nbformat": 4,


+ 9
- 11
0_python/7_Class.ipynb View File

@@ -11,7 +11,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Python中的变量、列表、字典等其实都是对象。不涉及面向对象编程的理论部分,在本教程中对概念进行解释。"
"Python中的变量、列表、字典等其实都是对象。"
]
},
{
@@ -1275,9 +1275,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"找各个方面的练习题,并独立完成能帮助你掌握Python的窍门,例如给自己一个问题并解决它们,你还可以在任何编程竞赛平台上提交问题求解。你编写的代码越多,你发现的越多,你就越开始欣赏这门语言。强烈建议把[Python作业](https://gitee.com/pi-lab/machinelearning_homework/blob/master/homework_01_python/README.md)完成,并在[其他编程练习](https://gitee.com/pi-lab/machinelearning_homework/blob/master/homework_01_python/README.md#references)里面找一些练习题或者项目做一下。\n",
"找各个方面的练习题,并独立完成能帮助你掌握Python的窍门,例如给自己一个问题并解决它们,你还可以在任何编程竞赛平台上提交问题求解。你编写的代码越多,你发现的越多,你就越开始欣赏这门语言。强烈建议把[Python作业](https://gitee.com/pi-lab/machinelearning_homework/blob/master/homework_01_python/README.md)完成,并在[其他编程练习](https://gitee.com/pi-lab/machinelearning_homework/blob/master/homework_01_python/README.md#references)里面找一些练习题或者项目做一下。\n",
"\n",
"现在已经向你介绍了Python,可以尝试感兴趣的领域中的不同Python库。强烈建议查看这个Python框架、库和软件列表 http://awesome-python.com\n",
"现在已经介绍了Python,可以尝试感兴趣的领域中的不同Python库。强烈建议查看这个Python框架、库和软件列表 http://awesome-python.com\n",
"\n",
"\n",
"Python 教程:\n",
@@ -1286,17 +1286,15 @@
"* [Python官方教程(中文版)](https://docs.python.org/zh-cn/3/tutorial/index.html)\n",
"* Python官方文档: https://docs.python.org/3/\n",
"* 本教程来源于:https://github.com/rajathkumarmp/Python-Lectures \n",
"\n",
"\n",
"**最后,享受解决问题的快乐!因为生命短暂,你需要Python!**"
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": []
"source": [
"**最后,享受解决问题的快乐!因为生命短暂,你需要Python!**"
]
}
],
"metadata": {
@@ -1315,7 +1313,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
"version": "3.7.9"
}
},
"nbformat": 4,


+ 8
- 7
0_python/README.md View File

@@ -9,22 +9,23 @@ Python 是一门上手简单、功能强大、通用型的脚本编程语言。P

## 内容
0. [Install Python](../references_tips/InstallPython.md)
1. [Basics](1_Basics.ipynb)
1. [IPython & Jupyter Notebook](0-ipython_notebook.ipynb)
2. [Basics](1_Basics.ipynb)
- Why Python, Zen of Python
- Variables, Operators, Built-in functions
2. [Print statement](2_Print_Statement.ipynb)
3. [Print statement](2_Print_Statement.ipynb)
- Tips of print
3. [Data structure - 1](3_Data_Structure_1.ipynb)
4. [Data structure - 1](3_Data_Structure_1.ipynb)
- Lists, Tuples, Sets
4. [Data structure - 2](4_Data_Structure_2.ipynb)
5. [Data structure - 2](4_Data_Structure_2.ipynb)
- Strings, Dictionaries
5. [Control flow](5_Control_Flow.ipynb)
6. [Control flow](5_Control_Flow.ipynb)
- if, else, elif, for, while, break, continue
6. [Functions](6_Function.ipynb)
7. [Functions](6_Function.ipynb)
- Function define, return, arguments
- Gloabl and local variables
- Lambda functions
7. [Class](7_Class.ipynb)
8. [Class](7_Class.ipynb)
- Class define
- Inheritance



BIN
0_python/images/jupyter_shortcuts.png View File

Before After
Width: 658  |  Height: 535  |  Size: 75 kB

1_numpy_matplotlib_scipy_sympy/test.txt → 0_python/test.txt View File


Loading…
Cancel
Save