Advertisement
jspill

webinar-python-exam-review-2022-07-30

Jul 30th, 2022
1,558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.09 KB | None | 0 0
  1. # Exam Review 2022 July 30
  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
  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 # [1, 2, 3]
  26. # dict # {"key": "value"}
  27. # tuple # (1, 2, 3) immutable, any series x, y, z -> interpreted as a tuple. return x,y -> return (x,y)
  28. # set # {1, 2, 3} # unordered, no repeats (all unique values)
  29.  
  30. # operators
  31. # = # assign a value
  32. # == # comparison, asking "are these equal?"
  33. # +
  34. # -
  35. # *
  36. # /
  37. # % # modulo, whole number remainder, "How many whole things are left over?"
  38. # // # floor division, x//y -> math.floor(x/y), for positive numbers same int(x/y)
  39. # ** # compare to math.pow()
  40. # += # increment, x += 1 is same as x = x + 1
  41. # -=
  42. # <
  43. # >
  44. # <=
  45. # >=
  46. # !=
  47. # # keywords that we use like operators
  48. # in # if __ in _someContainer_
  49. # not # if not __ in _someContainer__
  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...
  56. # LOOPS
  57. # WHILE # an IF an repeats
  58. # FOR # looping over a container... list, str, dict, tuple, set, others like range()
  59. # # for __ in __:
  60. # for item in myList: # list, set, tuple
  61. # for key in myDictionary: # key, k is a good name... use key to get val: myDict[key]
  62. # # for key, value in myDict.items()
  63. # for n in range(0, 5): # do this 5 times
  64. # for i in range(0, len(myList)) # use index numbers in looping over list, etc
  65.  
  66. # FUNCTIONS
  67. # defining/writing vs calling
  68. # parameters vs outside variable or "regular" variables
  69. # parameters vs arguments
  70. # a good function has ONE job, modular
  71. # methods are functions that "belong" to a type/class
  72.  
  73.  
  74. def someFunction(x, y):
  75.     return x + y
  76. # no other output outside of if name = main...
  77.  
  78. if "__name__" == "__main__":
  79.     myInput = int(input().strip())
  80.     # call our function
  81.     num = someFunction(myInput, someOtherNum)
  82.     print(num)
  83.  
  84. # See "tasks" in the last section of Ch 10, 11, 13, 14 for function writing practice
  85. # CodingBat also has good function-based Python questions:
  86. # https://codingbat.com/python
  87.  
  88. # BUILT-IN FUNCTIONS
  89. # print()
  90. # input()
  91. # len()
  92. # sum()
  93. # min()
  94. # max()
  95. # int()
  96. # float()
  97. # str()
  98. # list()
  99. # dict()
  100. # tuple()
  101. # set()
  102. # range()
  103. # round() # cousins math.ceil(), math.floor()
  104. # open() # IO/filestream: .read(), .readlines(), .write()
  105. # sorted() # does return the new list, compare to list.sort(), which doesn't
  106. # reversed() # does return the new list, compare to list.reverse(), which doesn't
  107.  
  108. # help()
  109. # dir()
  110. # # help(list)
  111. # # print(dir(str))
  112. # for item in dir(str):
  113. #     if not item.startswith("__"):
  114. #         print("{}()".format(item))
  115.  
  116. # STRINGS
  117. # be able to slice like it's second nature... myString[start:stop:step]
  118. # myString = "abcd"
  119. # myRevString = myString[::-1]
  120. # print(myRevString)
  121.  
  122. # KNOW YOUR WHITESPACE
  123. # # " " # ... and a lot of other Unicode spaces
  124. # "\n"
  125. # "\r"
  126. # "\b"
  127. # "\t"
  128. # "\f"
  129.  
  130. # # STRING METHODS
  131. # myString.format()
  132. # myString.strip() # myStrip.rstrip()
  133. # myString.split() # return a list of smaller strings
  134. # ",".join(someListOfStrings)
  135. # myString.replace(oldSubStr, newSubStr) # remove... myString.replace(oldSubStr, "")
  136. # myString.find(subStr) # compare of myString.index(subStr)
  137. # myString.count(subStr) # return int number of occurrences
  138. # # is/Boolean methods: isupper(), islower(), isdigit(), isnumeric(), isspace(), isalphanum()
  139. # # case changing: upper(), lower(), title()
  140.  
  141. # LISTS
  142. # be able to slice
  143.  
  144. # LIST METHODS
  145. # +
  146. # myList.append(item)
  147. # myList.insert(i, item)
  148. # myList.extend(anotherList)
  149. # # -
  150. # myList.pop() # myList.pop(i)
  151. # myList.remove(item) # pop by index, remove by value
  152. # # other
  153. # myList.count(item)
  154. # myList.sort()
  155. # myList.reverse()
  156. # # not as important
  157. # myList.index(item) # be careful, could be in there more than once
  158. # myList.copy()
  159. # myList.clear()
  160.  
  161. # # # use the key like an index
  162. # myDict[key] # retrieve the value for that key, compare to myDict.get()
  163. # myDict[key] = value # compare to myDict.update()
  164. # myDict.keys()
  165. # myDict.values()
  166. # myDict.items() # for k, v in myDict.items():
  167.  
  168. # MODULES
  169. # MATH MODULE
  170. # import math
  171. # math.factorial(x)
  172. # math.ceil(x.yz)
  173. # math.floor(x.yz)
  174. # math.pow(x, y)
  175. # math.sqrt(x)
  176. # math.fabs(x) # abs(x)
  177. # math.e
  178. # math.pi
  179.  
  180. # different import types
  181. # full import: math.factorial()
  182. # partial import:
  183. # from math import factorial
  184. # --> factorial()
  185. # from math import factorial, sqrt
  186. # from math import *
  187. # aliased imports
  188. # import math as m --> m.factorial()
  189.  
  190.  
  191. # READING files
  192. # with open("filename.txt", "r") as f:
  193. #     # when reading, I grab contents and get out of the open block
  194. #     contents = f.read() # whole file as one big string
  195. #     contents = f.readlines() # a list of line by line strings, WILL have \n
  196.  
  197.     import csv
  198.     contents = list(csv.reader(f)) # csv.reader(f, delimiter="\t")
  199.     # does one more step for us...
  200.     # ['1', 'Flossie', 'Levey', 'flevey0@jimdo.com', '129.134.226.30']
  201.     print(contents)
  202.  
  203. # WRITING to a file... you tend to stay in the with/open() block longer
  204. # below I'm writing out the original .csv's rows where the email ends in ".org"
  205. with open("mock_data2.csv", "w") as f2:
  206.     for line in contents:
  207.         if line[3].endswith(".org"):  # if line[3][-4:] == ".org"
  208.             # file write is NOT print(): no end or sep parameters and can't take multiple arguments
  209.             f2.write("{}\n".format(",".join(line))) # write() takes a single string argument
  210.             # ... don't forget that string usually needs to end in a line return
  211.  
  212.             # ... that being said you CAN also write out using print() using its file parameter
  213.             # print("{}\n".format(",".join(line)), file=f2)
  214.  
  215.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement