Advertisement
jspill

webinar-python-exam-review-2022-08-20

Aug 20th, 2022
1,196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.16 KB | None | 0 0
  1. # Exam Review 2022 Aug 20
  2.  
  3. # LABS
  4. # Ch 2-14... all Labs!
  5. # Ch 21-33 just ADDITIONAL LABS, but important practice!
  6. # Use Submit Mode!!!
  7.  
  8. # Watch your string input and output
  9. # # 1
  10. # myVar = input().strip() # myVar = input().rstrip()
  11. # # 2
  12. # print("some stuff", end=" ") # if you ever override end
  13. # print() # print(end="\n")
  14.  
  15. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  16. # Comp 2: Control Flow
  17. # Comp 3: Modules and File Reading/Writing
  18.  
  19. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  20. # Data Types
  21. # str
  22. # int
  23. # float
  24. # bool
  25. # list # [1, 2, 3]
  26. # dict # {"key": "value"}
  27. # tuple # (1, 2, 3) immutable, any series x, y, z -> Python sees this as a tuple
  28. # set # {1, 2, 3} unique, has no order (means index positions, no slices)
  29.  
  30. # Operators
  31. # = # assigns a value
  32. # == # comparison, asking "are these equal?"
  33. # +
  34. # -
  35. # *
  36. # /
  37. # % # modulo, get the WHOLE NUMBER remainder, asking "how many didn't fit?"
  38. # // # last even division, rounded down division... math.floor(x/y), sorta like int(x/y)
  39. # ** # math.pow(x, y)
  40. # += # increment
  41. # -= # decrement
  42. # <
  43. # >
  44. # <=
  45. # >=
  46. # !=
  47. # # # keywords that we use like operators
  48. # in # if __ in _someContainer_... if myNum in myList
  49. # not # if not __ in _someContainer_
  50. # and
  51. # or # any one True makes the combined condition True... # limit OR to 2 conditions
  52.  
  53. # Comp 2
  54. # Basic control structures
  55. # CONDITIONAL statements: if, if/else, if/elif, if/elif/else...
  56. # LOOPS
  57. # WHILE # an IF that repeats
  58. # FOR # iterating over a container, or a known # of times
  59. # for __ in _someContainer_:
  60. # for item in myList:
  61. # for char in myString:
  62. # for key in myDictionary:
  63. # for n in range(0, 5):
  64. # for i in range(0, len(myList)): # myList[i]
  65.  
  66. # SLICE... an abbreviated copy of str, list, tuple
  67. # myList = [1, 2, 3, 4, 5]
  68. # myRevList = myList[::-1]
  69. # print(myRevList)
  70.  
  71. # FUNCTIONS
  72. # defining/writing vs calling
  73. # parameters vs outside or "regular" variables
  74. # parameters vs arguments
  75. # a good function has ONE job, modular
  76. # methods are functions that "belong" to that type/class
  77.  
  78. def someFunction(x, y):
  79.     return x + y
  80. # no other output outside of if name = main...
  81.  
  82. if __name__ == "__main__":
  83.     myInput = int(input().strip())
  84.     num = someFunction(myInput, someOtherNum)
  85.     print(num)
  86.  
  87. # See "tasks" in the last section of Ch 10, 11, 13, 14 for function writing practice
  88. # CodingBat also has good function-based Python questions:
  89. # https://codingbat.com/python
  90.  
  91. # BUILT-IN FUNCTIONS
  92. # print()
  93. # input()
  94. # len()
  95. # range()
  96. # min()
  97. # max()
  98. # sum()
  99. # # creating or recasting
  100. # str()
  101. # int()
  102. # float()
  103. # list()
  104. # tuple()
  105. # set()
  106. # dict()
  107. # sorted() # does return a sorted list (list.sort() doesn't return a value)
  108. # reversed() # same
  109. # round() # cousins math.ceil(), math.floor()
  110. # open() # IO/filestream: .read(), .readlines(), .write()
  111. #
  112. # help() # help(str)
  113. # # help(str)
  114. # # help(str.strip)
  115. #
  116. # # print(dir(str))
  117. # for item in dir(dict):
  118. #     if not item.startswith("_"):
  119. #         print("{}()".format(item))
  120.  
  121. # STRINGS
  122. # be able to slice like it's second nature... myString[start:stop:step]
  123. # myString = "abcd"
  124. # myRevString = myString[::-1]
  125. # print(myRevString)
  126.  
  127. # KNOW YOUR WHITESPACE
  128. # # " " # ... and a lot of other Unicode spaces
  129. # "\n"
  130. # "\r"
  131. # "\b"
  132. # "\t"
  133. # "\f"
  134.  
  135. # # STRING METHODS
  136. # myString.format()
  137. # myString.strip() # myString.rstrip()
  138. # myString.split() # return a list of smaller strings
  139. # ", ".join(listOfStrings)
  140. # myString.replace(subStr, newSubStr) # remove... myString.replace(subStr, "")
  141. # myString.find(subStr) # return index where it starts, or -1 if not there
  142. # myString.count(subStr) # return int of number of occurences
  143. # # is/Boolean methods: isupper(), islower(), isdigit(), isspace(), isalpha
  144. # case changing: upper(), lower(), title()
  145.  
  146. # LISTS
  147. # be able to slice!
  148.  
  149. # LIST METHODS
  150. # +
  151. # myList.append(item)
  152. # myList.insert(i, item)
  153. # myList.extend(anotherList)
  154. # # -
  155. # myList.pop(index)
  156. # myList.remove(value/item)
  157. # # other
  158. # myList.count(item) # returns number of occurences
  159. # myList.sort() # no return, modifies in place
  160. # myList.reverse() # same
  161. # # not as important
  162. # myList.clear()
  163. # myList.copy()
  164. # myList.index(item)
  165.  
  166.  
  167. # DICTIONARIES
  168. # # # use the key like an index
  169. # myDict[key] # retrieves the value for that key, myDict.get()
  170. # myDict[key] = value # compare to myDict.update()
  171. # myDict.keys()
  172. # myDict.values()
  173. # myDict.items() # for k, v in myDict.items()
  174. #
  175. # # IN keyword with a dictionary is looking at KEYS, not values
  176. #
  177. # # MODULES
  178. # # MATH MODULE
  179. # import math
  180. # math.factorial()
  181. # math.ceil()
  182. # math.floor()
  183. # math.pow(x, y) # **, not to be confused with math.exp()
  184. # math.sqrt()
  185. # math.fabs() # abs()
  186. # math.pi
  187. # math.e
  188. #
  189. # # different import types
  190. # # full import: math.factorial()
  191. # # partial import
  192. # # from math import factorial
  193. # # --> factorial()
  194. # # from math import *
  195. # # still --> factorial(), sqrt()
  196. # # # aliased imports
  197. # # import math as m --> m.factorial()
  198. #
  199. # # RANDOM
  200. # # import random
  201. # # random.choice(myList) # returns a random item from a list
  202. # # random.randint(x, y) # INCLUDES the stop y
  203. # # random.randrange(x, y) # exclude the stop number y
  204. #
  205. # # CSV
  206. # csv.reader() # reads a .csv or .tsv and returns list-like object
  207. #
  208. # # READING files
  209. # with open("filename.txt", "r") as f:
  210. #     # when reading, I grab contents and get out of the open block
  211. #     # contents = f.read() # whole file as one big string
  212. #     contents = f.readlines() # a list of line by line strings, WILL have \n
  213. # # get off with/open quickly when reading
  214.  
  215. import csv
  216. with open("mock_data.csv", "r") as f:
  217.     contents = list(csv.reader(f)) # a list of lists of strings, individual cell values
  218.     # contents = list(csv.reader(f, delimiter="\t")) # to get a TSV's data
  219. # print(contents)
  220.  
  221. with open("mock_data_new.csv", "w") as f2:
  222.     for line in contents:
  223.         if line[3].endswith(".org"): # if line[3][-4:] == ".org"
  224.             # f2.write(",".join(line) + "\n") # takes a single string argument
  225.             # or
  226.             # f2.write("{}\n".format(",".join(line)))
  227.             # or
  228.             f2.write(f"{','.join(line)}\n")
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement