Advertisement
jspill

webinar-exam-review-2023-05-27

May 27th, 2023
1,702
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.66 KB | None | 0 0
  1. # Exam Review 2023 May 27
  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/echo/print()
  17. # print() # same as print(end="\n")
  18. # print("Something I'm printing.", end=" ") # if we override end...
  19. # # we gotta put the expected \n back
  20. # print()
  21. # # print("Clean new line!")
  22.  
  23. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  24. # Common Data Types
  25. # int
  26. # float
  27. # bool # True, False... cond expressions resolve to bool: x = 999, print(x>1000) --> False
  28. # str # ""
  29. # list # [ ]
  30. # dict # {key:value}
  31. # tuple # () immutable, Python sees any x,y,z as (x,y,z) --> return x,y --> return (x,y)
  32. # set # all unique/no duplicate, no ordered... no index, no slicing, no sort
  33. # range object # range()... range(0, 5) --> [0, 1, 2, 3, 4]
  34.  
  35. # Operators
  36. # = # assignment
  37. # == # equality, asking a question, comparing... if/elif, while
  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. # -= # x -= 1 --> x = x-1
  50. # ** # raise to power... pow() and math.pow()
  51. # !=
  52. # # keywords that we use like operators
  53. # in # if x in myList:
  54. # not # if not x in myList:
  55. # and
  56. # or # any one True means the combined condition is True... limit OR to 2 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: # myDict[key] gets the value for that key
  69. # for key, value in myDict.items():
  70. # for num in range(0, 5):
  71. # # if I need to know the index and the item/value in a list, str, tuple, etc
  72. # for i in range(0, len(myList)): # for i, item in enumerate(myList):
  73.  
  74. # FUNCTIONS
  75. # defining/writing vs calling
  76. # a function has ONE particular job
  77. # parameters are special variables... they don't work like "regular" variables
  78. # parameters vs arguments
  79. # return vs print()/output... or something else?
  80. #
  81. # def someFunction(x, y):
  82. #     return x / y
  83. #
  84. # if __name__ == "__main__": # is this script the one that's being run from?
  85. #     # we're solving THIS question
  86. #     myInput = int(input())
  87. #     thisOther = int(input())
  88. #     myNum = someFunction(myInput, thisOther)
  89. #     print(myNum)
  90.  
  91. # See "tasks" in the last section of Ch 10, 11, 13, 14 for function writing practice
  92. # # CodingBat also has good function-based Python questions:
  93. # # https://codingbat.com/python
  94.  
  95. # BUILT-IN FUNCTIONS
  96. # print()
  97. # input()
  98. # range()
  99. # list()
  100. # tuple()
  101. # set()
  102. # dict()
  103. # int()
  104. # float()
  105. # enumerate()
  106. # len()
  107. # sum()
  108. # min()
  109. # max()
  110. # abs() # compare to math.fabs()
  111. # pow() # compare to math.pow() and to **
  112. # open() # open a file, create a file object
  113. # round() # cousins math.ceil() and math.floor()
  114. # type() # print(type(x).__name__)
  115. # sorted()
  116. # reversed()
  117. # help() # help(str), help(list.append)
  118. # dir() # print(dir(list))
  119.  
  120. # STRINGS
  121. # be able to slice
  122. # myStr = "abcd"
  123. # revStr = myStr[::-1]
  124. # print(revStr)
  125.  
  126. # KNOW YOUR WHITESPACE
  127. # " "
  128. # a lot of spaces in Unicode
  129. # "\n" # NEW line return
  130. # "\t" # tab
  131. # "\r" # CARRIAGE return
  132. # help(str.isspace)
  133.  
  134. # STRING METHODS
  135. # myStr.format() # "stuff I want to put together {}".format(var)
  136. # myStr.strip()
  137. # myStr.split() # returns a list a smaller strings
  138. # myStr.join() # " ".join(listOfStrings)
  139. # myStr.replace(subStr, newStr) # "remove"... myStr = myStr.replace(subStr, "")
  140. # myStr.find(subStr) # returns int for the index, -1 or failure
  141. # myStr.count(subStr) # return in count of subStr in string
  142. # case: myStr.lower(), myStr.upper(), myStr.title(), myStr.capitalize()
  143. # is/Boolean: myStr.isupper(), myStr.islower(), myStr.isspace(), myStr.isalpha(), myStr.isalnum(), myStr.isnumeric(), myStr.isdigit(), etc
  144. # myStr.startswith(subStr), myStr.endswith(subStr)
  145.  
  146. # LISTS
  147. # be able to slice
  148.  
  149. # LIST METHODS
  150. # # +
  151. # myList.append(item)
  152. # myList.insert(i, item)
  153. # myList.extend(anotherList)
  154. # # -
  155. # myList.pop() # last or myList.pop(i)
  156. # myList.remove(item) # pop() by index, remove() by value
  157. # myList.clear()
  158. # # others
  159. # myList.count(item)
  160. # myList.sort()
  161. # myList.reverse()
  162. # myList.copy()
  163. # myList.index(item)
  164.  
  165. # DICT
  166. # use the key like an index []
  167. # myDict[key] # retrieve value for that key
  168. # myDict[key] = value # assign value to key
  169. # myDict.keys()
  170. # myDict.values()
  171. # myDict.items()
  172.  
  173. # MODULES
  174. # math and csv
  175.  
  176. # MATH MODULE
  177. import math # FULL IMPORT
  178. # math.factorial(x)
  179. # math.ceil(x.yz)
  180. # math.floor(x.yz)
  181. # math.pow(x, y) # not to be confused with math.exp()
  182. # math.sqrt(x)
  183. # math.fabs(x) # compare abs(x)
  184. # math.pi
  185. # math.e
  186.  
  187. # PARTIAL IMPORT
  188. from math import factorial # --> factorial(x)
  189. from math import ceil, sqrt # --> ceil(x.yz), sqrt(x)
  190. from math import * #  --> floor(x.yz)
  191.  
  192. # ALIAS IMPORT
  193. import math as m # --> m.floor(x.yz), m.factorial(x)
  194.  
  195. # FILES
  196.  
  197. # READ MODE
  198. with open("test.txt", "r") as f:
  199.     contents = f.readlines() # list a strings line by line
  200. print(contents)
  201. for line in contents:
  202.     line = line.strip()
  203.     print(line)
  204.  
  205. # CSV module
  206. import csv
  207. with open("mock_data.csv", "r") as f1:
  208.     contents = list(csv.reader(f1)) # for tsv... csv.reader(f1, delimiter="\t")
  209. # print(contents) # [['id', 'first_name', 'last_name', 'email', 'gender', 'ip_address'], ['1', 'Remington', 'Shilling', 'rshilling0@wsj.com', 'Male', '1.71.141.52']... ]
  210. for row in contents[0:30]: # slice just to shorten this
  211.     print(row)
  212.  
  213.  
  214. # # WRITE MODE
  215. # with open("output_data19.csv", "w") as f2:
  216. #     for row in contents:
  217. #         # only write into this new file if email is at spiegel.de
  218. #         # email is at list position 3
  219. #         # lazy or smart?
  220. #         # if "@spiegel.de" in row[3]:
  221. #         # endswith()
  222. #         if row[3].endswith("@spiegel.de"):
  223. #             # takes a single str argument
  224. #             f2.write(",".join(row)+"\n")
  225.  
  226. # APPEND MODE
  227. # with open("append_to_this.txt", "r") as f3:
  228. #     contents = f3.readlines()
  229. # print(contents)
  230. with open("append_to_this.txt", "a") as f3:
  231.     f3.write("Galadriel\nBalin\nGlorfindel\nThe Balrog\n") # added to file, rather than overwriting
  232.  
  233.  
  234.  
  235. # Best wishes to our exam takers!
  236.  
  237.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement