Advertisement
jspill

webinar-exam-review-2022-12-17

Dec 17th, 2022
870
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.28 KB | None | 0 0
  1. # Exam Review 2022 Dec 17
  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. # # if you ever override end...
  13. # print("some stuff", end=" ") # set it back right...
  14. # print() # print(end="\n")
  15. #
  16. # print("Clean new line!")
  17.  
  18. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  19. # Comp 2: Control Flow
  20. # Comp 3: Modules and Files
  21.  
  22. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  23. # Common Data Types
  24. # int
  25. # float
  26. # str # ""
  27. # list # [ ]
  28. # tuple # () immutable, Python sees any x,y,z as (x,y,z)... return x,y -> return (x,y)
  29. # set # {} all unique values (no duplication), no order (no index, no slice, no sort...)
  30. # dict # {k: v}
  31. # type() # -> type object... __name__
  32. # range() # -> range object
  33.  
  34. # operators
  35. # = # assignment
  36. # == # equality... asking, comparing... part of a 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 x += 1 --> x = x + 1
  48. # -= # decrement
  49. # != # asking if they are no equal
  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 n in range(0, 5): # [0, 1, 2, 3, 4]
  66. # for i in range(0, len(myList)): # i = index, item = myList[i]
  67. # for key in myDict: # key is key, value is myDict[key]
  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() # returns a str
  93. # range()
  94. # list()
  95. # int()
  96. # float()
  97. # set()
  98. # tuple()
  99. # dict()
  100. # type() # print(type(3.14).__name__)
  101. # len()
  102. # max()
  103. # min()
  104. # sum()
  105. # round() # buts its cousins math.ceil() and math.floor() are in the math module
  106. # sorted() # returns sorted list... compare list.sort() does not return anything
  107. # reversed() # returns reversed list... compare list.reverse() does not return anything
  108. # open() # IO/file --> .read(), .readlines(), .write()
  109. # help() # help(str) # help(str.isspace) # help(dict.keys)
  110. # dir() # print(dir(str)), etc
  111.  
  112. # STRINGS
  113. # be able to slice like it's 2nd nature: myString[start:stop:step]
  114. # myStr = "abcd"
  115. # revStr = myStr[::-1]
  116. # print(revStr)
  117.  
  118. # KNOW YOUR WHITESPACE
  119. # " " #... and many Unicode spaces
  120. # "\n"
  121. # "\r"
  122. # "\t"
  123.  
  124. # STRING METHODS
  125. # "stuff I want to put together {}".format(var) # or similar f strings
  126. # myStr.strip()
  127. # myStr.split() # returns a list of smaller strings
  128. # ",".join(listOfStrings)
  129. # myStr.replace(oldSubStr, newSubStr) # remove... myStr.replace(oldSubStr, "")
  130. # myStr.find(subStr) # return index, sim myStr.index(subStr)
  131. # myStr.count(subStr) # return int count of number occurrences
  132. # case: myStr.lower(), myStr.upper(), myStr.title(), myStr.capitalize()
  133. # is/Boolean: isupper(), islower(), isalpha(), isdigit(), isalnum(), isspace()
  134.  
  135. # myStr = "abc"
  136. # upperStr = myStr.upper()
  137. # print(upperStr)
  138.  
  139. # LISTS
  140. # again know indices and be able to slice
  141.  
  142. myList = [1, 2, 3, 1, 2]
  143. # LIST METHODS
  144. # +
  145. # myList.append(item)
  146. # myList.insert(i, item)
  147. # myList.extend(anotherList)
  148. # # -
  149. # myList.pop() # myList.pop(i)... pop() by index
  150. # myList.remove(item) # remove by value
  151. # # other...
  152. # myList.count(item) # return int number of occurences
  153. # myList.sort()
  154. # myList.reverse()
  155. # # not as important
  156. # myList.clear()
  157. # myList.copy()
  158. # myList.index(item)
  159.  
  160. # DICT
  161. # use the key like an index
  162. # myDict[key] # retrieve the value for that key, so like get()
  163. # myDict[key] = value # # assign (new) value for that key, so like update({k:v})
  164. # myDict.keys()
  165. # myDict.values()
  166.  
  167. # MODULES
  168. # math and csv
  169.  
  170. # MATH MODULE
  171. import math # <-- that's a FULL IMPORT
  172. # math.factorial(x)
  173. # math.ceil(x.yz)
  174. # math.floor(x.zy)
  175. # math.pow(x, y) # similar to **, not be confused with math.exp()
  176. # math.sqrt(x)
  177. # math.fabs() # similar to built-in abs()
  178. # math.e
  179. # math.pi
  180.  
  181. # PARTIAL IMPORT
  182. # from math import factorial
  183. # # don't say math.factorial()... we didn't import math
  184. # factorial()
  185. # from math import * # still a partial import... factorial(), floor()
  186. #
  187. # # ALIAS IMPORT
  188. # import math as m
  189. # m.floor() # etc
  190.  
  191. # FILES!!!
  192. # READ MODE
  193. # with open("test.txt", "r") as f:
  194. #     contents = f.readlines() # list of strings, line by line
  195. # print(contents)
  196. # for line in contents:
  197. #     line = line.strip()
  198. #     print(line)
  199.  
  200. # CSV MODULE
  201. import csv
  202. with open("mock_data.csv", "r") as f1:
  203.     contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t")
  204. # print(contents)
  205.  
  206. with open("output_data9.csv", "w") as f2:
  207.     for line in contents:
  208.         if len(line[1])  > 10: # if line[3].endswith(".org"): #line[3][-4:] == ".org"
  209.             # write() method takes one single str argument
  210.             f2.write(",".join(line)+"\n")
  211.  
  212. # APPEND MODE
  213. # with open("append_to_this.txt", "r") as f3:
  214. #     contents = f3.readlines()
  215. # print(contents)
  216. with open("append_to_this.txt", "a") as f4:
  217.     f4.write("Pippin\n")
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  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.  
  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.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement