Advertisement
jspill

webinar-exam-review-2023-05-18

May 18th, 2024
619
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.03 KB | None | 0 0
  1. # Exam Review 2024 May 18
  2.  
  3. # Do those LABS
  4. # Ch 2-14... all Labs!
  5. # Ch 21-32 just ADDITIONAL LABS, but important practice!
  6. # get to know the 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. # When you've gotten the Ch 33-34 Prac Tests to 100, go back and do each again.
  13. # This time, try to think of 2-3 more unit tests that could be run on each question.
  14.  
  15. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  16. # Comp 2: Control Flow
  17. # Comp 3: Modules and Files
  18.  
  19. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  20.  
  21. # Data Types/Classes
  22. # int
  23. # float
  24. # bool # True, False
  25. # str # ""
  26. # list # [ ]
  27. # dict # {key: value}
  28. # tuple # ( ) immutable, Python sees a, b, c as (a,b,c) --> return x,y --> return (x,y)
  29. # set # { } no duplicate values, no order --> no indices, can't slice it, can't sort it, can't reverse
  30. # range # range() container of consecutive numbers
  31. # file # open()... f.read(), f.readlines(), f.write()
  32.  
  33. # # Operators
  34. # = # assigns a value
  35. # == # asking a question... result in Boolean value
  36. # +
  37. # -
  38. # *
  39. # /
  40. # % modulo... whole number remainder... "how many things didn't fit since the last even division?"
  41. # // floor division... last even division
  42. # <
  43. # >
  44. # <=
  45. # >=
  46. # += # x += 1 ... x = x + 1
  47. # -=
  48. # ** # similar to pow() and math.pow()
  49. # !=
  50. # # keywords
  51. # in # membership check... if x in myList
  52. # not # if not x in myList
  53. # and
  54. # or # any one True would cause the combo to be True... limit OR to 2-3 conditions
  55.  
  56. # Comp 2
  57. # Control Flow! The how and when of our programs
  58. # IF statements... if, if/else, if/elif, if/elif/else
  59. # LOOPS
  60. # WHILE - a general purpose loop, an IF that repeats
  61. # FOR - repeating actions a known number of times --> once for everything in the container
  62. # FOR in Python is explicitly about looping over a container
  63. # # Check out my For Loops webinar in The Gotchas
  64. # for ___ in _someContainer_:
  65. # for item in myList:
  66. # for char in myStr:
  67. # for key in myDict: # loop var holds the key, myDict[key] holds the value
  68. # for n in range(0, someNum): # if you thought of range(5) as being [0, 1, 2, 3, 4]
  69. # for i in range(0, len(myList)):
  70. # for i, item in enumerate(myList):
  71.  
  72. # FUNCTIONS
  73. # defining/writing a function vs calling
  74. # modular... a function has ONE specific job
  75. # print/output or return (or maybe something else)
  76. # parameters are special variables for holding stuff coming into the function
  77. # parameters vs arguments
  78.  
  79. # def someFunction(x, y):
  80. #     return x + y
  81. #
  82. # if __name__ == "__main__": # is this script the one that's running?
  83. #     input1 = int(input())
  84. #     input2 = int(input())
  85. #     myNum = someFunction(input1, input2)
  86. #     print(myNum)
  87.  
  88. # See "tasks" in the last section of Ch 10, 11, 13, 14 for function writing practice
  89. # CodingBat also has good function-based Python questions:
  90. # https://codingbat.com/python
  91.  
  92. # Built-In Functions
  93. # input()
  94. # print()
  95. # len()
  96. # min()
  97. # max()
  98. # sum()
  99. # str()
  100. # list()
  101. # dict()
  102. # set()
  103. # range()
  104. # enumerate()
  105. # round() # cousins math.ceil(), math.floor()
  106. # type() # myVar = "4.3", print(type(myVar).__name__) --> str
  107. # pow()
  108. # abs() # cousin math.fabs()
  109. # open()
  110. # help() # # help(str), help(str.isspace)
  111. # dir() # print(dir(str))
  112.  
  113. # STRING
  114. # be able to refer by index, and to slice
  115. # myStr = "abcdef"
  116. # # mySlice[start:stop:step]
  117. # revStr = myStr[::-1]
  118. # print(revStr)
  119.  
  120. # KNOW YOUR WHITESPACE
  121. # " " # space from spacebar
  122. # # a lot of other Unicode spaces
  123. # "\n" # new line return
  124. # "\t" # tab
  125. # "\r" # carriage return
  126.  
  127. # unless otherwise stated... printed output should end a new line return...
  128. # 99% of the time it already does
  129. # print("hey") --> print("hey", end="\n")
  130. # we only need to change that when...
  131. #1 ... this specific says to do something else
  132. #2 ... you yourself overrode the end parameter of print() as the last thing you did... just call print() again after the loop
  133.  
  134. # STRING METHODS
  135. # myStr.format() # "Stuff I want to put into this string like {} and {:.2f}".format(var1, var2)
  136. # myStr.strip() # input().strip()
  137. # myStr.split() # returns a list of smaller strings
  138. # myStr.join(listOfStrings) # " ".join(listOfStrings)
  139. # myStr.replace(subStr, newStr) # "remove" myStr = myStr.replace(subStr, "")
  140. # myStr.index(subStr) # return int index where found, or raise error
  141. # myStr.find(subStr) # return int index where found, or return -1
  142. # myStr.count(subStr) # returns int of times subStr occurs
  143. # case: lower(), upper(), title(), capitalize()
  144. # is/Boolean: islower(), isupper(), isspace(), isalpha(), isalnum(), isnumeric(), isdigit()
  145. # myStr.startswith(subStr), myStr.endswith(subStr)
  146.  
  147. # LISTS
  148. # be able to refer by index and to slice
  149. # LIST METHODS
  150. # # +
  151. # myList.append(item)
  152. # myList.insert(i, item)
  153. # myList.extend(anotherList)
  154. # # -
  155. # myList.pop(i) # pop() by index
  156. # myList.remove(item) # remove() by value
  157. # myList.clear()
  158. # # other
  159. # myList.sort()
  160. # myList.reverse()
  161. # myList.count(item) # returns int num of occurrences
  162. # myList.index(item)
  163. # myList.copy()
  164.  
  165. # DICT
  166. # use the key like an index []... then you don't really need DICT methods
  167. # myDict[key] # get the value for that key
  168. # myDict[key] = value # assigns new value to that key
  169.  
  170. # membership check
  171. # if ___ in myDict: # looking at keys
  172.  
  173. # DICT METHODS
  174. # myDict.keys() # all dict keys in one set-like object
  175. # myDict.values() # all dict values in one object
  176.  
  177. # MODULES
  178. # math and csv
  179.  
  180. # MATH MODULES
  181. # import math # FULL IMPORT
  182. # math.factorial(x)
  183. # math.ceil(x)
  184. # math.floor(x)
  185. # math.sqrt(x)
  186. # math.pow(x, y)
  187. # math.fabs(x)
  188. # math.pi
  189. # math.e
  190. #
  191. # # PARTIAL IMPORT
  192. # from math import ceil # just ceil() not math.ceil()
  193. # from math import sqrt, fabs # just sqrt(), fabs()
  194. # from math import * # factorial(), floor(), etc no math.
  195. #
  196. # # ALIAS IMPORT
  197. # import math as m # m.floor(), m.ceil()
  198.  
  199. # FILES
  200. # modes: r, w, a
  201.  
  202. # READ MODE
  203. # f = open(filename)
  204. with open("plain_text_file.txt", "r") as f:
  205.     # f.read() # returns the whole file as one big string
  206.     # f.readlines() # returns a list of strings, each line is one string
  207.     # f.write(someStr) # writes a single str arg to file
  208.     contents = f.readlines()
  209. # print(contents)
  210.  
  211. # reading with csv module
  212. import csv
  213. # csv.reader()
  214.  
  215. with open("mock_data.csv", "r") as f1: # mockaroo.com
  216.     # for row in csv.reader(f1):
  217.     contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t") for .tsv
  218. # print(contents)
  219. # for row in contents[:10]:
  220. #     print(row)
  221.  
  222. # WRITE MODE
  223. # write out a new file where all the last names begin with "Mc"
  224. with open("output_data38.csv", "w") as f2:
  225.     for row in contents:
  226.         # ['5', 'Carling', 'Baude', 'cbaude4@liveinternet.ru', 'Male', '91.198.36.54']
  227.         # last name is row[2]
  228.         if row[2].startswith("Mc"):
  229.             f2.write(",".join(row) + "\n")
  230.  
  231. # APPEND MODE
  232. # reading it to see...
  233. # with open("append_to_this.txt") as f3:
  234. #     print(f3.readlines())
  235. with open("append_to_this.txt", "a") as f3:
  236.     f3.write("\nPippin")
  237.  
  238.  
  239.  
  240.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement