Advertisement
jspill

webinar-python-exam-review-v4-2021-10-31

Oct 30th, 2021
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.29 KB | None | 0 0
  1. # Exam Review 2021 Oct 31
  2.  
  3. # LABS
  4. # Ch 3-13
  5. # Ch 14-18 not as important
  6. # Ch 19-30 just LABS, but important practice!
  7.  
  8. # Look at ALL requirements of a question
  9. # CODE must be reusable
  10. # don't be afraid of Submit for Grading...
  11. # different TEST CASES are part of the problem
  12.  
  13. # Comp 1 Basic syntax and knowledge: operators, data types, etc
  14. # DATA TYPES
  15. # numeric types: int and float
  16. # strings... str
  17. # lists
  18. # dictionaries... dict
  19. # set... no order (which means no indices, no slices), all unique values
  20. # tuple... immutable
  21. # MODULES
  22. # math
  23.  
  24. # OPERATORS
  25. # +
  26. # -
  27. # *
  28. # /
  29. # % # modulo... gets the INT remainder, how many whole things didn't fit?
  30. # // # floor division
  31. # How many weeks and days is 41 days?
  32. # print(41//7) # gets the weeks
  33. # print(41%7) # gets the days leftover
  34. # ** # can also do with math.pow()
  35. # = # assignment
  36. # == # comparison, the "equality operator"
  37. # !=
  38. # <
  39. # >
  40. # <=
  41. # >=
  42. # +=
  43. # -=
  44. # KEYWORDS that are used like OPERATORS
  45. # not
  46. # in # if someItem in someList, if not someItem in someList
  47. # and
  48. # or
  49.  
  50. # Comp 2: Control Flow Structurs
  51. # IF... if, if/else, if/elif/else
  52. # LOOPS...
  53. # WHILE - like an IF that repeats as long as the condition stays True
  54. # FOR - doing a known number of times or doing some once for every ITEM in a CONTAINER
  55. # for someItem in someContainer:
  56. # for item in myList:
  57. # for char in myString:
  58. # for i in range(someNum):
  59. # for i in range(len(someList)): # i for the index, someList[i] for the value
  60. # FUNCTIONS
  61. # defining vs calling a function
  62. # parameters vs "regular" or "outside" variables
  63. # def myFunction(x, end="\n"):
  64. # return vs print(), etc
  65. # understand that methods are themselves functions
  66.  
  67. # ADVANCED CONTROL FLOW
  68. # Ch 10: try/except
  69. # Ch 13: classes
  70.  
  71. # How many lines or separate strings are in the data input window?
  72. # print(input())
  73. # print(input())
  74.  
  75. # BUILT-IN FUNCTIONS
  76. # help()
  77. # dir() # print(dir(str)), print(dir(list))
  78. # input()
  79. # print()
  80. # range()
  81. # enumerate()
  82. # len()
  83. # sum()
  84. # min()
  85. # max()
  86. # sorted() # compare to list.sort()
  87. # reversed() # compare to list.reverse()
  88. # int()
  89. # float()
  90. # list() # []
  91. # set()
  92. # tuple() # ()
  93. # dict() # {}
  94. # type() # print(type(5)) --> <class 'int'>
  95.  
  96. # Use help() and dir()!
  97. # help(list)
  98. # help(str)
  99. # print(dir(list))
  100. # import math
  101. # for item in dir(math):
  102. #     if not item.startswith("_"):
  103. #         print(item)
  104.  
  105. scoobiesDCT = {
  106.     "Scooby": "a blue collar",
  107.     "Shaggy": "green",
  108.     "Velma": "orange",
  109.     "Daphne": "purple",
  110.     "Fred": "an ascot"
  111. }
  112.  
  113. # DICTIONARY looping
  114. # for key in scoobiesDCT:
  115. #     # key: value
  116. #     print("{}: {}".format(key, scoobiesDCT[key])) # dictName[someKey]
  117. # for key, value in scoobiesDCT.items():
  118. #     print("{}--> {}".format(key, value))
  119.  
  120. # STRINGS
  121. # 4 ways to build strings
  122. # + # concatenation "this string" + " this string " + str(4)
  123. # string modulo # "%f" % (3.14)
  124. # "{} is a variable".format(myVar)
  125. # f"{myVar} is a variable"
  126. # import math
  127. # print("Pi to 5 places is {:.5f}".format(math.pi)) # <-- float precision
  128.  
  129. # SLICES
  130. myString = "hey there"
  131. # # myString[start:stop:step]
  132. # print(myString[::-1]) # yeh
  133.  
  134. # STRING METHODS
  135. # myString.split() # myString.split("\n")
  136. # " ".join(someList)
  137. # myString.format()
  138. # myString.replace(oldSubStr, newSubStr)
  139. # myString.upper() # myString.lower() and other case methods
  140. # myString.isupper() # and other is? methods, that return a Boolean
  141. # myString.count()
  142.  
  143. # LISTS
  144. # myList.append()
  145. # myList.extend(anotherList)
  146. # myList.insert(index, item)
  147. # myList.count()
  148. # myList.pop() # by index
  149. # myList.remove() # by value
  150. # myList.sort() # myList(reverse=True)
  151. # myList.reverse()
  152.  
  153. # SETS
  154. # mySet.add()
  155. # mySet.remove() # by value
  156. # mySet.discard() # by value
  157. # mySet.pop() # set pop() takes out a random value
  158.  
  159. # Comp 3
  160. # opening FILES
  161. # good practice here will be Ch 12 Task 4, 7, 8
  162. f = open(filename, "r")
  163. contents = f.read()
  164. f.close()
  165. # or
  166. with open(filename, "r"):
  167.     contents = f.read()
  168.  
  169.  
  170. # Lab 20.2, 23.6, 12.8
  171.  
  172. # Lab 20.2 unit conversion by 3's: hours, minutes, and seconds
  173. # whew 3 units instead of 2!
  174. # remember // to get the larger unit, % to get the smaller (original unit) left over
  175. # focus on the largest unit and the smallest, then figure out the middle
  176. total = int(input()) # 4000, 0, etc
  177. hours = total // (60*60) # round down to get last full hour value, after (60*60) or 3600 seconds
  178. min= (total % 3600)//60 # do this last
  179. sec= total % 60 # smallest unit num left almost always simple mod operation
  180. print("Hours: {}".format(hours))
  181. print("Minutes: {}".format(min))
  182. print("Seconds: {}".format(sec))
  183.  
  184.  
  185. # Lab 23.6... we don't know when the negative number is coming. So how
  186. # many calls to input()? Use a loop:
  187. value = 0
  188. numsList = []
  189. while value >= 0: # this will keep us going until we see the -1
  190.     value = int(input()) # if we're still going, I'm still grabbing inputs
  191.     numsList.append(value)
  192. print(max(numsList))
  193.  
  194.  
  195. # Lab 12.8 with a word list file
  196. filename = input()
  197. startWord = input()
  198. stopWord = input()
  199.  
  200. with open(filename, "r") as f:
  201.     outputList = f.readlines()
  202.  
  203.     for item in outputList:
  204.         item = item.strip("\n")
  205.         if item >= startWord and item <= stopWord:
  206.             print(item)
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement