Advertisement
jspill

webinar-exam-review-2023-04-22

Apr 22nd, 2023
968
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.20 KB | None | 0 0
  1. # Exam Review 2023 Apr 22
  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%!!!
  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("some stuff", end=" ") # if I ever override that \n from end...
  19. # print()
  20. # print("Clean new line!")
  21.  
  22. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  23. # Common Data Types
  24. # int
  25. # float
  26. # bool # True, False... if x > 5
  27. # str # " "
  28. # list # [ ]
  29. # dict # {key: value}
  30. # tuple # ( )  immutable, Python sees any a,b,c as (a,b,c)... return a, b -> return (a, b)
  31. # set #  { } no order... no index, no sorting, no slicing; all unique values
  32. # range # range() --> range object # range(5) or range(0, 5) --> # [0, 1, 2, 3, 4]
  33.  
  34. # operators
  35. # = # assigns
  36. # == # equality... asking a question, evaluation, used in a condition
  37. # +
  38. # -
  39. # *
  40. # /
  41. # % # modulo, gives us the int remainder, "how many whole things didn't fit?"
  42. # // # floor division... the last even division
  43. # <
  44. # >
  45. # <=
  46. # >=
  47. # += # x += 1 is the same x = x + 1
  48. # -=
  49. # ** # raise to a power... pow() or math.pow()
  50. # !=
  51. # # keywords used like operators
  52. # in # if value in container
  53. # not # if not value in container
  54. # and
  55. # or # any one True means whole condition is True... limit OR to 2 conditions
  56.  
  57. # Comp 2
  58. # the HOW stuff... control flow structures
  59. # IF statements... if, if/else, if/elif, if/elif/else
  60. # LOOPS
  61. # WHILE - an IF that repeats
  62. # FOR -  looping over a container, or a known number of times  #... hence range()
  63. # for ___ in _container_:
  64. # for item in myList:
  65. # for char in myStr:
  66. # for key in myDict:
  67. # for n in range(0, 12):
  68. # for i in range(len(myList)): # enumerate()
  69.  
  70. # FUNCTIONS
  71. # defining/writing vs calling
  72. # a function has ONE, PARTICULAR job
  73. # parameters are special variables... they don't work like "regular" variables
  74. # parameters vs arguments
  75. # return vs print()/output... vs other... do whatever the question says
  76.  
  77. # def someFunction(x, y): # don't use any parameters unless the question says to
  78. #     return x ** y
  79. # # print("hey")
  80. #
  81. # if __name__ == "__main__": # are we running from this very script I'm writing?
  82. #     myInput = int(input())
  83. #     myOther = int(input())
  84. #     myNum = someFunction(myInput, myOther)
  85. #     print(myNum)
  86.  
  87. # print(__name__) # __main__
  88.  
  89. # See "tasks" in the last section of Ch 10, 11, 13, 14 for function writing practice
  90. # # CodingBat also has good function-based Python questions:
  91. # # https://codingbat.com/python
  92.  
  93. # BUILT-IN FUNCTIONS
  94. # input()
  95. # print()
  96. # range()
  97. # len()
  98. # sum()
  99. # min()
  100. # max()
  101. # int()
  102. # float()
  103. # list()
  104. # str()
  105. # tuple()
  106. # set()
  107. # enumerate() # for i, item in enumerate(myList)
  108. # open() # opens a file and creates a file/IO stream object
  109. # type() # print(type(myVar).__name__)
  110. # round() # cousins math.ceil() and math.floor() are in math module
  111. # sorted() # returns sorted list.. compare list.sort() does not return anything
  112. # reversed() # returns reversed list... same
  113. # help() # help(str), help(str.isspace)
  114. # dir() # print(dir(str))
  115.  
  116. # STRINGS
  117. # be able to slice strings like it's second nature
  118. # [start:stop:step]
  119. myStr = "abcd"
  120. revStr = myStr[::-1]
  121. print(revStr)
  122.  
  123. # KNOW YOUR WHITESPACE
  124. # # " "
  125. # # many other Unicode spaces
  126. # "\n"
  127. # "\r"
  128. # "\t"
  129.  
  130. # STRING METHODS
  131. # myStr.format() # "stuff I want to put together {}".format(var)... compare to similar to f strings
  132. # myStr.split() # return a list of smaller strings
  133. # myStr.join() # " ".join(listOfStrings)
  134. # myStr.strip() # lstrip(), rstrip()
  135. # myStr.replace(subStr, newStr) # "remove"... myStr = myStr.replace(subStr, "")
  136. # myStr.find(subStr) # return int index of where found, or -1 if not there
  137. # myStr.count(subStr) # return int count of occurrences
  138. # case: myStr.lower(), myStr.upper()... title(), capitalize()
  139. # is/Boolean: myStr.isupper(), myStr.islower(), myStr.isspace(), myStr.isalpha(), myStr.isdigit(), myStr.isnumeric(), myStr.isalnum()
  140.  
  141. # LISTS
  142. # be able to use indices
  143. # be able 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.count(item)
  156. # myList.sort()
  157. # myList.reverse()
  158. # myList.copy()
  159. # myList.index(item)
  160.  
  161. # DICT
  162. # use the key like an index
  163. # myDict[key] # retrieve value for that key
  164. # myDict[key] = value # assign value to key
  165. # myDict.keys()
  166. # myDict.values()
  167. # dataList = [ " Agent Scully", "Queequeg", "Fred", " Dino", "Thor ", "Frog Thor"]
  168. # pets = {}
  169. # # pets[key.strip()] = value.strip()
  170. # for i in range(0, len(dataList), 2):
  171. #     pets[dataList[i].strip()] = dataList[i+1].strip()
  172. # print(pets)
  173.  
  174. # MODULES
  175. # math and csv
  176.  
  177. # MATH MODULE
  178. import math # full import
  179. # math.factorial(x)
  180. # math.ceil(x.yz)
  181. # math.floor(x.yz)
  182. # math.pow(x, y) # x**y
  183. # math.sqrt(x)
  184. # math.fabs(x) # abs(x)
  185. # math.pi
  186. # math.e
  187.  
  188. # # PARTIAL IMPORT
  189. # from math import factorial
  190. # from math import ceil, fabs
  191. # factorial(x) # not math.factorial(x)
  192. #
  193. # # ALIAS IMPORT
  194. # import math as m
  195. # m.floor(x.yz) # not math.floor(x.yz)
  196.  
  197. # FILES
  198. # READ MODE
  199. # with open("test.txt", "r") as f:
  200. #     contents = f.readlines() # like a list of strings of split()
  201. # print(contents)
  202. #
  203. # for line in contents:
  204. #     line = line.strip() # if I want to get rid of final \n
  205. #     print(line)
  206.  
  207.  
  208. # CSV MODULE
  209. import csv # csv.reader()
  210. with open("mock_data.csv", "r") as f1:
  211.     contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t")
  212. print(contents[0:20])
  213.  
  214. # WRITE MODE
  215. with open("output_data16.csv", "w") as f2:
  216.     for row in contents:
  217.         # email = row[3]
  218.         if row[3].endswith(".us"):
  219.             f2.write(",".join(row)+"\n")
  220.  
  221. # APPEND MODE
  222. with open("output_data16.csv", "a") as f3:
  223.  
  224.     # ['3', 'Enoch', 'Keady', 'ekeady2@hud.gov', 'Male', '216.15.244.138']
  225.     f3.write("1001, Barney, Fife, thelmasman@mayberry.us, Male, 157.108.244.162\n")
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement