Advertisement
jspill

webinar-python-exam-review-2022-05-28

May 28th, 2022
1,611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.64 KB | None | 0 0
  1. # Exam Review 2022 May 28
  2.  
  3. # LABS
  4. # Ch 3-13... all Labs!
  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. # fix your string input and output
  10. # 1
  11. # myVar = input().strip() # myVar = input().rstrip()
  12. # # 2
  13. # print("some stuff", end=" ") # if you ever override end
  14. # print() # print(end="\n")
  15.  
  16. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  17. # Comp 2: Control Flow
  18. # Comp 3: Modules and Files
  19.  
  20. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  21. # Data Types
  22. # int
  23. # float
  24. # str
  25. # lists
  26. # dict
  27. # tuples # () immutable, any series x, y, z -> interpreted as a tuple, return x, y, return (x,y)
  28. # sets # all unique/no dupes, no order --> no index, no slicing
  29.  
  30. # operators
  31. # = # assigning a value
  32. # == # asking, "equality operator"... use in conditions
  33. # +
  34. # -
  35. # *
  36. # /
  37. # % # modulo... whole number remainder, "How many whole things are left over?"
  38. # // # floor division... int(x/y) or really more like math.floor(x/y)
  39. # <
  40. # >
  41. # <=
  42. # <=
  43. # !=
  44. # += # increment... x += 1 same as x = x + 1
  45. # -= # decrement
  46. # # keywords used like operators
  47. # in # if __ in _someContainer_
  48. # not # if not __ in __
  49. # and
  50. # or # any one True means whole condition True... limit OR to 2 conditions
  51.  
  52. # Comp 2 Control Flow
  53. # Basic
  54. # IF statements... if, if/else, if/elif, if/elif/else, etc
  55. # LOOPS
  56. # WHILE - an IF that repeats
  57. # FOR - looping over some container, or looping a known number of times (range())
  58. # for __ in __:
  59. # for item in myList:
  60. # for n in range(0, 5):
  61. # for i in range(len(myList)):
  62. # for key in myDictionary:
  63.  
  64. # FUNCTIONS
  65. # defining/writing vs calling
  66. # parameters vs outside or "regular" variables
  67. # parameters vs arguments
  68. # a good function has ONE JOB, modular
  69. # return vs print()... vs write a file  # do whichever the question says
  70. # methods are functions that belong to a type/class
  71.  
  72. def someFunction(x, y):
  73.     return x + y
  74.  
  75. if "__name__" == "__main__": # am I running from this particular file?
  76.     myInput = input().strip()
  77.     # call your function
  78.     num = someFunction(myInput, someOtherNum)
  79.     print(num)
  80.  
  81. # BUILT-IN FUNCTIONS
  82. # print()
  83. # input() # input().strip()
  84. # # # constructing / recasting functions
  85. # int()
  86. # float()
  87. # str()
  88. # list()
  89. # dict()
  90. # set()
  91. # tuple()
  92. # range()
  93. # # # functions that work with lists or other containers
  94. # len()
  95. # sum()
  96. # min()
  97. # max()
  98. # sorted() # compare to list.sort()
  99. # reversed() # compare to list.reverse()
  100. # enumerate() # gives you an index and item/value variable to loop over container
  101. #
  102. # round() # cousins math.ceil(), math.floor()
  103. # open() # IO/file methods: .read(), .readlines(), .write()
  104. # type() # <class int>
  105. # isinstance() # returns True or False
  106. #
  107. # help()
  108. # dir()
  109. # # help(str)
  110. # # print(dir(str))
  111. # for item in dir(str):
  112. #     if not item.startswith("_"):
  113. #         print("{}()".format(item))
  114. #
  115. # help(str.find)
  116.  
  117. # STRINGS
  118. # be able to slice like it's 2nd nature... myString[start:stop:step]
  119. # myString = "abc"
  120. # myRevString = myString[::-1]
  121. # myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]
  122. # print(myList[::-1])
  123. #
  124. # # know your WHITESPACE
  125. # # " " # ... and a lot of other Unicode spaces
  126. # # "\n"
  127. # # "\r"
  128. # # "\b"
  129. # # "\t"
  130. # # "\f"
  131. #
  132. # # STRING METHODS
  133. # myString.format()
  134. # myString.strip() # myString.rstrip()... input().strip()
  135. # myString.split() # returns a list of smaller string
  136. # " ".join(someListOfStrings)
  137. # myString.replace(oldSubStr, newSubStr) # remove... myString.replace(oldSubStr, "")
  138. # myString.find(subStr) # compare myString.index()
  139. # myString.count(subStr) # return int of num occurrences
  140. # # case methods: myString.upper(), lower(), title(), capitalize()
  141. # # is/Boolean methods: isupper(), islower(), isdigit(), isnumeric(), isalphanum()
  142. #
  143. # # LISTS
  144. # # again be able to slice
  145. #
  146. # # LIST METHODS
  147. # # +
  148. # myList.append(item)
  149. # myList.insert(i, item)
  150. # myList.extend(anotherList)
  151. # # -
  152. # myList.pop(i) # pop by index, default is last
  153. # myList.remove(item) # remove by value
  154. # # others
  155. # myList.count(item)
  156. # myList.sort()
  157. # myList.reverse()
  158. # # not as important
  159. # myList.index(item) # be careful, could be in there more than once
  160. # myList.copy()
  161. # myList.clear()
  162. #
  163. # # DICT
  164. # # # use the key like an index
  165. # # # you can use the dict KEY to get its VALUE
  166. # myDictionary[key] # retrieve the value for that key... myDict.get()
  167. # myDictionary[key] = value # so kinda like myDict.update()
  168. # myDictionary.items() # for k, v in myDict.items()
  169. # myDictionary.keys() # returns a container of the keys
  170. # myDictionary.values() # returns a container of values
  171. #
  172. #
  173. # # SETS
  174. # mySet.add()
  175. # mySet.remove(item) # by value
  176. # mySet.pop() # removes a random item
  177. #
  178. # # MODULES
  179. # # MATH MODULE
  180. # import math
  181. # math.factorial(x)
  182. # math.ceil(x.yz)
  183. # math.floor(x.yz)
  184. # math.pow(x, y) # similar to **
  185. # math.sqrt(x)
  186. # math.fabs() # abs()
  187. # math.e
  188. # math.pi
  189. #
  190. # # full vs partial import
  191. # # import math --> math.factorial()
  192. # # from math import factorial --> factorial()
  193. # # from math import * --> factorial(), sqrt()
  194. # # # aliased imports
  195. # # import math as m --> m.factorial()
  196. #
  197. # # OPENING FILES
  198. # # # good practice: Ch 12 Task 4, 7, 8
  199. # with open("filename.txt", "r") as f:
  200. #     # # f.read() -> whole file as one big string
  201. #     # # f.readlines() -> a list of line by line strings
  202. #     myContent = f.readlines()
  203. #
  204. import csv
  205. # with open("filename.csv", "r") as f:
  206. #     # csv.reader(f)  # a list-like object of line by line lists of strings
  207. #     myContent = list(csv.reader(f))
  208. #
  209. # # writing to a file
  210. # with open("myNewFile.txt", "w") as f:
  211. #     # if you're figuring something out (and maybe looping to create strings)
  212. #     # ... you'll probably need to stay in this with/open block as you write the file
  213. #     f.write("I am writing into this file.\n")
  214.  
  215. with open("mock_data.csv", "r") as f:
  216.     contents = list(csv.reader(f))
  217.  
  218. print(contents)
  219.  
  220. with open("mock_data2.csv", "w") as f2:
  221.     for line in contents:
  222.         if line[3].endswith(".com"): # if line[3][-4] == ".com":
  223.             # f2.write(",".join(line) + "\n")
  224.             # f2.write(f"{",".join(line)}\n")
  225.             f2.write("{}\n".format(",".join(line)))
  226.  
  227. # try:
  228. #     # as if it all works
  229. # except:
  230. #     # as if an error got raised
  231.  
  232. # Question on 28.1
  233.     # Type your code here.
  234.     # try:  # as if it all works
  235.     #     # the idea here is that an error might occur when grabbing these inputs...
  236.     #     count = 0
  237.     #     v1 = int(input().strip())
  238.     #     count += 1
  239.     #     v2 = int(input().strip())
  240.     #     count += 1
  241.     #     v3 = int(input().strip())
  242.     #     count += 1
  243.     #
  244.     #     theMax = max([v1, v2, v3])
  245.     #     print(theMax)
  246.     #
  247.     # except EOFError:  # if it fails
  248.     #     # ... so we'll use the count var to see how many inputs we did get
  249.     #     print("{} input(s) read:".format(count))
  250.     #     if count == 0:
  251.     #         print("No max")
  252.     #     elif count == 1 or v1 >= v2:
  253.     #         print("Max is {}".format(v1))
  254.     #     else:
  255.     #         print("Max is {}".format(v2))
  256.  
  257. # Question on Lab 12.8 Words in a Range
  258. # Remember you can use comparison operators here
  259. # print("a" < "b") # True
  260. # print("b" < "a") # False
  261.  
  262.  
  263. # grab the 3 input strings, maybe call them...
  264. filename = input().strip()
  265. startWord = input().strip()
  266. stopWord = input().strip()
  267.  
  268. # with open the file:
  269. #   grab contents with readlines
  270.  
  271. # loop over those contents:
  272. #   reassign the loop var to the current line stripped (there will be a "\n" on the end)
  273. #   use an IF to check if startWord <= loop var <= stopWord: # or do as 2 conditions
  274. #       if yes, print it
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement