Advertisement
Guest User

Untitled

a guest
Feb 1st, 2015
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.97 KB | None | 0 0
  1. {
  2. "metadata": {
  3. "name": "",
  4. "signature": "sha256:250e0bc23963ee1fb2cb756e0f51fa9ba254dbe7fc5b16d5a318d2c9a1e2e59b"
  5. },
  6. "nbformat": 3,
  7. "nbformat_minor": 0,
  8. "worksheets": [
  9. {
  10. "cells": [
  11. {
  12. "cell_type": "code",
  13. "collapsed": false,
  14. "input": [
  15. "cd '/Users/jenniferli/Desktop/Python_Class/'"
  16. ],
  17. "language": "python",
  18. "metadata": {},
  19. "outputs": []
  20. },
  21. {
  22. "cell_type": "code",
  23. "collapsed": false,
  24. "input": [
  25. "run anthem.py"
  26. ],
  27. "language": "python",
  28. "metadata": {},
  29. "outputs": [],
  30. "prompt_number": 3
  31. },
  32. {
  33. "cell_type": "heading",
  34. "level": 2,
  35. "metadata": {},
  36. "source": [
  37. "Python as a calculator"
  38. ]
  39. },
  40. {
  41. "cell_type": "code",
  42. "collapsed": false,
  43. "input": [
  44. "1 + 1"
  45. ],
  46. "language": "python",
  47. "metadata": {},
  48. "outputs": []
  49. },
  50. {
  51. "cell_type": "code",
  52. "collapsed": false,
  53. "input": [
  54. "4 % 3"
  55. ],
  56. "language": "python",
  57. "metadata": {},
  58. "outputs": []
  59. },
  60. {
  61. "cell_type": "code",
  62. "collapsed": false,
  63. "input": [
  64. "1.0 + 5"
  65. ],
  66. "language": "python",
  67. "metadata": {},
  68. "outputs": []
  69. },
  70. {
  71. "cell_type": "heading",
  72. "level": 6,
  73. "metadata": {},
  74. "source": [
  75. "Is 6840827 divisible by 7?"
  76. ]
  77. },
  78. {
  79. "cell_type": "heading",
  80. "level": 2,
  81. "metadata": {},
  82. "source": [
  83. "Variables"
  84. ]
  85. },
  86. {
  87. "cell_type": "code",
  88. "collapsed": false,
  89. "input": [
  90. "x = 3\n",
  91. "y = 4.2\n",
  92. "x + y"
  93. ],
  94. "language": "python",
  95. "metadata": {},
  96. "outputs": []
  97. },
  98. {
  99. "cell_type": "heading",
  100. "level": 6,
  101. "metadata": {},
  102. "source": [
  103. "What happens if you enter an undefined variable, such as z?"
  104. ]
  105. },
  106. {
  107. "cell_type": "heading",
  108. "level": 2,
  109. "metadata": {},
  110. "source": [
  111. "Strings"
  112. ]
  113. },
  114. {
  115. "cell_type": "code",
  116. "collapsed": false,
  117. "input": [
  118. "mystring = \"I love Python!\"\n",
  119. "print mystring"
  120. ],
  121. "language": "python",
  122. "metadata": {},
  123. "outputs": []
  124. },
  125. {
  126. "cell_type": "heading",
  127. "level": 2,
  128. "metadata": {},
  129. "source": [
  130. "Comments"
  131. ]
  132. },
  133. {
  134. "cell_type": "code",
  135. "collapsed": false,
  136. "input": [
  137. "# This is a comment. \n",
  138. "# Comments allow you to annotate your code.\n",
  139. "# You can quickly comment blocks of code using Cmd + /\n",
  140. "# There is no spell-checking."
  141. ],
  142. "language": "python",
  143. "metadata": {},
  144. "outputs": []
  145. },
  146. {
  147. "cell_type": "heading",
  148. "level": 2,
  149. "metadata": {},
  150. "source": [
  151. "Logical Operators"
  152. ]
  153. },
  154. {
  155. "cell_type": "code",
  156. "collapsed": false,
  157. "input": [
  158. "1 == 1"
  159. ],
  160. "language": "python",
  161. "metadata": {},
  162. "outputs": []
  163. },
  164. {
  165. "cell_type": "code",
  166. "collapsed": false,
  167. "input": [
  168. "1 != 3"
  169. ],
  170. "language": "python",
  171. "metadata": {},
  172. "outputs": []
  173. },
  174. {
  175. "cell_type": "code",
  176. "collapsed": false,
  177. "input": [
  178. "x = 3\n",
  179. "(1 == 1) and x > 3\n",
  180. "(1 == 1) or x > 3"
  181. ],
  182. "language": "python",
  183. "metadata": {},
  184. "outputs": []
  185. },
  186. {
  187. "cell_type": "heading",
  188. "level": 2,
  189. "metadata": {},
  190. "source": [
  191. "If/Else Statements"
  192. ]
  193. },
  194. {
  195. "cell_type": "code",
  196. "collapsed": false,
  197. "input": [
  198. "# note the identation!\n",
  199. "cookies = 5\n",
  200. "kale = 4\n",
  201. "if cookies == kale:\n",
  202. " print \"cookies are equal to kale.\"\n",
  203. "elif cookies > kale:\n",
  204. " print \"cookies are better than kale.\"\n",
  205. "else:\n",
  206. " print \"kale is better than cookies.\""
  207. ],
  208. "language": "python",
  209. "metadata": {},
  210. "outputs": []
  211. },
  212. {
  213. "cell_type": "heading",
  214. "level": 6,
  215. "metadata": {},
  216. "source": [
  217. "Write code to check whether a number is even or odd. "
  218. ]
  219. },
  220. {
  221. "cell_type": "heading",
  222. "level": 2,
  223. "metadata": {},
  224. "source": [
  225. "Lists and Indexing"
  226. ]
  227. },
  228. {
  229. "cell_type": "code",
  230. "collapsed": false,
  231. "input": [
  232. "mylist = [1,2,3,7,4]\n",
  233. "mylist[0], mylist[-1]\n",
  234. "len(mylist)\n",
  235. "mylist.find(7)"
  236. ],
  237. "language": "python",
  238. "metadata": {},
  239. "outputs": []
  240. },
  241. {
  242. "cell_type": "heading",
  243. "level": 6,
  244. "metadata": {},
  245. "source": [
  246. "How do you get the first two elements of mylist?"
  247. ]
  248. },
  249. {
  250. "cell_type": "heading",
  251. "level": 6,
  252. "metadata": {},
  253. "source": [
  254. "Can you print mylist in reverse order? Bonus points if you can find two ways to do it. (Hint: try typing mylist.<Tab>?)"
  255. ]
  256. },
  257. {
  258. "cell_type": "heading",
  259. "level": 2,
  260. "metadata": {},
  261. "source": [
  262. "For-loops"
  263. ]
  264. },
  265. {
  266. "cell_type": "code",
  267. "collapsed": false,
  268. "input": [
  269. "for i in range(10):\n",
  270. " print i\n"
  271. ],
  272. "language": "python",
  273. "metadata": {},
  274. "outputs": []
  275. },
  276. {
  277. "cell_type": "heading",
  278. "level": 6,
  279. "metadata": {},
  280. "source": [
  281. "Write a for-loop to print out each letter in your name. "
  282. ]
  283. },
  284. {
  285. "cell_type": "code",
  286. "collapsed": false,
  287. "input": [
  288. "myname = \"jennifer\"\n",
  289. "for i in range(len(myname)):\n",
  290. " print myname[i],\n",
  291. " "
  292. ],
  293. "language": "python",
  294. "metadata": {},
  295. "outputs": []
  296. },
  297. {
  298. "cell_type": "heading",
  299. "level": 2,
  300. "metadata": {},
  301. "source": [
  302. "While-loops"
  303. ]
  304. },
  305. {
  306. "cell_type": "code",
  307. "collapsed": false,
  308. "input": [
  309. "i = 0\n",
  310. "while i < 10:\n",
  311. " print i\n",
  312. " i+=1"
  313. ],
  314. "language": "python",
  315. "metadata": {},
  316. "outputs": []
  317. },
  318. {
  319. "cell_type": "heading",
  320. "level": 6,
  321. "metadata": {},
  322. "source": [
  323. "Write a while-loop to print out each letter in your name."
  324. ]
  325. },
  326. {
  327. "cell_type": "code",
  328. "collapsed": false,
  329. "input": [
  330. "# Solution 1\n",
  331. "index = 0\n",
  332. "while index <len(myname):\n",
  333. " print myname[index],\n",
  334. " index +=1\n",
  335. "\n",
  336. "# Solution 2\n",
  337. "while myname:\n",
  338. " print myname[0],\n",
  339. " myname = myname[1:]\n",
  340. " "
  341. ],
  342. "language": "python",
  343. "metadata": {},
  344. "outputs": []
  345. },
  346. {
  347. "cell_type": "code",
  348. "collapsed": false,
  349. "input": [
  350. "# What happens if you run the following code? \n",
  351. "while True:\n",
  352. " print 1"
  353. ],
  354. "language": "python",
  355. "metadata": {},
  356. "outputs": []
  357. },
  358. {
  359. "cell_type": "heading",
  360. "level": 6,
  361. "metadata": {},
  362. "source": [
  363. "Print out all powers of 2 that are less than 1000."
  364. ]
  365. },
  366. {
  367. "cell_type": "code",
  368. "collapsed": false,
  369. "input": [
  370. "start = 1\n",
  371. "while start < 1000:\n",
  372. " print start,\n",
  373. " start *=2"
  374. ],
  375. "language": "python",
  376. "metadata": {},
  377. "outputs": []
  378. },
  379. {
  380. "cell_type": "code",
  381. "collapsed": false,
  382. "input": [
  383. "# How many times does the number 'e' appear in the following excerpt?\n",
  384. "\n",
  385. "mr_darcy = \"I have faults enough, but they are not, \\\n",
  386. "I hope, of understanding. My temper I dare not vouch for. \\\n",
  387. "It is, I believe, too little yielding\u2014 certainly too little for the convenience of the world. \\\n",
  388. "I cannot forget the follies and vices of other so soon as I ought, nor their offenses against myself. \\\n",
  389. "My feelings are not puffed about with every attempt to move them. \\\n",
  390. "My temper would perhaps be called resentful. \\\n",
  391. "My good opinion once lost, is lost forever.\" "
  392. ],
  393. "language": "python",
  394. "metadata": {},
  395. "outputs": []
  396. },
  397. {
  398. "cell_type": "code",
  399. "collapsed": false,
  400. "input": [
  401. "## Solution 1\n",
  402. "count = 0\n",
  403. "for i in mr_darcy:\n",
  404. " if i =='e':\n",
  405. " count+=1\n",
  406. " else:\n",
  407. " pass\n",
  408. " \n",
  409. "## Solution 2 (Advanced)\n",
  410. "sum([1 for i in mr_darcy if i =='e'])\n",
  411. " \n"
  412. ],
  413. "language": "python",
  414. "metadata": {},
  415. "outputs": []
  416. },
  417. {
  418. "cell_type": "code",
  419. "collapsed": false,
  420. "input": [
  421. "# Challenge! Your friend just sent you a secret text! Translate it using Python. \n",
  422. "\n",
  423. "english = \"abcdefghijklmnopqrstuvwxyz\"\n",
  424. "secret_language = \"hvicgnwzxqektmuspjlbyodrfa\"\n",
  425. "\n",
  426. "text = 'sfbzum xl bzg mgd vkhie'"
  427. ],
  428. "language": "python",
  429. "metadata": {},
  430. "outputs": []
  431. },
  432. {
  433. "cell_type": "code",
  434. "collapsed": false,
  435. "input": [
  436. "translated = ''\n",
  437. "for i in text:\n",
  438. " if i == ' ':\n",
  439. " translated = translated + ' '\n",
  440. " else:\n",
  441. " translated = translated + english[secret_language.find(i)]"
  442. ],
  443. "language": "python",
  444. "metadata": {},
  445. "outputs": []
  446. },
  447. {
  448. "cell_type": "heading",
  449. "level": 2,
  450. "metadata": {},
  451. "source": [
  452. "Functions "
  453. ]
  454. },
  455. {
  456. "cell_type": "heading",
  457. "level": 6,
  458. "metadata": {},
  459. "source": [
  460. "Why use functions? "
  461. ]
  462. },
  463. {
  464. "cell_type": "heading",
  465. "level": 6,
  466. "metadata": {},
  467. "source": [
  468. "Reason #1: Reusability"
  469. ]
  470. },
  471. {
  472. "cell_type": "code",
  473. "collapsed": false,
  474. "input": [
  475. "# a simple function \n",
  476. "def myfunction():\n",
  477. " print(\"I love Python.\")\n",
  478. "\n",
  479. "myfunction()"
  480. ],
  481. "language": "python",
  482. "metadata": {},
  483. "outputs": []
  484. },
  485. {
  486. "cell_type": "code",
  487. "collapsed": false,
  488. "input": [
  489. "def lovefunction(mystring):\n",
  490. " print(\"I love \" + mystring)\n",
  491. "\n",
  492. "lovefunction(\"cookies\")\n",
  493. "lovefunction(\"summer\")\n",
  494. "lovefunction(\"Python\")"
  495. ],
  496. "language": "python",
  497. "metadata": {},
  498. "outputs": []
  499. },
  500. {
  501. "cell_type": "code",
  502. "collapsed": false,
  503. "input": [
  504. "favorite_foods = [\"cookies\",\"ice cream\"]\n",
  505. "healthy_foods = [\"kale\",\"quinoa\"]\n",
  506. "\n",
  507. "def myfoodpicker(food_item):\n",
  508. " \n",
  509. " if food_item in favorite_foods:\n",
  510. " print(\"Yum!\")\n",
  511. " elif food_item in healthy_foods:\n",
  512. " print(\"Yuck!\")\n",
  513. " else:\n",
  514. " print(\"Can I have ice cream with that?\")\n"
  515. ],
  516. "language": "python",
  517. "metadata": {},
  518. "outputs": []
  519. },
  520. {
  521. "cell_type": "heading",
  522. "level": 6,
  523. "metadata": {},
  524. "source": [
  525. "Reason #2: Abstraction"
  526. ]
  527. },
  528. {
  529. "cell_type": "code",
  530. "collapsed": true,
  531. "input": [
  532. "## Find the largest prime less than or equal to a certain number \n",
  533. "def find_largest_prime(mynumber):\n",
  534. " ## use simple sieve method\n",
  535. " ## highly inefficient for large values\n",
  536. " possible_results = range(1, mynumber + 1)\n",
  537. " possible_factors = range(2, int(floor(mynumber/2))+1)\n",
  538. " \n",
  539. " def get_multiples(factor):\n",
  540. " # get all multiples of factor \n",
  541. " return [factor*i for i in range(1, int(floor(mynumber/factor)) + 1)]\n",
  542. " \n",
  543. " for i in possible_factors:\n",
  544. " # remove all multiples of said factor\n",
  545. " possible_results = [x for x in possible_results if x not in get_multiples(i)]\n",
  546. " \n",
  547. " return possible_results[-1] # return largest value\n",
  548. "\n",
  549. "## using the function \n",
  550. "\n",
  551. "find_largest_prime(100)\n",
  552. "find_largest_prime(50)\n",
  553. " "
  554. ],
  555. "language": "python",
  556. "metadata": {},
  557. "outputs": []
  558. },
  559. {
  560. "cell_type": "heading",
  561. "level": 6,
  562. "metadata": {},
  563. "source": [
  564. "Write a function to check if a number is divisible by 7."
  565. ]
  566. },
  567. {
  568. "cell_type": "code",
  569. "collapsed": false,
  570. "input": [
  571. "def divisible_by_7(mynumber):\n",
  572. " if mynumber%7 ==0:\n",
  573. " print \"This number is divisible by 7\"\n",
  574. " else:\n",
  575. " print \"This number is not divisible by 7\"\n"
  576. ],
  577. "language": "python",
  578. "metadata": {},
  579. "outputs": []
  580. },
  581. {
  582. "cell_type": "heading",
  583. "level": 6,
  584. "metadata": {},
  585. "source": [
  586. "Project: Your goal is to build a chatterbot that produces typical teenager responses.\n",
  587. "For example, if you ask the chatterbot a question, then it respond with \"It was boring.\"\n",
  588. "Feel free to be creative!"
  589. ]
  590. },
  591. {
  592. "cell_type": "code",
  593. "collapsed": false,
  594. "input": [
  595. "import string\n",
  596. "import re\n",
  597. "import numpy as np\n",
  598. "import sys\n",
  599. "\n",
  600. "def mood_generator(mood):\n",
  601. " if mood == 'good':\n",
  602. " score = np.random.random(1) + 0.1\n",
  603. " elif mood == 'bad':\n",
  604. " score = np.random.random(1) - 0.1\n",
  605. " else:\n",
  606. " score = np.random.random(1)\n",
  607. " return score\n",
  608. "\n",
  609. "\n",
  610. "def make_bot(mood):\n",
  611. " mood_score = mood_generator(mood)\n",
  612. " \n",
  613. " done = False\n",
  614. " while not done:\n",
  615. " mom_says = raw_input(\"Say something: (Type 'quit' to exit the program)\")\n",
  616. " ## remove quotes\n",
  617. " if mom_says == 'quit' or len(mom_says) == 0:\n",
  618. " done = True\n",
  619. " else:\n",
  620. " if mom_says[-1] == '?':\n",
  621. " if mood_score > 0.5:\n",
  622. " print \"It was alright.\"\n",
  623. " else:\n",
  624. " print \"Answer: Leave me alone.\"\n",
  625. " \n",
  626. " elif mom_says[-1] == '!':\n",
  627. " if mood_score > 0.5:\n",
  628. " print \"Ok! Ok!\"\n",
  629. " else:\n",
  630. " print \"Answer: Stop yelling!\"\n",
  631. " elif mom_says[-1] == '.':\n",
  632. " if mood_score > 0.5:\n",
  633. " print \"Answer: Whatever.\"\n",
  634. " else:\n",
  635. " print \"Answer: Whatever\"\n",
  636. " elif re.search('homework', string.lower(mom_says)):\n",
  637. " print \"I'll do that later\"\n",
  638. " else:\n",
  639. " pass\n",
  640. " \n",
  641. "if __name__ == '__main__':\n",
  642. " mood = sys.argv[1]\n",
  643. " make_bot(mood)"
  644. ],
  645. "language": "python",
  646. "metadata": {},
  647. "outputs": []
  648. }
  649. ],
  650. "metadata": {}
  651. }
  652. ]
  653. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement