Advertisement
jspill

webinar-exam-review-2023-01-21

Jan 21st, 2023
1,182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.02 KB | None | 0 0
  1. # Exam Review 2023 Jan 21
  2.  
  3. # LABS
  4. # Ch 2-14... all Labs!
  5. # Ch 21-34 just ADDITIONAL LABS, but important practice!
  6. # Use Submit Mode!!!
  7.  
  8. # Watch your string input and output
  9. # 1
  10. # myVar = input().strip()
  11. # 2
  12. # print() # print(end="\n")
  13. # # if you ever override end...
  14. # print("some stuff", end=" ") # set it back to a clean new line
  15. # print() # wrap up with it!
  16.  
  17. # print("Clean new line!")
  18.  
  19. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  20. # Comp 2: Control Flow
  21. # Comp 3: Modules and Files
  22.  
  23. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  24. # Common Data Types
  25. # int
  26. # float
  27. # str # ""
  28. # list # []
  29. # dict # {k:v}
  30. # tuple # () immutable, Python sees any x,y,z as (x,y,z)... return x,y -> return (x,y)
  31. # set # {} all unique / no duplication, no order (no index, no slice, no sort...)
  32. # range() # --> sequenced container of a series of number
  33.  
  34. # operators
  35. # = # assignment
  36. # == # equality... asking, comparing... condition
  37. # +
  38. # -
  39. # *
  40. # /
  41. # % # modulo... int remainder... "how many whole things left over?"
  42. # // # floor division... x//y -> math.floor(x/y)
  43. # >
  44. # <
  45. # >=
  46. # <=
  47. # += # increment
  48. # -= # decrement
  49. # != # not equal... asking/comparing
  50. # ** # raise to a power... pow() or math.pow()
  51. # # keywords used like operators
  52. # in # if _someValue_ in _someContainer_
  53. # not # if not _someValue_ in _someContainer_
  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 item in _container_:
  64. # for item in myList:
  65. # for key in myDict:
  66. # for num in range(0, 5): # [0, 1, 2, 3, 4]
  67. # for i in range(0, len(myList)):
  68.  
  69. # FUNCTIONS
  70. # defining/writing vs calling
  71. # parameters are special "variables"... they don't work like regular variables
  72. # parameters vs arguments
  73. # a function has ONE particular job
  74. # return vs print()... vs write a file... whatever the question says
  75. # method are functions that belong to a particular class/type
  76.  
  77. # def someFunction(x, y):
  78. #     return x + y
  79. #
  80. # if __name__ == "__main__":  # are we running from this very script I'm writing?
  81. #     myInput = int(input())
  82. #     myOther = int(input())
  83. #     num = someFunction(myInput, myOther)
  84. #     print(num)
  85.  
  86. # See "tasks" in the last section of Ch 10, 11, 13, 14 for function writing practice
  87. # # CodingBat also has good function-based Python questions:
  88. # # https://codingbat.com/python
  89.  
  90. # BUILT-IN FUNCTIONS
  91. # print()
  92. # input()
  93. # range()
  94. # list()
  95. # int()
  96. # float()
  97. # set()
  98. # tuple()
  99. # dict()
  100. # str()
  101. # len()
  102. # min()
  103. # max()
  104. # sum()
  105. # pow()
  106. # abs()
  107. # bool()
  108. # round() # buts its cousins math.ceil() and math.floor() are in the math module
  109. # chr()
  110. # ord()
  111. # open() # for IO/files --> .read(), .readlines(), .write()
  112. # sorted() # returns sorted list... compare list.sort() does not return anything
  113. # reversed() # same as above
  114. # enumerate()
  115. # type() # type objects have a .__name__ property
  116. # help() #help(str)
  117. # dir()
  118.  
  119. # print(dir(str))
  120. # help(str.isspace)
  121.  
  122. # STRINGS
  123. # be able to slice like it's 2nd nature: myString[start:stop:step]
  124. # myStr = "abcde"
  125. # revStr = myStr[::-1]
  126. # print(revStr)
  127.  
  128. # KNOW YOUR WHITESPACE
  129. # " "... and many Unicode spaces
  130. # "\n"
  131. # "\r"
  132. # "\t"
  133.  
  134. # STRING METHODS
  135. # "stuff I want to put together {}".format(var) # or similar f strings
  136. # myStr.strip()
  137. # myStr.split() # returns a list of smaller strings
  138. # ",".join(listOfStrings)
  139. # myStr.replace(oldStr, newStr) # remove... myStr = myStr(oldStr, "")
  140. # myStr.find(subStr) # return the index where substring starts, or -1... myStr.index(subStr) would error instead
  141. # myStr.count(subStr)
  142. # case: myStr.lower(), myStr.upper(), myStr.title(), myStr.capitalize()
  143. # is/Boolean: isupper(), islower(), isspace(), isdigit()... return Boolean on whether the whole str is that
  144.  
  145. # LISTS
  146. # again be able to slice
  147.  
  148. myList = [1, 2, 3, 1, 2]
  149. # LIST METHODS
  150. # +
  151. # myList.append(item)
  152. # myStr.insert(i, item)
  153. # myStr.extend(anotherList)
  154. # # -
  155. # myList.pop() # myList.pop(i)
  156. # myList.remove(item) # ... pop() by index, remove by value
  157. # # other
  158. # myList.count(item)
  159. # myList.sort()
  160. # myList.reverse()
  161. # myList.index(item)
  162. # myList.copy()
  163. # myList.clear()
  164.  
  165. # DICT
  166. # use the key like an index
  167. # myDict[key] # retrieve the value for that key
  168. # myDict[key] = value # assign a (new) value to that key
  169. # myDict.keys()
  170. # myDict.values()
  171. # myDict.items()
  172.  
  173. # MODULES
  174. # math and csv
  175.  
  176. # MATH MODULES
  177. # import math # <-- that's a FULL IMPORT
  178. # math.factorial(x)
  179. # math.ceil(x.yz)
  180. # math.floor(x.yz)
  181. # math.pow(x, y) # not to be confused with math.exp()
  182. # math.sqrt(x)
  183. # math.fabs() # compare to abs()
  184. # math.e
  185. # math.pi
  186. #
  187. # # PARTIAL IMPORT
  188. # from math import factorial
  189. # # don't say math.factorial()... we didn't import math
  190. # factorial()
  191.  
  192. # ALIAS IMPORT
  193. # import math as m
  194. # import math as m
  195. # m.floor() # etc
  196.  
  197. # FILES!!!
  198. # READ MODE
  199. with open("test.txt", "r") as f:
  200.     contents = f.readlines() # returns a list of strings line by line
  201. print(contents) # it looks like:
  202. # [
  203. #     'Hello.\n', # note new line returns on each line
  204. #     'This\n',
  205. #     'is\n' ...
  206. #     etc ...
  207. # ]
  208.  
  209. # for line in contents:
  210. #     line = line.strip()
  211. #     print(line)
  212.  
  213.  
  214. # CSV MODULE
  215. import csv
  216. with open("mock_data.csv", "r") as f1:
  217.     contents = list(csv.reader(f1)) # creates a list of lists, with each comma-separated value being a string in the inner lists
  218.     # if tsv... csv.reader(f1, delimiter="\t")
  219. # print(contents) # it looks like:
  220. # [
  221. #     ['id', 'first_name', 'last_name', 'email', 'gender', 'ip_address'],
  222. #     ['1', 'Remington', 'Shilling', 'rshilling0@wsj.com', 'Male', '1.71.141.52'],
  223. #     ['2', 'Milty', 'Chittey', 'mchittey1@china.com.cn', 'Male', '85.14.30.8'],
  224. #     ['3', 'Enoch', 'Keady', 'ekeady2@hud.gov', 'Male', '216.15.244.138'],
  225. #     ['4', 'Baldwin', 'McCaskill', 'bmccaskill3@icio.us', 'Male', '151.51.0.216'],
  226. #     ['5', 'Carling', 'Baude', 'cbaude4@liveinternet.ru', 'Male', '91.198.36.54']...
  227. #     etc ...
  228. # ]
  229.  
  230. # WRITE MODE
  231. # writes over file (if it already exists) destructively
  232. with open("output_data10.csv", "w") as f2:
  233.     for line in contents:
  234.         # look for emails ending in .gov to write out
  235.         # email = line[3]
  236.         # if email[-4:]
  237.         if line[3][-4:] == ".gov": # or line[3].endswith(".gov"):
  238.             # write() method takes one single str argument
  239.             f2.write(",".join(line)+"\n")
  240.  
  241. # APPEND MODE
  242. # writes onto the end of a file, non-destructively
  243. # with open("append_to_this.txt", "r") as f3:
  244. #     contents = f3.readlines()
  245. # print(contents)
  246. # [
  247. #     "Frodo\n",
  248. #     "Sam\n",
  249. #     "Merry\n" # btw, always worth seeing if this last line has a new line return or not
  250. # ]
  251. with open("append_to_this.txt", "a") as f3:
  252.     f3.write("Pippin\n")
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement