Advertisement
jspill

webinar-python-exam-review-2021-05-21

May 21st, 2022
890
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Exam Review 2022 May 21
  2.  
  3. # LABS
  4. # Ch 3-13... all Labs!
  5. # Ch 14-18 not as important, other than some good labs in Ch 15
  6. # Ch 19-30 just LABS, but important practice!
  7. # Use Submit Mode!!!
  8.  
  9. # fix your string input and output
  10. # 1
  11. # myVar = input().strip() # myVar = input().rstrip()
  12. # # 2
  13. # print("some stuff", end=" ") # if you ever override end
  14. # print() # print(end="\n")
  15.  
  16. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  17. # Data Types
  18. # int
  19. # float
  20. # str
  21. # lists
  22. # dict
  23. # tuples # immutable, any series x, y, z -> interpreted as a tuple... return x, y -> return (x, y)
  24. # sets # all unique, no order -> no index, no slicing
  25.  
  26. # operators
  27. # = # assignment
  28. # == # "equality operator"... asking a question?
  29. # <
  30. # >
  31. # >=
  32. # <=
  33. # !=
  34. # +
  35. # -
  36. # *
  37. # /
  38. # % # modulo - more USEFUL than it seems
  39. # // # floor division
  40. # # keywords used like operators
  41. # not
  42. # in # if __ in __... if myString in myList
  43. # and
  44. # or # any one True means whole condition True... limit OR to 2 conditions
  45.  
  46. # Comp 2 Control Flow
  47. # IF statements... if, if/else, if/elif, if/elif/else, etc
  48. # LOOPS
  49. # WHILE - an IF that repeats
  50. # FOR - looping over a container, or looping a known number of times
  51. # for __ in __:
  52. # for item in myList:
  53. # for n in range(0, 5):
  54. # for i in range(len(myList):
  55. # for key in myDictionary:
  56.  
  57. # FUNCTIONS
  58. # defining/writing vs calling
  59. # parameters vs outside "regular" variables
  60. # parameters vs arguments
  61. # return vs print() # do whichever the question says
  62. # a good function has ONE JOB, modular
  63. # methods are functions that "belong" to a class/type
  64. # if you're write a certain function
  65.  
  66. def someFunction(x, y):
  67.     return x + y
  68.  
  69. if "__name__" == "__main__": # am I running from this particular file?
  70.     myInput = input()
  71.     # call our function
  72.     num = someFunction(myInput, someOtherNum)
  73.     print(num)
  74.  
  75. # BUILT-IN FUNCTIONS
  76. # print()
  77. # input() # input().strip()
  78. # # constructing / recasting functions
  79. # int()
  80. # str()
  81. # float()
  82. # dict()
  83. # tuple()
  84. # set()
  85. # # functions that work with lists or other containers
  86. # len()
  87. # sum()
  88. # min()
  89. # max()
  90. # sorted() # compare to list.sort()
  91. # reversed() # compare to list.reverse()
  92. # range()
  93. # enumerate()
  94. # round() # cousins math.ceil(), math.floor()
  95. # open() # IO/file methods: f.write(), f.read(), f.readlines()
  96. # help()
  97. # dir()
  98. # type()
  99. # isinstance()
  100.  
  101.  
  102. # help(list)
  103. # print(dir(str))
  104. # help(str.isdigit)
  105. # for item in dir(str):
  106. #     if not item.startswith("_"):
  107. #         print(item)
  108.  
  109. # STRINGS
  110. # be able to slice like it's 2nd nature
  111. myString = "abc"
  112. # myString[start:stop:step]
  113. myRevString = myString[::-1]
  114. print(myRevString)
  115. #
  116. # # STRING METHODS
  117. # myString.format()
  118. # myString.strip() # .lstrip(), .rstrip()
  119. # myString.split() # returns a list of smaller strings
  120. # " ".join(listOfStrings)
  121. # myString.replace(oldSubStr, newSubStr) # remove... myStr.replace(subStr, "")
  122. # myString.count(subStr) # returns int
  123. # myString.find(subStr) # returns int
  124. # case methods: myString.upper(), myString.lower()
  125. # is/Boolean methods: isupper(), islower(), isdigit()
  126.  
  127. # know your WHITESPACE
  128. # " " # 20ish variations in Unicode
  129. # "\n"
  130. # "\r" # carriage return
  131. # "\b"
  132. # "\t"
  133. # "\f"
  134.  
  135. # LIST
  136. # again be able to slice
  137.  
  138. # LIST METHODS
  139. # +
  140. # myList.append(item)
  141. # myList.extend(anotherList)
  142. # myList.insert(i, item)
  143. # # -
  144. # myList.pop(i) # pop by index
  145. # myList.remove(item) # remove by value
  146. # # other
  147. # myList.count(item)
  148. # myList.sort() # "modify in place", no return... compare to built-in sorted()
  149. # myList.reverse() # "modify in place", no return... compare to built-in reversed()
  150. # # not as important
  151. # myList.index(item)
  152. # myList.copy()
  153. # myList.clear()
  154. #
  155. # # DICT
  156. # # use the key like an index
  157. # # you can use the dict KEY to get its VALUE
  158. # myDictionary[key] # retrieve the value for that key... kinda like dict.get()
  159. # # you can use the dict KEY to update/change its VALUE
  160. # myDictionary[key] = value # set a new value, kinda like dict.update()
  161. #
  162. # myDict.get(key)
  163. # myDict.update({k:v})
  164. # myDict.items() # for k, v in myDict.items()
  165. # myDict.keys() # returns a container of keys
  166. # myDict.values() # returns a container of values
  167. #
  168. # # SETS
  169. # mySet.add()
  170. # mySet.remove(item) # by value
  171. # mySet.pop() # removes a random entry
  172.  
  173. # COMP 3 files and modules
  174.  
  175. # MODULES
  176. # MATH
  177. # import math
  178. math.factorial(x)
  179. math.ceil(x.yz)
  180. math.floor(x.yz)
  181. math.pow(x, y) # similar to **
  182. math.sqrt(x)
  183.  
  184. # full vs partial import
  185. # import math --> math.factorial()
  186. # from math import factorial --> factorial()
  187. # from math import * --> factorial(), sqrt(), etc
  188. # import math as m --> m.factorial()
  189.  
  190. # OPENING FILES
  191.  
  192. # # good practice: Ch 12 Task 4, 7, 8
  193.  
  194. with open("filename.txt", "r") as f:
  195.     # # read() -> whole file as one big string
  196.     # # readlines() -> a list of line by line strings
  197.     myContent = f.readlines()
  198.  
  199. import csv
  200. with open("filename.csv", "r") as f:
  201.     # myContent = csv.reader(f) # a list-like object of line by line lists of strings
  202.     myContent = list(csv.reader(f)) # delimiter="/t"
  203.  
  204. # write
  205. with open("myNewFile.txt", "w") as f:
  206.     # if you're figuring something out (and maybe looping to create strings)
  207.     # ... you'll probably need to stay in this with/open block as you write the file
  208.     f.write("I am writing a string into this file." + "\n")
  209.  
  210.  
Advertisement
RAW Paste Data Copied
Advertisement