Advertisement
jspill

webinar-exam-review-2023-04-29

Apr 29th, 2023
857
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.25 KB | None | 0 0
  1. # Exam Review 2023 Apr 29
  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%!!!
  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. # Watch your string input and output
  14. # input...
  15. # myInput = input().strip()
  16. # output/print()
  17. # print() is the same print(end="\n")
  18. # print("Something I'm printing", end=" ")
  19. # print()
  20. # print("Clean new line!")
  21.  
  22. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  23. # int
  24. # float
  25. # bool # True, False
  26. # str # ""
  27. # list # [ ]
  28. # dict # {key:value}
  29. # tuple # () immutable, Python sees a,b,c as (a,b,c)... return x,y -> return (x,y)
  30. # set # all unique/no duplicates, no order... no index, no slicing, no sorting
  31. # range # range() --> range object # range(5), range(0, 5) --> [0, 1, 2, 3, 4]
  32.  
  33. # operators
  34. # = # assigns
  35. # == # equality... asking a question, part of a condition
  36. # +
  37. # -
  38. # *
  39. # /
  40. # % # modulo... gives the int remainder, "how many whole things didn't fit?"... since the last even division
  41. # // # floor division ... the last even division
  42. # <
  43. # >
  44. # <=
  45. # >=
  46. # += # x += 1 is x = x + 1
  47. # -=
  48. # ** # raising to a power... pow() and math.pow()
  49. # != # not equal
  50. # # keywords used like operators
  51. # in # if x in myList:
  52. # not # if not x in myList:
  53. # and
  54. # or # any one True means whole condition is True... limit OR to 2 condition
  55.  
  56. # Comp 2
  57. # the HOW stuff... control flow structures
  58. # IF statements... if, if/else, if/elif, if/elif/etc...
  59. # LOOPS
  60. # WHILE - an IF that repeats
  61. # FOR -  looping over a container, or a known number of times # hence... range()
  62. # for ___ in _container_:
  63. # for item in myList:
  64. # for char in myStr:
  65. # for key in myDict: # the value for that key is myDict[key]
  66. # for n in range(0, 12):
  67. # for i in range(0, len(myList)): # for i, item in enumerate(myList):
  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()/output... vs other... do whatever the question says
  75.  
  76. # def someFunction(x, y):
  77. #     return x // y
  78. #
  79. # if __name__ == "__main__":
  80. #     myInput = int(input())
  81. #     myOther = int(input())
  82. #     myNum = someFunction(myInput, myOther)
  83. #     print(myNum)
  84. # print(__name__) # "__main__" because this is the script that's running
  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. # input()
  92. # print()
  93. # range()
  94. # len()
  95. # min()
  96. # max()
  97. # sum()
  98. # int()
  99. # float()
  100. # list()
  101. # str()
  102. # dict()
  103. # tuple()
  104. # set()
  105. # enumerate()
  106. # round() # cousins math.ceil() and math.floor() are in math module
  107. # open() # open to a file, create a file object
  108. # type() # print(type(x)) # <class 'int'>, print(type(x).__name__) # int
  109. # help() # # help(str)
  110. # dir() # print(dir(list))
  111.  
  112. # STRINGS
  113. # be able to slice strings like it's second nature
  114. # [start:stop:step]
  115. # myStr = "abcdef"
  116. # revStr = myStr[::-1]
  117. # print(revStr) # fedcba
  118.  
  119. # KNOW YOUR WHITESPACE
  120. # # " "
  121. # # many Unicode spaces
  122. # "\n" # NEW line return
  123. # "\t" # tab
  124. # "\r" # carriage return
  125.  
  126. # STRING METHODS
  127. # myStr.format()  # "stuff I want to put together {}".format(var)... compare to similar to f strings
  128. # myStr.strip() # lstrip(), rstrip()
  129. # myStr.split() # returns a list of smaller strings
  130. # myStr.join() # " ".join(listOfStrings)
  131. # myStr.replace(subStr, newStr) # "remove"... myStr = myStr.replace(subStr, "")
  132. # myStr.find(subStr) # return int index of where found, or -1 if not there
  133. # myStr.count(subStr) # return int count of occurrences
  134. # case: myStr.lower(), myStr.upper()...myStr.title(), myStr.capitalize()
  135. # is/Boolean: myStr.isupper(), myStr.islower(), myStr.isspace(), myStr.isalpha(), myStr.isdigit(), myStr.isnumeric(), myStr.isalnum()
  136.  
  137. # LISTS
  138. # be able to use indices
  139. # be able to slice
  140.  
  141. # LIST METHODS
  142. # # +
  143. # myList.append(item)
  144. # myList.insert(i, item)
  145. # myList.extend(anotherList)
  146. # # -
  147. # myList.pop(i) # pop by index...
  148. # myList.remove(item) # remove by value
  149. # myList.clear()
  150. # # other
  151. # myList.count(item)
  152. # myList.sort()
  153. # myList.reverse()
  154. # myList.copy()
  155. # myList.index(item)
  156.  
  157. # DICT
  158. # use the key like an index
  159. # myDict[key] # retrieve value for that key
  160. # myDict[key] = value # assign value to key
  161. # myDict.keys()
  162. # myDict.values()
  163. # dataList = [ " Agent Scully", "Queequeg", "Fred", " Dino", "Thor ", "Frog Thor"]
  164. # pets = {}
  165. # for i in range(0, len(dataList), 2):
  166. #     # pets[key] = value
  167. #     pets[dataList[i].strip()] = dataList[i+1].strip()
  168. # print(pets)
  169.  
  170. # MODULES
  171. # math and csv
  172.  
  173. # MATH MODULE
  174. # import math # FULL IMPORT
  175. # math.factorial(x)
  176. # math.ceil(x.yz)
  177. # math.floor(x.yz)
  178. # math.pow(x, y) # not to be confused with math.exp()
  179. # math.sqrt(x)
  180. # math.fabs(x) # abs(x)
  181. # math.pi
  182. # math.e
  183. #
  184. # # PARTIAL IMPORT
  185. # from math import factorial
  186. # from math import ceil, sqrt
  187. # from math import *
  188. # factorial(x) # not math.factorial()
  189. # floor(x.yz) # not math.floor()
  190. #
  191. # # ALIAS IMPORT
  192. # import math as m
  193. # m.floor(x.yz) # not math.floor()
  194.  
  195. # FILES
  196. # READ MODE
  197. # with open("test.txt", "r") as f: # f is file object
  198. #     contents = f.readlines() # list of strings line by line
  199. # print(contents) # ['Hello.\n', 'This\n', 'is\n', 'just\n', 'a\n', 'string.\n', 'But...\n', 'with\n', 'many\n', 'line\n', 'breaks!']
  200. #
  201. # for line in contents:
  202. #     line = line.strip()
  203. #     print(line)
  204.  
  205. import csv # csv.reader()
  206. with open("mock_data.csv", "r") as f1:
  207.     contents = list(csv.reader(f1)) # if a tsv file... csv.reader(f1, delimiter="\t")
  208. # print(contents[0:25]) # slice!
  209.  
  210. # WRITE MODE
  211. with open("output_data17.csv", "w") as f2:
  212.     for row in contents:
  213.         # write out data for rows where the email address is at photobucket.com
  214.         if "@photobucket.com" in row[3]:
  215.             f2.write(",".join(row)+"\n") # takes a single string arg
  216.  
  217.  
  218. # APPEND MODE
  219. with open("output_data17.csv", "a") as f3:
  220.     # [1001, Gomer, Pyle, golly@usmc.gov, Male, 157.108.244.163]
  221.     f3.write("1001,Gomer,Pyle,golly@usmc.gov,Male,157.108.244.163\n")
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement