Advertisement
jspill

webinar-python-exam-review-2022-07-23

Jul 23rd, 2022
1,307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.85 KB | None | 0 0
  1. # Exam Review 2022 July 23
  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 Type
  21. # int
  22. # float
  23. # Boolean
  24. # str
  25. # list
  26. # dictionary
  27. # sets # unordered and no duplicate values
  28. # tuples # IMMUTABLE, any comma separated series x, y, z... return x, y same as return (x, y)
  29.  
  30. # Modules
  31. # math
  32. # csv
  33. # random # not needed but ... random.choice(), random.randint()/random.randrange()
  34.  
  35. # OPERATORS
  36. # = # assigns
  37. # == ## comparison, asking "are these equal?"
  38. # +
  39. # -
  40. # *
  41. # /
  42. # // # floor division... math.floor(x/y), for positive numbers int(x/y)
  43. # % # modulo... whole number remainder, "how many didn't fit?"
  44. # ** # raises to a power, similar to math.pow()
  45. # <
  46. # >
  47. # <=
  48. # >=
  49. # += # increment x += 1 same as x = x + 1
  50. # -= # decrement
  51. # !=
  52. # # # # # keywords used like operators
  53. # not # if not _value_ in _container_
  54. # in # if _value_ in _container_
  55. # and
  56. # or # any one True makes the combined condition True.. limit OR to 2 conditions
  57.  
  58. # Comp 2
  59. # Basic control structures
  60. # IF statements... if, if/elif, if/else, if/elif/else, etc
  61. # LOOPS
  62. # WHILE # an IF that repeats
  63. # FOR # looping over a container
  64. # for __ in __:
  65. # for item in myList:
  66. # for key in myDictionary: # or k... value for a key myDictionary[key]
  67. # for num in range(0, 5): # or range(5) # args are just like slice: start, stop, step
  68. # for i in range(len(myList)): # when you're interested in the index of a value, myList[i]
  69.  
  70. # FUNCTIONS
  71. # defining/writing vs calling
  72. # parameters vs outside, or "regular" variables
  73. # parameters vs arguments
  74. # a good function has ONE job, modular
  75. # return vs print() vs... whatever, write a file # do what the questions asked!
  76.  
  77. # def someFunction(x, y):
  78. #     return x + y
  79. # print(someFunction(1, 2)) # see the function printed? Then the function should return
  80. # # vs...
  81. # someFunction(3,4) # function called directly for output? Then it must print() instead
  82. #
  83. # # that if main thing...
  84. # def someFunctionWithIfName(x, y):
  85. #     return x + y
  86. #
  87. # if "__name__" == "__main__": # am I running directly from the file I'm writing?
  88. #     myInput = int(input().strip())
  89. #     # call our function
  90. #     # num = someFunctionWithIfName(myInput, someOtherNum)
  91. #     # print(num)
  92.  
  93. # BUILT-IN FUNCTIONS
  94. # print()
  95. # input()
  96. # len()
  97. # sum()
  98. # min()
  99. # max()
  100. # type()
  101. # # construct or recast a type
  102. # int()
  103. # float()
  104. # list()
  105. # tuple()
  106. # set()
  107. # dict()
  108. # str()
  109. # abs()
  110. # enumerate()
  111. # range()
  112. # round() # regular round, cousins math.ceil() and math.floor()
  113. # sorted() # returns new list, sorted
  114. # reversed() # returns new list, reversed
  115. # open() # IO/filestream methods: read(), readlines(), write()
  116. # help()
  117. # dir()
  118. #
  119. # # help() and dir()
  120. # # help(str)
  121. # # dir() returns a list of class "attributes"
  122. # # print(dir(str)) # can help me zero in...
  123. # # help(str.split)
  124. #
  125. # print(dir(dict))
  126.  
  127. # STRINGS
  128. # be able to slice like it's second nature... myString[start:stop:step]
  129. # myString = "abcd"
  130. # myRevString = myString[::-1]
  131. # print(myRevString)
  132.  
  133. # # KNOW YOUR WHITESPACE
  134. # # " " # ... and a lot of other Unicode spaces
  135. # "\n"
  136. # "\r"
  137. # "\b"
  138. # "\t"
  139. # "\f"
  140.  
  141. # STRING METHODS
  142. # myString.format()
  143. # myString.strip() # myString.lstrip(), myString.rstrip()
  144. # myString.split() # returns a list of smaller strings
  145. # ",".join(listOfStrings)
  146. # myString.replace(oldSubStr, newSubStr) # yString.replace(oldSubStr, "")
  147. # myString.find(subStr) # return the index, or -1
  148. # myString.count(subStr) #return int count
  149. # # # is/Boolean methods: isupper(), islower(), isdigit(), isnumeric(), isspace(), isalphanum()
  150. # # case changing: upper(), lower(), title()
  151.  
  152. # LISTS
  153. # be able is slice
  154.  
  155. # LIST METHODS
  156. # +
  157. # myList.append(item)
  158. # myList.insert(i, item)
  159. # myList.extend(anotherList)
  160. # # -
  161. # myList.pop() # myList.pop(i)
  162. # myList.remove(item) #  pop by index, remove by value
  163. # # other
  164. # myList.count(item)
  165. # myList.sort()
  166. # myList.reverse()
  167. # # not as imp
  168. # myList.index(item) # be careful, could be in there more than once
  169. # myList.copy()
  170. # myList.clear()
  171.  
  172. # DICT
  173. # # # use the key like an index
  174. # myDict[key] # retrieve the value for that key, compare to myDict.get()
  175. # myDict[key] = value # compare to myDict.update()
  176. # myDict.keys()
  177. # myDict.values()
  178. # myDict.items() # # for k, v in myDict.items():
  179.  
  180. # MODULES
  181. # MATH MODULE
  182. # import math
  183. # math.factorial(x)
  184. # math.ceil(x.yz)
  185. # math.floor(x.yz)
  186. # math.pow(x, y)
  187. # math.sqrt(x)
  188.  
  189. # different import types
  190. # full import: math.factorial()
  191. # partial import:
  192. # from math import factorial
  193. # --> factorial()
  194. # from math import factorial, sqrt
  195. # from math import *
  196. # aliased imports
  197. # import math as m --> m.factorial()
  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("mock_data.csv", "r") as f:
  209. #     contents = f.readlines()
  210. # print(contents)
  211.  
  212. with open("mock_data.csv", "r") as f:
  213.     import csv
  214.     contents = list(csv.reader(f))
  215. print(contents)
  216.  
  217. # WRITING to a file... you tend to stay in the with/open() block longer
  218. # below I'm writing out the original .csv's rows where the email ends in ".org"
  219. with open("mock_data2.csv", "w") as f2:
  220.     for line in contents:
  221.         if line[3].endswith(".org"):
  222.             f2.write("{}\n".format(",".join(line)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement