Advertisement
jspill

webinar-exam-review-2022-11-26

Nov 26th, 2022
1,181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.07 KB | None | 0 0
  1. # Exam Review Nov 26 2022
  2.  
  3. # LABS
  4. # Ch 2-14... all Labs!
  5. # Ch 21-34 just ADDITIONAL LABS, but important practice!
  6. # Use Submit Mode!!!
  7.  
  8. # Watch your string input and output
  9. # 1
  10. # myVar = input().strip()
  11. # # 2
  12. # print("some stuff", end=" ") # if you ever override end...
  13. # print() # print(end="\n")
  14.  
  15. # print("Clean new line!")
  16.  
  17. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  18. # Comp 2: Control Flow
  19. # Comp 3: Modules and Files
  20.  
  21. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  22. # Common Data Types
  23. # str # ""
  24. # int
  25. # float
  26. # list # [ ]
  27. # dict # {k:v, k:v}
  28. # tuple # ( ) immutable, Python sees any x,y,z as (x,y,z)... return x,y -> return (x,y)
  29. # set # { } no order (so no indices, etc), all unique values (no duplicates)
  30.  
  31. # operators
  32. # = # assigns
  33. # == # "equality operator"... asking a question for a CONDITION: if/elif, while
  34. # +
  35. # -
  36. # *
  37. # /
  38. # % # modulo, int remainder... "how many whole things left over?"
  39. # // # floor division... x//y -> math.floor(x/y)
  40. # <
  41. # >
  42. # <=
  43. # >=
  44. # += # increment
  45. # -= # decrement
  46. # ** # raising to a power... x**y -> math.pow(x, y)
  47. # !=
  48. # # keywords used like operators
  49. # in # if _someValue_ in _someContainer_
  50. # not # if not _someValue_ in _someContainer_
  51. # and
  52. # or # ny one True means whole condition is True... limit OR to 2 conditions
  53.  
  54. # Comp 2
  55. # # the HOW stuff... control flow structures
  56. # IF statements... if, if/else, if/elif/elif/else
  57. # LOOPS
  58. # WHILE - an IF that repeats
  59. # FOR - looping over a container, or a known number of times... range()
  60. # for _item_ in _container_:
  61. # for item in myList:
  62. # for n in range(0, 5): # [0, 1, 2, 3, 4]
  63. # for i in range(0, len(myList)): # myList[i]
  64. # for key in myDict: # myDict[key]
  65.  
  66. # FUNCTIONS
  67. # defining/writing vs calling
  68. # parameters are special "variables"... they don't work like "regular" variables
  69. # parameters vs arguments
  70. # a function has ONE particular job
  71. # return vs print()... writes a file... whatever the question says
  72. # method are functions that belong to a particular type/class
  73.  
  74. # def someFunction(x, y):
  75. #     return x + y
  76. #
  77. # if __name__ == "__main__":
  78. #     myInput = int(input())
  79. #     myOtherInput = int(input())
  80. #     num = someFunction(myInput, myOtherInput)
  81. #     print(num)
  82.  
  83. # See "tasks" in the last section of Ch 10, 11, 13, 14 for function writing practice
  84. # # CodingBat also has good function-based Python questions:
  85. # # https://codingbat.com/python
  86.  
  87. # BUILT-IN FUNCTIONS
  88. # print()
  89. # input()
  90. # len()
  91. # sum()
  92. # min()
  93. # max()
  94. # range()
  95. # list()
  96. # str()
  97. # int()
  98. # float()
  99. # dict()
  100. # set()
  101. # type() # print(type("3").__name__)
  102. # round() # buts its cousins math.ceil() and math.floor() are in the math module
  103. # open() # IO/file --> .read(), .readlines(), .write()
  104. # reversed() # returns reversed list... compare list.reverse() does not return anything
  105. # sorted() # returns sorted list... compare list.sort() does not return anything
  106. # help(str) # help(str.startswith)
  107. # print(dir(str))
  108.  
  109. # STRINGS
  110. # be able to slice like it's 2nd nature: myString[start:stop:step]
  111. # myString = "abc"
  112. # revString = myString[::-1]
  113. # print(revString)
  114.  
  115. # KNOW YOUR WHITESPACE
  116. # " " #... and many Unicode spaces
  117. # "\n"
  118. # "\r"
  119. # "\t"
  120. # "\f"
  121.  
  122. # STRING METHODS
  123. # "stuff I want to put together {}".format(var) # or similar f strings
  124. # myString.strip()
  125. # myString.split() # returns a list of smaller strings
  126. # ",".join(listOfStrings)
  127. # myString.replace(oldSubStr, newSubStr) # remove... myString.replace(oldSubStr, "")
  128. # myString.find(subStr) # return int index, or -1 if not found
  129. # myString.count(subStr) # return int count of number occurences
  130. # case: myString.lower(), myString.upper(), myString.title()...
  131. # is/Boolean: isupper(), islower(), isalpha(), isdigit(), isalnum(), isspace()
  132.  
  133. # LISTS
  134. # again know indices and be able to slice
  135.  
  136. # LIST METHODS
  137. # # +
  138. # myList.append(item)
  139. # myList.insert(i, item)
  140. # myList.extend(anotherList)
  141. # # -
  142. # myList.pop() # myList.pop(i)... pop() by INDEX
  143. # myList.remove(item) # remove() by VALUE
  144. # # other
  145. # myList.count(item) # returns int number of occurrences
  146. # myList.sort() # MODIFY in place... no return
  147. # myList.reverse() # "
  148. # # not as important
  149. # myList.clear()
  150. # myList.copy()
  151. # myList.index(item)
  152.  
  153. # DICT
  154. # use the key like an index
  155. # myDict[key] # retrieve the value for that key, so like get()
  156. # myDict[key] = value # assign (new) value for that key, so like update({k:v})
  157. # myDict.keys()
  158. # myDict.values()
  159. # myDict.items() # or for k, v in myDict.items()
  160.  
  161. # # MODULES
  162. # # math and csv
  163. # # remember there are different import styles that change how you reference them
  164.  
  165. # MATH MODULE
  166. import math
  167. # math.factorial(x)
  168. # math.ceil(x.yz)
  169. # math.floor(x.zy)
  170. # math.pow(x, y) # similar to **, not be confused with math.exp()
  171. # math.sqrt(x)
  172. # math.fabs() # similar to built-in abs()
  173. # math.pi
  174. # math.e
  175.  
  176. # PARTIAL IMPORT
  177. # from math import factorial
  178. # don't say math.factorial()... we didn't import math
  179. # factorial()
  180.  
  181. # ALIAS IMPORT
  182. # import math as m --> m.floor(), etc
  183.  
  184. # FILES!!!
  185. # READ MODE
  186. with open("test.txt", "r") as f:
  187.     contents = f.readlines() # list of strings, line by line
  188. print(contents)
  189. # for line in contents:
  190. #     line = line.strip()
  191. #     print(line)
  192.  
  193. # CSV MODULE
  194. import csv
  195. with open("mock_data.csv", "r") as f1:
  196.     contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t")
  197.     # contents = f.readlines()  # list of strings, line by line... ['Hello.\n', ...]
  198. # print(contents) # [['1', 'Remington', 'Shilling', 'rshilling0@wsj.com', 'Male', '1.71.141.52']... ]
  199.  
  200. # WRITE MODE
  201. with open("output_data8.csv", "w") as f2:
  202.     for line in contents:
  203.         if line[3].endswith(".org"):  #line[3][-4:] == ".org"
  204.             # write() method takes one single str argument
  205.             f2.write(",".join(line)+"\n")
  206.  
  207.  
  208. # APPEND MODE
  209. # with open("append_to_this.txt", "r") as f3:
  210. #     print(f3.readlines()) # --> ['Frodo\n', 'Sam\n', 'Merry\n'] # good to look at files to check last line! \n or no?
  211. with open("append_to_this.txt", "a") as f3:
  212.     f3.write("Pippin\n")
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement