Advertisement
jspill

webinar-python-exam-review-2022-06-25

Jun 25th, 2022
1,101
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.36 KB | None
  1. # Exam Review 2022 June 25
  2.  
  3. # LABS
  4. # Ch 2-14... all Labs!
  5. # Ch 21-31 just ADDITIONAL LABS, but important practice!
  6. # Use Submit Mode!!!
  7.  
  8. # Watch your string input and output
  9. # # 1
  10. # myVar = input().strip() # myVar = input().rstrip()
  11. # # 2
  12. # print("some stuff", end=" ") # if you ever override end
  13. # print() # print(end="\n")
  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. # Data Types
  21. # str
  22. # int
  23. # float
  24. # Boolean
  25. # list
  26. # dict
  27. # set # all unique values, no order
  28. # tuple # immutable, any series x, y, z -> interpreted as a tuple, return x,y -> return (x,y)
  29.  
  30. # operators
  31. # = # assigns a value
  32. # == # equality, asking a question
  33. # +
  34. # -
  35. # *
  36. # /
  37. # % # modulo... whole number remainder, "How many whole things are left over?"
  38. # // # floor division... math.floor(x/y), int(x/y)
  39. # += # increment
  40. # -= # decrement
  41. # <
  42. # >
  43. # <=
  44. # >=
  45. # !=
  46. # ** # math.pow(), pow()
  47. # # # # keywords used like operators
  48. # in # if _value_ in _container_
  49. # not # if not _value_ in _container_
  50. # and
  51. # or # any one True makes the combined condition True... limit OR to 2 conditions
  52.  
  53. # Comp 2
  54. # Basic control structures
  55. # IF statements... if, if/else, if/elif, if/elif/else, etc
  56. # LOOPS
  57. # WHILE # an IF that repeats
  58. # FOR # looping over some container, or a known number of times... range()
  59. # # for __ in __:
  60. # for item in myList:
  61. # for n in range(0, 5):
  62. # for i in range(len(myList)):
  63. # for key in myDictionary:
  64.  
  65. # FUNCTIONS
  66. # defining/writing vs calling
  67. # parameters vs outside, or "regular" variables
  68. # parameters vs arguments
  69. # a good function has ONE job, modular
  70. # return vs print() vs... whatever, write a file # do what the questions asked!
  71. # methods are functions that "belong" to a type/class
  72.  
  73. # def someFunction(x, y):
  74. #     return x + y
  75. #
  76. # if "__name__" == "__main__": # am I running from this file I'm writing
  77. #     myInput = int(input().strip())
  78. #     # call our function
  79. #     num = someFunction(myInput, someOtherNum)
  80. #     print(num)
  81.  
  82. # See "tasks" in the last section of Ch 10, 11, 13, 14 for function writing practice
  83. # CodingBat also has good function-based Python questions:
  84. # https://codingbat.com/python
  85.  
  86. # don't do this
  87. # count = 0
  88. # def upCount():
  89. #     count += 1
  90.  
  91. # BUILT-IN FUNCTIONS
  92. # print()
  93. # input()
  94. # len()
  95. # sum()
  96. # min()
  97. # max()
  98. # range()
  99. # str()
  100. # list()
  101. # dict()
  102. # tuple()
  103. # set()
  104. # round() # cousins math.ceil(), math.floor()
  105. # open() # IO/file methods: .read(), .readlines(), .write()
  106. # sorted() # does return the new list, compare to list.sort(), which doesn't
  107. # reversed()# does return the new list, compare to list.reverse(), which doesn't
  108. #
  109. # help()
  110. # dir()
  111. # # help(list)
  112. # # print(dir(str))
  113. # for item in dir(str):
  114. #     if not item.startswith("__"):
  115. #         print("{}()".format(item))
  116.  
  117. # STRINGS
  118. # be able to slice like it's second nature... myString[start:stop:step]
  119. # myString = "abcd"
  120. # myRevString = myString[::-1]
  121. # print(myRevString)
  122.  
  123. # KNOW YOUR WHITESPACE
  124. # # " " # ... and a lot of other Unicode spaces
  125. # # "\n"
  126. # # "\r"
  127. # # "\b"
  128. # # "\t"
  129. # # "\f"
  130.  
  131. # # STRING METHODS
  132. # myString.format()
  133. # myString.strip() # myStrip.rstrip()
  134. # myString.split() # return a list of smaller strings
  135. # ",".join(someListOfStrings)
  136. # myString.replace(oldSubStr, newSubStr) # remove... myString.replace(oldSubStr, "")
  137. # myString.find(subStr) # compare of myString.index(subStr)
  138. # myString.count(subStr) # return int number of occurrences
  139. # # is/Boolean methods: isupper(), islower(), isdigit(), isnumeric(), isspace(), isalphanum()
  140. # # case changing: upper(), lower(), title()
  141.  
  142. # LISTS
  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() # myList.pop(i)
  152. # myList.remove(item) # pop by index, remove by value
  153. # # other
  154. # myList.count(item)
  155. # myList.sort()
  156. # myList.reverse()
  157. # # not as important
  158. # myList.index(item) # be careful, could be in there more than once
  159. # myList.copy()
  160. # myList.clear()
  161.  
  162. # DICT
  163. # # # use the key like an index
  164. # myDict[key] # retrieve the value for that key, compare to myDict.get()
  165. # myDict[key] = value # compare to myDict.update()
  166. # myDict.keys()
  167. # myDict.values()
  168. # myDict.items() # for k, v in myDict.items():
  169.  
  170. # SETS
  171. # mySet.add(item)
  172. # mySet.remove(item)
  173. # mySet.pop() # remove a random item
  174.  
  175. # MODULES
  176. # MATH MODULE
  177. # import math
  178. # math.factorial(x)
  179. # math.ceil(x.yz)
  180. # math.floor(x.yz)
  181. # math.pow(x, y)
  182. # math.sqrt(x)
  183. # math.fabs(x) # abs(x)
  184. # math.e
  185. # math.pi
  186.  
  187.  
  188. # different import types
  189. # full import: math.factorial()
  190. # partial import:
  191. # from math import factorial
  192. # --> factorial()
  193. # from math import factorial, sqrt
  194. # from math import *
  195. # aliased imports
  196. # import math as m --> m.factorial()
  197.  
  198.  
  199. # # OPENING FILES
  200. #  good practice in Ch 14 Task 4, 7, 8
  201.  
  202. # READING files
  203. # with open("filename.txt", "r") as f:
  204. #     # when reading, I grab contents and get out of the open block
  205. #     contents = f.read() # whole file as one big string
  206. #     contents = f.readlines() # a list of line by line strings, WILL have \n
  207.  
  208. # with open("test.txt", "r") as f:
  209. #     contents = f.readlines()
  210. # print(contents)
  211. # for line in contents:
  212. #     line = line.strip()
  213. #     print(line)
  214.  
  215. with open("mock_data.csv", "r") as f:
  216.     # contents = f.readlines() # ["1,Flossie,Levey...\n"...]
  217.     # or...
  218.  
  219.     import csv
  220.     contents = list(csv.reader(f)) # csv.reader(f, delimiter="\t")
  221.     # does one more step for us...
  222.     # ['1', 'Flossie', 'Levey', 'flevey0@jimdo.com', '129.134.226.30']
  223.     print(contents)
  224.  
  225. # WRITING to a file... you tend to stay in the with/open() block longer
  226. # below I'm writing out the original .csv's rows where the email ends in ".org"
  227. with open("mock_data2.csv", "w") as f2:
  228.     for line in contents:
  229.         if line[3].endswith(".org"):  # if line[3][-4] == ".org"
  230.             # file write is NOT print(): no end or sep parameters and can't take multiple arguments
  231.             f2.write("{}\n".format(",".join(line))) # write() takes a single string argument
  232.             # ... don't forget that string usually needs to end in a line return
  233.  
  234.             # ... that being said you CAN also write out using print() using its file parameter
  235.             # print("{}\n".format(",".join(line)), file=f2)
  236. # the end!
Advertisement
RAW Paste Data Copied
Advertisement