Advertisement
jspill

webinar-python-exam-review-2023-09-30

Sep 30th, 2023
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.13 KB | None | 0 0
  1. # Exam Review 2023 Sept 30
  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%!!! And PAY ATTENTION to the unit tests!
  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. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  14.  
  15. # Operators
  16. # = # assigns
  17. # == # asking, comparison... conditional expression
  18. # +
  19. # -
  20. # *
  21. # /
  22. # // # floor division... the last even division?
  23. # % # modulo... whole number remainder... how many whole things didn't fit since the last even division?
  24. # <
  25. # >
  26. # <=
  27. # >=
  28. # += # x += 1 --> x = x + 1
  29. # -=
  30. # ** # raise to a power... math.pow()... or built-in pow()
  31. # !=
  32. # # keywords
  33. # in # if x in myList
  34. # not # if not x in myList
  35. # and
  36. # or # any one True means the combo is True... limit OR to 2-3 conditions
  37.  
  38. # Common Data Types/Classes
  39. # int
  40. # float
  41. # bool # True, False
  42. # str # " " most important type?
  43. # list # [ ]
  44. # dict # {key: value}... myDict[key]
  45. # tuple # ( ) immutable, Python sees x,y,z as (x,y,z) -> return a,b,c --> return (a,b,c)
  46. # set # { } # no duplicates, unordered --> no index, no slicing, no sorting
  47. # range # range()... range(0, 5, 1)... [0, 1, 2, 3, 4]
  48. # file # open()...  f.read(), f.readlines(), f.write()
  49.  
  50. # Comp 2
  51. # Control Flow! The HOW stuff
  52. # IF statements... if, if/else, if/elif, if/elif/else
  53. # LOOPS
  54. # WHILE - an IF that repeats
  55. # FOR - looping over a container, or a known number of times... hence range()
  56. # Check out my For Loops webinar in The Gotchas
  57. # for ___ in _someContainer_:
  58. # for item in myList:
  59. # for char in myStr:
  60. # for key in myDict: # myDict[key]... for k, v in myDict.items():
  61. # for num in range(0, 7):
  62. # for i in range(0, len(myList)): # myList[i]
  63. # for i, item in enumerate(myList):
  64.  
  65. # FUNCTIONS
  66. # defining/writing vs calling
  67. # modular... a function has ONE job
  68. # parameters are special variable holding things sent into the function
  69. # parameters vs arguments
  70. #
  71. # def someFunction(x, y):
  72. #     return x // y
  73. #
  74. # if __name__ == "__main__":
  75. #     # inside this block we're answering this specific question
  76. #     myInput = int(input())
  77. #     myOther = int(input())
  78. #     # myNum = someFunction(myInput, myOther)
  79. #     # print(myNum)
  80. #     print(someFunction(myInput, myOther))
  81.  
  82. # See "tasks" in the last section of Ch 10, 11, 13, 14 for function writing practice
  83. # # CodingBat also has good function-based Python questions:
  84. # https://codingbat.com/python
  85.  
  86. # BUILT-IN FUNCTIONS
  87. # input()
  88. # print()
  89. # len()
  90. # min()
  91. # max()
  92. # sum()
  93. # list()
  94. # int()
  95. # str()
  96. # dict()
  97. # range()
  98. # set()
  99. # tuple()
  100. # type() # print(type(x).__name__)
  101. # enumerate()
  102. # open()
  103. # round() # cousins math.ceil() and math.floor()
  104. # help() # help(str), help(str.isspace)
  105. # dir() # print(dir(str))
  106.  
  107. # STRINGS
  108. # be able to refer to indices, and slice
  109. # myStr = "abc"
  110. # revStr = myStr[::-1]   # mySlice[start:stop:step]
  111. # print(revStr)
  112.  
  113. # KNOW YOUR WHITESPACE
  114. # " " # space from spacebar
  115. # # a lot of Unicode spaces
  116. # \n # new line return... print(end="\n")
  117. # \t # tab
  118. # \r # carriage return
  119.  
  120. # STRING METHODS
  121. # myStr.format() # "stuff I want to put together {}".format(var)
  122. # myStr.strip() # input().strip()
  123. # myStr.split() # returns a list of smaller strings
  124. # " ".join(listOfStrings)
  125. # myStr.replace(subStr, newSubStr) # "remove" myStr = myStr.replace(subStr, "")
  126. # myStr.find(subStr) # return index (int) where found, or -1 on failure
  127. # myStr.count(subStr) # return int of how many times found
  128. # case: myStr.lower(), myStr.upper(). myStr.title(), myStr.capitalize()
  129. # is/Boolean: myStr.islower(), .isupper(), .isspace(), .isalpha(), .isnumeric(), .isdigit(), .isalnum()
  130. # myStr.startswith(subStr), myStr.endswith(subStr)
  131. #
  132.  
  133. # LISTS
  134. # be able to ref by index, slice
  135.  
  136. # LIST METHODS
  137. # # +
  138. # myList.append(item)
  139. # myList.insert(i, item)
  140. # myList.extend(anotherList)
  141. # # -
  142. # myList.pop(i) # pop by index...
  143. # myList.remove(item) # remove by value
  144. # myList.clear()
  145. # # other
  146. # myList.count(item) # return int count
  147. # myList.sort() # does not return a value!
  148. # myList.reverse() # does not return a value!
  149. # myList.copy()
  150. # myList.index(item)
  151.  
  152. # DICT
  153. # use the key like an index []... then you don't need DICT methods
  154. # myDict[key] # retrieve the value for that key
  155. # myDict[key] = value # assign a new value to that key
  156.  
  157. # DICT METHODS
  158. # myDict.keys()
  159. # myDict.values()
  160. # myDict.items() # for key, value in myDict.items()
  161.  
  162. # if var in myDict: # only looks at keys
  163.  
  164. # MODULES
  165.  
  166. # MATH MODULE
  167. # import math # FULL IMPORT
  168. # math.factorial(x)
  169. # math.ceil(x.yz)
  170. # math.floor(x.yz)
  171. # math.pow(x, y)
  172. # math.sqrt(x)
  173. # math.fabs(x) # built-in abs()
  174. # math.pi
  175. # math.e
  176.  
  177. # PARTIAL IMPORT
  178. from math import floor # floor(x.yz)
  179. from math import sqrt, factorial # factorial(x), sqrt(x.y)
  180. from math import * # sqrt(x.yz)
  181.  
  182. # ALIAS IMPORT
  183. import math as m # m.floor(x)
  184.  
  185. # FILES
  186. # READ MODE
  187.  
  188. with open("test.txt", "r") as f:
  189.     # f.read() # returns whole file as one big string
  190.     # f.readlines() # returns a list of strings, line by line
  191.     # f.write() # take one str arg and write into file
  192.     contents = f.readlines()
  193. # print(contents)
  194. # for line in contents:
  195. #     line = line.strip()
  196. #     print(line)
  197.  
  198. # CSV Module
  199. # csv.reader()
  200. import csv
  201. with open("mock_data.csv", "r") as f1: # mockaroo.com
  202.     contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t") for a .tsv
  203. # print(contents[0:20])
  204.  
  205. # WRITE MODE
  206. with open("output_data27.csv", "w") as f2:
  207.     for row in contents:
  208.         # write out a file with every row where the last name starts with D
  209.         # last name is at row[2]
  210.         # if row[2].startswith("d") or row[2].startswith("D"):  # if row[2][0].lower() == "d":
  211.         if row[2].lower().startswith("d"):
  212.             # we've found data to write
  213.             f2.write(",".join(row) + "\n")
  214.  
  215.  
  216. # APPEND MODE
  217. # with open("append_to_this.txt", "r") as f3: # check to see if last line has a \n, follow suit in yours
  218. #     print(f3.readlines())
  219. with open("append_to_this.txt", "a") as f3:
  220.     addMe = "Pippin"
  221.     f3.write(f"\n{addMe}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement