Advertisement
jspill

webinar-exam-review-2023-02-25

Feb 25th, 2023 (edited)
970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.08 KB | None | 0 0
  1. # Exam Review 2023 Feb 25
  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() # print(end="\n")
  13. # # if you ever override end...
  14. # print("some stuff", end="") # but we need to get back to that clean line following us
  15. # print()
  16. #
  17. # # print("Clean new line!")
  18.  
  19. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  20. # Comp 2: Control Flow
  21. # Comp 3: Modules and Files
  22.  
  23. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  24. # Common Data Types
  25. # int
  26. # float
  27. # bool
  28. # str # " "
  29. # list # [ ]
  30. # tuple # ( ) immutable, Python sees x,y,z as (x,y,z)... return x,y,z -> return (x,y,z)
  31. # set # { } all unique values (no repeats), is unordered... no index, no slicing, no sorting
  32. # dict # {key: value}
  33. # range() # --> range object... sorta like [0, 1, 2, 3]
  34.  
  35. # operators
  36. # = # assignment
  37. # == # equality... asking, comparing... part of condition
  38. # +
  39. # -
  40. # *
  41. # /
  42. # % # modulo, int remainder... "how many whole things left over?"
  43. # // # floor division... x//y -> math.floor(x/y)
  44. # <
  45. # >
  46. # <=
  47. # >=
  48. # += # increment... x+=1 --> x = x+1
  49. # -= # decrement
  50. # ** # raise to a power... pow() or math.pow()
  51. # !=
  52. # # keywords used like operator
  53. # in # if _someValue_ in _someContainer_
  54. # not # if not _someValue_ in _someContainer_
  55. # and
  56. # or  #  any one True means whole condition is True... limit OR to 2 conditions
  57.  
  58. # Comp 2
  59. # # the HOW stuff... control flow structures
  60. # IF statements... if, if/else, if/elif, if/elif/else
  61. # LOOPS
  62. # WHILE - an IF that repeats
  63. # FOR - looping over a container, or a known number of times # hence range()
  64. # for ___ in _someContainer_:
  65. # for item in myList:
  66. # for key in myDict: # myDict[key]
  67. # for n in range(5): # for n in range(0, 5) # --> [0, 1, 2, 3, 4]
  68. # for i in range(0, len(myList)):
  69.  
  70. # FUNCTIONS
  71. # defining/writing vs calling
  72. # a function has ONE particular job
  73. # parameters are special "variables"... they dont' work like "regular" variables
  74. # parameters vs arguments
  75. # return vs print()...vs write a file... whatever the question says
  76. # method are functions that belong to a particular class/type
  77. #
  78. # def someFunction(x, y):
  79. #     return x - y
  80. #
  81. # if __name__ == "__main__":
  82. #     myInput = int(input())
  83. #     myOther = int(input())
  84. #     num = someFunction(myInput, myOther)
  85. #     print(num)
  86.  
  87. # See "tasks" in the last section of Ch 10, 11, 13, 14 for function writing practice
  88. # # CodingBat also has good function-based Python questions:
  89. # # https://codingbat.com/python
  90.  
  91. # BUILT-IN FUNCTIONS
  92. # print()
  93. # input()
  94. # range()
  95. # list()
  96. # int()
  97. # float()
  98. # str()
  99. # dict()
  100. # tuple()
  101. # set()
  102. # type() # creates a type object
  103. # len()
  104. # enumerate()
  105. # min()
  106. # max()
  107. # sum()
  108. # round() # buts its cousins math.ceil() and math.floor() are in the math module
  109. # sorted() # returns sorted list.. compare list.sort() does not return anything
  110. # reversed() # returns reversed list... same
  111. # open() # IO/file --> .read(), .readlines()
  112. # help()
  113. # dir()
  114. # help(str) # help(str.isspace)
  115. # print(dir(str))
  116.  
  117. # STRINGS
  118. # be able to slice like it's 2nd nature: myString[start:stop:step]
  119. # myStr = "abcdef"
  120. # revStr = myStr[::-1]
  121. # print(revStr)
  122.  
  123. # KNOW YOUR WHITESPACE
  124. # " " # ... any many Unicode spaces
  125. # "\n" # new line return
  126. # "\t" # tab
  127. # "\r" # carriage return
  128. # ... and more! There are many whitespace characters.
  129.  
  130. # STRING METHODS #... print(dir(str))
  131. # "stuff I want to put together {:.2f}".format(var) # f strings
  132. # myStr.strip() # cousins: .lstrip(), .rstrip()
  133. # myStr.split() # returns a list of smaller strings
  134. # " ".join(listOfStrings)
  135. # myStr.replace(subStr, newSubStr) #  "remove"... myStr = myStr.replace(subStr, "")
  136. # myStr.find(subStr) # return int index or -1 if not there
  137. # myStr.count(subStr)  # return int count of occurrences
  138. # case: myStr.lower(), myStr.upper(), myStr.title(), myStr.capitalize()
  139. # is/Boolean: myStr.isupper(), islower(), isspace(), isalpha(), isdigit(), isnumeric()
  140.  
  141. # LISTS
  142. # again know indices and 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.sort()
  154. # myList.reverse()
  155. # myList.count(item)
  156. # # not as important
  157. # myList.clear()
  158. # myList.copy()
  159. # myList.index()
  160.  
  161. # DICT
  162. # use the key like an index... myDict[key]
  163. # myDict[key] # retrieve the value for that key, so like get()
  164. # myDict[key] = value # # assign (new) value for that key, so like update({k:v})
  165. # myDict.keys()
  166. # myDict.values()
  167. # myDict.items() # for k, v in myDict.items()
  168.  
  169. # MODULES
  170. # math and csv
  171.  
  172. # # MATH MODULE
  173. # import math # that's a FULL IMPORT
  174. # math.factorial(x)
  175. # math.ceil(x.yz)
  176. # math.floor(x.yz)
  177. # math.pow(x, y)
  178. # math.sqrt(x)
  179. # math.fabs() # also built-in abs()
  180. # math.e
  181. # math.pi
  182. #
  183. # # PARTIAL IMPORT
  184. # from math import factorial
  185. # from math import factorial, ceil
  186. # from math import * # but still a partial import
  187. # factorial()
  188. #
  189. # # ALIAS IMPORT
  190. # import math as m
  191. # m.floor(x.yz)
  192.  
  193. # FILES!!!
  194. # READ MODE
  195. with open("test.txt", "r") as f:
  196.     contents = f.readlines() # return a list of strings... each string is a line
  197. print(contents)
  198. for line in contents:
  199.     line = line.strip()
  200.     print(line)
  201.  
  202. # CSV Module
  203. import csv
  204. with open("mock_data.csv", "r") as f1:
  205.     contents = list(csv.reader(f1)) # if a tsv... csv.reader(f1, delimiter="\t")
  206. # print(contents)
  207. print(contents[:10])
  208.  
  209. # WRITE MODE
  210. with open("output_data13.csv", "w") as f2:
  211.     for line in contents:
  212.         if ".org" in line[3]: # if line[3].endswith(".org")
  213.             # the file write() method takes a SINGLE STRING argument
  214.             f2.write(",".join(line)+"\n")
  215.  
  216. # APPEND MODE
  217. # with open("append_to_this.txt", "r") as f3:
  218. #     contents = f3.readlines()
  219. # print(contents) # --> ["Frodo\n", "Sam\n", "Merry"]... note that the last line may or may not have a \n
  220. with open("append_to_this.txt", "a") as f4:
  221.     f4.write("\nPippin\n")
  222.    
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement