Advertisement
jspill

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

May 20th, 2023
1,563
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. # jerry.spiller@wgu.edu
  96.  
  97. # FUNCTIONS
  98. # defining/writing vs calling
  99. # parameters are special variables... they don't work like "regular" variables
  100. # parameters vs arguments
  101. # return vs print()/output... vs other... do whatever the question says
  102. # a function has ONE, PARTICULAR job
  103.  
  104. # def someFunction(x, y):
  105. #     return x * y
  106. #
  107. # if __name__ == "__main__": # is this script the one that's being run from?
  108. #     # we're solving THIS QUESTION
  109. #     myInput = int(input())
  110. #     myOther = int(input())
  111. #     myNum = someFunction(myInput, myOther)
  112. #     print(myNum)
  113.  
  114. # See "tasks" in the last section of Ch 10, 11, 13, 14 for function writing practice
  115. # # CodingBat also has good function-based Python questions:
  116. # # https://codingbat.com/python
  117.  
  118. # BUILT-IN FUNCTIONS
  119. # input()
  120. # print()
  121. # range()
  122. # type()
  123. # list()
  124. # int()
  125. # float()
  126. # dict()
  127. # tuple()
  128. # set()
  129. # enumerate()
  130. # round() # cousins math.ceil() and math.floor() are in math
  131. # open() # open file, create a file type object
  132. # len()
  133. # sum()
  134. # min()
  135. # max()
  136. # help()
  137. # dir()
  138. # help(list)
  139. # print(dir(str))
  140. # help(str.isspace)
  141.  
  142. # STRINGS
  143. # be able to slice...
  144. # myStr = "abc"
  145. # revStr = myStr[::-1] # "cba"
  146.  
  147. # KNOW YOUR WHITESPACE
  148. # " " # space bar space
  149. # # a lot of spaces in Unicode
  150. # "\n" # NEW line return
  151. # "\r" # carriage return
  152. # "\t"# tab
  153.  
  154. # STRING METHODS
  155. # myStr.format() # "stuff I want to put together {}".format(var)...
  156. # myStr.strip() #
  157. # myStr.split() # returns a list of smaller strings
  158. # myStr.join() # " ".join(listOfStrings)
  159. # myStr.replace(subStr, newStr) # "remove"... myStr = myStr.replace(subStr, "")
  160. # myStr.find(subStr) # returns int... index where subStr starts
  161. # myStr.count(subStr) # return int count
  162. # case: myStr.lower(), myStr.upper(), myStr.title(), myStr.capitalize()
  163. # is/Boolean: myStr.isupper(), myStr.islower(), myStr.isspace(), myStr.isalpha(), isnumeric(), isdigit(), isalnum()
  164. # startswith(), endswith()
  165.  
  166. # LIST
  167. # be able to use index, slices
  168.  
  169. # LIST METHODS
  170. # # +
  171. # myList.append(item)
  172. # myList.insert(i, item)
  173. # myList.extend(anotherList)
  174. # # -
  175. # myList.pop() # pop by index... myList.pop(0)
  176. # myList.remove(item) # remove by value
  177. # myList.clear()
  178. # # other
  179. # myList.count(item)
  180. # myList.sort()
  181. # myList.reverse()
  182. # # also rans
  183. # myList.copy()
  184. # myList.index(item)
  185.  
  186. # DICT
  187. # use the key like an index
  188. # myDict[key] # retrieve value for that key
  189. # myDict[key] = value # assign value to key
  190. # myDict.keys()
  191. # myDict.values()
  192. # myDict.items()
  193.  
  194. # MODULES
  195. # math and csv
  196.  
  197. # MATH MODULE
  198. # import math # FULL IMPORT
  199. # math.factorial(x)
  200. # math.ceil(x.yz)
  201. # math.floor(x.yz)
  202. # math.pow(x, y) # not to be confused with math.exp()
  203. # math.sqrt(x)
  204. # math.fabs(x) # abs(x)
  205. # math.pi
  206. # math.e
  207.  
  208. # # PARTIAL IMPORT
  209. from math import factorial # # factorial(x) # not math.factorial()
  210. # from math import ceil, sqrt
  211. # from math import *
  212. # floor(x.yz) # not math.floor()
  213.  
  214. # # ALIAS IMPORT
  215. # import math as m
  216. # m.floor(x.yz) # not math.floor()
  217.  
  218. # FILES
  219.  
  220. # READ MODE
  221. with open("test.txt", "r") as f:
  222.     contents = f.readlines() # list a strings line by line
  223. # print(contents)
  224. # for line in contents:
  225. #     line = line.strip()
  226. #     print(line)
  227.  
  228. # CSV Module
  229. import csv # csv.reader()
  230. with open("mock_data.csv", "r") as f1:
  231.     contents = list(csv.reader(f1)) # returns list of lists
  232.     # for tsv
  233.     # contents = list(csv.reader(f1, delimitter="\t"))
  234. # print(contents) # [['id', 'first_name', 'last_name', 'email', 'gender', 'ip_address'], ['1', 'Remington', 'Shilling', 'rshilling0@wsj.com', 'Male', '1.71.141.52']... ]
  235. # for row in contents[0:20]:
  236. #     print(row)
  237.  
  238. # WRITE MODE
  239. # with open("output_data18.csv", "w") as f2:
  240. #     for row in contents:
  241. #         # only write into this new file if email is at tripadvisor.com
  242. #         # email is at list position 3
  243. #         if "@tripadvisor.com" in row[3]: # if row[3].endswith("@tripadvisor.com")
  244. #             # takes a single str argument
  245. #             f2.write(",".join(row) + "\n")
  246.  
  247.  
  248. # APPEND MODE
  249. # with open("append_to_this.txt", "r") as f3:
  250. #     contents = f3.readlines()
  251. with open("append_to_this.txt", "a") as f3:
  252.     f3.write("Gimli\nShelob\nBilbo\nRadagast\n") # added to file, rather than overwriting
  253. print(contents)
  254.  
  255.  
  256.  
  257.  
  258.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement