Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 33.51 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# string & print fuction"
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": 1,
  13. "metadata": {
  14. "ExecuteTime": {
  15. "end_time": "2019-07-05T14:02:02.383542Z",
  16. "start_time": "2019-07-05T14:02:02.369578Z"
  17. }
  18. },
  19. "outputs": [
  20. {
  21. "name": "stdout",
  22. "output_type": "stream",
  23. "text": [
  24. "Hello, world!\n",
  25. "Hello, world!\n"
  26. ]
  27. }
  28. ],
  29. "source": [
  30. "print(\"Hello, world!\")\n",
  31. "print('Hello, world!')"
  32. ]
  33. },
  34. {
  35. "cell_type": "code",
  36. "execution_count": 37,
  37. "metadata": {
  38. "ExecuteTime": {
  39. "end_time": "2019-07-05T14:14:07.729480Z",
  40. "start_time": "2019-07-05T14:14:07.724517Z"
  41. }
  42. },
  43. "outputs": [
  44. {
  45. "name": "stdout",
  46. "output_type": "stream",
  47. "text": [
  48. "0123456789\n",
  49. "0\n"
  50. ]
  51. }
  52. ],
  53. "source": [
  54. "s = '0123456789'\n",
  55. "print(s)\n",
  56. "print(s[0])"
  57. ]
  58. },
  59. {
  60. "cell_type": "code",
  61. "execution_count": 36,
  62. "metadata": {
  63. "ExecuteTime": {
  64. "end_time": "2019-07-05T14:14:02.005765Z",
  65. "start_time": "2019-07-05T14:14:02.002740Z"
  66. }
  67. },
  68. "outputs": [
  69. {
  70. "name": "stdout",
  71. "output_type": "stream",
  72. "text": [
  73. "0123456\n",
  74. "789\n"
  75. ]
  76. }
  77. ],
  78. "source": [
  79. "print(s[:7])\n",
  80. "print(s[7:])"
  81. ]
  82. },
  83. {
  84. "cell_type": "code",
  85. "execution_count": 42,
  86. "metadata": {
  87. "ExecuteTime": {
  88. "end_time": "2019-07-05T14:14:41.380763Z",
  89. "start_time": "2019-07-05T14:14:41.376774Z"
  90. }
  91. },
  92. "outputs": [
  93. {
  94. "name": "stdout",
  95. "output_type": "stream",
  96. "text": [
  97. "9\n",
  98. "789\n",
  99. "0123456\n"
  100. ]
  101. }
  102. ],
  103. "source": [
  104. "print(s[-1])\n",
  105. "\n",
  106. "print(s[-3:])\n",
  107. "print(s[:-3])"
  108. ]
  109. },
  110. {
  111. "cell_type": "code",
  112. "execution_count": 43,
  113. "metadata": {
  114. "ExecuteTime": {
  115. "end_time": "2019-07-05T14:15:20.891823Z",
  116. "start_time": "2019-07-05T14:15:20.887825Z"
  117. }
  118. },
  119. "outputs": [
  120. {
  121. "name": "stdout",
  122. "output_type": "stream",
  123. "text": [
  124. "0123456789\n"
  125. ]
  126. }
  127. ],
  128. "source": [
  129. "s1 = '012345'\n",
  130. "s2 = '6789'\n",
  131. "print(s1 + s2)"
  132. ]
  133. },
  134. {
  135. "cell_type": "markdown",
  136. "metadata": {},
  137. "source": [
  138. "# if"
  139. ]
  140. },
  141. {
  142. "cell_type": "code",
  143. "execution_count": 7,
  144. "metadata": {
  145. "ExecuteTime": {
  146. "end_time": "2019-07-05T14:04:53.270628Z",
  147. "start_time": "2019-07-05T14:04:53.267635Z"
  148. }
  149. },
  150. "outputs": [
  151. {
  152. "name": "stdout",
  153. "output_type": "stream",
  154. "text": [
  155. "Yes\n"
  156. ]
  157. }
  158. ],
  159. "source": [
  160. "a = 10\n",
  161. "\n",
  162. "if a is 10:\n",
  163. " print('Yes')\n",
  164. "else:\n",
  165. " print('No')"
  166. ]
  167. },
  168. {
  169. "cell_type": "code",
  170. "execution_count": 10,
  171. "metadata": {
  172. "ExecuteTime": {
  173. "end_time": "2019-07-05T14:05:01.301839Z",
  174. "start_time": "2019-07-05T14:05:01.297851Z"
  175. }
  176. },
  177. "outputs": [
  178. {
  179. "name": "stdout",
  180. "output_type": "stream",
  181. "text": [
  182. "Yes\n"
  183. ]
  184. }
  185. ],
  186. "source": [
  187. "a = 10\n",
  188. "\n",
  189. "if a == 10:\n",
  190. " print('Yes')\n",
  191. "else:\n",
  192. " print('No')"
  193. ]
  194. },
  195. {
  196. "cell_type": "code",
  197. "execution_count": 11,
  198. "metadata": {
  199. "ExecuteTime": {
  200. "end_time": "2019-07-05T14:05:06.653557Z",
  201. "start_time": "2019-07-05T14:05:06.650565Z"
  202. }
  203. },
  204. "outputs": [
  205. {
  206. "name": "stdout",
  207. "output_type": "stream",
  208. "text": [
  209. "Yes\n"
  210. ]
  211. }
  212. ],
  213. "source": [
  214. "a = 10.55\n",
  215. "\n",
  216. "if a == 10.55:\n",
  217. " print('Yes')\n",
  218. "else:\n",
  219. " print('No')"
  220. ]
  221. },
  222. {
  223. "cell_type": "code",
  224. "execution_count": 160,
  225. "metadata": {
  226. "ExecuteTime": {
  227. "end_time": "2019-07-05T15:13:27.176536Z",
  228. "start_time": "2019-07-05T15:13:27.172546Z"
  229. }
  230. },
  231. "outputs": [
  232. {
  233. "name": "stdout",
  234. "output_type": "stream",
  235. "text": [
  236. "No\n"
  237. ]
  238. }
  239. ],
  240. "source": [
  241. "a = 10.55\n",
  242. "\n",
  243. "if a is 10.55:\n",
  244. " print('Yes')\n",
  245. "else:\n",
  246. " print('No')"
  247. ]
  248. },
  249. {
  250. "cell_type": "code",
  251. "execution_count": 159,
  252. "metadata": {
  253. "ExecuteTime": {
  254. "end_time": "2019-07-05T15:13:25.309230Z",
  255. "start_time": "2019-07-05T15:13:25.306207Z"
  256. }
  257. },
  258. "outputs": [
  259. {
  260. "name": "stdout",
  261. "output_type": "stream",
  262. "text": [
  263. "Yes\n"
  264. ]
  265. }
  266. ],
  267. "source": [
  268. "a = 'python'\n",
  269. "\n",
  270. "if a is 'python':\n",
  271. " print('Yes')\n",
  272. "else:\n",
  273. " print('No')"
  274. ]
  275. },
  276. {
  277. "cell_type": "code",
  278. "execution_count": 158,
  279. "metadata": {
  280. "ExecuteTime": {
  281. "end_time": "2019-07-05T15:13:22.000877Z",
  282. "start_time": "2019-07-05T15:13:21.995889Z"
  283. }
  284. },
  285. "outputs": [
  286. {
  287. "name": "stdout",
  288. "output_type": "stream",
  289. "text": [
  290. "Yes\n"
  291. ]
  292. }
  293. ],
  294. "source": [
  295. "a = 'python'\n",
  296. "\n",
  297. "if a == 'python':\n",
  298. " print('Yes')\n",
  299. "else:\n",
  300. " print('No')"
  301. ]
  302. },
  303. {
  304. "cell_type": "code",
  305. "execution_count": 154,
  306. "metadata": {
  307. "ExecuteTime": {
  308. "end_time": "2019-07-05T15:12:54.737936Z",
  309. "start_time": "2019-07-05T15:12:54.734909Z"
  310. }
  311. },
  312. "outputs": [
  313. {
  314. "name": "stdout",
  315. "output_type": "stream",
  316. "text": [
  317. "Yes\n"
  318. ]
  319. }
  320. ],
  321. "source": [
  322. "a = None\n",
  323. "\n",
  324. "if a is None:\n",
  325. " print('Yes')\n",
  326. "else:\n",
  327. " print('No')"
  328. ]
  329. },
  330. {
  331. "cell_type": "code",
  332. "execution_count": 157,
  333. "metadata": {
  334. "ExecuteTime": {
  335. "end_time": "2019-07-05T15:13:17.836663Z",
  336. "start_time": "2019-07-05T15:13:17.832674Z"
  337. }
  338. },
  339. "outputs": [
  340. {
  341. "name": "stdout",
  342. "output_type": "stream",
  343. "text": [
  344. "Yes\n"
  345. ]
  346. }
  347. ],
  348. "source": [
  349. "a = None\n",
  350. "\n",
  351. "if a == None:\n",
  352. " print('Yes')\n",
  353. "else:\n",
  354. " print('No')"
  355. ]
  356. },
  357. {
  358. "cell_type": "code",
  359. "execution_count": 155,
  360. "metadata": {
  361. "ExecuteTime": {
  362. "end_time": "2019-07-05T15:13:01.884652Z",
  363. "start_time": "2019-07-05T15:13:01.880662Z"
  364. }
  365. },
  366. "outputs": [
  367. {
  368. "name": "stdout",
  369. "output_type": "stream",
  370. "text": [
  371. "No\n"
  372. ]
  373. }
  374. ],
  375. "source": [
  376. "a = None\n",
  377. "\n",
  378. "if None:\n",
  379. " print('Yes')\n",
  380. "else:\n",
  381. " print('No')"
  382. ]
  383. },
  384. {
  385. "cell_type": "markdown",
  386. "metadata": {},
  387. "source": [
  388. "# + - * / %"
  389. ]
  390. },
  391. {
  392. "cell_type": "code",
  393. "execution_count": 17,
  394. "metadata": {
  395. "ExecuteTime": {
  396. "end_time": "2019-07-05T14:07:39.079192Z",
  397. "start_time": "2019-07-05T14:07:39.075205Z"
  398. }
  399. },
  400. "outputs": [
  401. {
  402. "ename": "SyntaxError",
  403. "evalue": "invalid syntax (<ipython-input-17-fd7858b5020a>, line 3)",
  404. "output_type": "error",
  405. "traceback": [
  406. "\u001b[1;36m File \u001b[1;32m\"<ipython-input-17-fd7858b5020a>\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m a ++\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
  407. ]
  408. }
  409. ],
  410. "source": [
  411. "a = 1\n",
  412. "print(a)\n",
  413. "a ++"
  414. ]
  415. },
  416. {
  417. "cell_type": "code",
  418. "execution_count": 26,
  419. "metadata": {
  420. "ExecuteTime": {
  421. "end_time": "2019-07-05T14:10:15.021490Z",
  422. "start_time": "2019-07-05T14:10:15.017502Z"
  423. }
  424. },
  425. "outputs": [
  426. {
  427. "name": "stdout",
  428. "output_type": "stream",
  429. "text": [
  430. "2\n"
  431. ]
  432. }
  433. ],
  434. "source": [
  435. "a = 1\n",
  436. "a += 1\n",
  437. "print(a)"
  438. ]
  439. },
  440. {
  441. "cell_type": "code",
  442. "execution_count": 15,
  443. "metadata": {
  444. "ExecuteTime": {
  445. "end_time": "2019-07-05T14:07:26.110306Z",
  446. "start_time": "2019-07-05T14:07:26.107314Z"
  447. }
  448. },
  449. "outputs": [
  450. {
  451. "name": "stdout",
  452. "output_type": "stream",
  453. "text": [
  454. "19\n"
  455. ]
  456. }
  457. ],
  458. "source": [
  459. "a = 20\n",
  460. "a -= 1\n",
  461. "print(a)"
  462. ]
  463. },
  464. {
  465. "cell_type": "code",
  466. "execution_count": 20,
  467. "metadata": {
  468. "ExecuteTime": {
  469. "end_time": "2019-07-05T14:08:13.259955Z",
  470. "start_time": "2019-07-05T14:08:13.256960Z"
  471. }
  472. },
  473. "outputs": [
  474. {
  475. "name": "stdout",
  476. "output_type": "stream",
  477. "text": [
  478. "5.0\n",
  479. "5\n"
  480. ]
  481. }
  482. ],
  483. "source": [
  484. "a = 10\n",
  485. "a = a / 2\n",
  486. "\n",
  487. "print(a)\n",
  488. "print(int(a))"
  489. ]
  490. },
  491. {
  492. "cell_type": "code",
  493. "execution_count": 128,
  494. "metadata": {
  495. "ExecuteTime": {
  496. "end_time": "2019-07-05T14:56:26.036475Z",
  497. "start_time": "2019-07-05T14:56:26.031489Z"
  498. }
  499. },
  500. "outputs": [
  501. {
  502. "name": "stdout",
  503. "output_type": "stream",
  504. "text": [
  505. "5\n",
  506. "5.0\n"
  507. ]
  508. }
  509. ],
  510. "source": [
  511. "a = 10\n",
  512. "a = a // 2\n",
  513. "\n",
  514. "print(a)\n",
  515. "print(float(a))"
  516. ]
  517. },
  518. {
  519. "cell_type": "code",
  520. "execution_count": 129,
  521. "metadata": {
  522. "ExecuteTime": {
  523. "end_time": "2019-07-05T14:57:05.869745Z",
  524. "start_time": "2019-07-05T14:57:05.866753Z"
  525. }
  526. },
  527. "outputs": [
  528. {
  529. "name": "stdout",
  530. "output_type": "stream",
  531. "text": [
  532. "20\n"
  533. ]
  534. }
  535. ],
  536. "source": [
  537. "a = 10\n",
  538. "a = a * 2\n",
  539. "print(a)"
  540. ]
  541. },
  542. {
  543. "cell_type": "code",
  544. "execution_count": 130,
  545. "metadata": {
  546. "ExecuteTime": {
  547. "end_time": "2019-07-05T14:57:06.483103Z",
  548. "start_time": "2019-07-05T14:57:06.479115Z"
  549. }
  550. },
  551. "outputs": [
  552. {
  553. "name": "stdout",
  554. "output_type": "stream",
  555. "text": [
  556. "100\n"
  557. ]
  558. }
  559. ],
  560. "source": [
  561. "# pow\n",
  562. "\n",
  563. "a = 10\n",
  564. "a = a ** 2\n",
  565. "print(a)"
  566. ]
  567. },
  568. {
  569. "cell_type": "code",
  570. "execution_count": 131,
  571. "metadata": {
  572. "ExecuteTime": {
  573. "end_time": "2019-07-05T14:57:09.864065Z",
  574. "start_time": "2019-07-05T14:57:09.860077Z"
  575. }
  576. },
  577. "outputs": [
  578. {
  579. "name": "stdout",
  580. "output_type": "stream",
  581. "text": [
  582. "100.0\n"
  583. ]
  584. }
  585. ],
  586. "source": [
  587. "# pow\n",
  588. "\n",
  589. "a = 10.0\n",
  590. "a = a ** 2\n",
  591. "print(a)"
  592. ]
  593. },
  594. {
  595. "cell_type": "code",
  596. "execution_count": 132,
  597. "metadata": {
  598. "ExecuteTime": {
  599. "end_time": "2019-07-05T14:57:23.498791Z",
  600. "start_time": "2019-07-05T14:57:23.494801Z"
  601. }
  602. },
  603. "outputs": [
  604. {
  605. "name": "stdout",
  606. "output_type": "stream",
  607. "text": [
  608. "1\n",
  609. "1.0\n"
  610. ]
  611. }
  612. ],
  613. "source": [
  614. "# mod\n",
  615. "\n",
  616. "a = 10\n",
  617. "print(a % 3)\n",
  618. "\n",
  619. "a = 10.0\n",
  620. "print(a % 3)"
  621. ]
  622. },
  623. {
  624. "cell_type": "code",
  625. "execution_count": 140,
  626. "metadata": {
  627. "ExecuteTime": {
  628. "end_time": "2019-07-05T14:59:56.413875Z",
  629. "start_time": "2019-07-05T14:59:56.409919Z"
  630. }
  631. },
  632. "outputs": [
  633. {
  634. "name": "stdout",
  635. "output_type": "stream",
  636. "text": [
  637. "2\n",
  638. "2.0\n",
  639. "1.0\n",
  640. "1.0\n",
  641. "25\n",
  642. "25.0\n"
  643. ]
  644. }
  645. ],
  646. "source": [
  647. "print(1 + 1)\n",
  648. "print(1.0 + 1)\n",
  649. "\n",
  650. "print(5 / 5) # *****\n",
  651. "print(5.0 / 5)\n",
  652. "\n",
  653. "print(5 * 5)\n",
  654. "print(5.0 * 5)"
  655. ]
  656. },
  657. {
  658. "cell_type": "markdown",
  659. "metadata": {},
  660. "source": [
  661. "# not"
  662. ]
  663. },
  664. {
  665. "cell_type": "code",
  666. "execution_count": 61,
  667. "metadata": {
  668. "ExecuteTime": {
  669. "end_time": "2019-07-05T14:20:34.620587Z",
  670. "start_time": "2019-07-05T14:20:34.616606Z"
  671. }
  672. },
  673. "outputs": [
  674. {
  675. "name": "stdout",
  676. "output_type": "stream",
  677. "text": [
  678. "True\n",
  679. "False\n"
  680. ]
  681. }
  682. ],
  683. "source": [
  684. "a = True\n",
  685. "\n",
  686. "print(a)\n",
  687. "print(not a)"
  688. ]
  689. },
  690. {
  691. "cell_type": "code",
  692. "execution_count": 59,
  693. "metadata": {
  694. "ExecuteTime": {
  695. "end_time": "2019-07-05T14:20:32.259551Z",
  696. "start_time": "2019-07-05T14:20:32.255541Z"
  697. }
  698. },
  699. "outputs": [
  700. {
  701. "name": "stdout",
  702. "output_type": "stream",
  703. "text": [
  704. "No\n"
  705. ]
  706. }
  707. ],
  708. "source": [
  709. "a = False\n",
  710. "\n",
  711. "if a:\n",
  712. " print('Yes')\n",
  713. "else:\n",
  714. " print('No')"
  715. ]
  716. },
  717. {
  718. "cell_type": "code",
  719. "execution_count": 64,
  720. "metadata": {
  721. "ExecuteTime": {
  722. "end_time": "2019-07-05T14:20:55.832261Z",
  723. "start_time": "2019-07-05T14:20:55.829273Z"
  724. }
  725. },
  726. "outputs": [
  727. {
  728. "name": "stdout",
  729. "output_type": "stream",
  730. "text": [
  731. "Yes\n"
  732. ]
  733. }
  734. ],
  735. "source": [
  736. "a = True\n",
  737. "\n",
  738. "if a is (not False):\n",
  739. " print('Yes')\n",
  740. "else:\n",
  741. " print('No')"
  742. ]
  743. },
  744. {
  745. "cell_type": "code",
  746. "execution_count": 63,
  747. "metadata": {
  748. "ExecuteTime": {
  749. "end_time": "2019-07-05T14:20:54.054418Z",
  750. "start_time": "2019-07-05T14:20:54.050430Z"
  751. }
  752. },
  753. "outputs": [
  754. {
  755. "name": "stdout",
  756. "output_type": "stream",
  757. "text": [
  758. "Yes\n"
  759. ]
  760. }
  761. ],
  762. "source": [
  763. "a = True\n",
  764. "\n",
  765. "if a is not False:\n",
  766. " print('Yes')\n",
  767. "else:\n",
  768. " print('No')"
  769. ]
  770. },
  771. {
  772. "cell_type": "markdown",
  773. "metadata": {},
  774. "source": [
  775. "## Input"
  776. ]
  777. },
  778. {
  779. "cell_type": "code",
  780. "execution_count": 49,
  781. "metadata": {
  782. "ExecuteTime": {
  783. "end_time": "2019-07-05T14:17:45.135771Z",
  784. "start_time": "2019-07-05T14:17:44.251136Z"
  785. }
  786. },
  787. "outputs": [
  788. {
  789. "name": "stdout",
  790. "output_type": "stream",
  791. "text": [
  792. "Enter: 7\n",
  793. "<class 'str'> 7\n"
  794. ]
  795. }
  796. ],
  797. "source": [
  798. "num = input('Enter: ')\n",
  799. "print(type(num), num)"
  800. ]
  801. },
  802. {
  803. "cell_type": "code",
  804. "execution_count": 50,
  805. "metadata": {
  806. "ExecuteTime": {
  807. "end_time": "2019-07-05T14:17:46.630272Z",
  808. "start_time": "2019-07-05T14:17:45.647578Z"
  809. }
  810. },
  811. "outputs": [
  812. {
  813. "name": "stdout",
  814. "output_type": "stream",
  815. "text": [
  816. "Enter: 7\n",
  817. "<class 'int'> 7\n"
  818. ]
  819. }
  820. ],
  821. "source": [
  822. "num = int(input('Enter: '))\n",
  823. "print(type(num), num)"
  824. ]
  825. },
  826. {
  827. "cell_type": "code",
  828. "execution_count": 51,
  829. "metadata": {
  830. "ExecuteTime": {
  831. "end_time": "2019-07-05T14:17:47.820032Z",
  832. "start_time": "2019-07-05T14:17:47.070242Z"
  833. }
  834. },
  835. "outputs": [
  836. {
  837. "name": "stdout",
  838. "output_type": "stream",
  839. "text": [
  840. "Enter: 7\n",
  841. "<class 'float'> 7.0\n"
  842. ]
  843. }
  844. ],
  845. "source": [
  846. "num = float(input('Enter: '))\n",
  847. "print(type(num), num)"
  848. ]
  849. },
  850. {
  851. "cell_type": "markdown",
  852. "metadata": {},
  853. "source": [
  854. "# for"
  855. ]
  856. },
  857. {
  858. "cell_type": "code",
  859. "execution_count": 65,
  860. "metadata": {
  861. "ExecuteTime": {
  862. "end_time": "2019-07-05T14:21:59.058138Z",
  863. "start_time": "2019-07-05T14:21:59.054122Z"
  864. }
  865. },
  866. "outputs": [
  867. {
  868. "data": {
  869. "text/plain": [
  870. "range(0, 10)"
  871. ]
  872. },
  873. "execution_count": 65,
  874. "metadata": {},
  875. "output_type": "execute_result"
  876. }
  877. ],
  878. "source": [
  879. "range(10)"
  880. ]
  881. },
  882. {
  883. "cell_type": "code",
  884. "execution_count": 66,
  885. "metadata": {
  886. "ExecuteTime": {
  887. "end_time": "2019-07-05T14:22:07.074757Z",
  888. "start_time": "2019-07-05T14:22:07.070731Z"
  889. }
  890. },
  891. "outputs": [
  892. {
  893. "data": {
  894. "text/plain": [
  895. "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
  896. ]
  897. },
  898. "execution_count": 66,
  899. "metadata": {},
  900. "output_type": "execute_result"
  901. }
  902. ],
  903. "source": [
  904. "list(range(10))"
  905. ]
  906. },
  907. {
  908. "cell_type": "code",
  909. "execution_count": 67,
  910. "metadata": {
  911. "ExecuteTime": {
  912. "end_time": "2019-07-05T14:22:18.282889Z",
  913. "start_time": "2019-07-05T14:22:18.278899Z"
  914. }
  915. },
  916. "outputs": [
  917. {
  918. "name": "stdout",
  919. "output_type": "stream",
  920. "text": [
  921. "0\n",
  922. "1\n",
  923. "2\n",
  924. "3\n",
  925. "4\n",
  926. "5\n",
  927. "6\n",
  928. "7\n",
  929. "8\n",
  930. "9\n"
  931. ]
  932. }
  933. ],
  934. "source": [
  935. "for i in range(10):\n",
  936. " print(i)"
  937. ]
  938. },
  939. {
  940. "cell_type": "code",
  941. "execution_count": 68,
  942. "metadata": {
  943. "ExecuteTime": {
  944. "end_time": "2019-07-05T14:22:26.288043Z",
  945. "start_time": "2019-07-05T14:22:26.283057Z"
  946. }
  947. },
  948. "outputs": [
  949. {
  950. "name": "stdout",
  951. "output_type": "stream",
  952. "text": [
  953. "0 1 2 3 4 5 6 7 8 9 "
  954. ]
  955. }
  956. ],
  957. "source": [
  958. "for i in range(10):\n",
  959. " print(i, end=' ')"
  960. ]
  961. },
  962. {
  963. "cell_type": "code",
  964. "execution_count": 70,
  965. "metadata": {
  966. "ExecuteTime": {
  967. "end_time": "2019-07-05T14:22:51.423004Z",
  968. "start_time": "2019-07-05T14:22:51.417020Z"
  969. }
  970. },
  971. "outputs": [
  972. {
  973. "data": {
  974. "text/plain": [
  975. "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
  976. ]
  977. },
  978. "execution_count": 70,
  979. "metadata": {},
  980. "output_type": "execute_result"
  981. }
  982. ],
  983. "source": [
  984. "[i for i in range(10)]"
  985. ]
  986. },
  987. {
  988. "cell_type": "code",
  989. "execution_count": 69,
  990. "metadata": {
  991. "ExecuteTime": {
  992. "end_time": "2019-07-05T14:22:43.246446Z",
  993. "start_time": "2019-07-05T14:22:43.241460Z"
  994. }
  995. },
  996. "outputs": [
  997. {
  998. "name": "stdout",
  999. "output_type": "stream",
  1000. "text": [
  1001. "0 1 2 3 4 5 6 7 8 9\n"
  1002. ]
  1003. }
  1004. ],
  1005. "source": [
  1006. "print(*[i for i in range(10)])"
  1007. ]
  1008. },
  1009. {
  1010. "cell_type": "code",
  1011. "execution_count": 71,
  1012. "metadata": {
  1013. "ExecuteTime": {
  1014. "end_time": "2019-07-05T14:23:10.222165Z",
  1015. "start_time": "2019-07-05T14:23:10.218176Z"
  1016. }
  1017. },
  1018. "outputs": [
  1019. {
  1020. "name": "stdout",
  1021. "output_type": "stream",
  1022. "text": [
  1023. "0\n",
  1024. "1\n",
  1025. "2\n",
  1026. "3\n",
  1027. "4\n",
  1028. "5\n",
  1029. "6\n",
  1030. "7\n",
  1031. "8\n",
  1032. "9\n"
  1033. ]
  1034. }
  1035. ],
  1036. "source": [
  1037. "print(*[i for i in range(10)], sep='\\n')"
  1038. ]
  1039. },
  1040. {
  1041. "cell_type": "markdown",
  1042. "metadata": {},
  1043. "source": [
  1044. "# while"
  1045. ]
  1046. },
  1047. {
  1048. "cell_type": "code",
  1049. "execution_count": 73,
  1050. "metadata": {
  1051. "ExecuteTime": {
  1052. "end_time": "2019-07-05T14:25:07.200314Z",
  1053. "start_time": "2019-07-05T14:25:07.196325Z"
  1054. }
  1055. },
  1056. "outputs": [
  1057. {
  1058. "name": "stdout",
  1059. "output_type": "stream",
  1060. "text": [
  1061. "0 1 2 3 4 5 6 7 8 9 "
  1062. ]
  1063. }
  1064. ],
  1065. "source": [
  1066. "i = 0\n",
  1067. "while i < 10:\n",
  1068. " print(i, end=' ')\n",
  1069. " i += 1"
  1070. ]
  1071. },
  1072. {
  1073. "cell_type": "code",
  1074. "execution_count": 75,
  1075. "metadata": {
  1076. "ExecuteTime": {
  1077. "end_time": "2019-07-05T14:25:36.396400Z",
  1078. "start_time": "2019-07-05T14:25:36.392409Z"
  1079. }
  1080. },
  1081. "outputs": [
  1082. {
  1083. "name": "stdout",
  1084. "output_type": "stream",
  1085. "text": [
  1086. "0 1 2 3 4 5 6 7 8 9 "
  1087. ]
  1088. }
  1089. ],
  1090. "source": [
  1091. "# do-while\n",
  1092. "\n",
  1093. "i = 0\n",
  1094. "while True:\n",
  1095. " print(i, end=' ')\n",
  1096. " i += 1\n",
  1097. " \n",
  1098. " if i >= 10:\n",
  1099. " break"
  1100. ]
  1101. },
  1102. {
  1103. "cell_type": "markdown",
  1104. "metadata": {},
  1105. "source": [
  1106. "# list"
  1107. ]
  1108. },
  1109. {
  1110. "cell_type": "code",
  1111. "execution_count": 78,
  1112. "metadata": {
  1113. "ExecuteTime": {
  1114. "end_time": "2019-07-05T14:29:10.683470Z",
  1115. "start_time": "2019-07-05T14:29:10.680478Z"
  1116. }
  1117. },
  1118. "outputs": [
  1119. {
  1120. "name": "stdout",
  1121. "output_type": "stream",
  1122. "text": [
  1123. "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
  1124. "<class 'list'>\n"
  1125. ]
  1126. }
  1127. ],
  1128. "source": [
  1129. "l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
  1130. "print(l)\n",
  1131. "print(type(l))"
  1132. ]
  1133. },
  1134. {
  1135. "cell_type": "code",
  1136. "execution_count": 79,
  1137. "metadata": {
  1138. "ExecuteTime": {
  1139. "end_time": "2019-07-05T14:29:21.575321Z",
  1140. "start_time": "2019-07-05T14:29:21.571332Z"
  1141. }
  1142. },
  1143. "outputs": [
  1144. {
  1145. "name": "stdout",
  1146. "output_type": "stream",
  1147. "text": [
  1148. "0\n",
  1149. "0123456\n",
  1150. "789\n"
  1151. ]
  1152. }
  1153. ],
  1154. "source": [
  1155. "print(l[0])\n",
  1156. "print(s[:7])\n",
  1157. "print(s[7:])"
  1158. ]
  1159. },
  1160. {
  1161. "cell_type": "code",
  1162. "execution_count": 80,
  1163. "metadata": {
  1164. "ExecuteTime": {
  1165. "end_time": "2019-07-05T14:29:26.777007Z",
  1166. "start_time": "2019-07-05T14:29:26.774017Z"
  1167. }
  1168. },
  1169. "outputs": [
  1170. {
  1171. "name": "stdout",
  1172. "output_type": "stream",
  1173. "text": [
  1174. "9\n",
  1175. "789\n",
  1176. "0123456\n"
  1177. ]
  1178. }
  1179. ],
  1180. "source": [
  1181. "print(s[-1])\n",
  1182. "\n",
  1183. "print(s[-3:])\n",
  1184. "print(s[:-3])"
  1185. ]
  1186. },
  1187. {
  1188. "cell_type": "code",
  1189. "execution_count": 88,
  1190. "metadata": {
  1191. "ExecuteTime": {
  1192. "end_time": "2019-07-05T14:37:02.761168Z",
  1193. "start_time": "2019-07-05T14:37:02.757178Z"
  1194. }
  1195. },
  1196. "outputs": [
  1197. {
  1198. "name": "stdout",
  1199. "output_type": "stream",
  1200. "text": [
  1201. "[1, 2, 3]\n",
  1202. "[1, 2, 3]\n"
  1203. ]
  1204. }
  1205. ],
  1206. "source": [
  1207. "l1 = [1, 2, 3]\n",
  1208. "print(l1)\n",
  1209. "\n",
  1210. "l2 = l1\n",
  1211. "print(l2)"
  1212. ]
  1213. },
  1214. {
  1215. "cell_type": "code",
  1216. "execution_count": 90,
  1217. "metadata": {
  1218. "ExecuteTime": {
  1219. "end_time": "2019-07-05T14:37:25.945273Z",
  1220. "start_time": "2019-07-05T14:37:25.942253Z"
  1221. }
  1222. },
  1223. "outputs": [
  1224. {
  1225. "name": "stdout",
  1226. "output_type": "stream",
  1227. "text": [
  1228. "[2, 3]\n",
  1229. "[2, 3]\n"
  1230. ]
  1231. }
  1232. ],
  1233. "source": [
  1234. "l2.remove(1)\n",
  1235. "\n",
  1236. "print(l1)\n",
  1237. "print(l2)"
  1238. ]
  1239. },
  1240. {
  1241. "cell_type": "code",
  1242. "execution_count": 91,
  1243. "metadata": {
  1244. "ExecuteTime": {
  1245. "end_time": "2019-07-05T14:37:48.744341Z",
  1246. "start_time": "2019-07-05T14:37:48.739357Z"
  1247. }
  1248. },
  1249. "outputs": [
  1250. {
  1251. "name": "stdout",
  1252. "output_type": "stream",
  1253. "text": [
  1254. "[1, 2, 3]\n",
  1255. "[1, 2, 3]\n"
  1256. ]
  1257. }
  1258. ],
  1259. "source": [
  1260. "l1 = [1, 2, 3]\n",
  1261. "print(l1)\n",
  1262. "\n",
  1263. "l2 = l1.copy()\n",
  1264. "print(l2)"
  1265. ]
  1266. },
  1267. {
  1268. "cell_type": "code",
  1269. "execution_count": 92,
  1270. "metadata": {
  1271. "ExecuteTime": {
  1272. "end_time": "2019-07-05T14:37:53.634445Z",
  1273. "start_time": "2019-07-05T14:37:53.630456Z"
  1274. }
  1275. },
  1276. "outputs": [
  1277. {
  1278. "name": "stdout",
  1279. "output_type": "stream",
  1280. "text": [
  1281. "[1, 2, 3]\n",
  1282. "[2, 3]\n"
  1283. ]
  1284. }
  1285. ],
  1286. "source": [
  1287. "l2.remove(1)\n",
  1288. "\n",
  1289. "print(l1)\n",
  1290. "print(l2)"
  1291. ]
  1292. },
  1293. {
  1294. "cell_type": "code",
  1295. "execution_count": 94,
  1296. "metadata": {
  1297. "ExecuteTime": {
  1298. "end_time": "2019-07-05T14:43:15.866660Z",
  1299. "start_time": "2019-07-05T14:43:15.862670Z"
  1300. }
  1301. },
  1302. "outputs": [
  1303. {
  1304. "name": "stdout",
  1305. "output_type": "stream",
  1306. "text": [
  1307. "[1, 2]\n",
  1308. "[1, 2, 3]\n"
  1309. ]
  1310. }
  1311. ],
  1312. "source": [
  1313. "l = [1, 2]\n",
  1314. "print(l)\n",
  1315. "\n",
  1316. "l.append(3)\n",
  1317. "print(l)"
  1318. ]
  1319. },
  1320. {
  1321. "cell_type": "code",
  1322. "execution_count": 95,
  1323. "metadata": {
  1324. "ExecuteTime": {
  1325. "end_time": "2019-07-05T14:43:28.214474Z",
  1326. "start_time": "2019-07-05T14:43:28.210486Z"
  1327. }
  1328. },
  1329. "outputs": [
  1330. {
  1331. "name": "stdout",
  1332. "output_type": "stream",
  1333. "text": [
  1334. "[1, 2]\n",
  1335. "[1, 2, 3]\n"
  1336. ]
  1337. }
  1338. ],
  1339. "source": [
  1340. "l = [1, 2]\n",
  1341. "print(l)\n",
  1342. "\n",
  1343. "l += [3]\n",
  1344. "print(l)"
  1345. ]
  1346. },
  1347. {
  1348. "cell_type": "code",
  1349. "execution_count": 96,
  1350. "metadata": {
  1351. "ExecuteTime": {
  1352. "end_time": "2019-07-05T14:44:12.911787Z",
  1353. "start_time": "2019-07-05T14:44:12.907800Z"
  1354. }
  1355. },
  1356. "outputs": [
  1357. {
  1358. "name": "stdout",
  1359. "output_type": "stream",
  1360. "text": [
  1361. "[1, 2]\n",
  1362. "[555, 2]\n"
  1363. ]
  1364. }
  1365. ],
  1366. "source": [
  1367. "l = [1, 2]\n",
  1368. "print(l)\n",
  1369. "\n",
  1370. "l[0] = 555\n",
  1371. "print(l)"
  1372. ]
  1373. },
  1374. {
  1375. "cell_type": "markdown",
  1376. "metadata": {},
  1377. "source": [
  1378. "# tuple"
  1379. ]
  1380. },
  1381. {
  1382. "cell_type": "code",
  1383. "execution_count": 98,
  1384. "metadata": {
  1385. "ExecuteTime": {
  1386. "end_time": "2019-07-05T14:44:38.668679Z",
  1387. "start_time": "2019-07-05T14:44:38.657708Z"
  1388. }
  1389. },
  1390. "outputs": [
  1391. {
  1392. "name": "stdout",
  1393. "output_type": "stream",
  1394. "text": [
  1395. "(1, 2)\n"
  1396. ]
  1397. },
  1398. {
  1399. "ename": "TypeError",
  1400. "evalue": "'tuple' object does not support item assignment",
  1401. "output_type": "error",
  1402. "traceback": [
  1403. "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
  1404. "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
  1405. "\u001b[1;32m<ipython-input-98-fed3ccab398e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0ml\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ml\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0ml\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m555\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
  1406. "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
  1407. ]
  1408. }
  1409. ],
  1410. "source": [
  1411. "l = (1, 2)\n",
  1412. "print(l)\n",
  1413. "l[0] = 555"
  1414. ]
  1415. },
  1416. {
  1417. "cell_type": "markdown",
  1418. "metadata": {},
  1419. "source": [
  1420. "# dict"
  1421. ]
  1422. },
  1423. {
  1424. "cell_type": "code",
  1425. "execution_count": 143,
  1426. "metadata": {
  1427. "ExecuteTime": {
  1428. "end_time": "2019-07-05T15:01:36.206173Z",
  1429. "start_time": "2019-07-05T15:01:36.202150Z"
  1430. }
  1431. },
  1432. "outputs": [],
  1433. "source": [
  1434. "d = {\n",
  1435. " 'A': 1,\n",
  1436. " 'B': 2,\n",
  1437. " 'C': 3\n",
  1438. "}"
  1439. ]
  1440. },
  1441. {
  1442. "cell_type": "code",
  1443. "execution_count": 144,
  1444. "metadata": {
  1445. "ExecuteTime": {
  1446. "end_time": "2019-07-05T15:01:39.953717Z",
  1447. "start_time": "2019-07-05T15:01:39.950725Z"
  1448. }
  1449. },
  1450. "outputs": [
  1451. {
  1452. "name": "stdout",
  1453. "output_type": "stream",
  1454. "text": [
  1455. "{'A': 1, 'B': 2, 'C': 3}\n"
  1456. ]
  1457. }
  1458. ],
  1459. "source": [
  1460. "print(d)"
  1461. ]
  1462. },
  1463. {
  1464. "cell_type": "code",
  1465. "execution_count": 145,
  1466. "metadata": {
  1467. "ExecuteTime": {
  1468. "end_time": "2019-07-05T15:01:51.853721Z",
  1469. "start_time": "2019-07-05T15:01:51.850697Z"
  1470. }
  1471. },
  1472. "outputs": [
  1473. {
  1474. "data": {
  1475. "text/plain": [
  1476. "True"
  1477. ]
  1478. },
  1479. "execution_count": 145,
  1480. "metadata": {},
  1481. "output_type": "execute_result"
  1482. }
  1483. ],
  1484. "source": [
  1485. "'A' in d"
  1486. ]
  1487. },
  1488. {
  1489. "cell_type": "code",
  1490. "execution_count": 146,
  1491. "metadata": {
  1492. "ExecuteTime": {
  1493. "end_time": "2019-07-05T15:02:01.276025Z",
  1494. "start_time": "2019-07-05T15:02:01.272035Z"
  1495. }
  1496. },
  1497. "outputs": [
  1498. {
  1499. "data": {
  1500. "text/plain": [
  1501. "False"
  1502. ]
  1503. },
  1504. "execution_count": 146,
  1505. "metadata": {},
  1506. "output_type": "execute_result"
  1507. }
  1508. ],
  1509. "source": [
  1510. "'D' in d"
  1511. ]
  1512. },
  1513. {
  1514. "cell_type": "code",
  1515. "execution_count": 147,
  1516. "metadata": {
  1517. "ExecuteTime": {
  1518. "end_time": "2019-07-05T15:02:22.935111Z",
  1519. "start_time": "2019-07-05T15:02:22.932119Z"
  1520. }
  1521. },
  1522. "outputs": [],
  1523. "source": [
  1524. "d['D'] = 4"
  1525. ]
  1526. },
  1527. {
  1528. "cell_type": "code",
  1529. "execution_count": 148,
  1530. "metadata": {
  1531. "ExecuteTime": {
  1532. "end_time": "2019-07-05T15:02:26.885948Z",
  1533. "start_time": "2019-07-05T15:02:26.882956Z"
  1534. }
  1535. },
  1536. "outputs": [
  1537. {
  1538. "name": "stdout",
  1539. "output_type": "stream",
  1540. "text": [
  1541. "{'A': 1, 'B': 2, 'C': 3, 'D': 4}\n"
  1542. ]
  1543. }
  1544. ],
  1545. "source": [
  1546. "print(d)"
  1547. ]
  1548. },
  1549. {
  1550. "cell_type": "code",
  1551. "execution_count": 149,
  1552. "metadata": {
  1553. "ExecuteTime": {
  1554. "end_time": "2019-07-05T15:02:34.317531Z",
  1555. "start_time": "2019-07-05T15:02:34.313540Z"
  1556. }
  1557. },
  1558. "outputs": [
  1559. {
  1560. "data": {
  1561. "text/plain": [
  1562. "True"
  1563. ]
  1564. },
  1565. "execution_count": 149,
  1566. "metadata": {},
  1567. "output_type": "execute_result"
  1568. }
  1569. ],
  1570. "source": [
  1571. "'D' in d"
  1572. ]
  1573. },
  1574. {
  1575. "cell_type": "code",
  1576. "execution_count": 150,
  1577. "metadata": {
  1578. "ExecuteTime": {
  1579. "end_time": "2019-07-05T15:02:45.231152Z",
  1580. "start_time": "2019-07-05T15:02:45.227164Z"
  1581. }
  1582. },
  1583. "outputs": [
  1584. {
  1585. "data": {
  1586. "text/plain": [
  1587. "1"
  1588. ]
  1589. },
  1590. "execution_count": 150,
  1591. "metadata": {},
  1592. "output_type": "execute_result"
  1593. }
  1594. ],
  1595. "source": [
  1596. "d['A']"
  1597. ]
  1598. },
  1599. {
  1600. "cell_type": "markdown",
  1601. "metadata": {},
  1602. "source": [
  1603. "# def"
  1604. ]
  1605. },
  1606. {
  1607. "cell_type": "code",
  1608. "execution_count": 87,
  1609. "metadata": {
  1610. "ExecuteTime": {
  1611. "end_time": "2019-07-05T14:34:46.448084Z",
  1612. "start_time": "2019-07-05T14:34:46.443101Z"
  1613. }
  1614. },
  1615. "outputs": [
  1616. {
  1617. "name": "stdout",
  1618. "output_type": "stream",
  1619. "text": [
  1620. "12\n"
  1621. ]
  1622. }
  1623. ],
  1624. "source": [
  1625. "def foo(num):\n",
  1626. " print(num + 5)\n",
  1627. "\n",
  1628. "\n",
  1629. "foo(7)"
  1630. ]
  1631. },
  1632. {
  1633. "cell_type": "code",
  1634. "execution_count": 85,
  1635. "metadata": {
  1636. "ExecuteTime": {
  1637. "end_time": "2019-07-05T14:34:07.306928Z",
  1638. "start_time": "2019-07-05T14:34:07.302939Z"
  1639. }
  1640. },
  1641. "outputs": [
  1642. {
  1643. "name": "stdout",
  1644. "output_type": "stream",
  1645. "text": [
  1646. "12\n"
  1647. ]
  1648. }
  1649. ],
  1650. "source": [
  1651. "def foo(num):\n",
  1652. " return num + 5\n",
  1653. "\n",
  1654. "\n",
  1655. "result = foo(7)\n",
  1656. "print(result)"
  1657. ]
  1658. },
  1659. {
  1660. "cell_type": "markdown",
  1661. "metadata": {},
  1662. "source": [
  1663. "# file - read"
  1664. ]
  1665. },
  1666. {
  1667. "cell_type": "code",
  1668. "execution_count": 115,
  1669. "metadata": {
  1670. "ExecuteTime": {
  1671. "end_time": "2019-07-05T14:49:39.193462Z",
  1672. "start_time": "2019-07-05T14:49:39.188510Z"
  1673. }
  1674. },
  1675. "outputs": [
  1676. {
  1677. "data": {
  1678. "text/plain": [
  1679. "['bar.txt', 'foo.txt']"
  1680. ]
  1681. },
  1682. "execution_count": 115,
  1683. "metadata": {},
  1684. "output_type": "execute_result"
  1685. }
  1686. ],
  1687. "source": [
  1688. "# list .txt file\n",
  1689. "\n",
  1690. "from glob import glob\n",
  1691. "glob('*.txt')"
  1692. ]
  1693. },
  1694. {
  1695. "cell_type": "code",
  1696. "execution_count": 118,
  1697. "metadata": {
  1698. "ExecuteTime": {
  1699. "end_time": "2019-07-05T14:51:02.107240Z",
  1700. "start_time": "2019-07-05T14:51:02.103252Z"
  1701. }
  1702. },
  1703. "outputs": [
  1704. {
  1705. "name": "stdout",
  1706. "output_type": "stream",
  1707. "text": [
  1708. "Hello, world!\n"
  1709. ]
  1710. }
  1711. ],
  1712. "source": [
  1713. "file = open('foo.txt')\n",
  1714. "\n",
  1715. "text = file.read()\n",
  1716. "print(text)\n",
  1717. "\n",
  1718. "file.close()"
  1719. ]
  1720. },
  1721. {
  1722. "cell_type": "code",
  1723. "execution_count": null,
  1724. "metadata": {},
  1725. "outputs": [],
  1726. "source": [
  1727. "with open('foo.txt', 'r') as file:\n",
  1728. " text = file.read()\n",
  1729. " print(text)"
  1730. ]
  1731. },
  1732. {
  1733. "cell_type": "code",
  1734. "execution_count": 123,
  1735. "metadata": {
  1736. "ExecuteTime": {
  1737. "end_time": "2019-07-05T14:54:04.828063Z",
  1738. "start_time": "2019-07-05T14:54:04.824074Z"
  1739. }
  1740. },
  1741. "outputs": [
  1742. {
  1743. "name": "stdout",
  1744. "output_type": "stream",
  1745. "text": [
  1746. "Hello, world!\n"
  1747. ]
  1748. }
  1749. ],
  1750. "source": [
  1751. "with open('foo.txt') as file:\n",
  1752. " text = file.read()\n",
  1753. " print(text)"
  1754. ]
  1755. },
  1756. {
  1757. "cell_type": "markdown",
  1758. "metadata": {},
  1759. "source": [
  1760. "# file - write"
  1761. ]
  1762. },
  1763. {
  1764. "cell_type": "code",
  1765. "execution_count": 125,
  1766. "metadata": {
  1767. "ExecuteTime": {
  1768. "end_time": "2019-07-05T14:54:24.207691Z",
  1769. "start_time": "2019-07-05T14:54:24.200710Z"
  1770. }
  1771. },
  1772. "outputs": [
  1773. {
  1774. "ename": "TypeError",
  1775. "evalue": "write() argument must be str, not int",
  1776. "output_type": "error",
  1777. "traceback": [
  1778. "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
  1779. "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
  1780. "\u001b[1;32m<ipython-input-125-284e7bf509ca>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'foo.txt'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'w'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mfile\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mtext\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m555555555555\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mfile\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtext\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
  1781. "\u001b[1;31mTypeError\u001b[0m: write() argument must be str, not int"
  1782. ]
  1783. }
  1784. ],
  1785. "source": [
  1786. "with open('foo.txt', 'w') as file:\n",
  1787. " text = 555555555555\n",
  1788. " file.write(text)"
  1789. ]
  1790. },
  1791. {
  1792. "cell_type": "code",
  1793. "execution_count": 126,
  1794. "metadata": {
  1795. "ExecuteTime": {
  1796. "end_time": "2019-07-05T14:54:30.858253Z",
  1797. "start_time": "2019-07-05T14:54:30.853235Z"
  1798. }
  1799. },
  1800. "outputs": [],
  1801. "source": [
  1802. "with open('foo.txt', 'w') as file:\n",
  1803. " text = 555555555555\n",
  1804. " file.write(str(text))"
  1805. ]
  1806. },
  1807. {
  1808. "cell_type": "code",
  1809. "execution_count": 127,
  1810. "metadata": {
  1811. "ExecuteTime": {
  1812. "end_time": "2019-07-05T14:54:38.569137Z",
  1813. "start_time": "2019-07-05T14:54:38.564152Z"
  1814. }
  1815. },
  1816. "outputs": [
  1817. {
  1818. "name": "stdout",
  1819. "output_type": "stream",
  1820. "text": [
  1821. "555555555555\n"
  1822. ]
  1823. }
  1824. ],
  1825. "source": [
  1826. "# read\n",
  1827. "\n",
  1828. "with open('foo.txt') as file:\n",
  1829. " text = file.read()\n",
  1830. " print(text)"
  1831. ]
  1832. },
  1833. {
  1834. "cell_type": "code",
  1835. "execution_count": null,
  1836. "metadata": {},
  1837. "outputs": [],
  1838. "source": []
  1839. }
  1840. ],
  1841. "metadata": {
  1842. "kernelspec": {
  1843. "display_name": "Python 3",
  1844. "language": "python",
  1845. "name": "python3"
  1846. },
  1847. "language_info": {
  1848. "codemirror_mode": {
  1849. "name": "ipython",
  1850. "version": 3
  1851. },
  1852. "file_extension": ".py",
  1853. "mimetype": "text/x-python",
  1854. "name": "python",
  1855. "nbconvert_exporter": "python",
  1856. "pygments_lexer": "ipython3",
  1857. "version": "3.6.5"
  1858. }
  1859. },
  1860. "nbformat": 4,
  1861. "nbformat_minor": 2
  1862. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement