Guest User

Untitled

a guest
May 20th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.60 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "metadata": {},
  7. "outputs": [],
  8. "source": [
  9. "import random"
  10. ]
  11. },
  12. {
  13. "cell_type": "code",
  14. "execution_count": 5,
  15. "metadata": {},
  16. "outputs": [
  17. {
  18. "data": {
  19. "text/plain": [
  20. "0.38597486890896215"
  21. ]
  22. },
  23. "execution_count": 5,
  24. "metadata": {},
  25. "output_type": "execute_result"
  26. }
  27. ],
  28. "source": [
  29. "random.random()"
  30. ]
  31. },
  32. {
  33. "cell_type": "code",
  34. "execution_count": 10,
  35. "metadata": {},
  36. "outputs": [
  37. {
  38. "data": {
  39. "text/plain": [
  40. "9"
  41. ]
  42. },
  43. "execution_count": 10,
  44. "metadata": {},
  45. "output_type": "execute_result"
  46. }
  47. ],
  48. "source": [
  49. "random.randint(1, 10)"
  50. ]
  51. },
  52. {
  53. "cell_type": "code",
  54. "execution_count": 11,
  55. "metadata": {},
  56. "outputs": [
  57. {
  58. "name": "stdout",
  59. "output_type": "stream",
  60. "text": [
  61. "The Zen of Python, by Tim Peters\n",
  62. "\n",
  63. "Beautiful is better than ugly.\n",
  64. "Explicit is better than implicit.\n",
  65. "Simple is better than complex.\n",
  66. "Complex is better than complicated.\n",
  67. "Flat is better than nested.\n",
  68. "Sparse is better than dense.\n",
  69. "Readability counts.\n",
  70. "Special cases aren't special enough to break the rules.\n",
  71. "Although practicality beats purity.\n",
  72. "Errors should never pass silently.\n",
  73. "Unless explicitly silenced.\n",
  74. "In the face of ambiguity, refuse the temptation to guess.\n",
  75. "There should be one-- and preferably only one --obvious way to do it.\n",
  76. "Although that way may not be obvious at first unless you're Dutch.\n",
  77. "Now is better than never.\n",
  78. "Although never is often better than *right* now.\n",
  79. "If the implementation is hard to explain, it's a bad idea.\n",
  80. "If the implementation is easy to explain, it may be a good idea.\n",
  81. "Namespaces are one honking great idea -- let's do more of those!\n"
  82. ]
  83. }
  84. ],
  85. "source": [
  86. "import this"
  87. ]
  88. },
  89. {
  90. "cell_type": "code",
  91. "execution_count": 12,
  92. "metadata": {},
  93. "outputs": [],
  94. "source": [
  95. "x = 3\n",
  96. "assert isinstance(x, int)"
  97. ]
  98. },
  99. {
  100. "cell_type": "code",
  101. "execution_count": 13,
  102. "metadata": {},
  103. "outputs": [],
  104. "source": [
  105. "def foo(x):\n",
  106. " \"\"\"This is the foo function\"\"\"\n",
  107. " \n",
  108. " # print the value of x\n",
  109. " print(x)"
  110. ]
  111. },
  112. {
  113. "cell_type": "code",
  114. "execution_count": 14,
  115. "metadata": {},
  116. "outputs": [
  117. {
  118. "data": {
  119. "text/plain": [
  120. "'This is the foo function'"
  121. ]
  122. },
  123. "execution_count": 14,
  124. "metadata": {},
  125. "output_type": "execute_result"
  126. }
  127. ],
  128. "source": [
  129. "foo.__doc__"
  130. ]
  131. },
  132. {
  133. "cell_type": "code",
  134. "execution_count": 15,
  135. "metadata": {},
  136. "outputs": [
  137. {
  138. "name": "stdout",
  139. "output_type": "stream",
  140. "text": [
  141. "Help on function foo in module __main__:\n",
  142. "\n",
  143. "foo(x)\n",
  144. " This is the foo function\n",
  145. "\n"
  146. ]
  147. }
  148. ],
  149. "source": [
  150. "help(foo)"
  151. ]
  152. },
  153. {
  154. "cell_type": "code",
  155. "execution_count": 16,
  156. "metadata": {},
  157. "outputs": [
  158. {
  159. "name": "stdout",
  160. "output_type": "stream",
  161. "text": [
  162. "Help on method randint in module random:\n",
  163. "\n",
  164. "randint(a, b) method of random.Random instance\n",
  165. " Return random integer in range [a, b], including both end points.\n",
  166. "\n"
  167. ]
  168. }
  169. ],
  170. "source": [
  171. "help(random.randint)"
  172. ]
  173. },
  174. {
  175. "cell_type": "code",
  176. "execution_count": 26,
  177. "metadata": {},
  178. "outputs": [
  179. {
  180. "data": {
  181. "text/plain": [
  182. "-0.08792641085583411"
  183. ]
  184. },
  185. "execution_count": 26,
  186. "metadata": {},
  187. "output_type": "execute_result"
  188. }
  189. ],
  190. "source": [
  191. "random.normalvariate(0., 1.)"
  192. ]
  193. },
  194. {
  195. "cell_type": "code",
  196. "execution_count": 40,
  197. "metadata": {},
  198. "outputs": [],
  199. "source": [
  200. "def foo(bar: int = 0) -> 'nothing is returned':\n",
  201. " print(bar)"
  202. ]
  203. },
  204. {
  205. "cell_type": "code",
  206. "execution_count": 41,
  207. "metadata": {},
  208. "outputs": [
  209. {
  210. "data": {
  211. "text/plain": [
  212. "{'bar': int, 'return': 'nothing is returned'}"
  213. ]
  214. },
  215. "execution_count": 41,
  216. "metadata": {},
  217. "output_type": "execute_result"
  218. }
  219. ],
  220. "source": [
  221. "foo.__annotations__"
  222. ]
  223. },
  224. {
  225. "cell_type": "code",
  226. "execution_count": 42,
  227. "metadata": {},
  228. "outputs": [
  229. {
  230. "name": "stdout",
  231. "output_type": "stream",
  232. "text": [
  233. "s\n"
  234. ]
  235. }
  236. ],
  237. "source": [
  238. "foo('s')"
  239. ]
  240. },
  241. {
  242. "cell_type": "code",
  243. "execution_count": 45,
  244. "metadata": {},
  245. "outputs": [],
  246. "source": [
  247. "del x\n",
  248. "x: int"
  249. ]
  250. },
  251. {
  252. "cell_type": "code",
  253. "execution_count": 46,
  254. "metadata": {},
  255. "outputs": [
  256. {
  257. "ename": "NameError",
  258. "evalue": "name 'x' is not defined",
  259. "output_type": "error",
  260. "traceback": [
  261. "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
  262. "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
  263. "\u001b[0;32m<ipython-input-46-6fcf9dfbd479>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
  264. "\u001b[0;31mNameError\u001b[0m: name 'x' is not defined"
  265. ]
  266. }
  267. ],
  268. "source": [
  269. "x"
  270. ]
  271. },
  272. {
  273. "cell_type": "code",
  274. "execution_count": 47,
  275. "metadata": {},
  276. "outputs": [],
  277. "source": [
  278. "x = 3"
  279. ]
  280. },
  281. {
  282. "cell_type": "code",
  283. "execution_count": 48,
  284. "metadata": {},
  285. "outputs": [],
  286. "source": [
  287. "x = 'this is a string'"
  288. ]
  289. },
  290. {
  291. "cell_type": "code",
  292. "execution_count": 49,
  293. "metadata": {},
  294. "outputs": [],
  295. "source": [
  296. "import sys"
  297. ]
  298. },
  299. {
  300. "cell_type": "code",
  301. "execution_count": 50,
  302. "metadata": {},
  303. "outputs": [],
  304. "source": [
  305. "main = sys.modules['__main__']"
  306. ]
  307. },
  308. {
  309. "cell_type": "code",
  310. "execution_count": 51,
  311. "metadata": {},
  312. "outputs": [
  313. {
  314. "data": {
  315. "text/plain": [
  316. "{'x': int}"
  317. ]
  318. },
  319. "execution_count": 51,
  320. "metadata": {},
  321. "output_type": "execute_result"
  322. }
  323. ],
  324. "source": [
  325. "main.__annotations__"
  326. ]
  327. },
  328. {
  329. "cell_type": "code",
  330. "execution_count": 52,
  331. "metadata": {},
  332. "outputs": [],
  333. "source": [
  334. "y: float"
  335. ]
  336. },
  337. {
  338. "cell_type": "code",
  339. "execution_count": 53,
  340. "metadata": {},
  341. "outputs": [
  342. {
  343. "data": {
  344. "text/plain": [
  345. "{'x': int, 'y': float}"
  346. ]
  347. },
  348. "execution_count": 53,
  349. "metadata": {},
  350. "output_type": "execute_result"
  351. }
  352. ],
  353. "source": [
  354. "main.__annotations__"
  355. ]
  356. },
  357. {
  358. "cell_type": "code",
  359. "execution_count": 58,
  360. "metadata": {},
  361. "outputs": [],
  362. "source": [
  363. "import time\n",
  364. "from functools import wraps\n",
  365. "\n",
  366. "def timethis(func):\n",
  367. " @wraps(func)\n",
  368. " def new_func(*args, **kwargs):\n",
  369. " t_start = time.time()\n",
  370. " retval = func(*args, **kwargs)\n",
  371. " t_end = time.time()\n",
  372. " print(f'{t_end - t_start} seconds elapsed')\n",
  373. " return retval\n",
  374. " return new_func"
  375. ]
  376. },
  377. {
  378. "cell_type": "code",
  379. "execution_count": 59,
  380. "metadata": {},
  381. "outputs": [],
  382. "source": [
  383. "timed_max = timethis(max)"
  384. ]
  385. },
  386. {
  387. "cell_type": "code",
  388. "execution_count": 56,
  389. "metadata": {},
  390. "outputs": [
  391. {
  392. "name": "stdout",
  393. "output_type": "stream",
  394. "text": [
  395. "3.814697265625e-06 seconds elapsed\n"
  396. ]
  397. }
  398. ],
  399. "source": [
  400. "max_val = timed_max(1, 2, 3)"
  401. ]
  402. },
  403. {
  404. "cell_type": "code",
  405. "execution_count": 57,
  406. "metadata": {},
  407. "outputs": [
  408. {
  409. "data": {
  410. "text/plain": [
  411. "3"
  412. ]
  413. },
  414. "execution_count": 57,
  415. "metadata": {},
  416. "output_type": "execute_result"
  417. }
  418. ],
  419. "source": [
  420. "max_val"
  421. ]
  422. },
  423. {
  424. "cell_type": "code",
  425. "execution_count": 60,
  426. "metadata": {},
  427. "outputs": [
  428. {
  429. "name": "stdout",
  430. "output_type": "stream",
  431. "text": [
  432. "Help on function max in module builtins:\n",
  433. "\n",
  434. "max(...)\n",
  435. " max(iterable, *[, default=obj, key=func]) -> value\n",
  436. " max(arg1, arg2, *args, *[, key=func]) -> value\n",
  437. " \n",
  438. " With a single iterable argument, return its biggest item. The\n",
  439. " default keyword-only argument specifies an object to return if\n",
  440. " the provided iterable is empty.\n",
  441. " With two or more arguments, return the largest argument.\n",
  442. "\n"
  443. ]
  444. }
  445. ],
  446. "source": [
  447. "help(timed_max)"
  448. ]
  449. },
  450. {
  451. "cell_type": "code",
  452. "execution_count": 61,
  453. "metadata": {},
  454. "outputs": [],
  455. "source": [
  456. "x = 0"
  457. ]
  458. },
  459. {
  460. "cell_type": "code",
  461. "execution_count": 62,
  462. "metadata": {},
  463. "outputs": [],
  464. "source": [
  465. "x = 3"
  466. ]
  467. },
  468. {
  469. "cell_type": "code",
  470. "execution_count": 63,
  471. "metadata": {},
  472. "outputs": [
  473. {
  474. "data": {
  475. "text/plain": [
  476. "3"
  477. ]
  478. },
  479. "execution_count": 63,
  480. "metadata": {},
  481. "output_type": "execute_result"
  482. }
  483. ],
  484. "source": [
  485. "x.numerator"
  486. ]
  487. },
  488. {
  489. "cell_type": "code",
  490. "execution_count": 64,
  491. "metadata": {},
  492. "outputs": [
  493. {
  494. "data": {
  495. "text/plain": [
  496. "1"
  497. ]
  498. },
  499. "execution_count": 64,
  500. "metadata": {},
  501. "output_type": "execute_result"
  502. }
  503. ],
  504. "source": [
  505. "x.denominator"
  506. ]
  507. },
  508. {
  509. "cell_type": "code",
  510. "execution_count": 66,
  511. "metadata": {},
  512. "outputs": [],
  513. "source": [
  514. "from numbers import Rational, Integral"
  515. ]
  516. },
  517. {
  518. "cell_type": "code",
  519. "execution_count": 67,
  520. "metadata": {},
  521. "outputs": [
  522. {
  523. "data": {
  524. "text/plain": [
  525. "True"
  526. ]
  527. },
  528. "execution_count": 67,
  529. "metadata": {},
  530. "output_type": "execute_result"
  531. }
  532. ],
  533. "source": [
  534. "isinstance(x, Rational)"
  535. ]
  536. },
  537. {
  538. "cell_type": "code",
  539. "execution_count": null,
  540. "metadata": {},
  541. "outputs": [],
  542. "source": [
  543. "~x\n",
  544. "x.__inverse__()\n"
  545. ]
  546. },
  547. {
  548. "cell_type": "code",
  549. "execution_count": 68,
  550. "metadata": {},
  551. "outputs": [
  552. {
  553. "data": {
  554. "text/plain": [
  555. "-6"
  556. ]
  557. },
  558. "execution_count": 68,
  559. "metadata": {},
  560. "output_type": "execute_result"
  561. }
  562. ],
  563. "source": [
  564. "~5"
  565. ]
  566. },
  567. {
  568. "cell_type": "code",
  569. "execution_count": 69,
  570. "metadata": {},
  571. "outputs": [
  572. {
  573. "data": {
  574. "text/plain": [
  575. "'0b101'"
  576. ]
  577. },
  578. "execution_count": 69,
  579. "metadata": {},
  580. "output_type": "execute_result"
  581. }
  582. ],
  583. "source": [
  584. "bin(5)"
  585. ]
  586. },
  587. {
  588. "cell_type": "code",
  589. "execution_count": 70,
  590. "metadata": {},
  591. "outputs": [
  592. {
  593. "data": {
  594. "text/plain": [
  595. "'-0b110'"
  596. ]
  597. },
  598. "execution_count": 70,
  599. "metadata": {},
  600. "output_type": "execute_result"
  601. }
  602. ],
  603. "source": [
  604. "bin(-6)"
  605. ]
  606. },
  607. {
  608. "cell_type": "code",
  609. "execution_count": 71,
  610. "metadata": {},
  611. "outputs": [],
  612. "source": [
  613. "x = (1, 2, 3)"
  614. ]
  615. },
  616. {
  617. "cell_type": "code",
  618. "execution_count": 72,
  619. "metadata": {},
  620. "outputs": [
  621. {
  622. "ename": "ValueError",
  623. "evalue": "too many values to unpack (expected 2)",
  624. "output_type": "error",
  625. "traceback": [
  626. "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
  627. "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
  628. "\u001b[0;32m<ipython-input-72-4430d8d5d46d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
  629. "\u001b[0;31mValueError\u001b[0m: too many values to unpack (expected 2)"
  630. ]
  631. }
  632. ],
  633. "source": [
  634. "a, b = x"
  635. ]
  636. },
  637. {
  638. "cell_type": "code",
  639. "execution_count": null,
  640. "metadata": {},
  641. "outputs": [],
  642. "source": []
  643. }
  644. ],
  645. "metadata": {
  646. "kernelspec": {
  647. "display_name": "Python 3",
  648. "language": "python",
  649. "name": "python3"
  650. },
  651. "language_info": {
  652. "codemirror_mode": {
  653. "name": "ipython",
  654. "version": 3
  655. },
  656. "file_extension": ".py",
  657. "mimetype": "text/x-python",
  658. "name": "python",
  659. "nbconvert_exporter": "python",
  660. "pygments_lexer": "ipython3",
  661. "version": "3.6.5"
  662. }
  663. },
  664. "nbformat": 4,
  665. "nbformat_minor": 2
  666. }
Add Comment
Please, Sign In to add comment