Advertisement
jspill

webinar-exam-review-2023-03-18

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