Advertisement
Guest User

A0.ipynb

a guest
Nov 2nd, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.19 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# KTByte CS82/CS84 Assignment 0\n",
  8. "\n",
  9. "## Variables and Operators\n",
  10. "\n",
  11. "In this ungraded assignment, you will explore the basics of Python. This Jupyter Notebook is meant to be a quick introduction to Python; while this is by no means a comprehensive tutorial of the language, it should contain enough information for you to do most of the assignements in our class. We fully expect students to look up how to do things in Python that we haven't learned in class.\n",
  12. "\n",
  13. "First, run the code below by double-clicking on it and hit Shift+Enter."
  14. ]
  15. },
  16. {
  17. "cell_type": "code",
  18. "execution_count": 37,
  19. "metadata": {
  20. "collapsed": false
  21. },
  22. "outputs": [
  23. {
  24. "name": "stdout",
  25. "output_type": "stream",
  26. "text": [
  27. "9001\n",
  28. "helloworld\n",
  29. "The length of a is: 7 and the content is: [2, 5, 7, 0, 8, 1, 9]\n"
  30. ]
  31. }
  32. ],
  33. "source": [
  34. "from __future__ import print_function\n",
  35. "\n",
  36. "# create int variable\n",
  37. "x = 9001\n",
  38. "print(x)\n",
  39. "\n",
  40. "# create strings\n",
  41. "s = \"hello\"\n",
  42. "t = \"world\"\n",
  43. "print(s + t)\n",
  44. "\n",
  45. "# create list\n",
  46. "a = [2, 5, 7, 0, 8, 1, 9]\n",
  47. "print(\"The length of a is: \", len(a), \" and the content is: \", a)"
  48. ]
  49. },
  50. {
  51. "cell_type": "markdown",
  52. "metadata": {},
  53. "source": [
  54. "Python is a *dynamically-typed* language, meaning you can create variables without specifying what its data type is. In the above example, we created 1 int variable, 2 string variables and 1 list variable (we call them lists instead of arrays in Python.)\n",
  55. "\n",
  56. "You can query the type of a variable or a value using the `type()` function"
  57. ]
  58. },
  59. {
  60. "cell_type": "code",
  61. "execution_count": 38,
  62. "metadata": {
  63. "collapsed": false
  64. },
  65. "outputs": [
  66. {
  67. "name": "stdout",
  68. "output_type": "stream",
  69. "text": [
  70. "<type 'int'>\n",
  71. "<type 'str'>\n",
  72. "<type 'list'>\n"
  73. ]
  74. }
  75. ],
  76. "source": [
  77. "print(type(x))\n",
  78. "print(type(s))\n",
  79. "print(type(a))"
  80. ]
  81. },
  82. {
  83. "cell_type": "markdown",
  84. "metadata": {},
  85. "source": [
  86. "On the other hand, Python is a somewhat *strongly-typed*, i.e. you still have to be mindful of data types when performing operations. If you attempt to use the following code to contenate to a string variables, you will get an error:"
  87. ]
  88. },
  89. {
  90. "cell_type": "code",
  91. "execution_count": 39,
  92. "metadata": {
  93. "collapsed": false
  94. },
  95. "outputs": [
  96. {
  97. "ename": "TypeError",
  98. "evalue": "cannot concatenate 'str' and 'int' objects",
  99. "output_type": "error",
  100. "traceback": [
  101. "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
  102. "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
  103. "\u001b[0;32m<ipython-input-39-1f3cf10ef9d8>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# TODO: use the str(x) function to convert 66 to a string and try running this again\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0ms\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"Route \"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0ms\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0;36m66\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;32mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
  104. "\u001b[0;31mTypeError\u001b[0m: cannot concatenate 'str' and 'int' objects"
  105. ]
  106. }
  107. ],
  108. "source": [
  109. "# TODO: use the str(x) function to convert 66 to a string and try running this again\n",
  110. "s = \"Route \"\n",
  111. "s += 66 \n",
  112. "print(s)"
  113. ]
  114. },
  115. {
  116. "cell_type": "markdown",
  117. "metadata": {},
  118. "source": [
  119. "## Booleans, If Statements and Comparisons\n",
  120. "\n",
  121. "Python uses the keywords `True` and `False` for boolean values (note the upper case). It uses the keywords: `and`, `or` and `not` for boolean operators. Also, Python does not use curly braces. To create an if statement, you indent the block of code inside the if."
  122. ]
  123. },
  124. {
  125. "cell_type": "code",
  126. "execution_count": 40,
  127. "metadata": {
  128. "collapsed": false
  129. },
  130. "outputs": [
  131. {
  132. "name": "stdout",
  133. "output_type": "stream",
  134. "text": [
  135. "clap\n",
  136. "huh\n"
  137. ]
  138. }
  139. ],
  140. "source": [
  141. "# TODO: change the values of happy and know_it to experiement with the code below\n",
  142. "happy = True\n",
  143. "know_it = True\n",
  144. "\n",
  145. "if happy and know_it:\n",
  146. " print(\"clap\")\n",
  147. "\n",
  148. "if happy or know_it:\n",
  149. " print(\"huh\")\n",
  150. " \n",
  151. "if not know_it:\n",
  152. " print(\"what\")"
  153. ]
  154. },
  155. {
  156. "cell_type": "markdown",
  157. "metadata": {},
  158. "source": [
  159. "Comparison operators look and work just about the way you expect in Java: `==`, `!=`, `<`, `<=`, `>`, `>=`"
  160. ]
  161. },
  162. {
  163. "cell_type": "code",
  164. "execution_count": 40,
  165. "metadata": {
  166. "collapsed": false
  167. },
  168. "outputs": [
  169. {
  170. "name": "stdout",
  171. "output_type": "stream",
  172. "text": [
  173. "high\n"
  174. ]
  175. }
  176. ],
  177. "source": [
  178. "x = 10\n",
  179. "if x > 5:\n",
  180. " print(\"high\")\n",
  181. "else:\n",
  182. " print(\"low\")"
  183. ]
  184. },
  185. {
  186. "cell_type": "markdown",
  187. "metadata": {},
  188. "source": [
  189. "However, unlike Java, Python actually allows you to chain number comparison:"
  190. ]
  191. },
  192. {
  193. "cell_type": "code",
  194. "execution_count": 42,
  195. "metadata": {
  196. "collapsed": false
  197. },
  198. "outputs": [
  199. {
  200. "name": "stdout",
  201. "output_type": "stream",
  202. "text": [
  203. "x is within range\n"
  204. ]
  205. }
  206. ],
  207. "source": [
  208. "x = 10\n",
  209. "if 0 < x < 20:\n",
  210. " print(\"x is within range\")"
  211. ]
  212. },
  213. {
  214. "cell_type": "markdown",
  215. "metadata": {},
  216. "source": [
  217. "Note that `==` performs a value copmarison in Python, even for Strings and other objects. For exmaple, the following code show how to compare two Strings in Python:"
  218. ]
  219. },
  220. {
  221. "cell_type": "code",
  222. "execution_count": 41,
  223. "metadata": {
  224. "collapsed": false
  225. },
  226. "outputs": [
  227. {
  228. "name": "stdout",
  229. "output_type": "stream",
  230. "text": [
  231. "yay\n"
  232. ]
  233. }
  234. ],
  235. "source": [
  236. "x = \"hello\"\n",
  237. "if x == 'hello': # Python allows using both \"\" and '' for Strings\n",
  238. " print(\"yay\")"
  239. ]
  240. },
  241. {
  242. "cell_type": "markdown",
  243. "metadata": {},
  244. "source": [
  245. "Another Example with lists:"
  246. ]
  247. },
  248. {
  249. "cell_type": "code",
  250. "execution_count": 43,
  251. "metadata": {
  252. "collapsed": false
  253. },
  254. "outputs": [
  255. {
  256. "name": "stdout",
  257. "output_type": "stream",
  258. "text": [
  259. "True\n"
  260. ]
  261. }
  262. ],
  263. "source": [
  264. "list1 = [1, 2, 3]\n",
  265. "list2 = [1, 2, 3]\n",
  266. "print(list1 == list2) # True, because the content of the 2 lists are the same"
  267. ]
  268. },
  269. {
  270. "cell_type": "markdown",
  271. "metadata": {},
  272. "source": [
  273. "If you want to compare whether two variables are referring to the same object, use the `is` operator:"
  274. ]
  275. },
  276. {
  277. "cell_type": "code",
  278. "execution_count": 44,
  279. "metadata": {
  280. "collapsed": false
  281. },
  282. "outputs": [
  283. {
  284. "name": "stdout",
  285. "output_type": "stream",
  286. "text": [
  287. "False\n"
  288. ]
  289. }
  290. ],
  291. "source": [
  292. "print(list1 is list2) # False, because they are 2 separate lists, albeit with same elements"
  293. ]
  294. },
  295. {
  296. "cell_type": "markdown",
  297. "metadata": {},
  298. "source": [
  299. "In Python, we use `None` to represent null value (i.e. the non-existance of an object). Also use `is` when comparing whether something is `None`"
  300. ]
  301. },
  302. {
  303. "cell_type": "code",
  304. "execution_count": 45,
  305. "metadata": {
  306. "collapsed": false
  307. },
  308. "outputs": [
  309. {
  310. "name": "stdout",
  311. "output_type": "stream",
  312. "text": [
  313. "True\n",
  314. "False\n"
  315. ]
  316. }
  317. ],
  318. "source": [
  319. "a = None\n",
  320. "print(a is None)\n",
  321. "print(a is not None)"
  322. ]
  323. },
  324. {
  325. "cell_type": "markdown",
  326. "metadata": {},
  327. "source": [
  328. "## Tuple\n",
  329. "\n",
  330. "If you need to wrap two (or more) values into one value, we can use a tuple. A tuple is basically an ordered pair/triple/quadruple/..."
  331. ]
  332. },
  333. {
  334. "cell_type": "code",
  335. "execution_count": 46,
  336. "metadata": {
  337. "collapsed": false
  338. },
  339. "outputs": [
  340. {
  341. "name": "stdout",
  342. "output_type": "stream",
  343. "text": [
  344. "0\n",
  345. "hello\n",
  346. "123\n"
  347. ]
  348. }
  349. ],
  350. "source": [
  351. "t = (0, 'hello', 123)\n",
  352. "print(t[0])\n",
  353. "print(t[1])\n",
  354. "print(t[2])"
  355. ]
  356. },
  357. {
  358. "cell_type": "markdown",
  359. "metadata": {},
  360. "source": [
  361. "You can unroll a tuple like this:"
  362. ]
  363. },
  364. {
  365. "cell_type": "code",
  366. "execution_count": 47,
  367. "metadata": {
  368. "collapsed": false
  369. },
  370. "outputs": [
  371. {
  372. "name": "stdout",
  373. "output_type": "stream",
  374. "text": [
  375. "0\n",
  376. "hello\n",
  377. "123\n"
  378. ]
  379. }
  380. ],
  381. "source": [
  382. "a, b, c = t\n",
  383. "print(a)\n",
  384. "print(b)\n",
  385. "print(c)"
  386. ]
  387. },
  388. {
  389. "cell_type": "markdown",
  390. "metadata": {},
  391. "source": [
  392. "Tuples are very useful if you want to a function return multiple values (we'll get to that in a later section.) Tuples are similar to lists on the surface, and some list functions work with tuples as well. The biggest difference between the two is that tuples are *immutable*, meaning its value cannot be changed after it is created."
  393. ]
  394. },
  395. {
  396. "cell_type": "code",
  397. "execution_count": 48,
  398. "metadata": {
  399. "collapsed": false
  400. },
  401. "outputs": [
  402. {
  403. "name": "stdout",
  404. "output_type": "stream",
  405. "text": [
  406. "3\n"
  407. ]
  408. },
  409. {
  410. "ename": "TypeError",
  411. "evalue": "'tuple' object does not support item assignment",
  412. "output_type": "error",
  413. "traceback": [
  414. "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
  415. "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
  416. "\u001b[0;32m<ipython-input-48-123686a85b3b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# length of the tuple\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m5\u001b[0m \u001b[0;31m# this will give you an error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
  417. "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
  418. ]
  419. }
  420. ],
  421. "source": [
  422. "print(len(t)) # length of the tuple\n",
  423. "t[1] = 5 # this will give you an error"
  424. ]
  425. },
  426. {
  427. "cell_type": "markdown",
  428. "metadata": {},
  429. "source": [
  430. "## Lists\n",
  431. "\n",
  432. "List will be used extensively through this class. Lists are 0-indexing just like Java, and can hold values of different types:"
  433. ]
  434. },
  435. {
  436. "cell_type": "code",
  437. "execution_count": 49,
  438. "metadata": {
  439. "collapsed": false
  440. },
  441. "outputs": [
  442. {
  443. "name": "stdout",
  444. "output_type": "stream",
  445. "text": [
  446. "[2, 'hello', 5, 6, True]\n",
  447. "2\n"
  448. ]
  449. }
  450. ],
  451. "source": [
  452. "a = [2, 'hello', 5, 6, True]\n",
  453. "print(a)\n",
  454. "print(a[0])"
  455. ]
  456. },
  457. {
  458. "cell_type": "markdown",
  459. "metadata": {},
  460. "source": [
  461. "List indexing can be done with negative numbers, `a[-1]` denotes the last element, `a[2]` the second last, and so on."
  462. ]
  463. },
  464. {
  465. "cell_type": "code",
  466. "execution_count": 50,
  467. "metadata": {
  468. "collapsed": false
  469. },
  470. "outputs": [
  471. {
  472. "name": "stdout",
  473. "output_type": "stream",
  474. "text": [
  475. "True\n",
  476. "6\n"
  477. ]
  478. }
  479. ],
  480. "source": [
  481. "print(a[-1])\n",
  482. "print(a[-2])"
  483. ]
  484. },
  485. {
  486. "cell_type": "markdown",
  487. "metadata": {},
  488. "source": [
  489. "Note that it is still possible to get an index out of range error in Python; the valid indices of a list is from -length to length-1."
  490. ]
  491. },
  492. {
  493. "cell_type": "markdown",
  494. "metadata": {},
  495. "source": [
  496. "Here are a few list functions that are really handy:"
  497. ]
  498. },
  499. {
  500. "cell_type": "code",
  501. "execution_count": 51,
  502. "metadata": {
  503. "collapsed": false
  504. },
  505. "outputs": [
  506. {
  507. "name": "stdout",
  508. "output_type": "stream",
  509. "text": [
  510. "7\n",
  511. "32\n",
  512. "0\n",
  513. "9\n",
  514. "[0, 1, 2, 5, 7, 8, 9]\n"
  515. ]
  516. }
  517. ],
  518. "source": [
  519. "a = [2, 5, 7, 0, 8, 1, 9]\n",
  520. "print(len(a)) # number of elements in list a\n",
  521. "print(sum(a)) # sum of all elements in list a\n",
  522. "print(min(a)) # min element in list a\n",
  523. "print(max(a)) # max element is list a\n",
  524. "a.sort() # sorts the list\n",
  525. "print(a)"
  526. ]
  527. },
  528. {
  529. "cell_type": "markdown",
  530. "metadata": {},
  531. "source": [
  532. "## Loops\n",
  533. "\n",
  534. "Python has a very simple for-each loop that you will be using quite a lot:"
  535. ]
  536. },
  537. {
  538. "cell_type": "code",
  539. "execution_count": 53,
  540. "metadata": {
  541. "collapsed": false
  542. },
  543. "outputs": [
  544. {
  545. "name": "stdout",
  546. "output_type": "stream",
  547. "text": [
  548. "1\n",
  549. "2\n",
  550. "3\n",
  551. "4\n",
  552. "5\n"
  553. ]
  554. }
  555. ],
  556. "source": [
  557. "for x in [1, 2, 3, 4, 5]:\n",
  558. " print(x)"
  559. ]
  560. },
  561. {
  562. "cell_type": "markdown",
  563. "metadata": {},
  564. "source": [
  565. "It has a `range()` function that supports generating a list of integers over a given range:"
  566. ]
  567. },
  568. {
  569. "cell_type": "code",
  570. "execution_count": 58,
  571. "metadata": {
  572. "collapsed": false
  573. },
  574. "outputs": [
  575. {
  576. "name": "stdout",
  577. "output_type": "stream",
  578. "text": [
  579. "0\n",
  580. "1\n",
  581. "2\n",
  582. "3\n",
  583. "4\n",
  584. "5\n",
  585. "6\n",
  586. "7\n",
  587. "8\n",
  588. "9\n",
  589. "=========\n",
  590. "2\n",
  591. "3\n",
  592. "4\n"
  593. ]
  594. }
  595. ],
  596. "source": [
  597. "for x in range(10): # print 0 to 9\n",
  598. " print(x)\n",
  599. "print(\"=========\")\n",
  600. "for x in range(2, 5): # print 2 to 4\n",
  601. " print(x)"
  602. ]
  603. },
  604. {
  605. "cell_type": "markdown",
  606. "metadata": {},
  607. "source": [
  608. "It also has a `while` loop that works the same as in Java:"
  609. ]
  610. },
  611. {
  612. "cell_type": "code",
  613. "execution_count": 62,
  614. "metadata": {
  615. "collapsed": false
  616. },
  617. "outputs": [
  618. {
  619. "name": "stdout",
  620. "output_type": "stream",
  621. "text": [
  622. "10\n"
  623. ]
  624. }
  625. ],
  626. "source": [
  627. "# calculate base-2 log of x by counting how many times we can divide x by 2 before it reaches 1\n",
  628. "x = 1024\n",
  629. "count = 0\n",
  630. "while x > 1:\n",
  631. " count += 1\n",
  632. " x /= 2\n",
  633. "print(count)"
  634. ]
  635. },
  636. {
  637. "cell_type": "markdown",
  638. "metadata": {},
  639. "source": [
  640. "## Functions and List Comprehension\n",
  641. "\n"
  642. ]
  643. },
  644. {
  645. "cell_type": "code",
  646. "execution_count": 52,
  647. "metadata": {
  648. "collapsed": false
  649. },
  650. "outputs": [
  651. {
  652. "name": "stdout",
  653. "output_type": "stream",
  654. "text": [
  655. "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]\n"
  656. ]
  657. }
  658. ],
  659. "source": [
  660. "def isPrime(x):\n",
  661. " for i in range(2, x):\n",
  662. " if x % i == 0:\n",
  663. " return False\n",
  664. " return True\n",
  665. "\n",
  666. "primes = [p for p in range(2,1000) if isPrime(p)]\n",
  667. "print(primes)"
  668. ]
  669. },
  670. {
  671. "cell_type": "code",
  672. "execution_count": 60,
  673. "metadata": {
  674. "collapsed": false
  675. },
  676. "outputs": [
  677. {
  678. "name": "stdout",
  679. "output_type": "stream",
  680. "text": [
  681. "[3, 5, 6, 9, 10, 12, 18, 20, 21, 24, 25, 27, 33, 35, 36, 39, 40, 42, 48, 50, 51, 54, 55, 57, 63, 65, 66, 69, 70, 72, 78, 80, 81, 84, 85, 87, 93, 95, 96, 99]\n"
  682. ]
  683. }
  684. ],
  685. "source": [
  686. "fizzbuzz = [x for x in range(100) if (x % 3 == 0 or x % 5 == 0) and x % 15 != 0]\n",
  687. "print(fizzbuzz)"
  688. ]
  689. },
  690. {
  691. "cell_type": "code",
  692. "execution_count": null,
  693. "metadata": {
  694. "collapsed": true
  695. },
  696. "outputs": [],
  697. "source": []
  698. },
  699. {
  700. "cell_type": "markdown",
  701. "metadata": {},
  702. "source": [
  703. "- If statements, == vs is, boolean operators\n",
  704. "- functions, named params, optional params\n",
  705. "- Tuples, Lists, Maps\n",
  706. "- List comprehension\n"
  707. ]
  708. }
  709. ],
  710. "metadata": {
  711. "kernelspec": {
  712. "display_name": "Python 2",
  713. "language": "python",
  714. "name": "python2"
  715. },
  716. "language_info": {
  717. "codemirror_mode": {
  718. "name": "ipython",
  719. "version": 2
  720. },
  721. "file_extension": ".py",
  722. "mimetype": "text/x-python",
  723. "name": "python",
  724. "nbconvert_exporter": "python",
  725. "pygments_lexer": "ipython2",
  726. "version": "2.7.11"
  727. }
  728. },
  729. "nbformat": 4,
  730. "nbformat_minor": 0
  731. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement