Advertisement
jspill

webinar-python-exam-review-2023-07-22

Jul 22nd, 2023 (edited)
1,701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.04 KB | None | 0 0
  1. # Exam Review 2023 July 22
  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. # Watch your string input and output
  14. # input...
  15. # myInput = input().strip()
  16. # # output/print()
  17. # print() # print(end="\n")
  18. # print("Something I'm printing", end=" ") # if we ever override end...
  19. # print()
  20. # # print("This should start on a clean new line!")
  21.  
  22.  
  23. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  24. # Common Data Types/Classes
  25. # int
  26. # float
  27. # bool # True, False... x > 5... evaluates to Bool True or False
  28. # str # " "
  29. # list # [ ]
  30. # dict # {key: value}
  31. # tuple # ( ) immutable, Python sees x,y,z as (x,y,z) --> return x,y --> return (x, y)
  32. # set # { } all unique values/no duplicates, no order --> no indices, no slicing, no sorting
  33. # range # range()... range(0, 5) --> [0, 1, 2, 3, 4]
  34.  
  35. # Operators
  36. # = # assigning
  37. # == # equality... asking "are these equal?" in a conditional expression
  38. # +
  39. # -
  40. # *
  41. # /
  42. # % # modulo... gives an int remainder, "How many whole things didn't fit... since the last even division?"
  43. # // # floor division... the last even division
  44. # <
  45. # >
  46. # >=
  47. # <=
  48. # += # x += 1 --> x = x + 1
  49. # -=
  50. # ** # raise to a power like math.pow()... or built-in pow()
  51. # !=
  52. # # keywords
  53. # in # if x in myList
  54. # not # if not x in myList
  55. # and
  56. # or # any one True means combined condition is True... limit OR to 2-3 conditions
  57.  
  58. # Comp 2
  59. # Control Flow! The HOW stuff
  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. # Check out my For Loops webinar in The Gotchas
  65. # for ___ in _someContainer_:
  66. # for item in myList:
  67. # for char in myStr:
  68. # for key in myDict:
  69. # for num in range(0, 8):
  70. # for i in range(len(myList)): # myList[i]
  71. # for i, item in enumerate(myList):
  72.  
  73. # FUNCTIONS
  74. # defining/writing vs calling
  75. # a function has ONE particular job
  76. # parameters are special variables... they don't work like regular variables
  77. # parameters vs arguments
  78. # return vs print()/output vs something else?
  79. #
  80. # def someFunction(x, y):
  81. #     return x * y
  82. #
  83. # if __name__ == "__main__":
  84. #     # we're solving this particular question
  85. #     myInput = int(input())
  86. #     myOther = int(input())
  87. #     myNum = someFunction(myInput, myOther)
  88. #     print(myNum)
  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. # print()
  96. # input()
  97. # len()
  98. # range()
  99. # enumerate()
  100. # min()
  101. # max()
  102. # sum()
  103. # int()
  104. # float()
  105. # list()
  106. # dict()
  107. # set()
  108. # type() # x = 3 # print(type(x).__name__)
  109. # round() # cousins are in the math module: math.ceil(), math.floor()
  110. # abs() # cousin math.fabs()
  111. # open()
  112. # help() # help(str), help(str.isdigit)
  113. # dir() # print(dir(str))
  114.  
  115.  
  116. # STRINGS
  117. # be able to refer to indices, and slice
  118. # myStr = "abcdef"
  119. # myRevStr = myStr[::-1]
  120. # print(myRevStr)
  121.  
  122. # KNOW YOUR WHITESPACE
  123. # " "
  124. # a lot of Unicode spaces
  125. # "\n" # new line return
  126. # "\t" # tab
  127. # "\r" # carriage return
  128.  
  129. # STRING METHODS
  130. # myStr.format() # "stuff I want to put together {}".format(var)
  131. # myStr.strip()
  132. # myStr.split() # returns a list of smaller strings
  133. # " ".join(listOfStrings)
  134. # myStr.replace(subStr, newSubStr) # "remove"... myStr = myStr.replace(subStr, "")
  135. # myStr.find(subStr) # returns an index where it finds it, or -1
  136. # myStr.count(subStr) # returns number of times that occurs
  137. # case: .lower(), .upper(), .title(), .capitalize()
  138. # is/Boolean: .isupper(), .islower(), .isspace(), .isalpha(), .isalnum(), .isdigit(), .isnumeric()
  139. # myStr.startswith(subStr), myStr.endswith(subStr)
  140.  
  141. # LISTS
  142. # be able to use indices, to slice
  143.  
  144. # LIST METHODS
  145. # +
  146. # myList.append(item)
  147. # myList.insert(i, item)
  148. # myList.extend(anotherList)
  149. # # -
  150. # myList.pop(i) # pop by index
  151. # myList.remove(item) # remove by value
  152. # myList.clear()
  153. # # other
  154. # myList.count(item)
  155. # myList.sort()
  156. # myList.reverse()
  157. # myList.copy()
  158. # myList.index()
  159. #
  160. # # DICT METHODS
  161. # # use the key like an index []... then you don't need DICT methods
  162. # myDict[key] # retrieves value for that key
  163. # myDict[key] = value # assigns a new value
  164. #
  165. # myDict.keys()
  166. # myDict.values()
  167. # myDict.items() # for key, value in myDict.items()
  168.  
  169. # MODULES
  170. # math and csv
  171.  
  172. # MATH MODULE
  173. # import math # 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(x) # or built-in abs()
  180. # math.pi
  181. # math.e
  182.  
  183. # PARTIAL IMPORT
  184. # from math import factorial # --> factorial(x)
  185. # from math import ceil, floor # --> ceil(x.yz)
  186. # from math import * # --> sqrt(x)
  187. #
  188. # # ALIAS IMPORT
  189. # import math as m # m.floor(x.yz)
  190.  
  191. # FILES
  192.  
  193. # READ MODE
  194. # filename = input()
  195. with open("test.txt", "r") as f: # with open(filename, "r")
  196.     contents = f.readlines()
  197. print(contents)
  198.  
  199. for line in contents:
  200.     line = line.strip()
  201.     print(line)
  202.  
  203. # CSV
  204. import csv
  205. with open("mock_data.csv", "r") as f1: # fake data from mockaroo.com
  206.     contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t") for tsv files
  207. print(contents[:20])
  208.  
  209.  
  210. # WRITE MODE
  211. with open("output_data22.csv", "w") as f2:
  212.     for row in contents:
  213.         # only write out if email is at reference.com
  214.         # email is position 3
  215.         if "@reference.com" in row[3]: # if row[3].endswith("@reference.com"):
  216.             # write() takes a single str argument
  217.             f2.write(",".join(row) + "\n")
  218.  
  219.  
  220.  
  221. # APPEND MODE
  222. # with open("append_to_this.txt", "r") as f3: # it's always worth taking a look at what you have!
  223. #     print(f3.readlines()) # ['Frodo\n', 'Sam\n', 'Merry\n']
  224.  
  225. with open("append_to_this.txt", "a") as f3:
  226.     f3.write("Pippin\n")
  227.  
  228.  
  229. # the end
  230. # Best wishes to our exam takers this week!
  231.  
  232.  
  233.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement