Advertisement
jspill

webinar-python-exam-review-2022-09-24

Sep 24th, 2022
1,309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.75 KB | None | 0 0
  1. # # Exam Review 2022 Sept 24
  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. #
  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. # # operators
  22. # # = # assignment
  23. # # == # asking "are these equal?"... writing a condition
  24. # # +
  25. # # -
  26. # # *
  27. # # /
  28. # # % # modulo, asking "how many whole things didn't fit?" "how many left over?"
  29. # # // # floor division, the last even division
  30. # # <
  31. # # >
  32. # # <=
  33. # # >=
  34. # # += # increment
  35. # # -= # decrement
  36. # # !=
  37. # # ** # raising to a power, similar to math.pow()
  38. # # # keywords
  39. # # in # if _someValue_ in _someContainer_
  40. # # not # if not _someValue_ in _someContainer_
  41. # # and
  42. # # or # any one True means whole condition is True... limit OR to 2 conditions
  43. #
  44. # # Data Types
  45. # # str # ""
  46. # # int
  47. # # float
  48. # # list # []
  49. # # dict # {key: value}
  50. # # set # {} all unique values, no order --> no index, can't be sorted, can't be sliced
  51. # # tuple # () immutable... Python sees any x,y,z as (x,y,z)... return x, y -> return (x,y)
  52. #
  53. # # Comp 2
  54. # # the HOW stuff... control flow structures
  55. # # IF statements... if, if/else, if/elif, if/elif/else...
  56. # # LOOPS
  57. # # WHILE - an IF that repeats
  58. # # FOR - looping over a container, or a known number of times... (see range())
  59. # # for _item_ in _container_:
  60. # # for item in myList:
  61. # # for n in range(0, 5): # [0, 1, 2, 3, 4]
  62. # # for i in range(len(myList)): # myList[i]
  63. # # for key in myDictionary: # myDictionary[key]... myDictionary[key] = value
  64. #
  65. # # FUNCTIONS
  66. # # defining/writing vs calling
  67. # # parameters are special "variables"... they don't work like "regular" variables
  68. # # parameters vs arguments
  69. # # a function has ONE particular job
  70. # # return vs print()... write a file?... whatever the question said its job is
  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. # #     myOther = int(input())
  79. # #     num = someFunction(myInput, myOther)
  80. # #     print(num)
  81. #
  82. # # See "tasks" in the last section of Ch 10, 11, 13, 14 for function writing practice
  83. # # CodingBat also has good function-based Python questions:
  84. # # https://codingbat.com/python
  85. #
  86. # # BUILT-IN FUNCTIONS
  87. # # print() # end, sep
  88. # # input()
  89. # # len()
  90. # # max()
  91. # # min()
  92. # # sum()
  93. # # type() # <class 'int'>
  94. # # range()
  95. # # list()
  96. # # dict()
  97. # # tuple()
  98. # # int()
  99. # # float()
  100. # # set()
  101. # # sorted() # compare to list.sort()
  102. # # reversed() # compare to list.reverse()
  103. # # round() # but its cousins math.ceil() and math.floor() are in the math
  104. # # open() # IO/file: .read(), .readlines(), .write()
  105. #
  106. # # help(str)
  107. # # print(dir(str))
  108. # # help(str.isalpha)
  109. #
  110. # # print(dir(dict))
  111. #
  112. # # STRINGS
  113. # # be able to slice like it's 2nd nature: myString[start:stop:step]
  114. # myString = "abc"
  115. # # revString = myString[::-1]
  116. # # print(revString)
  117. #
  118. # # KNOW YOUR WHITESPACE
  119. # # # " " # ... and a lot of other Unicode spaces
  120. # # "\n"
  121. # # "\r"
  122. # # "\t"
  123. # # "\f"
  124. #
  125. # # STRING METHODS
  126. # "stuff I want to put together {}".format(var) # or the similar f strings
  127. # # myString.strip() # input().strip()
  128. # # myString.split() # returns a list of smaller strings
  129. # # ",".join(listOfStrings)
  130. # # myString.replace(oldSubStr, newSubStr) # remove... myString.replace(subStr, "")
  131. # # myString.find(subStr) # return index, or -1
  132. # # myString.count(subStr) # returns number of occurences
  133. # # # case: lower(), upper(), capitalize(), title()
  134. # # # is/Boolean: isupper(), islower(), isalpha(), isdigit(), isalnum(), isspace()
  135. # #
  136. # # # LISTS
  137. # # # again be to slice
  138. # #
  139. # # # LIST METHODS
  140. # # # +
  141. # # myList.append(item)
  142. # # myList.insert(i, item)
  143. # # myList.extend(anotherList)
  144. # # # -
  145. # # myList.pop() # pop by index
  146. # # myList.remove(item) # remove by value
  147. # # # other modifications
  148. # # myList.count(item) # returns that number
  149. # # myList.sort()
  150. # # myList.reverse()
  151. # # # also rans
  152. # # myList.copy()
  153. # # myList.index(item)
  154. # # myList.clear()
  155. # #
  156. # # # DICT
  157. # # # use the key like an index
  158. # # # myDict[key] # retrieve the value for that key, so like get()
  159. # # # myDict[key] = value # assign (new) value for that key, so like update({k:v})
  160. # # myDict.keys()
  161. # # myDict.values()
  162. # # myDict.items() # for k, v in myDict.items()... x, y = [2, 8]
  163. # #
  164. # # # SETS
  165. # # mySet.add(item)
  166. # # mySet.remove(item)
  167. # # mySet.pop() # removes something random... crazy
  168. #
  169. # # MODULES
  170. # # math and csv
  171. # # remember there are different import styles that change how you reference them
  172. #
  173. # # MATH MODULE
  174. # # import math
  175. # # math.factorial(x)
  176. # # math.ceil(x.yz)
  177. # # math.floor(x.zy)
  178. # # math.pow(x, y) # similar to **, not be confused with math.exp()
  179. # # math.sqrt(x)
  180. # # math.fabs() # similar to built-in abs()
  181. # # math.pi
  182. # # math.e
  183. #
  184. # # different import types
  185. # # full import...
  186. # # import math --> math.factorial()
  187. #
  188. # # partial import
  189. # # from math import factorial --> factorial()
  190. # # from math import sqrt, floor --> sqrt(), floor()
  191. # # from math import * --> factorial(), sqrt(), floor()
  192. #
  193. # # alias import
  194. # # import math as m --> m.floor(), etc
  195. #
  196. # # OPENING FILES
  197. # # good practice in Ch 14 Task 4, 7, 8
  198.  
  199. # READ MODE
  200. # with open("test.txt", "r") as f:
  201. #     contents = f.readlines()  # a list of line by line strings
  202. # # print(contents)
  203. # for line in contents:
  204. #     line = line.strip()
  205. #     print(line)
  206.  
  207. import csv
  208. with open("mock_data.csv", "r") as f1:
  209.     contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t")
  210. print(contents) # [['id', 'first_name', 'last_name', 'email', 'gender', 'ip_address'], ['1'...]]
  211.  
  212.  
  213. # WRITE MODE... writes over pre-existing files destructively
  214. with open("output_data5.csv", "w") as f2:
  215.     for line in contents:
  216.         if line[3][-4:] == ".edu": # if line[3].endswith(".edu"):
  217.             # the write() method takes a SINGLE string as its arg
  218.             f2.write(",".join(line) + "\n") # or...
  219.             # f2.write("{}{}".format(",".join(line), "\n")) # or...
  220.             # f2.write(f"{','.join(line)}{'\n'}")
  221.  
  222.  
  223. # APPEND MODE... puts us at the end of a pre-existing file so we can write non-destructively to it
  224. # let's read first...
  225. # with open("append_to_this.txt", "r") as f3:
  226. #     contents = f3.readlines()
  227. # print(contents) # ['Frodo\n', 'Sam\n', 'Merry\n']
  228.  
  229. with open("append_to_this.txt", "a") as f3:
  230.     f3.write("Pippin\n")
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement