Advertisement
jspill

webinar-exam-review-v4-2023-01-28

Jan 28th, 2023
783
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.38 KB | None | 0 0
  1. # Exam Review 2023 Jan 28
  2.  
  3. # LABS
  4. # Ch 2-14... all Labs!
  5. # Ch 21-34 just ADDITIONAL LABS, but important practice!
  6. # Use Submit Mode!!!
  7.  
  8. # Watch your string input and output
  9. # 1
  10. # myVar = input().strip()
  11. # 2
  12. # print() # print(end="\n")
  13. # # if you ever override end...
  14. # print("some stuff", end=" ") # set it back to a clean new line
  15. # print()  # print(end="\n") # wrap up with it!
  16.  
  17. # print("Clean new line!")
  18.  
  19. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  20. # Comp 2: Control Flow
  21. # Comp 3: Modules and Files
  22.  
  23. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  24. # Common Data Types
  25. # int
  26. # float
  27. # str # " "
  28. # list # [ ]
  29. # dict # {key:value, key:value}
  30. # tuple # ( ) immutable, Python sees any x,y,z as (x,y,z)... return x,y -> return (x,y)
  31. # set # { } all unique values (no duplicates), unordered... no index, no sorting, no slices
  32. # range() # -> range object
  33.  
  34. # operators
  35. # = # assignment
  36. # == # equality... asking, comparing... part of condition
  37. # +
  38. # -
  39. # *
  40. # /
  41. # % # modulo, int remainder..."how many whole things left over?"
  42. # // # floor division... x//y -> math.floor(x/y)
  43. # <
  44. # >
  45. # <=
  46. # >=
  47. # += # increment x += 1 --> x = x + 1
  48. # -=
  49. # ** # raise to a power... pow() or math.pow()
  50. # !=
  51. # # keywords used like operator
  52. # in # if _someValue_ in _someContainer_
  53. # not # if not _someValue_ in _someContainer_
  54. # and
  55. # or #  any one True means whole condition is True... limit OR to 2 conditions
  56.  
  57. # Comp 2
  58. # # the HOW stuff... control flow structures
  59. # IF statements... if, if/else, if/elif, if/elif/else
  60. # LOOPS...
  61. # WHILE - an IF that repeats
  62. # FOR - looping over a container, or a known number of times # hence range()
  63. # for _item_ in _someContainer_:
  64. # for item in myList:
  65. # for key in myDict: # key is key, value is myDict[key]
  66. # for n in range(0, 5): # sorta like [0, 1, 2, 3, 4]
  67. # for i in range(0, len(myList)):
  68. #     # treat last one differently
  69. #     if i == len(myList)-1:
  70. #         # the last one
  71. #     else:
  72. #         # all the others
  73.  
  74. # FUNCTIONS
  75. # defining/writing vs calling
  76. # parameters are special "variables"... they don't work like regular variables
  77. # parameters vs arguments
  78. # a function has ONE particular job
  79. # return vs print()...vs write a file... whatever the question says
  80. # method are functions that belong to a particular class/type
  81.  
  82. # def someFunction(x, y):
  83. #     return x + y
  84. #
  85. #
  86. # if __name__ == "__main__": # are we running from this very script I'm writing?
  87. #     myInput = int(input())
  88. #     myOther = int(input())
  89. #     num = someFunction(myInput, myOther)
  90. #     print(num)
  91.  
  92. # See "tasks" in the last section of Ch 10, 11, 13, 14 for function writing practice
  93. # # CodingBat also has good function-based Python questions:
  94. # # https://codingbat.com/python
  95.  
  96. # BUILT-IN FUNCTIONS
  97. # print()
  98. # input()
  99. # len()
  100. # list()
  101. # dict()
  102. # int()
  103. # float()
  104. # set()
  105. # tuple()
  106. # range()
  107. # sum()
  108. # min()
  109. # max()
  110. # type() # print(type("hi").__name__)
  111. # round() # buts its cousins math.ceil() and math.floor() are in the math module
  112. # sorted() # returns sorted list.. compare list.sort() does not return anything
  113. # reversed() # returns reversed list... same
  114. # open() # IO/file --> .read(), .readlines(), .write()
  115. # help()# help(str) or help(str.isspace)
  116. # dir() print(dir(str))
  117.  
  118. # STRINGS
  119. # be able to slice like it's 2nd nature: myString[start:stop:step]
  120. # myStr = "abcdef"
  121. # revStr = myStr[::-1]
  122. # print(revStr)
  123.  
  124. # KNOW YOUR WHITESPACE
  125. # " " #... and many Unicode spaces
  126. # "\n"
  127. # "\t"
  128. # "\r"
  129.  
  130. # STRING METHODS
  131. # "stuff I want to put together {}".format(var)  # or similar f strings
  132. # myStr.split() # returns a list of smaller strings
  133. # " ".join(listOfStrings)
  134. # myStr.strip() # lstrip(), rstrip()
  135. # myStr.replace(subStr, newSubStr) # "remove"... myStr = myStr.replace(subStr, "")
  136. # myStr.find(subStr) # return int index or -1 if not there, compare to myStr.index(subStr)
  137. # myStr.count(subStr) # return int count of occurrences
  138. # case: myStr.lower(), myStr.upper(), myStr.title(), myStr.capitalize()
  139. # is/Boolean: myStr.isupper(), islower(), isspace(), isalpha(), isdigit(), isnumeric()
  140.  
  141. # LISTS
  142. # again know indices and be able to slice
  143.  
  144. myList = [1, 2, 3, 1, 2]
  145.  
  146. # LIST METHODS
  147. # +
  148. # myList.append(item)
  149. # myList.insert(i, item)
  150. # myList.extend(anotherList)
  151. # # -
  152. # myList.pop() # myList.pop(i)
  153. # myList.remove(item) # pop by INDEX, remove by VALUE
  154. # # other...
  155. # myList.count(item)
  156. # myList.sort() # modify in place, no return value
  157. # myList.reverse()# modify in place, no return value
  158. # and not as important
  159. # myList.clear()
  160. # myList.copy()
  161. # myList.index(item)
  162.  
  163. # DICT
  164. # use the key like an index
  165. # myDict[key] # retrieve the value for that key, so like get()
  166. # myDict[key] = value # # assign (new) value for that key, so like update({k:v})
  167. # myDict.keys()
  168. # myDict.values()
  169. # myDict.items() # for k, v in myDict.items()
  170.  
  171. # MODULES
  172. # math and csv
  173.  
  174. # MATH MODULE
  175. # import math # <-- that's a FULL IMPORT
  176. # math.factorial(x)
  177. # math.ceil(x.yz)
  178. # math.floor(x.yz)
  179. # math.pow(x, y) # similar to **, not be confused with math.exp()
  180. # math.sqrt(x)
  181. # math.fabs() # similar to built-in abs()
  182. # math.e
  183. # math.pi
  184. #
  185. # # PARTIAL IMPORT
  186. # from math import factorial
  187. # from math import factorial, ceil
  188. # # don't say math.factorial()... we didn't import math
  189. # factorial()
  190. # from math import * # still a partial import... factorial(), floor()
  191. #
  192. # # ALIAS IMPORT
  193. # import math as m
  194. # m.floor()
  195.  
  196. # FILES!!!
  197. # READ MODE
  198. # with open("test.txt", "r") as f:
  199. #     contents = f.readlines() # list of strings, line by line
  200. # # print(contents)
  201. # # newContents = [] ... if you want a new list of stripped lines
  202. # for line in contents:
  203. #     line = line.strip() # if you want to get rid of line return as you work
  204. #      # newContents.append(line)
  205. #     print(line)
  206.  
  207. # CSV Module
  208. import csv
  209. with open("mock_data.csv", "r") as f1:
  210.     contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t")
  211. contents = contents[0:20]
  212. # print(contents)
  213.  
  214. # WRITE MODE
  215. with open("output_data11.csv", "w") as f2:
  216.     for line in contents:
  217.         if ".edu" in line[3]:
  218.             # write() method takes one single str argument
  219.             f2.write(",".join(line)+"\n")
  220.  
  221. # APPEND MODE
  222. # with open("append_to_this.txt", "r") as f3:
  223. #     contents = f3.readlines()
  224. # print(contents)
  225. with open("append_to_this.txt", "a") as f4:
  226.     f4.write("Pippin\n")
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement