Advertisement
jspill

webinar-python-exam-review-2023-05-20

May 20th, 2023
1,696
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.09 KB | None | 0 0
  1. # Exam Review 2023 May 20
  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
  7. # Use Submit Mode and get them to 100%!!! And PAY ATTENTION to the unit tests!
  8.  
  9. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  10. # Comp 2: Control Flow
  11. # Comp 3: Modules and Files
  12.  
  13. # Watch your string input and output
  14. # input...
  15. # myInput = input().strip()
  16. # output/print()
  17. # print() is the same print(end="\n")
  18. # print("Something I'm printing", end=" ")
  19. # print()
  20. # print("Clean new line!") # I checked myself before I wrecked myself
  21.  
  22. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  23. # int
  24. # float
  25. # bool # True, False # x = 7 # if x > 5: # print(type(x>5))
  26. # str # ""
  27. # list # [ ]
  28. # dict # {key:value}
  29. # tuple # () immutable, Python sees a,b,c as (a,b,c)... return x,y --> return (x,y)
  30. # set # { } all unique/no duplicates, no order... no index, no slicing, no sort
  31. # range object # range()... range(0, 5) --> # [0, 1, 2, 3, 4]
  32.  
  33. # operators
  34. # = # assignment, assigning a value
  35. # == # equality, asking a question, comparing... if/elif, while
  36. # +
  37. # -
  38. # *
  39. # /
  40. # % # modulo... gives the int remainder, "how many whole things didn't fit?"... since the last even division
  41. # // # floor division... is that last even division
  42. # <
  43. # >
  44. # <=
  45. # >=
  46. # += # x += 1 is x = x+1
  47. # -=
  48. # ** # raising to a power... pow() and math.pow()
  49. # !=
  50. # # # keywords used like operators
  51. # in # if x in myList:
  52. # not # if not x in myList:
  53. # and
  54. # or # any one True means the whole conditions is True... limit OR to 2 conditions
  55.  
  56. # Comp 2
  57. # the HOW stuff... control flow structures
  58. # IF statements... if, if/else, if/elif, if/elif/else
  59. # LOOPS
  60. # WHILE - an IF that repeats
  61. # FOR - looping over a container, or a known number of times # hence... range()
  62. # for ___ in __someContainer__:
  63. # for item in myList:
  64. # for char in myStr:
  65. foodTVSubs = {
  66.     # "": "Nancy asks if you can have a blank key", # legal!
  67.     "canned ham": "Spam",
  68.     "shell candies": "M&M's",
  69.     "cereal treats": "Rice Krispies treats"
  70. }
  71. # for key in myDict: # loop var holds key
  72. # We can use dict keys as if they indices
  73. # print(foodTVSubs["canned ham"]) # retrives the value for the key
  74. # # We can assign a new value to a key
  75. # foodTVSubs["canned ham"] = "Turkey Spam"
  76. # print(foodTVSubs["canned ham"])
  77. #
  78. # for k in foodTVSubs:
  79. #     v = foodTVSubs[k]
  80. #     print(f"We have to say '{k}' instead of '{v}' on Food Network.")
  81. #
  82. # # dict items() method
  83. # for key, value in foodTVSubs.items():
  84. #     print(f"We have to say '{key}' instead of '{value}' on Food Network.")
  85.  
  86. # for n in range(0, 12):
  87. # for i in range(0, len(myList)): # for i, item in enumerate(myList):
  88. #     print(myList[i])
  89.  
  90. # 1st value tells me how many more times to call input()
  91. # myInput = int(input())
  92. # for n in range(myInput):
  93. #     nextValue = input()
  94.  
  95.  
  96. # FUNCTIONS
  97. # defining/writing vs calling
  98. # parameters are special variables... they don't work like "regular" variables
  99. # parameters vs arguments
  100. # return vs print()/output... vs other... do whatever the question says
  101. # a function has ONE, PARTICULAR job
  102.  
  103. # def someFunction(x, y):
  104. #     return x * y
  105. #
  106. # if __name__ == "__main__": # is this script the one that's being run from?
  107. #     # we're solving THIS QUESTION
  108. #     myInput = int(input())
  109. #     myOther = int(input())
  110. #     myNum = someFunction(myInput, myOther)
  111. #     print(myNum)
  112.  
  113. # See "tasks" in the last section of Ch 10, 11, 13, 14 for function writing practice
  114. # # CodingBat also has good function-based Python questions:
  115. # # https://codingbat.com/python
  116.  
  117. # BUILT-IN FUNCTIONS
  118. # input()
  119. # print()
  120. # range()
  121. # type()
  122. # list()
  123. # int()
  124. # float()
  125. # dict()
  126. # tuple()
  127. # set()
  128. # enumerate()
  129. # round() # cousins math.ceil() and math.floor() are in math
  130. # open() # open file, create a file type object
  131. # len()
  132. # sum()
  133. # min()
  134. # max()
  135. # help()
  136. # dir()
  137. # help(list)
  138. # print(dir(str))
  139. # help(str.isspace)
  140.  
  141. # STRINGS
  142. # be able to slice...
  143. # myStr = "abc"
  144. # revStr = myStr[::-1] # "cba"
  145.  
  146. # KNOW YOUR WHITESPACE
  147. # " " # space bar space
  148. # # a lot of spaces in Unicode
  149. # "\n" # NEW line return
  150. # "\r" # carriage return
  151. # "\t"# tab
  152.  
  153. # STRING METHODS
  154. # myStr.format() # "stuff I want to put together {}".format(var)...
  155. # myStr.strip() #
  156. # myStr.split() # returns a list of smaller strings
  157. # myStr.join() # " ".join(listOfStrings)
  158. # myStr.replace(subStr, newStr) # "remove"... myStr = myStr.replace(subStr, "")
  159. # myStr.find(subStr) # returns int... index where subStr starts
  160. # myStr.count(subStr) # return int count
  161. # case: myStr.lower(), myStr.upper(), myStr.title(), myStr.capitalize()
  162. # is/Boolean: myStr.isupper(), myStr.islower(), myStr.isspace(), myStr.isalpha(), isnumeric(), isdigit(), isalnum()
  163. # startswith(), endswith()
  164.  
  165. # LIST
  166. # be able to use index, slices
  167.  
  168. # LIST METHODS
  169. # # +
  170. # myList.append(item)
  171. # myList.insert(i, item)
  172. # myList.extend(anotherList)
  173. # # -
  174. # myList.pop() # pop by index... myList.pop(0)
  175. # myList.remove(item) # remove by value
  176. # myList.clear()
  177. # # other
  178. # myList.count(item)
  179. # myList.sort()
  180. # myList.reverse()
  181. # # also rans
  182. # myList.copy()
  183. # myList.index(item)
  184.  
  185. # DICT
  186. # use the key like an index
  187. # myDict[key] # retrieve value for that key
  188. # myDict[key] = value # assign value to key
  189. # myDict.keys()
  190. # myDict.values()
  191. # myDict.items()
  192.  
  193. # MODULES
  194. # math and csv
  195.  
  196. # MATH MODULE
  197. # import math # FULL IMPORT
  198. # math.factorial(x)
  199. # math.ceil(x.yz)
  200. # math.floor(x.yz)
  201. # math.pow(x, y) # not to be confused with math.exp()
  202. # math.sqrt(x)
  203. # math.fabs(x) # abs(x)
  204. # math.pi
  205. # math.e
  206.  
  207. # # PARTIAL IMPORT
  208. from math import factorial # # factorial(x) # not math.factorial()
  209. # from math import ceil, sqrt
  210. # from math import *
  211. # floor(x.yz) # not math.floor()
  212.  
  213. # # ALIAS IMPORT
  214. # import math as m
  215. # m.floor(x.yz) # not math.floor()
  216.  
  217. # FILES
  218.  
  219. # READ MODE
  220. with open("test.txt", "r") as f:
  221.     contents = f.readlines() # list a strings line by line
  222. # print(contents)
  223. # for line in contents:
  224. #     line = line.strip()
  225. #     print(line)
  226.  
  227. # CSV Module
  228. import csv # csv.reader()
  229. with open("mock_data.csv", "r") as f1:
  230.     contents = list(csv.reader(f1)) # returns list of lists
  231.     # for tsv
  232.     # contents = list(csv.reader(f1, delimitter="\t"))
  233. # print(contents) # [['id', 'first_name', 'last_name', 'email', 'gender', 'ip_address'], ['1', 'Remington', 'Shilling', '[email protected]', 'Male', '1.71.141.52']... ]
  234. # for row in contents[0:20]:
  235. #     print(row)
  236.  
  237. # WRITE MODE
  238. # with open("output_data18.csv", "w") as f2:
  239. #     for row in contents:
  240. #         # only write into this new file if email is at tripadvisor.com
  241. #         # email is at list position 3
  242. #         if "@tripadvisor.com" in row[3]: # if row[3].endswith("@tripadvisor.com")
  243. #             # takes a single str argument
  244. #             f2.write(",".join(row) + "\n")
  245.  
  246.  
  247. # APPEND MODE
  248. # with open("append_to_this.txt", "r") as f3:
  249. #     contents = f3.readlines()
  250. with open("append_to_this.txt", "a") as f3:
  251.     f3.write("Gimli\nShelob\nBilbo\nRadagast\n") # added to file, rather than overwriting
  252. print(contents)
  253.  
  254.  
  255.  
  256.  
  257.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement