Advertisement
jspill

webinar-python-exam-review-2023-11-18

Nov 18th, 2023
1,178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.95 KB | None | 0 0
  1. # Exam Review 2023 Nov 18
  2.  
  3. # Do those LABS
  4. # Ch 2-14... all Labs!
  5. # Ch 21-32 just ADDITIONAL LABS, but important practice!
  6. # Prac Tests, Ch 33 and 34... more than the Pre
  7.  
  8. # Use Submit Mode and get them to 100%!!!
  9. # PAY ATTENTION to the unit tests!
  10. # ... then UNIT TEST more! Unit test, unit test, unit test!
  11.  
  12. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  13. # Comp 2: Control Flow
  14. # Comp 3: Modules and Files
  15.  
  16. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  17.  
  18. # Operators
  19. # = # assignment
  20. # == # asking a question? A condition
  21. # +
  22. # -
  23. # *
  24. # /
  25. # % # modulo... whole number remainder... "how many whole things didn't fit since the last even division?"
  26. # // # floor division... last even division
  27. # <
  28. # >
  29. # <=
  30. # >=
  31. # += # x+=1 --> x = x + 1
  32. # -=
  33. # **  # compare to pow(), math.pow()
  34. # !=
  35. # # keywords
  36. # in # if x in myList, if something in myDict
  37. # not # if not x in myList
  38. # and
  39. # or # any one True means the combo is True... limit OR to 2-3 conditions
  40.  
  41. # Data Types/Classes
  42. # int
  43. # float
  44. # bool # True, False
  45. # str # "" most important data type
  46. # list # [ ]
  47. # dict # {key: value}
  48. # tuple # ( )  immutable, Python sees a,b,c as (a,b,c) --> return x,y --> return (x,y)
  49. # set # no duplicates, unordered -> no index, no slicing, no sorting
  50. # range # range()... container of consecutive numbers --> range(start, stop, step)
  51. # file # open()... f.read(), f.readlines(), f.write()
  52.  
  53. # Comp 2
  54. # Control Flow! The HOW stuff
  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... hence range()
  59. # # Check out my For Loops webinar in The Gotchas
  60. # for ___ in _someContainer__:
  61. # for item in myList:
  62. # for char in myStr:
  63. # for key in myDict: # myDict[key] or use for k, v in myDict.items(): --> x, y = [2, 3]
  64. # for n in range(5):
  65. # for i in range(len(myList)): # myList[i]
  66. # for i, item in enumerate(myList):
  67.  
  68. # FUNCTIONS
  69. # defining/writing vs calling
  70. # modular... a function has ONE job
  71. # parameters are special variables for holding stuff coming into the function
  72. # parameters vs arguments
  73. # pay attention to whether the function is asked to return or print()/output
  74.  
  75. # def someFunction(x, y):
  76. #     return x * y
  77. #
  78. # if __name__ == "__main__": # is this script the one that's running now
  79. #     # inside this block we're answering this specific question
  80. #     input1 = int(input())
  81. #     input2 = int(input())
  82. #     # myNum = someFunction(input1, input2)
  83. #     # print(myNum)
  84. #     print(someFunction(input1, input2))
  85.  
  86. # See "tasks" in the last section of Ch 10, 11, 13, 14 for function writing practice
  87. # CodingBat also has good function-based Python questions:
  88. # https://codingbat.com/python
  89.  
  90. # BUILT-IN FUNCTIONS
  91. # input()
  92. # print()
  93. # range()
  94. # enumerate()
  95. # min()
  96. # max()
  97. # sum()
  98. # len()
  99. # open()
  100. # int()
  101. # float()
  102. # str()
  103. # list()
  104. # dict()
  105. # set()
  106. # tuple()
  107. # type() # x = "5.426353 print(type(x).__name__) --> str
  108. # round() # cousins math.ceil(), math.floor()
  109. # pow() # compare to math.pow()
  110. # abs() # math.fabs()
  111. # help() # help(str), help(str.isspace)
  112. # dir() # print(dir(str))
  113.  
  114. # STRINGS
  115. # be able to refer to indices, and slice
  116. # myStr = "abcd"
  117. # # mySlice[start:stop:step]
  118. # revStr = myStr[::-1]
  119. # print(revStr)
  120.  
  121. # KNOW YOUR WHITESPACE
  122. # " " # space from the spacebar
  123. # # a lot of Unicode space
  124. # \n # new line return
  125. # \r # carriage return
  126. # \t # tab
  127.  
  128. # print() # --> print(end="\n")
  129.  
  130. # STRING METHODS
  131. # myStr.format() # "Stuff I want to put together {:.2f}".format(myVar)
  132. # myStr.strip() # input().strip()
  133. # myStr.split() # returns a list of smaller strings
  134. # ",".join(listOfStrings)
  135. # myStr.replace(subStr, newSubStr) # "remove" myStr = myStr.replace(subStr, "")
  136. # myStr.index(subStr), myStr.find(subStr)
  137. # myStr.count(subStr) # return number of times found
  138. # case: .lower(), .upper(), .title(), capitalize()
  139. # is/Boolean: .islower(), .isupper(), .isspace(), .isalpha(), .isnumeric(), .isdigit(), .isalnum()
  140. # myStr.startswith(subStr), myStr.endswith(subStr)
  141.  
  142. # LISTS
  143. # be able to refer by index and to slice
  144.  
  145. # LIST METHODS
  146. # # +
  147. # myList.append(item)
  148. # myList.insert(i, item)
  149. # myList.extend(anotherList)
  150. # # -
  151. # myList.pop(i)
  152. # myList.remove(item) # pop() by index, remove() by value
  153. # myList.clear()
  154. # # other
  155. # myList.sort()
  156. # myList.reverse()
  157. # myList.count(item)
  158. # myList.copy()
  159. # myList.index(item)
  160.  
  161. # DICT
  162. # use the key like an index []... then you don't really need DICT methods
  163. # myDict[key] # get the value for that key --> similar to myDict.get()
  164. # myDict[key] = value # assigns new value to key --> similar to myDict.update({k:v})
  165.  
  166. # DICT METHODS
  167. # myDict.keys()
  168. # myDict.values()
  169. # myDict.items() # for k, v in myDict.items()
  170.  
  171. # membership check is on keys
  172. # if _key_ in myDict: # looks at keys, not values
  173.  
  174. # MODULES
  175. # math and csv
  176.  
  177. # MATH MODULE
  178. # import math # FULL IMPORT
  179. # math.factorial(x)
  180. # math.ceil(x)
  181. # math.floor(x)
  182. # math.pow(x, y)
  183. # math.sqrt(x)
  184. # math.fabs(x)
  185. # math.pi
  186. # math.e
  187.  
  188. # PARTIAL IMPORT
  189. # from math import floor # --> floor(x)
  190. # from math import ceil, sqrt # --> sqrt(x), ceil(x)
  191. # from math import * # floor(x), sqrt(x)
  192. #
  193. # # ALIAS IMPORT
  194. # import math as m
  195. # math.floor(x) # can't do!
  196.  
  197. # FILES
  198. # modes: r, w, a
  199.  
  200. # READ MODE
  201. with open("test.txt", "r") as f:
  202.     # f.read() # returns whole file as one big string
  203.     # f.readlines() # returns a list of strings, line by line
  204.     # f.write() # take one str arg and write into file (can't do here bc I'm in read mode)
  205.     contents = f.readlines()
  206. # print(contents) # ['Hello.\n', 'This\n', 'is\n', 'just\n', 'a\n', 'string.\n', 'But...\n', 'with\n', 'many\n', 'line\n', 'breaks!']
  207. # for line in contents:
  208. #     line = line.strip()
  209. #     print(line) # print(line, end="\n")
  210.  
  211. # CSV Module
  212. import csv # csv.reader()
  213. with open("mock_data.csv", "r") as f1: # mockaroo.com
  214.     contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t") for .tsv
  215. # print(contents)
  216. # for row in contents[:20]:
  217. #     print(row)
  218.  
  219. # WRITE MODE
  220. with open("output_data30.csv", "w") as f2:
  221.     # write rows into new file where firstName begins with "Re"
  222.     for row in contents:
  223.         # firstname is a position 1
  224.         # if row[1][:2] == "Re":
  225.         if row[1].startswith("Re"):
  226.             f2.write(",".join(row) + "\n") # file write() takes one str arg
  227.             # f2.write(f'{",".join(row)}\n')
  228.             # f2.write('{}\n'.format(",".join(row)))
  229.  
  230. # APPEND MODE
  231. # with open("append_to_this.txt", "r") as f3:
  232. #     print(f3.readlines())
  233. with open("append_to_this.txt", "a") as f3:
  234.     f3.write("\nPippin")
  235.  
  236. # Final advice:
  237. # Unit test, unit test, unit test!
  238.  
  239. # < >, check numbers on either side
  240. # doing math? check 0, check really big numbers
  241. # check leading O's in numbers when converting type:
  242. # input of 007 -> that's "007" -> int("007") -> 7
  243. # check possible keys from input to see if in dict
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement