Advertisement
jspill

webinar-python-exam-review-2023-10-28

Oct 28th, 2023
1,052
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.23 KB | None | 0 0
  1. # Exam Review 2023 Oct 28
  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
  21. # +
  22. # -
  23. # *
  24. # /
  25. # // # floor division... last even division
  26. # % # modulo... whole number remainder... "how many whole things didn't fit since the last even division?"
  27. # <
  28. # >
  29. # <=
  30. # >=
  31. # += # x = x + 1
  32. # -=
  33. # ** # compare to pow(), math.pow()
  34. # !=
  35. # # keywords
  36. # in # if x in myList:
  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 x,y,z as (a,b,c) --> return a,b --> return (a,b)
  49. # set # no duplicates, no order... no index, no slicing, no sort
  50. # range # range()... container of consecutive numbers --> range(0, 5, 1)... [0, 1, 2, 3, 4]
  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 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: # value is myDict[key]
  64. # for num 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. #     myInput = int(input())
  81. #     myOther = int(input())
  82. #     myNum = someFunction(myInput, myOther)
  83. #     print(myNum)
  84.  
  85. # See "tasks" in the last section of Ch 10, 11, 13, 14 for function writing practice
  86. # # CodingBat also has good function-based Python questions:
  87. # https://codingbat.com/python
  88.  
  89. # BUILT-IN FUNCTIONS
  90. # input()
  91. # print()
  92. # len()
  93. # min()
  94. # max()
  95. # sum()
  96. # range()
  97. # int()
  98. # list()
  99. # float()
  100. # set()
  101. # tuple()
  102. # dict()
  103. # type() # print(type(myVar).__name__)
  104. # enumerate()
  105. # open()
  106. # round() # cousins math.ceil(), math.floor()
  107. # pow()
  108. # abs()
  109. # help()
  110. # dir() # print(dir(str))
  111. # help(str) # help(str.isspace)
  112.  
  113. # STRINGS
  114. # be able to refer to indices, and slice
  115. # myStr = "abc"
  116. # # mySlice[start:stop:step]
  117. # revStr = myStr[::-1]
  118. # print(revStr)
  119.  
  120. # KNOW YOUR WHITESPACE
  121. # " " # space from the spacebar
  122. # # a lot of Unicode space
  123. # \n # new line return
  124. # \r # carriage return
  125. # \t # tab
  126.  
  127. # STRING METHODS
  128. # myStr.format() # "Stuff I want to put together {}".format(myVar)
  129. # myStr.strip() # input().strip()
  130. # myStr.split() # returns a list of smaller strings
  131. # ",".join(listOfStrings)
  132. # myStr.replace(subStr, newSubStr) # "remove" myStr = myStr.replace(subStr, "")
  133. # myStr.find(subStr) # return index where it's found, or -1 on failure
  134. # myStr.count(subStr) # return int of number found of that thing
  135. # case: .lower(), .upper(), .title(), .capitalize()
  136. # is/Boolean: .islower(), .isupper(), .isspace(), isalpha(), .isnumeric(), .isdigit(), .isalnum()
  137. # myStr.startswith(subStr), myStr.endswith(subStr)
  138.  
  139. # LISTS
  140. # be able to refer by index and to slice
  141.  
  142. # LIST METHODS
  143. # +
  144. # myList.append(item)
  145. # myList.insert(i, item)
  146. # myList.extend(anotherList)
  147. # # -
  148. # myList.pop(i)
  149. # myList.remove(item) # pop() by index, remove() by value
  150. # myList.clear()
  151. # # other
  152. # myList.count(item)
  153. # myList.sort()
  154. # myList.reverse()
  155. # myList.copy()
  156. # myList.index(item)
  157.  
  158. # DICT
  159. # use the key like an index []... then you don't really need DICT methods
  160. # myDict[key] # get the value for that key, get()
  161. # myDict[key] = value # assigns a new value to key, takes the place update({key:value})
  162.  
  163. # DICT METHODS
  164. # myDict.keys()
  165. # myDict.values()
  166. # myDict.items() # for k, v in myDict.items()
  167.  
  168. # if _key_ in myDict:
  169.  
  170. # MODULES
  171. # math and csv
  172.  
  173. # MATH MODULE
  174. # import math # FULL IMPORT
  175. # math.factorial(x)
  176. # math.ceil(x)
  177. # math.floor(y)
  178. # math.pow(x, y)
  179. # math.sqrt(x)
  180. # math.fabs(x)
  181. # math.pi
  182. # math.e
  183.  
  184. # PARTIAL IMPORT
  185. # from math import floor # floor(x)
  186. # from math import ceil, sqrt # sqrt(x), ceil(x)
  187. # from math import * # floor(x)
  188. #
  189. # # ALIAS IMPORT
  190. # import math as m # m.floor(x)
  191.  
  192. # FILES
  193.  
  194. # READ MODE
  195. with open("test.txt", "r") as f:
  196.     # f.read() # returns whole file as one big string
  197.     # f.readlines() # returns a list of strings, line by line
  198.     # f.write() # take one str arg and write into file
  199.     contents = f.readlines()
  200. # print(contents)
  201. # for line in contents:
  202. #     line = line.strip()
  203. #     print(line)
  204.  
  205. # CSV Module
  206. import csv
  207. with open("mock_data.csv", "r") as f1: # mockaroo.com
  208.     contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t")
  209. print(contents[0:20])
  210.  
  211. # WRITE MODE
  212. with open("output_data29.csv", "w") as f2:
  213.     # write rows into new file where email address is Australian
  214.     for row in contents:
  215.         # email address is at row[3]
  216.         if row[3].endswith(".au"): # or slice, etc
  217.             f2.write(",".join(row) + "\n")
  218.  
  219. # APPEND MODE
  220. with open("append_to_this.txt", "r") as f3:
  221.     print(f3.readlines())
  222. with open("append_to_this.txt", "a") as f3:
  223.     f3.write("\nPippin")
  224.  
  225. # I'll end with this:
  226. # Unit test, unit test, unit test!
  227. #
  228. # < >, check numbers on either side
  229. # math: check 0, check a really big numbers
  230. # check leading O's in numbers when converting type:
  231. #   input of 011 -> int("011") -> 11
  232. # check possible keys from input to see if in dict (what happens if you input something that isn't in there?)
  233.  
  234.  
  235.  
  236.  
  237.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement