Guest User

Untitled

a guest
Feb 20th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.17 KB | None | 0 0
  1. #
  2. # RWET Programming Exercise B
  3. #
  4. # This worksheet is also a Python program. Your task is to read the
  5. # task descriptions below and then write one or more Python statements to
  6. # carry out the tasks. There's a Python "print" statement before each
  7. # task that will display the expected output for that task; you can use
  8. # this to ensure that your statements are correct.
  9. #
  10.  
  11. print "------"
  12. print "Task 1: Arithmetic expressions"
  13. print "Expected output: 7"
  14.  
  15. # Task 1: Add parentheses to the Python statement below so that it prints
  16. # out the number 7.
  17.  
  18. print 10 + 4 / 2
  19.  
  20. #------------------------------------------------------------------------
  21.  
  22. print "\n------"
  23. print "Task 2: Expressions of inequality"
  24. print "Expected output: True"
  25.  
  26. # Task 2: Change the operator in the statement below so that it displays
  27. # "True" instead of "False."
  28.  
  29. print 14 > 15
  30.  
  31. #------------------------------------------------------------------------
  32.  
  33. print "\n------"
  34. print "Task 3: Variable assignment"
  35. print "Expected output: 32"
  36.  
  37. # Task 3: Change the variable assignment below so that the print statement
  38. # displays "32." (Don't change the print statement!)
  39.  
  40. my_number = 17
  41. print my_number
  42.  
  43. #------------------------------------------------------------------------
  44.  
  45. print "\n------"
  46. print "Task 4: Types"
  47. print "Expected output: <type 'str'>"
  48.  
  49. # Task 4: Three variables are assigned below, all with different types.
  50. # Replace the word "None" inside the parentheses of type() in the print
  51. # statement below so that it prints "<type 'str'>".
  52.  
  53. x = 14
  54. y = 17.4
  55. z = "mother said there'd be days like these"
  56. print type(None)
  57.  
  58. #------------------------------------------------------------------------
  59.  
  60. print "\n------"
  61. print "Task 5: String literals"
  62. print "Expected output: We aren't friends now."
  63.  
  64. # Task 5: Modify the print statement below so that it displays the string
  65. # "We aren't friends now." (i.e., change "are" to "aren't".) Use a
  66. # single quoted string---don't change it to double quotes.
  67.  
  68. print 'We are friends now.'
  69.  
  70. #------------------------------------------------------------------------
  71.  
  72. print "\n------"
  73. print "Task 6: Questions about strings"
  74. print "Expected output: 51"
  75.  
  76. # Task 6: After "print" below, write an expression that evaluates to the
  77. # sum of the lengths of the two string variables defined below (first_line
  78. # and second_line). Use the len() function.
  79.  
  80. first_line = "It was the best of times."
  81. second_line = "It was the worst of times."
  82. print # your code here!
  83.  
  84. #------------------------------------------------------------------------
  85.  
  86. print "\n------"
  87. print "Task 7: Questions about strings, part 2"
  88. print "Expected output: 37"
  89.  
  90. # Task 7: After "print" below, write an expression that evaluates to the
  91. # position of the word "window" in the string defined in the variable
  92. # called "romeo." Use the .find() method.
  93.  
  94. romeo = "But, soft! what light through yonder window breaks?"
  95. print # your code here!
  96.  
  97. #------------------------------------------------------------------------
  98.  
  99. print "\n------"
  100. print "Task 8: String transformations"
  101. print "Expected output: and the horse you rode in on"
  102.  
  103. # Task 8: Modify the print statement below so that it prints out the contents
  104. # of the variable "benediction", but with all white space removed from
  105. # the beginning and end of the string. Use the .strip() method.
  106.  
  107. benediction = " and the horse you rode in on \n"
  108. print benediction
  109.  
  110. #------------------------------------------------------------------------
  111.  
  112. print "\n------"
  113. print "Task 9: String transformations, part 2"
  114. print "Expected output: AND THE HORSE YOU RODE IN ON"
  115.  
  116. # Task 9: Using the previously defined "benediction" variable, write an
  117. # expression after the word "print" below that evaluates to the content
  118. # of the string, with all whitespace removed, and with all letters
  119. # converted to uppercase. Use the .upper() method.
  120.  
  121. print # your code here!
  122.  
  123. #------------------------------------------------------------------------
  124.  
  125. print "\n------"
  126. print "Task 10: String indexing"
  127. print "Expected output: p"
  128.  
  129. # Task 10: Modify the value assigned to variable "offset" below so that
  130. # the following "print" statement displays the letter "p".
  131.  
  132. offset = 0
  133. print "apple"[offset]
  134.  
  135. #------------------------------------------------------------------------
  136.  
  137. print "\n------"
  138. print "Task 11: String slices"
  139. print "Expected output: yonder"
  140.  
  141. # Task 11: Modify the values assigned to variables "start" and "end"
  142. # below so that the following "print" statement displays the word "yonder".
  143.  
  144. start = 0
  145. end = 10
  146. romeo = "But, soft! what light through yonder window breaks?"
  147. print romeo[start:end]
  148.  
  149. #------------------------------------------------------------------------
  150.  
  151. print "\n------"
  152. print "Task 12: Integers and strings"
  153. print "Expected output: 100"
  154.  
  155. # Task 12: Modify the statement below so that it displays the number 100.
  156. # Do this using the int() function (hint: you need to use it twice).
  157.  
  158. print "19" + "81"
  159.  
  160. print "\n------"
  161. print "Task 13: List indexes"
  162. print "Expected output: alpha"
  163.  
  164. # Task 13: A variable "greek" is defined below. The value of this variable
  165. # is of type list. Change the expression below the variable definition so
  166. # that it prints "alpha" (instead of "beta").
  167.  
  168. greek = ["alpha", "beta", "gamma", "delta", "epsilon"]
  169. print greek[1]
  170.  
  171. #------------------------------------------------------------------------
  172.  
  173. print "\n------"
  174. print "Task 14: List slices"
  175. print "Expected output: ['beta', 'gamma', 'delta']"
  176.  
  177. # Task 14: Change the values of the variables "start" and "finish" below so that
  178. # the print statement displays the second through fourth items in the list
  179. # "greek" (defined above).
  180.  
  181. start = 0
  182. finish = 6
  183. print greek[start:finish]
  184.  
  185. #------------------------------------------------------------------------
  186.  
  187. print "\n------"
  188. print "Task 15: List slices, part 2"
  189. print "Expected output: ['gamma', 'delta']"
  190.  
  191. # Task 15: Change the value of the variable "foo" below so that the print
  192. # statement displays the last two members of the list "greek" (defined above).
  193. # Use a negative number for "foo".
  194.  
  195. foo = 0
  196. print greek[foo:]
  197.  
  198. #------------------------------------------------------------------------
  199.  
  200. print "\n------"
  201. print "Task 16: List operations"
  202. print "Expected output: True"
  203.  
  204. # Task 16: Change the value of the variable "letter_to_look_for' below so
  205. # that the print statement displays "True."
  206.  
  207. letter_to_look_for = "aleph"
  208. print letter_to_look_for in greek
  209.  
  210. #------------------------------------------------------------------------
  211.  
  212. print "\n------"
  213. print "Task 17: List operations, part 2"
  214. print "Expected output: ['alpha', 'beta', 'delta', 'epsilon', 'gamma']"
  215.  
  216. # Task 17: Change the expression below so that the print statement displays
  217. # the list "greek" (defined above) in alphabetical order. (Use the "sorted"
  218. # function.
  219.  
  220. print greek
  221.  
  222. #------------------------------------------------------------------------
  223.  
  224. print "\n------"
  225. print "Task 18: Modifying lists"
  226. print "Expected output: ['alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta']"
  227.  
  228. # Task 18: Write a Python statement that adds a new item, "zeta", to the
  229. # list "greek" (defined above). The print statement should display the updated
  230. # list.
  231.  
  232. # write your statement here
  233. print greek
  234.  
  235. #------------------------------------------------------------------------
  236.  
  237. print "\n------"
  238. print "Task 19: Loops"
  239. print "Expected output:"
  240. print " alpha"
  241. print " beta"
  242. print " gamma"
  243. print " delta"
  244. print " epsilon"
  245. print " zeta"
  246.  
  247. # Task 19: Write a "for" loop below that prints out each item in the list
  248. # "greek" (defined above). (The list should contain the item that you
  249. # added to the list in task 6.)
  250.  
  251.  
  252.  
  253.  
  254. #------------------------------------------------------------------------
  255.  
  256. print "\n------"
  257. print "Task 20: Loops, part 2"
  258. print "Expected output:"
  259. print " Alpha"
  260. print " Beta"
  261. print " Gamma"
  262. print " Delta"
  263. print " Epsilon"
  264. print " Zeta"
  265.  
  266. # Task 20: Write a "for" loop below that prints out each item in the list
  267. # "greek" (defined above), but with the first letter of each item capitalized.
  268. # (The list should contain the item that you added to the list in task 6.)
  269.  
  270.  
  271.  
  272.  
  273. #------------------------------------------------------------------------
  274.  
  275. print "\n------"
  276. print "Task 21: Split and join"
  277. print "Expected output:"
  278. print " 81"
  279. print " 9-18-81"
  280.  
  281. # Task 21: Modify the variable "separator" below so that the first print
  282. # statement displays "81". Modify the variable "glue" so that the second print
  283. # statement displays "9-18-81".
  284.  
  285. separator = "?"
  286. glue = "?"
  287. parts = "9/18/81".split(separator)
  288. print parts[-1]
  289. print glue.join(parts)
  290.  
  291. #------------------------------------------------------------------------
  292.  
  293. print "\n------"
  294. print "Task 22: All together now"
  295. print "Expected output: alpha, beta, gamma, delta, epsilon, zeta, eta, theta"
  296.  
  297. # Task 22: Make three changes on the Python code below, as follows: (1) replace
  298. # [] with an expression that evaluates to a list with two items, "eta" and
  299. # "theta" (using the .split() method). (2) Replace the word "pass" with a
  300. # Python statement, so that the "for" loop has the effect of adding two new
  301. # items to the list "greek". (Use the .append() method.) (3) Change the value
  302. # of the variable "glue" so that the desired output is displayed.
  303.  
  304. new_letters = "eta theta"
  305. new_letters_list = [] # <-- replace this
  306.  
  307. for letter_name in new_letters_list:
  308. pass # <-- and replace this
  309.  
  310. glue = "?" # <-- and replace this
  311.  
  312. print glue.join(greek)
Add Comment
Please, Sign In to add comment