Advertisement
jspill

webinar-python-exam-review-2021-12-18

Dec 18th, 2021
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.21 KB | None | 0 0
  1. # Exam Review 2021 Nov 12/18
  2.  
  3. # LABS
  4. # Ch 3-13
  5. # Ch 14-18 not as important, other than some good labs in Ch 15
  6. # Ch 19-30 just LABS, but important practice!
  7. # Use Submit Mode!!!
  8.  
  9.  
  10. # Comp 1 Basic syntax and knowledge: operators, data types, etc
  11. # DATA TYPES
  12. # str
  13. # int
  14. # floats
  15. # Boolean
  16. # list
  17. # dict # keys, value pairs
  18. # tuple
  19. # set # no order, and are all unique values
  20. # MODULES
  21. # math
  22. # csv # often basic filestream methods read(), readlines(), write()
  23.  
  24. # OPERATORS
  25. # +
  26. # -
  27. # *
  28. # /
  29. # % # modulo - whole number remainder, "how many whole things didn't fit?"
  30. # // # floor division - truncated integer division
  31. # = # assignment operator, setting something equal
  32. # == # equality operator, asking if it's equal, part of a condition: if, while
  33. # !=
  34. # += # incrementing, x += 1 is the same x = x + 1
  35. # -= # decrementing
  36. # <
  37. # >
  38. # <=
  39. # >=
  40. # # KEYWORDS used like OPERATORS
  41. # not # sometimes we're looking not, but there's no equals, not if __ in __
  42. # in # if __ in __, for __ in ___
  43. # and # if something and something
  44. # or # if something or something
  45. # def
  46. # del
  47.  
  48.  
  49. # Comp 2: Control Flow Structures
  50. # Basic
  51. # IF statements... if, if/else, if/elif/else
  52. # LOOPS
  53. # WHILE - more general, like an if that repeats
  54. # FOR - doing a known number of times or doing some once for every ITEM in a CONTAINER
  55. # for __ in __:
  56. # for item in myList:
  57. # for char in myString:
  58. # for key in myDictionary: # myDictionary[key]
  59. # for n in range(0, 5): # do this 5 times
  60. # for i in range(len(myList)): # if you need the index as much as the item/value at that index
  61. # myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # X-Files characters
  62. # if "Agent Scully" in myList:
  63. #     print("Found her.")
  64. # if "Godzilla" in myList:
  65. #     print("Found him.")
  66. # else:
  67. #     print("No Godzilla.")
  68. # print("Godzilla" in myList)
  69.  
  70. # FUNCTIONS
  71. # defining vs calling
  72. # parameter vs "regular"/"outside" variable - the function call gives the parameter a value
  73. # # def myFunction(x):
  74. # #     # never say x = something
  75. # #     return x//16
  76. # return vs print() # do whichever one the question
  77. # understand that methods are just functions
  78.  
  79. # Advanced Control Structures
  80. # Ch 10: try/except... read 10.3 then Lab 10.7
  81. # Ch 13: classes... Lab 13.13
  82.  
  83. # IF NAME == MAIN
  84. # import myModule
  85. # a bunch of function definitions
  86. # if __name__ = "__main__": # is this file the file I'm running from? Or was it imported?
  87.     # call some functions
  88.     # do some other stuff... but only myModule is the actual file I'm running
  89.  
  90. # define functions
  91. # if __name__ = "__main__":
  92.     # myVar = input()
  93.     # myOutput = myFunction(myVar)
  94.     # print(myOutput)
  95.  
  96. # BUILT IN FUNCTIONS
  97. # print()
  98. # input() # str... int(input()), float(input()), input().split()
  99. # help()
  100. # dir()
  101. # round()
  102. # len()
  103. # max()
  104. # min()
  105. # sum()
  106. # range()
  107. # int()
  108. # float()
  109. # str()
  110. # list() # []
  111. # tuple() # ()
  112. # set()
  113. # dict() # {}
  114. # type() # print(type([1, 2, 3]))
  115.  
  116. # STRINGS
  117. # Build up larger strings with str.format() or f strings
  118. # CONCATENATION... "this string" + "another string"
  119. # STRING MODULO... print("%s added in this string" % myString)
  120. # string.format()
  121. # "{} is a variable".format(myVar)
  122. # f string
  123. # f"{myVar} is a variable"
  124.  
  125. # SLICES for strings and list
  126. # myString[start:stop] # myString[start:stop:step]
  127. # myString = "Just sit right back and you'll hear a tale."
  128. # print(myString[0:8])
  129. # print(myString[::-1])
  130. # print(myString)
  131. #
  132. # # STRING METHODS
  133. # myString.format()
  134. # myString.split() # myString.split(","), myString.split("\n")
  135. # " ".join(myList)
  136. # myString.replace(someSubStr, newSubStr) # also to remove myString.replace(someSubStr, "")
  137. # myString.upper() # and other case related methods
  138. # myString.isupper() # isSomething? return Boolean
  139. # myString.count() # myString.count(someSubStr)
  140. #
  141. # # LIST METHODS
  142. # myList.append(item)
  143. # myList.extend(anotherList) # merging a second list item by item
  144. # myList.insert(i, item)
  145. # myList.remove(item) # by value
  146. # myList.pop(i) # by index
  147. # myList.sort() # myList.sort(reverse=True)
  148. # myList.reverse() # compare to sorted() and reversed()
  149. #
  150. # # DICTIONARY
  151. # myDictionary[key] # retrieves the value
  152. # myDictionary[key] = value # assign a value for the key
  153. # # if __ in myDictionary: # it's checking the keys
  154. # # if __ in myDictionary.keys(): # don't really need keys() here
  155. #
  156. # # MATH
  157. # math.pow(x, y) # same as **, do not confuse math.pow() with math.exp()
  158. # math.e
  159. # math.pi
  160. # math.sqrt()
  161. # math.ceil()
  162. # math.floor() # and the built in round()
  163. # math.factorial()
  164.  
  165. # What's in the data window for input()?
  166. # a string
  167. # another string
  168. # another string
  169. # TEST IT OUT
  170. # print(input())
  171.  
  172. # Ask you to grab inputs until you see -1
  173. # myVar = input()
  174. # while myVar != -1:
  175. #     # do our stuff
  176. #     myVar = input()
  177.  
  178.  
  179. # Comp 3: modules, working with files
  180. # full, normal import
  181. # import wholeModule
  182. # wholeModule.thisMethod()
  183. # math.floor()
  184. # # partial import
  185. # from wholeModule import thisMethod
  186. # thisMethod() # not wholeModule.thisMethod()
  187. # floor() # not math.floor()
  188. # # aliased import
  189. # import wholeModule as mm
  190. # mm.thisMethod()
  191.  
  192. # OPENING FILES
  193. # good practice: Ch 12 Task 4, 7, 8
  194. # f = open(filename, "r") # "w" for write, "a" for append
  195. # contents = f.read() # get the whole file one big string
  196. # or
  197. # contents = f.readlines() # list of strings, line by line
  198. # f.close()
  199.  
  200. # with open(filename, "r") as f:
  201. #     contents = f.readlines()
  202. #
  203. # # write back out
  204. # with open(filename, "w") as f:
  205. #     f.write("my string")
  206.  
  207. # TAKE THE PRACTICE TEST... CH 31
  208. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # X-Files characters
  209. for i in range(len(myList)):
  210.     print("{}: {}".format(i, myList[i]))
  211. # for __, __ in enumerate(myList):
  212. for i, item in enumerate(myList):
  213.     print("{} --> {}".format(i, item))
  214.  
  215. # Fibonacci
  216. # 0 1 1 2 3 5 8 13 21 34
  217.  
  218. def fibonacci(n):
  219.     if n < 0:
  220.         return -1
  221.     elif n <= 1:
  222.         return n
  223.     else:
  224.         fibList = [0, 1] # [0, 1, n]
  225.         while len(fibList) < n + 1:
  226.             # fibList[n-1] + fibList[n-2]
  227.             fibList.append(fibList[-1] + fibList[-2])
  228.         return fibList[n]
  229.  
  230. print(fibonacci(9)) # 34
  231. print(fibonacci(8)) # 21
  232. print(fibonacci(3)) # 2
  233.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement