Browse Source

Update Python tutorial

pull/5/head
bushuhui 3 years ago
parent
commit
8b0a401c2d
9 changed files with 490 additions and 618 deletions
  1. +2
    -1
      0_python/0-ipython_notebook.ipynb
  2. +82
    -70
      0_python/1_Basics.ipynb
  3. +13
    -6
      0_python/2_Print_Statement.ipynb
  4. +53
    -118
      0_python/3_Data_Structure_1.ipynb
  5. +14
    -6
      0_python/4_Data_Structure_2.ipynb
  6. +19
    -68
      0_python/5_Control_Flow.ipynb
  7. +135
    -96
      0_python/6_Function.ipynb
  8. +171
    -253
      0_python/7_Class.ipynb
  9. +1
    -0
      0_python/README.md

+ 2
- 1
0_python/0-ipython_notebook.ipynb View File

@@ -336,6 +336,7 @@
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true,
"podoc": {
"output_text": "Screenshot of the pager"
}
@@ -362,7 +363,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
"version": "3.5.4"
}
},
"nbformat": 4,


+ 82
- 70
0_python/1_Basics.ipynb View File

@@ -22,24 +22,28 @@
{
"data": {
"text/plain": [
"['.ipynb_checkpoints',\n",
" 'Python.pdf',\n",
" '1_Basics_EN.ipynb',\n",
" '2_Print_Statement_EN.ipynb',\n",
" '4_Data_Structure_2_EN.ipynb',\n",
" '5_Control_Flow_EN.ipynb',\n",
" '6_Function_EN.ipynb',\n",
" 'README.md',\n",
" 'README_EN.md',\n",
"['0-ipython_notebook.ipynb',\n",
" '0-ipython_notebook_EN.ipynb',\n",
" '1_Basics.ipynb',\n",
" '1_Basics_EN.ipynb',\n",
" '2_Print_Statement.ipynb',\n",
" '2_Print_Statement_EN.ipynb',\n",
" '3_Data_Structure_1.ipynb',\n",
" '3_Data_Structure_1_EN.ipynb',\n",
" '4_Data_Structure_2.ipynb',\n",
" '4_Data_Structure_2_EN.ipynb',\n",
" '5_Control_Flow.ipynb',\n",
" '5_Control_Flow_EN.ipynb',\n",
" '6_Function.ipynb',\n",
" '6_Function_EN.ipynb',\n",
" '7_Class.ipynb',\n",
" '7_Class_EN.ipynb']"
" '7_Class_EN.ipynb',\n",
" 'Python.pdf',\n",
" 'README.md',\n",
" 'README_ENG.md',\n",
" 'images',\n",
" 'test.txt',\n",
" '.ipynb_checkpoints']"
]
},
"execution_count": 1,
@@ -130,8 +134,10 @@
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x = 2\n",
@@ -141,7 +147,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"outputs": [
{
@@ -165,8 +171,10 @@
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x = y = 1"
@@ -174,7 +182,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"outputs": [
{
@@ -220,7 +228,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"outputs": [
{
@@ -229,7 +237,7 @@
"3"
]
},
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
@@ -240,7 +248,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"outputs": [
{
@@ -249,7 +257,7 @@
"1"
]
},
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
@@ -260,7 +268,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 9,
"metadata": {},
"outputs": [
{
@@ -269,7 +277,7 @@
"2"
]
},
"execution_count": 6,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
@@ -280,7 +288,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@@ -289,7 +297,7 @@
"0.5"
]
},
"execution_count": 8,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
@@ -307,7 +315,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 11,
"metadata": {},
"outputs": [
{
@@ -316,7 +324,7 @@
"0.5"
]
},
"execution_count": 9,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
@@ -327,7 +335,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 12,
"metadata": {},
"outputs": [
{
@@ -336,7 +344,7 @@
"0.5"
]
},
"execution_count": 10,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
@@ -347,7 +355,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 13,
"metadata": {},
"outputs": [
{
@@ -356,7 +364,7 @@
"5"
]
},
"execution_count": 9,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
@@ -374,7 +382,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 14,
"metadata": {},
"outputs": [
{
@@ -383,7 +391,7 @@
"1.0"
]
},
"execution_count": 10,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
@@ -415,8 +423,10 @@
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"execution_count": 15,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"z = 1"
@@ -424,7 +434,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 16,
"metadata": {},
"outputs": [
{
@@ -433,7 +443,7 @@
"True"
]
},
"execution_count": 13,
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
@@ -444,7 +454,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 17,
"metadata": {},
"outputs": [
{
@@ -453,7 +463,7 @@
"False"
]
},
"execution_count": 14,
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
@@ -485,8 +495,10 @@
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"execution_count": 18,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"a = 2 #10\n",
@@ -495,7 +507,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 19,
"metadata": {},
"outputs": [
{
@@ -514,7 +526,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 20,
"metadata": {},
"outputs": [
{
@@ -523,7 +535,7 @@
"2"
]
},
"execution_count": 13,
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
@@ -545,7 +557,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 21,
"metadata": {},
"outputs": [
{
@@ -554,7 +566,7 @@
"10"
]
},
"execution_count": 19,
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
@@ -604,7 +616,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 22,
"metadata": {},
"outputs": [
{
@@ -613,7 +625,7 @@
"'0xaa'"
]
},
"execution_count": 14,
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
@@ -624,7 +636,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 23,
"metadata": {},
"outputs": [
{
@@ -633,7 +645,7 @@
"170"
]
},
"execution_count": 15,
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
@@ -644,7 +656,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 24,
"metadata": {},
"outputs": [
{
@@ -653,7 +665,7 @@
"'0o10'"
]
},
"execution_count": 16,
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
@@ -671,7 +683,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 25,
"metadata": {},
"outputs": [
{
@@ -699,7 +711,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 26,
"metadata": {},
"outputs": [
{
@@ -725,7 +737,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 27,
"metadata": {},
"outputs": [
{
@@ -734,7 +746,7 @@
"'b'"
]
},
"execution_count": 24,
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
@@ -745,7 +757,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 28,
"metadata": {},
"outputs": [
{
@@ -754,7 +766,7 @@
"98"
]
},
"execution_count": 25,
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
@@ -767,7 +779,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4.2 简化算术运算"
"### 4.2 数值函数"
]
},
{
@@ -779,7 +791,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 29,
"metadata": {
"scrolled": false
},
@@ -807,7 +819,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 30,
"metadata": {},
"outputs": [
{
@@ -832,7 +844,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 31,
"metadata": {},
"outputs": [
{
@@ -841,7 +853,7 @@
"(4, 1)"
]
},
"execution_count": 20,
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
@@ -859,7 +871,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 32,
"metadata": {},
"outputs": [
{
@@ -887,7 +899,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 33,
"metadata": {},
"outputs": [
{
@@ -913,7 +925,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 38,
"metadata": {},
"outputs": [
{
@@ -948,14 +960,14 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Type something here and it will be stored in variable abc \t20\n"
"Type something here and it will be stored in variable abc \thello\n"
]
}
],
@@ -965,7 +977,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 36,
"metadata": {},
"outputs": [
{
@@ -974,7 +986,7 @@
"str"
]
},
"execution_count": 26,
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
@@ -1000,7 +1012,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
"version": "3.5.4"
}
},
"nbformat": 4,


+ 13
- 6
0_python/2_Print_Statement.ipynb View File

@@ -20,6 +20,13 @@
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**需要注意的是,Python2中`print`是一个语句,但是在Python3变成函数,打印的内容需要用`()`括起来**"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
@@ -40,9 +47,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"在Python中,单引号、双引号和三引号用于表示字符串。\n",
"大部分单引号用于声明一个字符。\n",
"声明一行时使用双引号,声明段落/多行时使用三引号。"
"在Python中,**单引号****双引号****三引号**用于表示字符串。\n",
"* 大部分单引号用于声明一个字符。\n",
"* 声明一行时使用双引号,声明段落/多行时使用三引号。"
]
},
{
@@ -93,7 +100,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"字符串可以分配给变量 _string1_ 和string2,使用`print`语句时可以调用。"
"字符串可以分配给变量 `string1` 和 `string2`,使用`print`语句时可以调用。"
]
},
{
@@ -148,7 +155,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**%s** 用于引用包含字符串的变量。"
"`%s` 用于引用包含字符串的变量。"
]
},
{
@@ -585,7 +592,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
"version": "3.5.4"
}
},
"nbformat": 4,


+ 53
- 118
0_python/3_Data_Structure_1.ipynb View File

@@ -4,14 +4,16 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# 数据结构1"
"# 数据结构 - 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"数据结构是计算机存储、组织数据的方式,简单来说是指相互之间存在一种或多种特定关系的数据元素的集合"
"数据结构是计算机存储、组织数据的方式,简单来说是指相互之间存在一种或多种特定关系的数据元素的集合。\n",
"\n",
"Python中的数据结构设计的非常巧妙,使用起来非常方便,几乎绝大多数的数据结构都可以通过`list`, `tuple`, `dict`, `string`, `set`等表示,因此用户几乎不需要自己定义数据结构,仅仅使用Python内置的数据结构即可完成非常复杂的算法。"
]
},
{
@@ -33,7 +35,9 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"a = []"
@@ -152,7 +156,9 @@
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"y = ['carrot','potato']"
@@ -207,10 +213,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"如果您不了解在Python中索引是如何工作的,那么在嵌套列表中索引可能会非常令人困惑。所以让我们把它分解一下,然后得出一个结论。\n",
"\n",
"让我们在上述嵌套列表中获得数据'apple'。\n",
"首先在索引为0处,有一个列表`['apple','orange']` 而在索引为1处有另外一个列表`['carrot','potato']` 。因此z[0] 应该给我们第一个包含'apple'的列表。"
"如何获得嵌套列表中的某个元素?让我们在上述嵌套列表中获得数据'apple'为例。\n",
"* 首先在索引为0处,有一个列表`['apple','orange']` 而在索引为1处有另外一个列表`['carrot','potato']` 。\n",
"* 因此z[0] 应该给我们第一个包含'apple'的列表。"
]
},
{
@@ -303,9 +308,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"索引只限于访问单个元素,而切片则是访问列表内的一系列数据。换句话说,“切片”列表。\n",
"索引只限于访问单个元素,而切片则是访问列表内的一系列数据。换句话说,`切片`返回的是一个列表。\n",
"\n",
"切片是通过定义切片列表中需要的父列表中的第一个元素和最后一个元素的索引值来完成的。它被写成parentlist[a: b],其中a,b是父列表的索引值。如果a或b未定义,则认为该索引值是a未定义时的第一个值,以及b未定义时的最后一个值。"
"切片是通过定义切片列表中需要的父列表中的第一个元素和最后一个元素的索引值来完成的。它被写成parentlist[a: b],其中`a`,`b`是父列表的索引值。如果`a``b`未定义,则认为该索引值是`a`未定义时的第一个值,以及`b`未定义时的最后一个值。"
]
},
{
@@ -471,7 +476,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"列表可以通过添加'+'来连接。生成的列表将包含添加的列表的所有元素。结果列表将不是嵌套列表。"
"列表可以通过添加\"`+`\"来连接。生成的列表将包含添加的列表的所有元素。结果列表将不是嵌套列表。"
]
},
{
@@ -504,7 +509,9 @@
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"names = ['Earth','Air','Fire','Water']"
@@ -587,7 +594,9 @@
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"mlist = ['bzaa','ds','nc','az','z','klm']"
@@ -622,7 +631,9 @@
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"nlist = ['1','94','93','1000']"
@@ -722,7 +733,9 @@
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"lst = [1,1,4,8,7]"
@@ -783,7 +796,9 @@
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"lst1 = [5,4,2,8]"
@@ -1252,7 +1267,9 @@
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"lista= [2,1,4,3]"
@@ -1333,7 +1350,9 @@
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"lista = [2,1,4,3]"
@@ -1465,7 +1484,9 @@
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"tup = ()\n",
@@ -1593,7 +1614,9 @@
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"(a,b,c)= ('alpha','beta','gamma')"
@@ -1782,7 +1805,9 @@
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"set1 = set([1,2,3])"
@@ -1791,7 +1816,9 @@
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"set2 = set([2,3,4,5])"
@@ -1928,107 +1955,15 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**symmetric_difference( )** 函数输出一个函数,该函数包含一个集合中的元素。"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{0, 1, 4, 5}"
]
},
"execution_count": 95,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"set2.symmetric_difference(set1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**issubset( ), isdisjoint( ), issuperset( )** 分别用于检查set1/set2是否是set2/set1的子集、不相交或超集。"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 96,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"set1.issubset(set2)"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 97,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"set2.isdisjoint(set1)"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 97,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"set2.issuperset(set1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**pop( )** 是用来移除集合中的任意元素。"
]
},
{
"cell_type": "code",
"execution_count": 115,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"set1=set([10, 9, 1, 2, 4])"
@@ -2125,7 +2060,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
"version": "3.5.4"
}
},
"nbformat": 4,


+ 14
- 6
0_python/4_Data_Structure_2.ipynb View File

@@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# 数据结构2\n",
"# 数据结构 - 2\n",
"\n",
"## 1. 字符串"
]
@@ -19,7 +19,9 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"String0 = 'Taj Mahal is beautiful'\n",
@@ -663,7 +665,9 @@
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"f = ' hello '"
@@ -706,7 +710,9 @@
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"f = ' ***----hello---******* '"
@@ -902,7 +908,9 @@
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"names = ['One', 'Two', 'Three', 'Four', 'Five']\n",
@@ -1190,7 +1198,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
"version": "3.5.4"
}
},
"nbformat": 4,


+ 19
- 68
0_python/5_Control_Flow.ipynb View File

@@ -25,7 +25,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 1,
"metadata": {},
"outputs": [
{
@@ -194,7 +194,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"metadata": {},
"outputs": [
{
@@ -216,7 +216,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 6,
"metadata": {},
"outputs": [
{
@@ -375,7 +375,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@@ -416,7 +416,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 11,
"metadata": {},
"outputs": [
{
@@ -461,7 +461,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 12,
"metadata": {},
"outputs": [
{
@@ -489,7 +489,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 13,
"metadata": {},
"outputs": [
{
@@ -498,7 +498,7 @@
"[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]"
]
},
"execution_count": 1,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
@@ -523,7 +523,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 14,
"metadata": {},
"outputs": [
{
@@ -532,7 +532,7 @@
"[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]"
]
},
"execution_count": 12,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
@@ -543,7 +543,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 15,
"metadata": {
"scrolled": true
},
@@ -563,7 +563,7 @@
" '81': 81}"
]
},
"execution_count": 13,
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
@@ -574,7 +574,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 16,
"metadata": {},
"outputs": [
{
@@ -583,7 +583,7 @@
"(27, 54, 81, 108, 135, 162, 189, 216, 243, 270)"
]
},
"execution_count": 14,
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
@@ -601,71 +601,22 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1,\n",
" 2,\n",
" 3,\n",
" 4,\n",
" 5,\n",
" 6,\n",
" 7,\n",
" 8,\n",
" 9,\n",
" 10,\n",
" 28,\n",
" 29,\n",
" 30,\n",
" 31,\n",
" 32,\n",
" 33,\n",
" 34,\n",
" 35,\n",
" 36,\n",
" 37,\n",
" 55,\n",
" 56,\n",
" 57,\n",
" 58,\n",
" 59,\n",
" 60,\n",
" 61,\n",
" 62,\n",
" 63,\n",
" 64,\n",
" 82,\n",
" 83,\n",
" 84,\n",
" 85,\n",
" 86,\n",
" 87,\n",
" 88,\n",
" 89,\n",
" 90,\n",
" 91,\n",
" 109,\n",
" 110,\n",
" 111,\n",
" 112,\n",
" 113,\n",
" 114,\n",
" 115,\n",
" 116,\n",
" 117,\n",
" 118]"
"[1, 2, 3, 4, 28, 29, 30, 31, 55, 56, 57, 58]"
]
},
"execution_count": 15,
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[27*i+z for i in range(50) if i<5 for z in range(1,11)]"
"[27*i+z for i in range(5) if i<3 for z in range(1,5)]"
]
}
],
@@ -685,7 +636,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
"version": "3.5.4"
}
},
"nbformat": 4,


+ 135
- 96
0_python/6_Function.ipynb View File

@@ -46,14 +46,14 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Hey Rajath!\n",
"Rajath, How do you do?\n"
"Hey Jack!\n",
"Jack, How do you do?\n"
]
}
],
"source": [
"print(\"Hey Rajath!\")\n",
"print(\"Rajath, How do you do?\")"
"print(\"Hey Jack!\")\n",
"print(\"Jack, How do you do?\")"
]
},
{
@@ -67,28 +67,30 @@
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def first_func():\n",
" print(\"Hey Rajath!\")\n",
" print(\"Rajath, How do you do?\")"
" print(\"Hey Jack!\")\n",
" print(\"Jack, How do you do?\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hey Rajath!\n",
"Rajath, How do you do?\n",
"Hey Rajath!\n",
"Rajath, How do you do?\n"
"Hey Jack!\n",
"Jack, How do you do?\n",
"Hey Jack!\n",
"Jack, How do you do?\n"
]
}
],
@@ -107,8 +109,10 @@
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def first_func(username):\n",
@@ -118,7 +122,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 5,
"metadata": {},
"outputs": [
{
@@ -142,7 +146,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 6,
"metadata": {},
"outputs": [
{
@@ -167,8 +171,10 @@
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def first_func(username):\n",
@@ -181,16 +187,16 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter your name : Joe\n",
"Hey Joe!\n",
"Joe, How do you do?\n"
"Please enter your name : Tom\n",
"Hey Tom!\n",
"Tom, How do you do?\n"
]
}
],
@@ -214,8 +220,10 @@
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def times(x,y):\n",
@@ -232,7 +240,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@@ -264,8 +272,10 @@
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def times(x,y):\n",
@@ -275,7 +285,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 12,
"metadata": {},
"outputs": [
{
@@ -300,7 +310,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 13,
"metadata": {},
"outputs": [
{
@@ -321,8 +331,10 @@
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"execution_count": 14,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"times?"
@@ -337,8 +349,10 @@
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"execution_count": 15,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"eglist = [10,50,30,12,6,8,100]"
@@ -346,8 +360,10 @@
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"execution_count": 16,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def egfunc(eglist):\n",
@@ -367,7 +383,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 17,
"metadata": {},
"outputs": [
{
@@ -385,7 +401,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 18,
"metadata": {},
"outputs": [
{
@@ -420,11 +436,13 @@
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"execution_count": 20,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def implicitadd(x, addnumber=3):\n",
"def implicit_add(x, addnumber=3):\n",
" return x+addnumber"
]
},
@@ -432,19 +450,19 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**implicitadd( )** 是一个函数接受两个参数,但大多数时候第一个参数只需要加3。因此,第二个参数被赋值为3。这里第二个参数是隐式的。"
"**implicit_add( )** 是一个函数接受两个参数,但大多数时候第一个参数只需要加3。因此,第二个参数被赋值为3。这里第二个参数是隐式的。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"现在,如果在调用**implicitadd()** 函数时没有定义第二个参数,则将其视为3。"
"现在,如果在调用**implicit_add()** 函数时没有定义第二个参数,则将其视为3。"
]
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 21,
"metadata": {},
"outputs": [
{
@@ -453,13 +471,13 @@
"7"
]
},
"execution_count": 23,
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"implicitadd(4)"
"implicit_add(4)"
]
},
{
@@ -471,7 +489,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 22,
"metadata": {},
"outputs": [
{
@@ -480,18 +498,18 @@
"8"
]
},
"execution_count": 24,
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"implicitadd(4,4)"
"implicit_add(4,4)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 23,
"metadata": {},
"outputs": [
{
@@ -500,13 +518,13 @@
"11"
]
},
"execution_count": 25,
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"implicitadd(5, addnumber=6)"
"implicit_add(5, addnumber=6)"
]
},
{
@@ -525,8 +543,10 @@
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"execution_count": 24,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def add_n(*args):\n",
@@ -547,7 +567,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 25,
"metadata": {},
"outputs": [
{
@@ -563,7 +583,7 @@
"15"
]
},
"execution_count": 27,
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
@@ -574,7 +594,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 26,
"metadata": {},
"outputs": [
{
@@ -590,7 +610,7 @@
"6"
]
},
"execution_count": 28,
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
@@ -600,15 +620,22 @@
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"参数列表也可以通过 \"param_name = value\" 的形式传入到函数"
]
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[10, 20, 30]\n"
"[30, 10, 20]\n"
]
},
{
@@ -617,7 +644,7 @@
"60"
]
},
"execution_count": 29,
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
@@ -645,13 +672,15 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"在函数内部声明的变量是局部变量,在函数外部声明的是全局变量。"
"在函数内部声明的变量是局部变量,生命周期限于函数执行期间;在函数外部声明的是全局变量。"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"execution_count": 28,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"eg1 = [1,2,3,4,5]"
@@ -666,8 +695,10 @@
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"execution_count": 29,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def egfunc1():\n",
@@ -686,7 +717,7 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 30,
"metadata": {},
"outputs": [
{
@@ -732,8 +763,10 @@
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"execution_count": 31,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"z = lambda x: x * x"
@@ -741,7 +774,7 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 32,
"metadata": {},
"outputs": [
{
@@ -750,7 +783,7 @@
"64"
]
},
"execution_count": 35,
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
@@ -761,7 +794,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 33,
"metadata": {},
"outputs": [
{
@@ -770,7 +803,7 @@
"(6, 8)"
]
},
"execution_count": 36,
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
@@ -782,7 +815,7 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 34,
"metadata": {},
"outputs": [
{
@@ -791,7 +824,7 @@
"function"
]
},
"execution_count": 37,
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
@@ -802,7 +835,7 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": 35,
"metadata": {},
"outputs": [
{
@@ -811,7 +844,7 @@
"function"
]
},
"execution_count": 38,
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
@@ -839,8 +872,10 @@
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"execution_count": 36,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"list1 = [1,2,3,4,5,6,7,8,9]"
@@ -848,7 +883,7 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 37,
"metadata": {},
"outputs": [
{
@@ -866,7 +901,7 @@
},
{
"cell_type": "code",
"execution_count": 42,
"execution_count": 38,
"metadata": {},
"outputs": [
{
@@ -891,8 +926,10 @@
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"execution_count": 39,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"list2 = [9,8,7,6,5,4,3,2,1]"
@@ -900,7 +937,7 @@
},
{
"cell_type": "code",
"execution_count": 44,
"execution_count": 40,
"metadata": {},
"outputs": [
{
@@ -925,14 +962,14 @@
},
{
"cell_type": "code",
"execution_count": 62,
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<map object at 0x7fd754688198>\n"
"<map object at 0x7fba384fd320>\n"
]
}
],
@@ -957,8 +994,10 @@
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"execution_count": 42,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"list1 = [1,2,3,4,5,6,7,8,9]"
@@ -973,7 +1012,7 @@
},
{
"cell_type": "code",
"execution_count": 46,
"execution_count": 43,
"metadata": {},
"outputs": [
{
@@ -998,16 +1037,16 @@
},
{
"cell_type": "code",
"execution_count": 47,
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<map at 0x7ff0482c5d50>"
"<map at 0x7fba384fd550>"
]
},
"execution_count": 47,
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
@@ -1025,16 +1064,16 @@
},
{
"cell_type": "code",
"execution_count": 65,
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<filter at 0x7fd754688320>"
"<filter at 0x7fba384fd240>"
]
},
"execution_count": 65,
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
@@ -1060,7 +1099,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
"version": "3.5.4"
}
},
"nbformat": 4,


+ 171
- 253
0_python/7_Class.ipynb View File

@@ -35,7 +35,9 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# 一个最简单的类\n",
@@ -60,7 +62,9 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"egclass = FirstClass()"
@@ -124,38 +128,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"这有助于简化实例的初始化过程。例如,\n",
"\n",
"在不使用魔法方法或被成为构造函数的`__init__`的情况下,我们必须定义一个**init()** 方法并调用**init()** 函数。"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'FirstClass' object has no attribute 'init'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-4-d15e7b8e3d78>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0meg0\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mFirstClass\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0meg0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m: 'FirstClass' object has no attribute 'init'"
]
}
],
"source": [
"eg0 = FirstClass()\n",
"eg0.init()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"但是当构造函数被定义后,`__init__`被调用,这样初始化实例被创建。"
"当构造函数被定义后,`__init__`被调用,这样初始化实例被创建。"
]
},
{
@@ -169,16 +142,18 @@
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class FirstClass:\n",
" \"\"\"My first class\"\"\"\n",
" class_var = 10\n",
" def __init__(self,name,symbol):\n",
" def __init__(self,name,value):\n",
" self.name = name\n",
" self.symbol = symbol"
" self.value = value"
]
},
{
@@ -190,8 +165,10 @@
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"eg1 = FirstClass('one',1)\n",
@@ -200,7 +177,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 3,
"metadata": {},
"outputs": [
{
@@ -214,8 +191,8 @@
}
],
"source": [
"print(eg1.name, eg1.symbol)\n",
"print(eg2.name, eg2.symbol)\n",
"print(eg1.name, eg1.value)\n",
"print(eg2.name, eg2.value)\n",
"print(eg1.__doc__)"
]
},
@@ -228,7 +205,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 4,
"metadata": {
"scrolled": false
},
@@ -248,7 +225,6 @@
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__init_subclass__',\n",
" '__le__',\n",
" '__lt__',\n",
" '__module__',\n",
@@ -265,7 +241,7 @@
" 'class_var']"
]
},
"execution_count": 12,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
@@ -276,7 +252,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 5,
"metadata": {},
"outputs": [
{
@@ -285,7 +261,7 @@
"'My first class'"
]
},
"execution_count": 14,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
@@ -303,7 +279,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 6,
"metadata": {},
"outputs": [
{
@@ -321,7 +297,6 @@
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__init_subclass__',\n",
" '__le__',\n",
" '__lt__',\n",
" '__module__',\n",
@@ -337,10 +312,10 @@
" '__weakref__',\n",
" 'class_var',\n",
" 'name',\n",
" 'symbol']"
" 'value']"
]
},
"execution_count": 15,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
@@ -358,14 +333,16 @@
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class FirstClass:\n",
" def __init__(self,name,symbol):\n",
" def __init__(self,name,value):\n",
" self.n = name\n",
" self.s = symbol"
" self.v = value"
]
},
{
@@ -377,8 +354,10 @@
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"eg1 = FirstClass('one',1)\n",
@@ -387,7 +366,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 9,
"metadata": {},
"outputs": [
{
@@ -397,7 +376,7 @@
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-16-4ab7dec1c737>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0meg1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0meg1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msymbol\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0meg2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0meg2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msymbol\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-9-5eb87775240a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0meg1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0meg1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msymbol\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0meg2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0meg2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msymbol\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAttributeError\u001b[0m: 'FirstClass' object has no attribute 'name'"
]
}
@@ -416,7 +395,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@@ -434,7 +413,6 @@
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__init_subclass__',\n",
" '__le__',\n",
" '__lt__',\n",
" '__module__',\n",
@@ -449,10 +427,10 @@
" '__subclasshook__',\n",
" '__weakref__',\n",
" 'n',\n",
" 's']"
" 'v']"
]
},
"execution_count": 17,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
@@ -463,7 +441,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 11,
"metadata": {},
"outputs": [
{
@@ -476,8 +454,8 @@
}
],
"source": [
"print(eg1.n, eg1.s)\n",
"print(eg2.n, eg2.s)"
"print(eg1.n, eg1.v)\n",
"print(eg2.n, eg2.v)"
]
},
{
@@ -486,29 +464,33 @@
"source": [
"现在我们解决了这个错误。现在让我们比较一下我们看到的两个例子。\n",
"\n",
"当我声明self.name和self.symbol时,使用eg1.name和eg1.symbol没有属性错误。当我声明self.n和self.s时,使用eg1.n和eg1.s没有属性错误。\n",
"当我声明self.name和self.value,使用eg1.name和eg1.value没有属性错误。当我声明self.n和self.s时,使用eg1.n和eg1.s没有属性错误。\n",
"\n",
"从以上我们可以得出self就是实例本身。\n",
"从以上我们可以得出**self**就是实例本身。\n",
"\n",
"记住,self不是预定义的,它是用户定义的。你可以利用任何你觉得舒服的东西。但是使用self已经成为一种常见的做法。"
"记住,**self**不是Python的关键词,它是用户定义的。你可以利用任何你觉得舒服的东西。但是使用self已经成为一种常见的做法。"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class FirstClass:\n",
" def __init__(asdf1234,name,symbol):\n",
" def __init__(asdf1234,name,value):\n",
" asdf1234.n = name\n",
" asdf1234.s = symbol"
" asdf1234.v = value"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"execution_count": 13,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"eg1 = FirstClass('one',1)\n",
@@ -517,7 +499,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 14,
"metadata": {},
"outputs": [
{
@@ -530,8 +512,8 @@
}
],
"source": [
"print(eg1.n, eg1.s)\n",
"print(eg2.n, eg2.s)"
"print(eg1.n, eg1.v)\n",
"print(eg2.n, eg2.v)"
]
},
{
@@ -543,8 +525,10 @@
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"execution_count": 15,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"eg1.cube = 1\n",
@@ -553,7 +537,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 16,
"metadata": {},
"outputs": [
{
@@ -571,7 +555,6 @@
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__init_subclass__',\n",
" '__le__',\n",
" '__lt__',\n",
" '__module__',\n",
@@ -587,10 +570,10 @@
" '__weakref__',\n",
" 'cube',\n",
" 'n',\n",
" 's']"
" 'v']"
]
},
"execution_count": 24,
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
@@ -615,14 +598,16 @@
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class FirstClass:\n",
" test = 'test'\n",
" def __init__(self,name,symbol):\n",
" def __init__(self,name,value):\n",
" self.name = name\n",
" self.symbol = symbol"
" self.value = value"
]
},
{
@@ -634,7 +619,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 17,
"metadata": {},
"outputs": [
{
@@ -654,14 +639,18 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"test Three\n"
"ename": "AttributeError",
"evalue": "'FirstClass' object has no attribute 'test'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-18-91e356838a25>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0meg3\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtest\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0meg3\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m: 'FirstClass' object has no attribute 'test'"
]
}
],
@@ -678,26 +667,30 @@
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"execution_count": 19,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class FirstClass:\n",
" def __init__(self,name,symbol):\n",
" def __init__(self,name,value):\n",
" self.name = name\n",
" self.symbol = symbol\n",
" self.value = value\n",
" def square(self):\n",
" return self.symbol * self.symbol\n",
" return self.value * self.value\n",
" def cube(self):\n",
" return self.symbol * self.symbol * self.symbol\n",
" return self.value * self.value * self.value\n",
" def multiply(self, x):\n",
" return self.symbol * x"
" return self.value * x"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"execution_count": 20,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"eg4 = FirstClass('Five',5)"
@@ -705,7 +698,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 21,
"metadata": {},
"outputs": [
{
@@ -724,7 +717,7 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 22,
"metadata": {},
"outputs": [
{
@@ -733,7 +726,7 @@
"10"
]
},
"execution_count": 33,
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
@@ -751,7 +744,7 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 23,
"metadata": {},
"outputs": [
{
@@ -760,7 +753,7 @@
"10"
]
},
"execution_count": 34,
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
@@ -792,8 +785,10 @@
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"execution_count": 25,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class Person:\n",
@@ -807,23 +802,23 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"a = Person('Kartik',26)"
"a = Person('Jerry',26)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Kartik earns 40000\n"
"Jerry earns 40000\n"
]
}
],
@@ -833,7 +828,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 28,
"metadata": {},
"outputs": [
{
@@ -851,7 +846,6 @@
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__init_subclass__',\n",
" '__le__',\n",
" '__lt__',\n",
" '__module__',\n",
@@ -868,7 +862,7 @@
" 'salary']"
]
},
"execution_count": 27,
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
@@ -886,8 +880,10 @@
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"execution_count": 29,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class Artist:\n",
@@ -904,24 +900,26 @@
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"execution_count": 30,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"b = Artist('Nitin',20)"
"b = Artist('Nick',20)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Nitin earns 50000\n",
"Nitin is a Musician\n"
"Nick earns 50000\n",
"Nick is a Musician\n"
]
}
],
@@ -932,7 +930,7 @@
},
{
"cell_type": "code",
"execution_count": 43,
"execution_count": 32,
"metadata": {},
"outputs": [
{
@@ -950,7 +948,6 @@
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__init_subclass__',\n",
" '__le__',\n",
" '__lt__',\n",
" '__module__',\n",
@@ -968,7 +965,7 @@
" 'salary']"
]
},
"execution_count": 43,
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
@@ -986,8 +983,10 @@
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"execution_count": 33,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class Artist(Person):\n",
@@ -998,16 +997,18 @@
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"execution_count": 34,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"c = Artist('Nishanth',21)"
"c = Artist('Tom',21)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 35,
"metadata": {},
"outputs": [
{
@@ -1025,7 +1026,6 @@
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__init_subclass__',\n",
" '__le__',\n",
" '__lt__',\n",
" '__module__',\n",
@@ -1043,7 +1043,7 @@
" 'salary']"
]
},
"execution_count": 34,
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
@@ -1054,15 +1054,15 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Nishanth earns 60000\n",
"Nishanth is a Dancer\n"
"Tom earns 60000\n",
"Tom is a Dancer\n"
]
}
],
@@ -1081,7 +1081,9 @@
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class Artist(Person):\n",
@@ -1096,25 +1098,27 @@
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"execution_count": 38,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"c = Artist('Nishanth',21)"
"c = Artist('Tom',21)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"execution_count": 39,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Nishanth earns 60000\n",
"Tom earns 60000\n",
"I am overriding the SoftwareEngineer class's salary method\n",
"Nishanth is a Dancer\n"
"Tom is a Dancer\n"
]
}
],
@@ -1127,118 +1131,28 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"如果不确定方法将被调用多少次,那么就很难声明那么多变量来携带每个结果,因此最好声明一个列表并附加结果。"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"class EmptyList:\n",
" def __init__(self):\n",
" self.data = []\n",
" def one(self,x):\n",
" self.data.append(x)\n",
" def two(self, x ):\n",
" self.data.append(x**2)\n",
" def three(self, x):\n",
" self.data.append(x**3)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"xc = EmptyList()"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1]\n"
]
}
],
"source": [
"xc.one(1)\n",
"print(xc.data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"因为xc.data是一个列表,直接的列表操作也是可以进行的。"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 8]\n"
]
}
],
"source": [
"xc.data.append(8)\n",
"print(xc.data)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 8, 9]\n"
]
}
],
"source": [
"xc.two(3)\n",
"print(xc.data)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"如果输入参数的数量因实例而异,则可以使用星号。"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class NotSure:\n",
" def __init__(self, *args):\n",
" self.data = ''.join(list(args)) "
" self.data = ' '.join(list(args)) "
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"execution_count": 44,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"yz = NotSure('I', 'Do' , 'Not', 'Know', 'What', 'To','Type')"
@@ -1246,16 +1160,16 @@
},
{
"cell_type": "code",
"execution_count": 64,
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'IDoNotKnowWhatToType'"
"'I Do Not Know What To Type'"
]
},
"execution_count": 64,
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
@@ -1275,7 +1189,12 @@
"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,仅仅看教程是不够的,需要做大量的练习题,可以使用教程里列的练习题,也可以自己找各个方面的练习题。\n",
"\n",
"* 编程比较重要的培养编程思维,如果抄别人写好的代码,发现不了Python的窍门、技巧,因此需要独立自主完成编程练习,也可以给自己出一些小项目,病解决它们,你还可以在任何编程竞赛平台上提交问题求解。\n",
"* 你编写的代码越多,你发现的越多,你就越开始欣赏这门语言。\n",
"* 强烈建议把[《Python作业》](https://gitee.com/pi-lab/machinelearning_homework/blob/master/homework_01_python/README.md)完成\n",
"* 在完成基本的编程习题之后,可以在[《其他编程练习》](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",
"\n",
@@ -1284,16 +1203,15 @@
"* [Python tutorial (廖雪峰)](https://www.liaoxuefeng.com/wiki/1016959663602400)\n",
"* [Python基础教程](https://www.runoob.com/python/python-tutorial.html)\n",
"* [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"
"* [Python官方文档](https://docs.python.org/3/)\n",
"* [跟海龟学Python](https://gitee.com/pi-lab/python_turtle)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**最后,享受解决问题的快乐!因为生命短暂,你需要Python!**"
"## **最后,享受解决问题的快乐!因为生命短暂,你需要Python!**"
]
}
],
@@ -1313,7 +1231,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
"version": "3.5.4"
}
},
"nbformat": 4,


+ 1
- 0
0_python/README.md View File

@@ -34,6 +34,7 @@ Python 是一门上手简单、功能强大、通用型的脚本编程语言。P
* [安装Python环境](../references_tips/InstallPython.md)
* [IPython Notebooks to learn Python](https://github.com/rajathkmp/Python-Lectures)
* [廖雪峰的Python教程](https://www.liaoxuefeng.com/wiki/1016959663602400)
* [跟海龟学Python](https://gitee.com/pi-lab/python_turtle)
* [智能系统实验室入门教程-Python](https://gitee.com/pi-lab/SummerCamp/tree/master/python)
* [Python Tips](../references_tips/python)
* [Get Started with Python](Python.pdf)

Loading…
Cancel
Save