jspill

webinar-python-exam-review-2022-08-27

Aug 27th, 2022
1,582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.18 KB | None | 0 0
  1. # Exam Review 2022 Aug 27
  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 Files
  18.  
  19. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  20. # Data Types
  21. # int
  22. # float
  23. # bool
  24. # str
  25. # list # []
  26. # dict # {key: value, key:value}
  27. # set # {} # all unique values, no order -> no index, can't slice
  28. # tuple # () immutable, Python sees any x,y,z as a tuple (x,y,z) --> return x,y
  29.  
  30. # operators
  31. # = # assigning a value
  32. # == # comparison, asking if they're equal
  33. # +
  34. # -
  35. # *
  36. # /
  37. # % # modulo, whole number remainder, how many WHOLE THINGS didn't fit?
  38. # // # floor division, last even division
  39. # <
  40. # >
  41. # <=
  42. # >=
  43. # !=
  44. # += # increment
  45. # -= # decrement
  46. # ** # raise to a power... similar to math.pow()
  47. # # keywords used like operators
  48. # in # if _value_ in _someContainer_
  49. # not # flip around Boolean --> if not _value_ in _someContainer_
  50. # and
  51. # or # any one True means whole condition is True... limit OR to 2 conditions
  52.  
  53. # Comp 2
  54. # the HOW stuff... control flow structures
  55. # IF statements... if, if/else, if/elif, if/elif/else, etc
  56. # LOOPS
  57. # WHILE - an IF that repeats
  58. # FOR - looping over a container, or a known number of times (range)
  59. # for _value_ in _container_:
  60. # for item in myList:
  61. # for n in range(0, 4): # [0, 1, 2, 3]
  62. # for i in range(len(myList)): # myList[i]
  63. # for key in myDictionary: # x, y = [1, 2]... for k, v in myDictionary.items()
  64.  
  65. # FUNCTIONS
  66. # defining/writing vs calling
  67. # parameters vs "outside" or "regular" variables
  68. # parameters vs arguments
  69. # a function has ONE particular job, modular
  70. # return vs print() or "output" # do whichever the question
  71. # methods are functions that belong to a particular class or data type
  72.  
  73. # def someFunction(x, y):
  74. #     return x + y
  75. #
  76. # if __name__ == "__main__":
  77. #     myInput = int(input())
  78. #     num = someFunction(myInput, someOtherNum)
  79. #     print(num)
  80.  
  81. # See "tasks" in the last section of Ch 10, 11, 13, 14 for function writing practice
  82. # CodingBat also has good function-based Python questions:
  83. # https://codingbat.com/python
  84.  
  85. # BUILT-IN FUNCTIONS
  86. # print()
  87. # input()
  88. # len()
  89. # sum()
  90. # min()
  91. # max()
  92. # range()
  93. # list()
  94. # dict()
  95. # tuple()
  96. # set()
  97. # sorted() # compare to list.sort()
  98. # reversed() # compare to list.reverse()
  99. # round() # regular round() is a built-in, cousins math.ceil(), math.floor()
  100. # open() # IO/filestream: .read(), .readlines(), .write()
  101. #
  102. # help() # help(str), help(list)
  103. # dir()
  104. # # help(str)
  105. # # help(str.capitalize)
  106. #
  107. # # dir() # returns a LIST of attributes
  108. # # print(dir(dict))
  109. #
  110. # for item in dir(str):
  111. #     if not item.startswith("_"):
  112. #         print("{}()".format(item))
  113.  
  114. # STRINGS
  115. # be able to slice like it's 2nd nature: myString[start:stop:step]
  116. # myString = "abc"
  117. # myRevString = myString[::-1]
  118. # print(myRevString)
  119.  
  120. # KNOW YOUR WHITESPACE
  121. # " " # ... and a lot of other Unicode spaces
  122. # "\n"
  123. # "\r"
  124. # "\t"
  125. # "\f"
  126.  
  127. # STRING METHODS
  128. # myString.format() # or the similar f string
  129. # myString.strip()
  130. # myString.split()
  131. # ",".join(someListOfStrings)
  132. # myString.replace(subStr, newSubStr) # use it to remove myString.replace(subStr, "")
  133. # myString.find(subStr) # return index, or -1
  134. # myString.count(subStr) # return int of number of occurences
  135. # case: title(), upper(), lower(), capitalize()
  136. # is/Boolean methods: isupper(), islower(), isdigit(), isspace()
  137.  
  138.  
  139. ### sidequest... question on using .join() when you don't have a list of strings
  140. ### ... well, you'd need a new list with the values of ints or float recast as strings
  141. ### I would do that with a plain old for loop and a new list,
  142. ### but you could also do it with a comprehension, which is just a shorter version of the same
  143.  
  144. ### jdList = ["1", "2", "3"]
  145. ### print("|".join(jdList))
  146. ### jdList = [1, 2, 3]
  147. ### if you really like comprehension: expression for item in container ** opt if **
  148. ### print("|".join([str(x) for x in jdList]))
  149.  
  150.  
  151.  
  152. # LISTS
  153. # again, be able to slice
  154.  
  155. # LIST METHODS
  156. # +
  157. # myList.append(item)
  158. # myList.insert(i, item)
  159. # myList.extend(anotherList)
  160. # # -
  161. # myList.pop() # pop by index
  162. # myList.remove(item) # remove by value
  163. # # other
  164. # myList.count(item) # don't confuse with len()
  165. # myList.sort()
  166. # myList.reverse()
  167. # # not as important
  168. # myList.index() # be careful, could be in there more than once
  169. # myList.copy()
  170. # myList.clear()
  171.  
  172. # DICT
  173. # use the key like an index
  174. # myDict[key] # retrieve the value for that key, so like get()
  175. # myDict[key] = value # assign (new) value for that key, so like update()
  176. # myDict.items() # for k, v in myDict.items()
  177. # myDict.keys()
  178. # myDict.values()
  179.  
  180. # SETS
  181. # mySet.add()
  182. # mySet.remove(item) # by value
  183. # mySet.pop() # removes a random item
  184.  
  185. # MODULES
  186. # math and csv
  187.  
  188. # MATH MODULE
  189. # import math
  190. # math.factorial(x)
  191. # math.ceil(x.yz)
  192. # math.floor(x.yz)
  193. # math.pow(x, y) # similar to **, not to be confused math.exp()
  194. # math.sqrt(x)
  195. # math.fabs() # abs()
  196. # math.e
  197. # math.pi
  198.  
  199. # different import types
  200. # full import: math.factorial()
  201. # from math import factorial
  202. # --> factorial()
  203. # from math import factorial, floor
  204. # --> floor()
  205. # --> factorial()
  206. # from math import *
  207. # still factorial(), sqrt()
  208. # aliased imports
  209. # import math as m --> m.factorial()
  210.  
  211. # # OPENING FILES
  212. #  good practice in Ch 14 Task 4, 7, 8
  213.  
  214. with open("test.txt", "r") as f:
  215.     contents = f.readlines() #  a list of line by line strings
  216. print(contents)
  217.  
  218. for line in contents:
  219.     line = line.strip()
  220.     print(line) # print(line, end="\n")
  221.  
  222.  
  223. import csv # for csv.reader()
  224. with open("mock_data.csv", "r") as f:
  225.     content = list(csv.reader(f)) # csv.reader(f, delimiter="\t")
  226. print(content)
  227.  
  228. with open("output_data2.csv", "w") as f2:
  229.     for line in contents:
  230.         if line[3].endswith(".org") # if line[3][-4:]:
  231.             f2.write(",".join(line)+"\n") # write() takes a single string as its arg
  232.             # f2.write("{}\n".format(",".join(line)))
  233.  
Advertisement
Add Comment
Please, Sign In to add comment