Advertisement
jspill

webinar-exam-review-2022-11-19

Nov 19th, 2022
1,299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.21 KB | None | 0 0
  1. # Exam Review Nov 19 2022
  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("some stuff", end=" ") # if you ever override end...
  13. # print() # print(end="\n")
  14.  
  15. # print("Clean new line!")
  16.  
  17. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  18. # Comp 2: Control Flow
  19. # Comp 3: Modules and Files
  20.  
  21. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  22.  
  23. # # Common Data Types
  24. # str
  25. # int
  26. # floats
  27. # list # []
  28. # tuple # () immutable... Python sees any x,y,z as (x,y,z)... return x,y --> return (x,y)
  29. # set # {} no order, no duplicate (all unique)
  30. # dict # {k:v}
  31. #
  32. # # important modules
  33. # math
  34. # csv
  35.  
  36. # OPERATORS
  37. # = # assigns a value
  38. # == # asking a question: are they equal? for a CONDITION
  39. # +
  40. # -
  41. # *
  42. # /
  43. # % # modulo, int remainder... "How many whole things didn't fit?"
  44. # // # floor division... the last even division, same as math.floor(x/y)
  45. # <
  46. # >
  47. # <=
  48. # >=
  49. # ** # raise to a power... math.pow(x, y)
  50. # += # increment
  51. # -= # decrement
  52. # != # asking for a condition... NOT EQUAL
  53. # # keyword used like operators
  54. # in # if _someValue_ in _someContainer_
  55. # not # if not _someValue in _someContainer_
  56. # and
  57. # or # any one True means whole condition is True... limit OR to 2 conditions
  58.  
  59. # COMP 2 CONTROL FLOW
  60. # the HOW stuff
  61. # IF... if, if/else, if/elif, if/elif/else, etc
  62. # LOOPS
  63. # WHILE - an IF that repeats
  64. # FOR - looping over a container... or a known number of times
  65. # for _item_ in _container_:
  66. # for item in myList:
  67. # for key in myDict: # value for "key" ... myDict[key]
  68. # for n in range(5): # range(0, 5) --> [0, 1, 2, 3, 4]
  69. # for i in range(len(myList)): # myList[i]
  70.  
  71. # FUNCTIONS
  72. # defining/writing vs calling
  73. # parameter are special "variables"... they don't work like regular "outside" variables
  74. # parameters vs arguments
  75. # a function has ONE particular job
  76. # return vs print() vs... write a file, whatever its job is
  77. # functions that are asked for in a question will BE TESTED
  78. # method are functions that belong to a particular type/class
  79.  
  80. # def someFunction(x, y):
  81. #     return x + y
  82. #
  83. # if __name__ == "__main__":
  84. #     myInput = int(input())
  85. #     myOtherInput = int(input())
  86. #     num = someFunction(myInput, myOtherInput)
  87. #     print(num)
  88.  
  89. # See "tasks" in the last section of Ch 10, 11, 13, 14 for function writing practice
  90. # # CodingBat also has good function-based Python questions:
  91. # # https://codingbat.com/python
  92.  
  93. # # BUILT-IN FUNCTIONS
  94. # input()
  95. # print()
  96. # len()
  97. # range()
  98. # sum()
  99. # min()
  100. # max()
  101. # round() # is a built-in, cousins math.ceil() and math.floor()
  102. # type() # type(5).__name__ --> int
  103. # list()
  104. # set()
  105. # tuple()
  106. # dict()
  107. # open() # IO/file... .read(), .readlines(), .write()
  108. # reversed() # returns reversed list... list.reverse() modifies in place, so it DOES NOT return a list
  109. # sorted() # same, returns sorted list... list.sort() doesn't
  110.  
  111. # help(str)
  112. # print(dir(str))
  113. # help(str.isspace)
  114.  
  115.  
  116. # STRINGS
  117. # be able to slice like it's 2nd nature: myStr[start:stop:step]
  118. myStr = "abc"
  119. revStr = myStr[::-1]
  120. print(revStr)
  121.  
  122. # KNOW YOUR WHITESPACE
  123. # " " # # ... and a lot of other Unicode spaces
  124. # "\n"
  125. # "\r"
  126. # "\t"
  127. # "\f"
  128.  
  129. # STRING METHODS
  130. # "stuff I want to put together {}".format(var) # or similar f strings
  131. # myString.strip() # input().strip()
  132. # myString.split() # RETURNS a list of smaller strings
  133. # ",".join(listOfStrings)
  134. # myString.replace(oldStr, newStr) # remove... myString.replace(oldStr, "")
  135. # myString.find(subStr) # returns INT index, or -1 if it doesn't, compares to .index(subStr)
  136. # myString.count(subStr) # return INT count of num times occurs
  137. # case: myString.lower(), myString.upper()...
  138. # is/Boolean: isupper(), islower(), isalpha(), isdigit(), isalnum(), isspace()
  139.  
  140. # LISTS
  141. # again be able to slice
  142.  
  143. # LIST METHODS
  144. # # +
  145. # myList.append(item)
  146. # myList.insert(i, item)
  147. # myList.extend(anotherList)
  148. # # -
  149. # myList.pop(0) # by index
  150. # myList.remove(item) # by value
  151. # # other mods
  152. # myList.sort()
  153. # myList.reverse()
  154. # myList.count(item)
  155. # # also rans
  156. # myList.clear()
  157. # myList.copy()
  158. # myList.index(item)
  159.  
  160. # DICTS
  161. # use the key like an index
  162. # myDict[key] # retrieve the value for that key, so like get()
  163. # myDict[key] = value # assign (new) value for that key, so like update({k:v})
  164. # myDict.keys()
  165. # myDict.values()
  166. # myDict.items() # for k, v in myDict.items():
  167.  
  168. # # MODULES
  169. # # math and csv
  170. # # remember there are different import styles that change how you reference them
  171.  
  172. # MATH MODULE
  173. # import math # <-- full import
  174. # math.factorial(x)
  175. # math.ceil(x.yz)
  176. # math.floor(x.yz)
  177. # math.pow(x, y) # not to be confused with math.exp(x), which math.e to a power
  178. # math.sqrt(x)
  179. # math.fabs() # similar to built-in abs()
  180. # math.pi
  181. # math.e
  182. #
  183. # # PARTIAL IMPORT
  184. # from math import factorial # --> not math.factorial() but factorial()
  185. # from math import ceil, floor # --> ceil() and floor()
  186. #
  187. # # ALIAS IMPORT
  188. # import math as m # m.sqrt() not math.sqrt()
  189.  
  190.  
  191. # FILES!!!
  192. # with open("test.txt", "r") as f:
  193. #     contents = f.readlines() # a list of line-by-line strings
  194. # print(contents)
  195. # for line in contents:
  196. #     line = line.strip()
  197. #     print(line)
  198.  
  199. # CSV module
  200. import csv
  201. with open("mock_data.csv", "r") as f1:
  202.     contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t")
  203. # print(contents) # [ ['1', 'Remington', 'Shilling', 'rshilling0@wsj.com', 'Male', '1.71.141.52'], [another list]... ]
  204.  
  205. with open("output_data7.csv", "w") as f2:
  206.     for line in contents:
  207.         # let's get every line where the email address ends in "au"
  208.         if line[3][-3:] == ".au": # line[3].endswith(".au):
  209.             # the write() method takes a SINGLE string as its arg
  210.             f2.write(",".join(line) + "\n")
  211.             # f2.write("{}{}".format(",".join(line), "\n"))
  212.             # f2.write(f"{','.join(line)}{'\n'}")
  213.  
  214. # APPEND MODE
  215. # reading to check last line return...
  216. with open("append_to_this.txt", "r") as f3:
  217.     contents = f3.readlines()
  218. print(contents) # ['Frodo\n', 'Sam\n', 'Merry\n']
  219.  
  220. with open("append_to_this.txt", "a") as f3:
  221.     f3.write("Pippin\n")
  222.  
  223.  
  224.  
  225.  
  226.  
  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.  
  252.  
  253.  
  254.  
  255.  
  256.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement