Advertisement
jspill

webinar-python-exam-review-2023-09-23

Sep 23rd, 2023
1,428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.03 KB | None | 0 0
  1. # Exam Review 2023 Sept 23
  2.  
  3. # Do those LABS
  4. # Ch 2-14... all Labs!
  5. # Ch 21-32 just ADDITIONAL LABS, but important practice!
  6. # Prac Tests, Ch 33 and 34
  7. # Use Submit Mode and get them to 100%!!! And PAY ATTENTION to the unit tests!
  8.  
  9. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  10. # Comp 2: Control Flow
  11. # Comp 3: Modules and Files
  12.  
  13. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  14. # Common Data Types/Classes
  15. # int
  16. # float
  17. # bool # True, False
  18. # str # "" most important type?
  19. # list # [ ]
  20. # dict # {key:value}
  21. # tuple # ( ) immutable, Python sees any a,b,c as (a,b,c)... return x,y --> return (x,y)
  22. # set # { } all unique/ no duplicate values, no order... no indices, no slicing, no sorting
  23. # range # range()... range(0, 4) --> [0, 1, 2, 3]
  24.  
  25. # Operators
  26. # = # assigns
  27. # == # asking if they're equal... a conditional expression
  28. # +
  29. # -
  30. # *
  31. # /
  32. # % # modulo (remainder... whole number)... how many whole things didn't fit since the last even division?
  33. # // # floor division... the last even division
  34. # <
  35. # > # x > 5... print(x>5)
  36. # <=
  37. # >=
  38. # += # x = x+1
  39. # -=
  40. # ** # raise to a power... math.pow(x, y)... or built-in pow(x, y)
  41. # != # not equals... not x == y
  42. # # keywords
  43. # in # if x in myList (with a dictionary in only looks at the keys)
  44. # not # if not x in myList
  45. # and
  46. # or # any one True make the combo True... limit OR to 2-3 conditions
  47.  
  48. # Comp 2
  49. # Control Flow! The HOW stuff
  50. # IF statements... if, if/else, if/elif/else,
  51. # LOOPS
  52. # WHILE - an IF that repeats
  53. # FOR - looping over a container, or a known number of times... hence range()
  54. # for ___ in _someContainer_:
  55. # for item in myList:
  56. # for char in myStr:
  57. # for key in myDict:  # getting the value... myDict[key]
  58. # for num in range(0, 8):
  59. # for i in range(len(myList)): # myList[i]
  60. # for i, item in enumerate(myList):
  61.  
  62. # FUNCTIONS
  63. # defining/writing vs calling
  64. # a function has ONE particular job
  65. # parameters (vs arguments)
  66.  
  67. # def someFunction(x, y):
  68. #     return x+y
  69. # # print("hey") # will cause failure
  70. # if __name__ == "__main__": # is this the script that was run?
  71. #     myInput = int(input())
  72. #     myOther = int(input())
  73. #     myNum = someFunction(myInput, myOther)
  74. #     print(myNum)
  75. #
  76.  
  77. # See "tasks" in the last section of Ch 10, 11, 13, 14 for function writing practice
  78. # # CodingBat also has good function-based Python questions:
  79. # https://codingbat.com/python
  80.  
  81. # BUILT-IN FUNCTIONS
  82. # input()
  83. # print()
  84. # range()
  85. # list()
  86. # set()
  87. # tuple()
  88. # dict()
  89. # str()
  90. # int()
  91. # float()
  92. # min()
  93. # max()
  94. # sum()
  95. # len()
  96. # enumerate()
  97. # type() # type(x).__name__
  98. # round() # cousins math.floor() and math.ceil()
  99. # open() # create a file/IO object
  100.  
  101. # help() # help(str) vs help(str.isspace)
  102. # dir() # print(dir(str)), print(dir(list))
  103.  
  104. # STRINGS
  105. # be able to refer by index, and slice
  106. # myStr = "xyz"
  107. # revStr = myStr[::-1] # mySlice[start:stop:step]
  108. # print(revStr)
  109.  
  110. # KNOW YOUR WHITESPACE
  111. # " " # space from spacebar
  112. # # a lot of other spaces in Unicode
  113. # \n # new line return
  114. # \t # tab
  115. # \r # carriage return
  116.  
  117. # see Ch 2.9 expectation printed output ends with a new line return, unless otherwise stated
  118. print() # print(end="\n")
  119.  
  120. # STRING METHODS
  121. # myStr.format() # "stuff I want to put together {}".format(var)
  122. # myStr.strip()
  123. # myStr.split() # returns a list of smaller strings
  124. # " ".join(listOfStrings)
  125. # myStr.replace(subStr, newStr) # "remove" myStr = myStr.replace(subStr, "")
  126. # myStr.find(subStr) # return index where found, similar myStr.index()
  127. # myStr.count(subStr) # return int count of occurrences
  128. # case: myStr.lower(), myStr.upper(), myStr.title(), myStr.capitalize()
  129. # is/Boolean: myStr.islower(), myStr.isupper(), myStr.isspace(), .isalpha(), .isalnum(), .isnumeric(), .isdigit()
  130. # myStr.startswith(subStr), myStr.endswith(subStr)
  131.  
  132. # LISTS
  133. # be able to slice, refer by index
  134.  
  135. # LIST METHODS
  136. # # +
  137. # myList.append(item)
  138. # myList.insert(i, item)
  139. # myList.extend(anotherList)
  140. # # -
  141. # myList.pop(i) # or last item
  142. # myList.remove(item) # pop by index, remove by value
  143. # myList.clear()
  144. # # others
  145. # myList.count(item)
  146. # myList.sort()  # does not in return a value!
  147. # myList.reverse() # does not in return a value!
  148. # myList.copy()
  149. # myList.index(item)
  150.  
  151. # DICT
  152. # use the key like an index []... then you don't need DICT methods
  153. # myDict[key] # retrieve the value for that key
  154. # myDict[key] = value # assign a new value to that key
  155. #
  156. # # DICT METHODS
  157. # myDict.keys()
  158. # myDict.values()
  159. # myDict.items() # for key, value in myDict.items()
  160.  
  161. # MODULES
  162. # MATH MODULE
  163. # import math # FULL IMPORT
  164. # math.factorial(x)
  165. # math.ceil(x.yz)
  166. # math.floor(x.yz)
  167. # math.pow(x, y) # not math.exp()
  168. # math.sqrt(x)
  169. # math.fabs(x) # built-in abs()
  170. # math.pi
  171. # math.e
  172.  
  173. # PARTIAL IMPORT
  174. from math import ceil # ceil(x.yz)
  175. from math import factorial, floor # factorial(x), floor(x), sqrt(x)
  176. from math import * # ceil(y.z)
  177.  
  178. # ALIAS IMPORT
  179. import math as m # m.floor(x.yz)
  180.  
  181. # FILES
  182. # READ MODE
  183. with open("test.txt", "r") as f:
  184.     # file objects have file methods
  185.     # f.read() # returns whole files as one str
  186.     # f.readlines() # returns a list of strings, line by line
  187.     # f.write() # writes to the file, in write or append modes
  188.     contents = f.readlines()
  189. # print(contents)
  190.  
  191. # CSV Module
  192. import csv
  193. with open("mock_data.csv", "r") as f1: # mockaroo.com
  194.     contents = list(csv.reader(f1)) # contents = list(csv.reader(f1, delimiter="\t"))
  195. # print(contents)
  196.  
  197.  
  198. # WRITE MODE
  199. with open("output_data26.csv", "w") as f2:
  200.     for row in contents:
  201.         # only write line to new file if last name begins with P
  202.         # last name is pos 2 of each inner list, which is row
  203.         if row[2][0] == "P":  # if row[2].startswith("P"):
  204.             # one single str argument
  205.             f2.write(",".join(row) + "\n")
  206.  
  207. # APPEND MODE
  208. # with open("append_to_this.txt", "r") as f3:
  209. #     print(f3.readlines())
  210. with open("append_to_this.txt", "a") as f3:
  211.     f3.write("Pippin\n") # take note of whether last line of provided file has a \n, and adjust as needed!
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement