diff --git a/01.01 python 基础.ipynb b/01.01 python 基础.ipynb index a0a6950..529b05c 100644 --- a/01.01 python 基础.ipynb +++ b/01.01 python 基础.ipynb @@ -34,7 +34,7 @@ "metadata": {}, "outputs": [], "source": [ - "print (\"hello world!\")" + "print(\"hello world!\")" ] }, { @@ -149,8 +149,8 @@ "metadata": {}, "outputs": [], "source": [ - "print (min(2, 3, 4, 5))\n", - "print (max(2, 4, 3))" + "print(min(2, 3, 4, 5))\n", + "print(max(2, 4, 3))" ] }, { @@ -167,7 +167,7 @@ "metadata": {}, "outputs": [], "source": [ - "print (int(-3.32))" + "print(int(-3.32))" ] }, { @@ -183,7 +183,7 @@ "metadata": {}, "outputs": [], "source": [ - "print (float(1))" + "print(float(1))" ] }, { @@ -239,7 +239,7 @@ "outputs": [], "source": [ "s = \"hello, world\"\n", - "print (s)" + "print(s)" ] }, { @@ -249,7 +249,7 @@ "outputs": [], "source": [ "s = 'hello, world'\n", - "print (s)" + "print(s)" ] }, { @@ -305,7 +305,7 @@ "source": [ "line = \"1 2 3 4 5\"\n", "numbers = line.split()\n", - "print (numbers)\n", + "print(numbers)\n", "type(numbers)" ] }, @@ -372,10 +372,10 @@ "outputs": [], "source": [ "s = \"HELLO WORLD\"\n", - "print (s.lower())\n", + "print(s.lower())\n", "\n", "# 不会改变原来s的值\n", - "print (s)" + "print(s)" ] }, { @@ -488,7 +488,7 @@ "outputs": [], "source": [ "l = [1, 2.0, 'hello']\n", - "print (l)" + "print(l)" ] }, { @@ -646,7 +646,7 @@ "source": [ "a = [10, 11, 12]\n", "a.append(11)\n", - "print (a)" + "print(a)" ] }, { @@ -664,7 +664,7 @@ "source": [ "a = [10, 11, 12]\n", "a.append(['a', 'b'])\n", - "print (a)" + "print(a)" ] }, { @@ -683,7 +683,7 @@ "source": [ "a = [10, 11, 12]\n", "a.extend(['a', 'b'])\n", - "print (a)" + "print(a)" ] }, { @@ -703,7 +703,7 @@ "a = [10, 11, 12, 13, 11]\n", "# 在索引 3 插入 'a'\n", "a.insert(3, 'a')\n", - "print (a)" + "print(a)" ] }, { @@ -729,7 +729,7 @@ "# 根据下标进行删除\n", "a = [1002, 'a', 'b', 'c']\n", "del a[0]\n", - "print (a)" + "print(a)" ] }, { @@ -748,7 +748,7 @@ "source": [ "a = [1002, 'a', 'b', 'c']\n", "a.pop()\n", - "print (a)" + "print(a)" ] }, { @@ -767,7 +767,7 @@ "source": [ "a = [1002, 'a', 'b', 'c', 'b']\n", "a.remove(\"b\")\n", - "print (a)" + "print(a)" ] }, { @@ -787,8 +787,8 @@ "outputs": [], "source": [ "a = [10, 11, 12, 13, 11]\n", - "print (10 in a)\n", - "print (10 not in a)" + "print(10 in a)\n", + "print(10 not in a)" ] }, { @@ -870,7 +870,7 @@ "# 从小到大排序\n", "a = [10, 1, 11, 13, 11, 2]\n", "a.sort()\n", - "print (a)" + "print(a)" ] }, { @@ -882,7 +882,7 @@ "# 从大到小排序\n", "a = [10, 1, 11, 13, 11, 2]\n", "a.sort(reverse=True)\n", - "print (a)" + "print(a)" ] }, { @@ -900,8 +900,8 @@ "source": [ "a = [10, 1, 11, 13, 11, 2]\n", "b = sorted(a)\n", - "print (\"a:\",a)\n", - "print (\"b:\",b)" + "print(\"a:\",a)\n", + "print(\"b:\",b)" ] }, { @@ -926,7 +926,7 @@ "source": [ "a = [10, 1, 11, 13, 11, 2]\n", "a.reverse()\n", - "print (a)" + "print(a)" ] }, { @@ -944,8 +944,8 @@ "source": [ "a = [10, 1, 11, 13, 11, 2]\n", "b = a[::-1]\n", - "print (\"a:\",a)\n", - "print (\"b:\",b)" + "print(\"a:\",a)\n", + "print(\"b:\",b)" ] }, { @@ -1312,8 +1312,8 @@ "source": [ "x = 0.5\n", "if x > 0:\n", - " print (\"Hey!\")\n", - " print (\"x is positive\")" + " print(\"Hey!\")\n", + " print(\"x is positive\")" ] }, { @@ -1348,10 +1348,10 @@ "source": [ "x = 0.5\n", "if x > 0:\n", - " print (\"Hey!\")\n", - " print (\"x is positive\")\n", - " print (\"This is still part of the block\")\n", - "print (\"This isn't part of the block, and will always print.\")" + " print(\"Hey!\")\n", + " print(\"x is positive\")\n", + " print(\"This is still part of the block\")\n", + "print(\"This isn't part of the block, and will always print.\")" ] }, { @@ -1369,10 +1369,10 @@ "source": [ "x = -0.5\n", "if x > 0:\n", - " print (\"Hey!\")\n", - " print (\"x is positive\")\n", - " print (\"This is still part of the block\")\n", - "print (\"This isn't part of the block, and will always print.\")\n" + " print(\"Hey!\")\n", + " print(\"x is positive\")\n", + " print(\"This is still part of the block\")\n", + "print(\"This isn't part of the block, and will always print.\")\n" ] }, { @@ -1404,11 +1404,11 @@ "source": [ "x = 0\n", "if x > 0:\n", - " print (\"x is positive\")\n", + " print(\"x is positive\")\n", "elif x == 0:\n", - " print (\"x is zero\")\n", + " print(\"x is zero\")\n", "else:\n", - " print (\"x is negative\")\n" + " print(\"x is negative\")\n" ] }, { @@ -1466,12 +1466,12 @@ "source": [ "year = 1900\n", "if year % 400 == 0:\n", - " print (\"This is a leap year!\")\n", + " print(\"This is a leap year!\")\n", "# 两个条件都满足才执行\n", "elif year % 4 == 0 and year % 100 != 0:\n", - " print (\"This is a leap year!\")\n", + " print(\"This is a leap year!\")\n", "else:\n", - " print (\"This is not a leap year.\")\n" + " print(\"This is not a leap year.\")\n" ] }, { @@ -1503,9 +1503,9 @@ "source": [ "mylist = [3, 1, 4, 1, 5, 9]\n", "if mylist:\n", - " print (\"The first element is:\", mylist[0])\n", + " print(\"The first element is:\", mylist[0])\n", "else:\n", - " print (\"There is no first element.\")" + " print(\"There is no first element.\")" ] }, { @@ -1523,9 +1523,9 @@ "source": [ "mylist = []\n", "if mylist:\n", - " print (\"The first element is:\", mylist[0])\n", + " print(\"The first element is:\", mylist[0])\n", "else:\n", - " print (\"There is no first element.\")\n" + " print(\"There is no first element.\")\n" ] }, { @@ -1563,15 +1563,13 @@ "\n", "# 循环条件\n", "while i < 100:\n", - " \n", " # 求和累加\n", " total += i\n", - " \n", " # 变量递增\n", " i += 1\n", " \n", "# 打印结果\n", - "print (total)" + "print(total)" ] }, { @@ -1590,7 +1588,7 @@ "plays = ['Hamlet', 'Macbeth', 'King Lear']\n", "while plays:\n", " play = plays.pop()\n", - " print ('Perform', play)" + " print('Perform', play)" ] }, { @@ -1622,7 +1620,7 @@ "source": [ "plays = ['Hamlet', 'Macbeth', 'King Lear']\n", "for play in plays:\n", - " print ('Perform', play)" + " print('Perform', play)" ] }, { @@ -1643,7 +1641,7 @@ "total = 0\n", "for i in range(100):\n", " total += i\n", - "print (total)" + "print(total)" ] }, { @@ -1668,7 +1666,7 @@ " if i % 2 != 0:\n", " # 忽略奇数\n", " continue\n", - " print (i/2)" + " print(i/2)" ] }, { @@ -1731,10 +1729,10 @@ "values = [7, 6, 4, 7, 19, 2, 1]\n", "for x in values:\n", " if x <= 10:\n", - " print ('Found:', x)\n", + " print('Found:', x)\n", " break\n", "else:\n", - " print ('All values greater than 10')\n" + " print('All values greater than 10')\n" ] }, { @@ -1753,10 +1751,10 @@ "values = [11, 12, 13, 100]\n", "for x in values:\n", " if x <= 10:\n", - " print ('Found:', x)\n", + " print('Found:', x)\n", " break\n", "else:\n", - " print ('All values greater than 10')\n" + " print('All values greater than 10')\n" ] } ], diff --git a/01.02 python 进阶.ipynb b/01.02 python 进阶.ipynb index 69c6dbd..577197a 100644 --- a/01.02 python 进阶.ipynb +++ b/01.02 python 进阶.ipynb @@ -173,8 +173,8 @@ "metadata": {}, "outputs": [], "source": [ - "print (add(2, 3))\n", - "print (add('foo', 'bar'))" + "print(add(2, 3))\n", + "print(add('foo', 'bar'))" ] }, { @@ -190,7 +190,7 @@ "metadata": {}, "outputs": [], "source": [ - "print (add(2, \"foo\"))" + "print(add(2, \"foo\"))" ] }, { @@ -222,8 +222,8 @@ "metadata": {}, "outputs": [], "source": [ - "print (add(x=2, y=3))\n", - "print (add(y=\"foo\", x=\"bar\"))" + "print(add(x=2, y=3))\n", + "print(add(y=\"foo\", x=\"bar\"))" ] }, { @@ -239,7 +239,7 @@ "metadata": {}, "outputs": [], "source": [ - "print (add(2, y=3))" + "print(add(2, y=3))" ] }, { @@ -274,7 +274,7 @@ "metadata": {}, "outputs": [], "source": [ - "print (quad(2.0))" + "print(quad(2.0))" ] }, { @@ -290,16 +290,16 @@ "metadata": {}, "outputs": [], "source": [ - "print (quad(2.0, b=3))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "print (quad(2.0, 2, c=4))" + "print(quad(2.0, b=3))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(quad(2.0, 2, c=4))" ] }, { @@ -317,7 +317,7 @@ "metadata": {}, "outputs": [], "source": [ - "print (quad(2.0, 2, a=2))" + "print(quad(2.0, 2, a=2))" ] }, { @@ -355,8 +355,8 @@ "metadata": {}, "outputs": [], "source": [ - "print (add(1, 2, 3, 4))\n", - "print (add(1, 2))\n" + "print(add(1, 2, 3, 4))\n", + "print(add(1, 2))\n" ] }, { @@ -375,7 +375,7 @@ "def add(x, **kwargs):\n", " total = x\n", " for arg, value in kwargs.items():\n", - " print (\"adding %s=%s\"%(arg,value))\n", + " print(\"adding %s=%s\"%(arg,value))\n", " total += value\n", " return total\n" ] @@ -393,7 +393,7 @@ "metadata": {}, "outputs": [], "source": [ - "print (add(10, y=11, z=12, w=13))" + "print(add(10, y=11, z=12, w=13))" ] }, { @@ -410,7 +410,7 @@ "outputs": [], "source": [ "def foo(*args, **kwargs):\n", - " print (args, kwargs)\n", + " print(args, kwargs)\n", "\n", "foo(2, 3, x='bar', z=10)\n" ] @@ -488,7 +488,7 @@ "outputs": [], "source": [ "a, b, c = [1, 2, 3]\n", - "print (a, b, c)" + "print(a, b, c)" ] }, { @@ -588,7 +588,7 @@ "\n", "a = (10, 6, 7)\n", "b = [2, 5, 3]\n", - "print (list(map(divid,a,b)))" + "print(list(map(divid,a,b)))" ] }, { @@ -633,7 +633,7 @@ " return tot\n", "\n", "w = [0, 1, 2, 3]\n", - "print (sum(w), PI)\n", + "print(sum(w), PI)\n", "\n" ] }, @@ -697,7 +697,7 @@ "metadata": {}, "outputs": [], "source": [ - "print (ex1.PI)" + "print(ex1.PI)" ] }, { @@ -707,7 +707,7 @@ "outputs": [], "source": [ "ex1.PI = 3.141592653\n", - "print (ex1.PI)" + "print(ex1.PI)" ] }, { @@ -727,7 +727,7 @@ "metadata": {}, "outputs": [], "source": [ - "print (ex1.sum([2, 3, 4]))" + "print(ex1.sum([2, 3, 4]))" ] }, { @@ -819,7 +819,7 @@ "def test():\n", " w = [0,1,2,3]\n", " assert(sum(w) == 6)\n", - " print ('test passed.')\n", + " print('test passed.')\n", "\n", "if __name__ == '__main__':\n", " test()\n" @@ -1014,7 +1014,7 @@ " break\n", " x = float(text)\n", " y = math.log10(x)\n", - " print (\"log10({0}) = {1}\".format(x, y))\n", + " print(\"log10({0}) = {1}\".format(x, y))\n", "```\n", "\n", "这段代码接收命令行的输入,当输入为数字时,计算它的对数并输出,直到输入值为 `q` 为止。\n", @@ -1036,7 +1036,7 @@ " break\n", " x = float(text)\n", " y = math.log10(x)\n", - " print (\"log10({0}) = {1}\".format(x, y))\n" + " print(\"log10({0}) = {1}\".format(x, y))\n" ] }, { @@ -1063,9 +1063,9 @@ " break\n", " x = float(text)\n", " y = math.log10(x)\n", - " print \"log10({0}) = {1}\".format(x, y)\n", + " print(\"log10({0}) = {1}\".format(x, y))\n", " except ValueError:\n", - " print (\"the value must be greater than 0\")\n", + " print(\"the value must be greater than 0\")\n", "```" ] }, @@ -1093,9 +1093,9 @@ " break\n", " x = float(text)\n", " y = math.log10(x)\n", - " print (\"log10({0}) = {1}\".format(x, y))\n", + " print(\"log10({0}) = {1}\".format(x, y))\n", " except ValueError:\n", - " print (\"the value must be greater than 0\")\n" + " print(\"the value must be greater than 0\")\n" ] }, { @@ -1119,9 +1119,9 @@ " break\n", " x = float(text)\n", " y = 1 / math.log10(x)\n", - " print \"log10({0}) = {1}\".format(x, y)\n", + " print(\"log10({0}) = {1}\".format(x, y))\n", " except ValueError:\n", - " print \"the value must be greater than 0\"\n", + " print(\"the value must be greater than 0\")\n", "```\n", "\n", "假设我们将这里的 `y` 更改为 `1 / math.log10(x)`,此时输入 `1`:" @@ -1142,9 +1142,9 @@ " break\n", " x = float(text)\n", " y = 1 / math.log10(x)\n", - " print (\"log10({0}) = {1}\".format(x, y))\n", + " print(\"log10({0}) = {1}\".format(x, y))\n", " except ValueError:\n", - " print (\"the value must be greater than 0\")\n" + " print(\"the value must be greater than 0\")\n" ] }, { @@ -1178,9 +1178,9 @@ " break\n", " x = float(text)\n", " y = 1 / math.log10(x)\n", - " print (\"1 / log10({0}) = {1}\".format(x, y))\n", + " print(\"1 / log10({0}) = {1}\".format(x, y))\n", " except Exception:\n", - " print (\"invalid value\")\n" + " print(\"invalid value\")\n" ] }, { @@ -1207,9 +1207,9 @@ " break\n", " x = float(text)\n", " y = 1 / math.log10(x)\n", - " print (\"1 / log10({0}) = {1}\".format(x, y))\n", + " print(\"1 / log10({0}) = {1}\".format(x, y))\n", " except (ValueError, ZeroDivisionError):\n", - " print (\"invalid value\")\n" + " print(\"invalid value\")\n" ] }, { @@ -1234,11 +1234,11 @@ " break\n", " x = float(text)\n", " y = 1 / math.log10(x)\n", - " print (\"1 / log10({0}) = {1}\".format(x, y))\n", + " print(\"1 / log10({0}) = {1}\".format(x, y))\n", " except ValueError:\n", - " print (\"the value must be greater than 0\")\n", + " print(\"the value must be greater than 0\")\n", " except ZeroDivisionError:\n", - " print (\"the value must not be 1\")\n" + " print(\"the value must not be 1\")\n" ] }, { @@ -1263,13 +1263,13 @@ " break\n", " x = float(text)\n", " y = 1 / math.log10(x)\n", - " print (\"1 / log10({0}) = {1}\".format(x, y))\n", + " print(\"1 / log10({0}) = {1}\".format(x, y))\n", " except ValueError:\n", - " print (\"the value must be greater than 0\")\n", + " print(\"the value must be greater than 0\")\n", " except ZeroDivisionError:\n", - " print (\"the value must not be 1\")\n", + " print(\"the value must not be 1\")\n", " except Exception:\n", - " print (\"unexpected error\")\n" + " print(\"unexpected error\")\n" ] }, { @@ -1303,16 +1303,16 @@ " break\n", " x = float(text)\n", " y = 1 / math.log10(x)\n", - " print (\"1 / log10({0}) = {1}\".format(x, y))\n", + " print(\"1 / log10({0}) = {1}\".format(x, y))\n", " except ValueError as exc:\n", " if exc.message == \"math domain error\":\n", - " print (\"the value must be greater than 0\")\n", + " print(\"the value must be greater than 0\")\n", " else:\n", - " print (\"could not convert '%s' to float\" % text)\n", + " print(\"could not convert '%s' to float\" % text)\n", " except ZeroDivisionError:\n", - " print (\"the value must not be 1\")\n", + " print(\"the value must not be 1\")\n", " except Exception as exc:\n", - " print (\"unexpected error:\", exc.message)\n" + " print(\"unexpected error:\", exc.message)\n" ] }, { @@ -1359,11 +1359,11 @@ "outputs": [], "source": [ "try:\n", - " print (1)\n", + " print(1)\n", "except:\n", " pass\n", "else:\n", - " print ('else was called.')" + " print('else was called.')" ] }, { @@ -1380,11 +1380,11 @@ "outputs": [], "source": [ "try:\n", - " print (1/0)\n", + " print(1/0)\n", "except ZeroDivisionError:\n", - " print ('divide by 0.')\n", + " print('divide by 0.')\n", "else:\n", - " print ('else was called.')" + " print('else was called.')" ] }, { @@ -1405,9 +1405,9 @@ "outputs": [], "source": [ "try:\n", - " print (1)\n", + " print(1)\n", "finally:\n", - " print ('finally was called.')" + " print('finally was called.')" ] }, { @@ -1424,9 +1424,9 @@ "outputs": [], "source": [ "try:\n", - " print (1 / 0)\n", + " print(1 / 0)\n", "finally:\n", - " print ('finally was called.')" + " print('finally was called.')" ] }, { @@ -1443,11 +1443,11 @@ "outputs": [], "source": [ "try:\n", - " print (1 / 0)\n", + " print(1 / 0)\n", "except ZeroDivisionError:\n", - " print ('divide by 0.')\n", + " print('divide by 0.')\n", "finally:\n", - " print ('finally was called.')\n" + " print('finally was called.')\n" ] }, { @@ -1595,7 +1595,7 @@ "outputs": [], "source": [ "text = f.read()\n", - "print (text)" + "print(text)" ] }, { @@ -1613,7 +1613,7 @@ "source": [ "f = open('test.txt')\n", "lines = f.readlines()\n", - "print (lines)\n" + "print(lines)\n" ] }, { @@ -1647,7 +1647,7 @@ "source": [ "f = open('test.txt')\n", "for line in f:\n", - " print (line)\n", + " print(line)\n", "f.close()\n" ] }, @@ -1706,7 +1706,7 @@ "metadata": {}, "outputs": [], "source": [ - "print (open('myfile.txt').read())" + "print(open('myfile.txt').read())" ] }, { @@ -1725,7 +1725,7 @@ "f = open('myfile.txt', 'w')\n", "f.write('another hello world!')\n", "f.close()\n", - "print (open('myfile.txt').read())\n" + "print(open('myfile.txt').read())\n" ] }, { @@ -1744,7 +1744,7 @@ "f = open('myfile.txt', 'a')\n", "f.write('... and more')\n", "f.close()\n", - "print (open('myfile.txt').read())\n" + "print(open('myfile.txt').read())\n" ] }, { @@ -1765,7 +1765,7 @@ "f = open('myfile.txt', 'w+')\n", "f.write('hello world!')\n", "f.seek(6)\n", - "print (f.read())\n", + "print(f.read())\n", "f.close()\n" ] }, @@ -1815,7 +1815,7 @@ "f = open('newfile.txt','w')\n", "f.write('hello world')\n", "g = open('newfile.txt', 'r')\n", - "print (repr(g.read()))\n" + "print(repr(g.read()))\n" ] }, { @@ -1838,7 +1838,7 @@ " f.write('hello world: ' + str(i) + '\\n')\n", "\n", "g = open('newfile.txt', 'r')\n", - "print (g.read())\n", + "print(g.read())\n", "f.close()\n", "g.close()\n" ] @@ -1886,7 +1886,7 @@ "outputs": [], "source": [ "g = open('newfile.txt', 'r')\n", - "print (g.read())\n", + "print(g.read())\n", "f.close()\n", "g.close()\n" ] @@ -2045,7 +2045,7 @@ "\n", "# 可以按行迭代数据\n", "for row in r:\n", - " print (row)\n", + " print(row)\n", "\n", "# 关闭文件\n", "fp.close()\n" diff --git a/01.03 机器学习常用的包.ipynb b/01.03 机器学习常用的包.ipynb index 9adc028..8f2da4b 100644 --- a/01.03 机器学习常用的包.ipynb +++ b/01.03 机器学习常用的包.ipynb @@ -1052,7 +1052,6 @@ "source": [ "# 生成 series\n", "s = pd.Series([1,3,5,np.nan,6,8])\n", - "\n", "print(s)" ] }, @@ -1064,9 +1063,7 @@ "source": [ "# 生成 dataframe \n", "dates = pd.date_range('20200101', periods=15)\n", - "\n", "df = pd.DataFrame(np.random.randn(15,4), index=dates, columns=list('ABCD'))\n", - "\n", "df" ] },