Advertisement
jspill

webinar-python-exam-review-2022-01-22

Jan 22nd, 2022
1,953
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Exam Review 2022 Jan 22
  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. # Comp 1 Basic syntax and knowledge: operators, data types, etc
  10. # Comp 2: Control Flow Structures
  11. # Basic: if statements, loops, functions
  12. # Adv: try/except and raising errors, classes
  13. # Comp 3: modules, working with files
  14.  
  15. # Comp 1 Basic syntax and knowledge: operators, data types, etc
  16. # OPERATORS
  17. # +
  18. # -
  19. # /
  20. # *
  21. # //
  22. # %
  23. # = # assignment
  24. # == # "equality operator" - asking, condition
  25. # !=
  26. # +=
  27. # -=
  28. # <
  29. # >
  30. # <=
  31. # >=
  32. # ** # raise to a power, similar math.pow() or pow()
  33. # # KEYWORDS used like OPERATORS
  34. # not
  35. # in # membership check... if myVar in myList
  36. # and # all must be True
  37. # or # any one makes the combo True
  38.  
  39. # DATA TYPES
  40. # int
  41. # float
  42. # str
  43. # Boolean
  44. # dict
  45. # list
  46. # tuple
  47. # set # no order, all unique values
  48. # # MODULES
  49. # math
  50. # csv # you don't necessarily need it... read() and readlines() often work just as well
  51.  
  52. # Comp 2: Control Flow Structures
  53. # Basic
  54. # IF statements... if, if/else, if/elif/else
  55. # LOOPS: WHILE and FOR
  56. # WHILE is basically an IF that repeats until it's no longer True
  57. # FOR loops for going over every value in a container
  58. # for __ in ___:
  59. # for item in myList:
  60. # FUNCTIONS
  61. # defining/writing vs calling
  62. # parameters/arguments vs "regular" or outside variables
  63. # x = 5 # don't do this to parameters
  64. # return vs print()
  65. # functions are modular - they have ONE job
  66. # class methods like str.split() or list.append() are functions
  67.  
  68. # if name = main
  69. # define functions
  70. # if __name__ = "__main__":
  71. #     myVar = input()
  72. #     myOutput = myFunction(myVar)
  73. #     print(myOutput)
  74.  
  75. # help() and dir() in Labs and OA!!!
  76. # print('Enter your program here')
  77. # help(str)
  78.  
  79. # dir() -- Use dir() to zero in
  80. # print(dir(str))
  81.  
  82. # Call help more specifically
  83. # help(str.rstrip)
  84. # # myString.rstrip() # rstrip() by default!
  85. #
  86. # # BUILT IN FUNCTIONS
  87. # print()
  88. # input()
  89. # help()
  90. # dir()
  91. # min()
  92. # max()
  93. # len()
  94. # sum()
  95. # range()
  96. # int()
  97. # float()
  98. # str() # ""
  99. # dict() # {}
  100. # list() # [ ]
  101. # set()
  102. # tuple() # ()
  103. # round() # cousins: math.floor(), math.ceil()
  104. # reversed() # compare to list.reverse()
  105. # sorted() # compare to list.sort()
  106.  
  107. # STRINGS
  108. # Build up larger strings with str.format() or f strings
  109. # CONCATENATION... "this string" + "another string"
  110. # STRING MODULO... print("%s added in this string" % myString)
  111. # string.format()
  112. # "{} is a variable".format(myVar)
  113. # f string
  114. # f"{myVar} is a variable"
  115.  
  116. # SLICES for strings and list
  117. # myString[start:stop] # myString[start:stop:step]
  118. # myString[::-1]
  119.  
  120. # STRING METHODS
  121. # myString.format()
  122. # myString.split() # returns a list of smaller strings
  123. # " ".join(myList)
  124. # myString.replace(someSubStr, newSubStr)
  125. # myString.upper() # and other case methods
  126. # myString.isupper() # and other Boolean methods
  127. # myString.strip() # cousins: myString.lstrip() and myString.rstrip()
  128. # myString.count(subStr)
  129.  
  130. # LIST METHODS
  131. # myList.append(item)
  132. # myList.insert(index, item)
  133. # myList.extend(anotherList)
  134. # myList.pop() # optional arg: index
  135. # myList.remove() # remove by value
  136. # myList.count(item)
  137. # myList.sort() # myList.sort(reverse=True)
  138. # myList.reverse() # compare to sorted() and reversed()
  139.  
  140. # DICT
  141. # myDictionary[key] # retrieves the value
  142. # myDictionary[key] = value # assign a value for the key
  143. # if __ in myDictionary: # checking the KEYS, NOT the values
  144. # if __ in myDictionary.keys(): # same as above
  145.  
  146. # SETS
  147. # mySet.add()
  148. # mySet.pop() # random!
  149. # mySet.remove() # remove by value
  150.  
  151. # MATH
  152. # import math
  153. # math.pow(x, y) # do not confuse with math.exp()
  154. # math.e # logarithmic constant
  155. # math.pi
  156. # math.sqrt()
  157. # math.floor()
  158. # math.ceil()
  159. # math.factorial()
  160.  
  161. # What's in the data window for input()?
  162. # a string
  163. # another string
  164. # another string
  165. # TEST IT OUT
  166. # print(input())
  167.  
  168. # Ask you to grab inputs until you see -1
  169. # myVar = input()
  170. # while myVar != "-1":
  171. #     # do stuff
  172. #     myVar = input()
  173.  
  174. # Comp 3: modules, working with files
  175. # full, normal import
  176. # import wholeModule # import math, import csv... math.floor()
  177. # PARTIAL IMPORT
  178. #from module import thisMethod... from math import floor... floor()
  179. # # ALIASED import
  180. # import wholeModule as mm
  181. # mm.thisMethod()
  182.  
  183. # OPENING FILES
  184. # good practice: Ch 12 Task 4, 7, 8
  185. # f = open(filename, "r") # "w" for write, "a" for append
  186. # contents = f.read() # get the whole file one big string
  187. # or
  188. # contents = f.readlines() # list of strings, line by line
  189. # f.close()
  190.  
  191. # with open(filename, "r") as f:
  192. #     contents = f.readlines()
  193. #
  194. # # write back out
  195. # with open(filename, "w") as f:
  196. #     f.write("my string")
  197.  
  198. # Fibonacci
  199. # 0 1 1 2 3 5 8 13 21 34
  200.  
  201. def fibonacci(n):
  202.     # return that index of the sequence
  203.     if n < 0:
  204.         return -1
  205.     elif n <= 1:
  206.         return n
  207.     else:
  208.         fibList = [0, 1]
  209.         while len(fibList) < n + 1:
  210.             fibList.append(fibList[-1] + fibList[-2])
  211.         return fibList[n]
  212.  
  213.  
  214. print(fibonacci(9))  # 34
  215. print(fibonacci(8))  # 21
  216. print(fibonacci(3))  # 2
  217.  
  218.  
  219. # Another cause of bad string output:
  220. print(myVar, end=" ") # don't leave the cursor hanging
  221. print() # give a blank print() so you get a clean line
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement