d0da19c
joyvan 6 years ago
3 changed file(s) with 135 addition(s) and 140 deletion(s). Raw diff Collapse all Expand all
3333 "metadata": {},
3434 "outputs": [],
3535 "source": [
36 "print (\"hello world!\")"
36 "print(\"hello world!\")"
3737 ]
3838 },
3939 {
148148 "metadata": {},
149149 "outputs": [],
150150 "source": [
151 "print (min(2, 3, 4, 5))\n",
152 "print (max(2, 4, 3))"
151 "print(min(2, 3, 4, 5))\n",
152 "print(max(2, 4, 3))"
153153 ]
154154 },
155155 {
166166 "metadata": {},
167167 "outputs": [],
168168 "source": [
169 "print (int(-3.32))"
169 "print(int(-3.32))"
170170 ]
171171 },
172172 {
182182 "metadata": {},
183183 "outputs": [],
184184 "source": [
185 "print (float(1))"
185 "print(float(1))"
186186 ]
187187 },
188188 {
238238 "outputs": [],
239239 "source": [
240240 "s = \"hello, world\"\n",
241 "print (s)"
241 "print(s)"
242242 ]
243243 },
244244 {
248248 "outputs": [],
249249 "source": [
250250 "s = 'hello, world'\n",
251 "print (s)"
251 "print(s)"
252252 ]
253253 },
254254 {
304304 "source": [
305305 "line = \"1 2 3 4 5\"\n",
306306 "numbers = line.split()\n",
307 "print (numbers)\n",
307 "print(numbers)\n",
308308 "type(numbers)"
309309 ]
310310 },
371371 "outputs": [],
372372 "source": [
373373 "s = \"HELLO WORLD\"\n",
374 "print (s.lower())\n",
374 "print(s.lower())\n",
375375 "\n",
376376 "# 不会改变原来s的值\n",
377 "print (s)"
377 "print(s)"
378378 ]
379379 },
380380 {
487487 "outputs": [],
488488 "source": [
489489 "l = [1, 2.0, 'hello']\n",
490 "print (l)"
490 "print(l)"
491491 ]
492492 },
493493 {
645645 "source": [
646646 "a = [10, 11, 12]\n",
647647 "a.append(11)\n",
648 "print (a)"
648 "print(a)"
649649 ]
650650 },
651651 {
663663 "source": [
664664 "a = [10, 11, 12]\n",
665665 "a.append(['a', 'b'])\n",
666 "print (a)"
666 "print(a)"
667667 ]
668668 },
669669 {
682682 "source": [
683683 "a = [10, 11, 12]\n",
684684 "a.extend(['a', 'b'])\n",
685 "print (a)"
685 "print(a)"
686686 ]
687687 },
688688 {
702702 "a = [10, 11, 12, 13, 11]\n",
703703 "# 在索引 3 插入 'a'\n",
704704 "a.insert(3, 'a')\n",
705 "print (a)"
705 "print(a)"
706706 ]
707707 },
708708 {
728728 "# 根据下标进行删除\n",
729729 "a = [1002, 'a', 'b', 'c']\n",
730730 "del a[0]\n",
731 "print (a)"
731 "print(a)"
732732 ]
733733 },
734734 {
747747 "source": [
748748 "a = [1002, 'a', 'b', 'c']\n",
749749 "a.pop()\n",
750 "print (a)"
750 "print(a)"
751751 ]
752752 },
753753 {
766766 "source": [
767767 "a = [1002, 'a', 'b', 'c', 'b']\n",
768768 "a.remove(\"b\")\n",
769 "print (a)"
769 "print(a)"
770770 ]
771771 },
772772 {
786786 "outputs": [],
787787 "source": [
788788 "a = [10, 11, 12, 13, 11]\n",
789 "print (10 in a)\n",
790 "print (10 not in a)"
789 "print(10 in a)\n",
790 "print(10 not in a)"
791791 ]
792792 },
793793 {
869869 "# 从小到大排序\n",
870870 "a = [10, 1, 11, 13, 11, 2]\n",
871871 "a.sort()\n",
872 "print (a)"
872 "print(a)"
873873 ]
874874 },
875875 {
881881 "# 从大到小排序\n",
882882 "a = [10, 1, 11, 13, 11, 2]\n",
883883 "a.sort(reverse=True)\n",
884 "print (a)"
884 "print(a)"
885885 ]
886886 },
887887 {
899899 "source": [
900900 "a = [10, 1, 11, 13, 11, 2]\n",
901901 "b = sorted(a)\n",
902 "print (\"a:\",a)\n",
903 "print (\"b:\",b)"
902 "print(\"a:\",a)\n",
903 "print(\"b:\",b)"
904904 ]
905905 },
906906 {
925925 "source": [
926926 "a = [10, 1, 11, 13, 11, 2]\n",
927927 "a.reverse()\n",
928 "print (a)"
928 "print(a)"
929929 ]
930930 },
931931 {
943943 "source": [
944944 "a = [10, 1, 11, 13, 11, 2]\n",
945945 "b = a[::-1]\n",
946 "print (\"a:\",a)\n",
947 "print (\"b:\",b)"
946 "print(\"a:\",a)\n",
947 "print(\"b:\",b)"
948948 ]
949949 },
950950 {
13111311 "source": [
13121312 "x = 0.5\n",
13131313 "if x > 0:\n",
1314 " print (\"Hey!\")\n",
1315 " print (\"x is positive\")"
1314 " print(\"Hey!\")\n",
1315 " print(\"x is positive\")"
13161316 ]
13171317 },
13181318 {
13471347 "source": [
13481348 "x = 0.5\n",
13491349 "if x > 0:\n",
1350 " print (\"Hey!\")\n",
1351 " print (\"x is positive\")\n",
1352 " print (\"This is still part of the block\")\n",
1353 "print (\"This isn't part of the block, and will always print.\")"
1350 " print(\"Hey!\")\n",
1351 " print(\"x is positive\")\n",
1352 " print(\"This is still part of the block\")\n",
1353 "print(\"This isn't part of the block, and will always print.\")"
13541354 ]
13551355 },
13561356 {
13681368 "source": [
13691369 "x = -0.5\n",
13701370 "if x > 0:\n",
1371 " print (\"Hey!\")\n",
1372 " print (\"x is positive\")\n",
1373 " print (\"This is still part of the block\")\n",
1374 "print (\"This isn't part of the block, and will always print.\")\n"
1371 " print(\"Hey!\")\n",
1372 " print(\"x is positive\")\n",
1373 " print(\"This is still part of the block\")\n",
1374 "print(\"This isn't part of the block, and will always print.\")\n"
13751375 ]
13761376 },
13771377 {
14031403 "source": [
14041404 "x = 0\n",
14051405 "if x > 0:\n",
1406 " print (\"x is positive\")\n",
1406 " print(\"x is positive\")\n",
14071407 "elif x == 0:\n",
1408 " print (\"x is zero\")\n",
1408 " print(\"x is zero\")\n",
14091409 "else:\n",
1410 " print (\"x is negative\")\n"
1410 " print(\"x is negative\")\n"
14111411 ]
14121412 },
14131413 {
14651465 "source": [
14661466 "year = 1900\n",
14671467 "if year % 400 == 0:\n",
1468 " print (\"This is a leap year!\")\n",
1468 " print(\"This is a leap year!\")\n",
14691469 "# 两个条件都满足才执行\n",
14701470 "elif year % 4 == 0 and year % 100 != 0:\n",
1471 " print (\"This is a leap year!\")\n",
1471 " print(\"This is a leap year!\")\n",
14721472 "else:\n",
1473 " print (\"This is not a leap year.\")\n"
1473 " print(\"This is not a leap year.\")\n"
14741474 ]
14751475 },
14761476 {
15021502 "source": [
15031503 "mylist = [3, 1, 4, 1, 5, 9]\n",
15041504 "if mylist:\n",
1505 " print (\"The first element is:\", mylist[0])\n",
1505 " print(\"The first element is:\", mylist[0])\n",
15061506 "else:\n",
1507 " print (\"There is no first element.\")"
1507 " print(\"There is no first element.\")"
15081508 ]
15091509 },
15101510 {
15221522 "source": [
15231523 "mylist = []\n",
15241524 "if mylist:\n",
1525 " print (\"The first element is:\", mylist[0])\n",
1525 " print(\"The first element is:\", mylist[0])\n",
15261526 "else:\n",
1527 " print (\"There is no first element.\")\n"
1527 " print(\"There is no first element.\")\n"
15281528 ]
15291529 },
15301530 {
15621562 "\n",
15631563 "# 循环条件\n",
15641564 "while i < 100:\n",
1565 " \n",
15661565 " # 求和累加\n",
15671566 " total += i\n",
1568 " \n",
15691567 " # 变量递增\n",
15701568 " i += 1\n",
15711569 " \n",
15721570 "# 打印结果\n",
1573 "print (total)"
1571 "print(total)"
15741572 ]
15751573 },
15761574 {
15891587 "plays = ['Hamlet', 'Macbeth', 'King Lear']\n",
15901588 "while plays:\n",
15911589 " play = plays.pop()\n",
1592 " print ('Perform', play)"
1590 " print('Perform', play)"
15931591 ]
15941592 },
15951593 {
16211619 "source": [
16221620 "plays = ['Hamlet', 'Macbeth', 'King Lear']\n",
16231621 "for play in plays:\n",
1624 " print ('Perform', play)"
1622 " print('Perform', play)"
16251623 ]
16261624 },
16271625 {
16421640 "total = 0\n",
16431641 "for i in range(100):\n",
16441642 " total += i\n",
1645 "print (total)"
1643 "print(total)"
16461644 ]
16471645 },
16481646 {
16671665 " if i % 2 != 0:\n",
16681666 " # 忽略奇数\n",
16691667 " continue\n",
1670 " print (i/2)"
1668 " print(i/2)"
16711669 ]
16721670 },
16731671 {
17301728 "values = [7, 6, 4, 7, 19, 2, 1]\n",
17311729 "for x in values:\n",
17321730 " if x <= 10:\n",
1733 " print ('Found:', x)\n",
1731 " print('Found:', x)\n",
17341732 " break\n",
17351733 "else:\n",
1736 " print ('All values greater than 10')\n"
1734 " print('All values greater than 10')\n"
17371735 ]
17381736 },
17391737 {
17521750 "values = [11, 12, 13, 100]\n",
17531751 "for x in values:\n",
17541752 " if x <= 10:\n",
1755 " print ('Found:', x)\n",
1753 " print('Found:', x)\n",
17561754 " break\n",
17571755 "else:\n",
1758 " print ('All values greater than 10')\n"
1756 " print('All values greater than 10')\n"
17591757 ]
17601758 }
17611759 ],
172172 "metadata": {},
173173 "outputs": [],
174174 "source": [
175 "print (add(2, 3))\n",
176 "print (add('foo', 'bar'))"
175 "print(add(2, 3))\n",
176 "print(add('foo', 'bar'))"
177177 ]
178178 },
179179 {
189189 "metadata": {},
190190 "outputs": [],
191191 "source": [
192 "print (add(2, \"foo\"))"
192 "print(add(2, \"foo\"))"
193193 ]
194194 },
195195 {
221221 "metadata": {},
222222 "outputs": [],
223223 "source": [
224 "print (add(x=2, y=3))\n",
225 "print (add(y=\"foo\", x=\"bar\"))"
224 "print(add(x=2, y=3))\n",
225 "print(add(y=\"foo\", x=\"bar\"))"
226226 ]
227227 },
228228 {
238238 "metadata": {},
239239 "outputs": [],
240240 "source": [
241 "print (add(2, y=3))"
241 "print(add(2, y=3))"
242242 ]
243243 },
244244 {
273273 "metadata": {},
274274 "outputs": [],
275275 "source": [
276 "print (quad(2.0))"
276 "print(quad(2.0))"
277277 ]
278278 },
279279 {
289289 "metadata": {},
290290 "outputs": [],
291291 "source": [
292 "print (quad(2.0, b=3))"
293 ]
294 },
295 {
296 "cell_type": "code",
297 "execution_count": null,
298 "metadata": {},
299 "outputs": [],
300 "source": [
301 "print (quad(2.0, 2, c=4))"
292 "print(quad(2.0, b=3))"
293 ]
294 },
295 {
296 "cell_type": "code",
297 "execution_count": null,
298 "metadata": {},
299 "outputs": [],
300 "source": [
301 "print(quad(2.0, 2, c=4))"
302302 ]
303303 },
304304 {
316316 "metadata": {},
317317 "outputs": [],
318318 "source": [
319 "print (quad(2.0, 2, a=2))"
319 "print(quad(2.0, 2, a=2))"
320320 ]
321321 },
322322 {
354354 "metadata": {},
355355 "outputs": [],
356356 "source": [
357 "print (add(1, 2, 3, 4))\n",
358 "print (add(1, 2))\n"
357 "print(add(1, 2, 3, 4))\n",
358 "print(add(1, 2))\n"
359359 ]
360360 },
361361 {
374374 "def add(x, **kwargs):\n",
375375 " total = x\n",
376376 " for arg, value in kwargs.items():\n",
377 " print (\"adding %s=%s\"%(arg,value))\n",
377 " print(\"adding %s=%s\"%(arg,value))\n",
378378 " total += value\n",
379379 " return total\n"
380380 ]
392392 "metadata": {},
393393 "outputs": [],
394394 "source": [
395 "print (add(10, y=11, z=12, w=13))"
395 "print(add(10, y=11, z=12, w=13))"
396396 ]
397397 },
398398 {
409409 "outputs": [],
410410 "source": [
411411 "def foo(*args, **kwargs):\n",
412 " print (args, kwargs)\n",
412 " print(args, kwargs)\n",
413413 "\n",
414414 "foo(2, 3, x='bar', z=10)\n"
415415 ]
487487 "outputs": [],
488488 "source": [
489489 "a, b, c = [1, 2, 3]\n",
490 "print (a, b, c)"
490 "print(a, b, c)"
491491 ]
492492 },
493493 {
587587 "\n",
588588 "a = (10, 6, 7)\n",
589589 "b = [2, 5, 3]\n",
590 "print (list(map(divid,a,b)))"
590 "print(list(map(divid,a,b)))"
591591 ]
592592 },
593593 {
632632 " return tot\n",
633633 "\n",
634634 "w = [0, 1, 2, 3]\n",
635 "print (sum(w), PI)\n",
635 "print(sum(w), PI)\n",
636636 "\n"
637637 ]
638638 },
696696 "metadata": {},
697697 "outputs": [],
698698 "source": [
699 "print (ex1.PI)"
699 "print(ex1.PI)"
700700 ]
701701 },
702702 {
706706 "outputs": [],
707707 "source": [
708708 "ex1.PI = 3.141592653\n",
709 "print (ex1.PI)"
709 "print(ex1.PI)"
710710 ]
711711 },
712712 {
726726 "metadata": {},
727727 "outputs": [],
728728 "source": [
729 "print (ex1.sum([2, 3, 4]))"
729 "print(ex1.sum([2, 3, 4]))"
730730 ]
731731 },
732732 {
818818 "def test():\n",
819819 " w = [0,1,2,3]\n",
820820 " assert(sum(w) == 6)\n",
821 " print ('test passed.')\n",
821 " print('test passed.')\n",
822822 "\n",
823823 "if __name__ == '__main__':\n",
824824 " test()\n"
10131013 " break\n",
10141014 " x = float(text)\n",
10151015 " y = math.log10(x)\n",
1016 " print (\"log10({0}) = {1}\".format(x, y))\n",
1016 " print(\"log10({0}) = {1}\".format(x, y))\n",
10171017 "```\n",
10181018 "\n",
10191019 "这段代码接收命令行的输入,当输入为数字时,计算它的对数并输出,直到输入值为 `q` 为止。\n",
10351035 " break\n",
10361036 " x = float(text)\n",
10371037 " y = math.log10(x)\n",
1038 " print (\"log10({0}) = {1}\".format(x, y))\n"
1038 " print(\"log10({0}) = {1}\".format(x, y))\n"
10391039 ]
10401040 },
10411041 {
10621062 " break\n",
10631063 " x = float(text)\n",
10641064 " y = math.log10(x)\n",
1065 " print \"log10({0}) = {1}\".format(x, y)\n",
1065 " print(\"log10({0}) = {1}\".format(x, y))\n",
10661066 " except ValueError:\n",
1067 " print (\"the value must be greater than 0\")\n",
1067 " print(\"the value must be greater than 0\")\n",
10681068 "```"
10691069 ]
10701070 },
10921092 " break\n",
10931093 " x = float(text)\n",
10941094 " y = math.log10(x)\n",
1095 " print (\"log10({0}) = {1}\".format(x, y))\n",
1095 " print(\"log10({0}) = {1}\".format(x, y))\n",
10961096 " except ValueError:\n",
1097 " print (\"the value must be greater than 0\")\n"
1097 " print(\"the value must be greater than 0\")\n"
10981098 ]
10991099 },
11001100 {
11181118 " break\n",
11191119 " x = float(text)\n",
11201120 " y = 1 / math.log10(x)\n",
1121 " print \"log10({0}) = {1}\".format(x, y)\n",
1121 " print(\"log10({0}) = {1}\".format(x, y))\n",
11221122 " except ValueError:\n",
1123 " print \"the value must be greater than 0\"\n",
1123 " print(\"the value must be greater than 0\")\n",
11241124 "```\n",
11251125 "\n",
11261126 "假设我们将这里的 `y` 更改为 `1 / math.log10(x)`,此时输入 `1`:"
11411141 " break\n",
11421142 " x = float(text)\n",
11431143 " y = 1 / math.log10(x)\n",
1144 " print (\"log10({0}) = {1}\".format(x, y))\n",
1144 " print(\"log10({0}) = {1}\".format(x, y))\n",
11451145 " except ValueError:\n",
1146 " print (\"the value must be greater than 0\")\n"
1146 " print(\"the value must be greater than 0\")\n"
11471147 ]
11481148 },
11491149 {
11771177 " break\n",
11781178 " x = float(text)\n",
11791179 " y = 1 / math.log10(x)\n",
1180 " print (\"1 / log10({0}) = {1}\".format(x, y))\n",
1180 " print(\"1 / log10({0}) = {1}\".format(x, y))\n",
11811181 " except Exception:\n",
1182 " print (\"invalid value\")\n"
1182 " print(\"invalid value\")\n"
11831183 ]
11841184 },
11851185 {
12061206 " break\n",
12071207 " x = float(text)\n",
12081208 " y = 1 / math.log10(x)\n",
1209 " print (\"1 / log10({0}) = {1}\".format(x, y))\n",
1209 " print(\"1 / log10({0}) = {1}\".format(x, y))\n",
12101210 " except (ValueError, ZeroDivisionError):\n",
1211 " print (\"invalid value\")\n"
1211 " print(\"invalid value\")\n"
12121212 ]
12131213 },
12141214 {
12331233 " break\n",
12341234 " x = float(text)\n",
12351235 " y = 1 / math.log10(x)\n",
1236 " print (\"1 / log10({0}) = {1}\".format(x, y))\n",
1236 " print(\"1 / log10({0}) = {1}\".format(x, y))\n",
12371237 " except ValueError:\n",
1238 " print (\"the value must be greater than 0\")\n",
1238 " print(\"the value must be greater than 0\")\n",
12391239 " except ZeroDivisionError:\n",
1240 " print (\"the value must not be 1\")\n"
1240 " print(\"the value must not be 1\")\n"
12411241 ]
12421242 },
12431243 {
12621262 " break\n",
12631263 " x = float(text)\n",
12641264 " y = 1 / math.log10(x)\n",
1265 " print (\"1 / log10({0}) = {1}\".format(x, y))\n",
1265 " print(\"1 / log10({0}) = {1}\".format(x, y))\n",
12661266 " except ValueError:\n",
1267 " print (\"the value must be greater than 0\")\n",
1267 " print(\"the value must be greater than 0\")\n",
12681268 " except ZeroDivisionError:\n",
1269 " print (\"the value must not be 1\")\n",
1269 " print(\"the value must not be 1\")\n",
12701270 " except Exception:\n",
1271 " print (\"unexpected error\")\n"
1271 " print(\"unexpected error\")\n"
12721272 ]
12731273 },
12741274 {
13021302 " break\n",
13031303 " x = float(text)\n",
13041304 " y = 1 / math.log10(x)\n",
1305 " print (\"1 / log10({0}) = {1}\".format(x, y))\n",
1305 " print(\"1 / log10({0}) = {1}\".format(x, y))\n",
13061306 " except ValueError as exc:\n",
13071307 " if exc.message == \"math domain error\":\n",
1308 " print (\"the value must be greater than 0\")\n",
1308 " print(\"the value must be greater than 0\")\n",
13091309 " else:\n",
1310 " print (\"could not convert '%s' to float\" % text)\n",
1310 " print(\"could not convert '%s' to float\" % text)\n",
13111311 " except ZeroDivisionError:\n",
1312 " print (\"the value must not be 1\")\n",
1312 " print(\"the value must not be 1\")\n",
13131313 " except Exception as exc:\n",
1314 " print (\"unexpected error:\", exc.message)\n"
1314 " print(\"unexpected error:\", exc.message)\n"
13151315 ]
13161316 },
13171317 {
13581358 "outputs": [],
13591359 "source": [
13601360 "try:\n",
1361 " print (1)\n",
1361 " print(1)\n",
13621362 "except:\n",
13631363 " pass\n",
13641364 "else:\n",
1365 " print ('else was called.')"
1365 " print('else was called.')"
13661366 ]
13671367 },
13681368 {
13791379 "outputs": [],
13801380 "source": [
13811381 "try:\n",
1382 " print (1/0)\n",
1382 " print(1/0)\n",
13831383 "except ZeroDivisionError:\n",
1384 " print ('divide by 0.')\n",
1384 " print('divide by 0.')\n",
13851385 "else:\n",
1386 " print ('else was called.')"
1386 " print('else was called.')"
13871387 ]
13881388 },
13891389 {
14041404 "outputs": [],
14051405 "source": [
14061406 "try:\n",
1407 " print (1)\n",
1407 " print(1)\n",
14081408 "finally:\n",
1409 " print ('finally was called.')"
1409 " print('finally was called.')"
14101410 ]
14111411 },
14121412 {
14231423 "outputs": [],
14241424 "source": [
14251425 "try:\n",
1426 " print (1 / 0)\n",
1426 " print(1 / 0)\n",
14271427 "finally:\n",
1428 " print ('finally was called.')"
1428 " print('finally was called.')"
14291429 ]
14301430 },
14311431 {
14421442 "outputs": [],
14431443 "source": [
14441444 "try:\n",
1445 " print (1 / 0)\n",
1445 " print(1 / 0)\n",
14461446 "except ZeroDivisionError:\n",
1447 " print ('divide by 0.')\n",
1447 " print('divide by 0.')\n",
14481448 "finally:\n",
1449 " print ('finally was called.')\n"
1449 " print('finally was called.')\n"
14501450 ]
14511451 },
14521452 {
15941594 "outputs": [],
15951595 "source": [
15961596 "text = f.read()\n",
1597 "print (text)"
1597 "print(text)"
15981598 ]
15991599 },
16001600 {
16121612 "source": [
16131613 "f = open('test.txt')\n",
16141614 "lines = f.readlines()\n",
1615 "print (lines)\n"
1615 "print(lines)\n"
16161616 ]
16171617 },
16181618 {
16461646 "source": [
16471647 "f = open('test.txt')\n",
16481648 "for line in f:\n",
1649 " print (line)\n",
1649 " print(line)\n",
16501650 "f.close()\n"
16511651 ]
16521652 },
17051705 "metadata": {},
17061706 "outputs": [],
17071707 "source": [
1708 "print (open('myfile.txt').read())"
1708 "print(open('myfile.txt').read())"
17091709 ]
17101710 },
17111711 {
17241724 "f = open('myfile.txt', 'w')\n",
17251725 "f.write('another hello world!')\n",
17261726 "f.close()\n",
1727 "print (open('myfile.txt').read())\n"
1727 "print(open('myfile.txt').read())\n"
17281728 ]
17291729 },
17301730 {
17431743 "f = open('myfile.txt', 'a')\n",
17441744 "f.write('... and more')\n",
17451745 "f.close()\n",
1746 "print (open('myfile.txt').read())\n"
1746 "print(open('myfile.txt').read())\n"
17471747 ]
17481748 },
17491749 {
17641764 "f = open('myfile.txt', 'w+')\n",
17651765 "f.write('hello world!')\n",
17661766 "f.seek(6)\n",
1767 "print (f.read())\n",
1767 "print(f.read())\n",
17681768 "f.close()\n"
17691769 ]
17701770 },
18141814 "f = open('newfile.txt','w')\n",
18151815 "f.write('hello world')\n",
18161816 "g = open('newfile.txt', 'r')\n",
1817 "print (repr(g.read()))\n"
1817 "print(repr(g.read()))\n"
18181818 ]
18191819 },
18201820 {
18371837 " f.write('hello world: ' + str(i) + '\\n')\n",
18381838 "\n",
18391839 "g = open('newfile.txt', 'r')\n",
1840 "print (g.read())\n",
1840 "print(g.read())\n",
18411841 "f.close()\n",
18421842 "g.close()\n"
18431843 ]
18851885 "outputs": [],
18861886 "source": [
18871887 "g = open('newfile.txt', 'r')\n",
1888 "print (g.read())\n",
1888 "print(g.read())\n",
18891889 "f.close()\n",
18901890 "g.close()\n"
18911891 ]
20442044 "\n",
20452045 "# 可以按行迭代数据\n",
20462046 "for row in r:\n",
2047 " print (row)\n",
2047 " print(row)\n",
20482048 "\n",
20492049 "# 关闭文件\n",
20502050 "fp.close()\n"
10511051 "source": [
10521052 "# 生成 series\n",
10531053 "s = pd.Series([1,3,5,np.nan,6,8])\n",
1054 "\n",
10551054 "print(s)"
10561055 ]
10571056 },
10631062 "source": [
10641063 "# 生成 dataframe \n",
10651064 "dates = pd.date_range('20200101', periods=15)\n",
1066 "\n",
10671065 "df = pd.DataFrame(np.random.randn(15,4), index=dates, columns=list('ABCD'))\n",
1068 "\n",
10691066 "df"
10701067 ]
10711068 },