Advertisement
jspill

webinar-python-exam-review-2023-06-17

Jun 17th, 2023
853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.94 KB | None | 0 0
  1. # Exam Review 2023 June 17
  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() # 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... print(x > 5)
  28. # str # ""
  29. # list # [ ]
  30. # dict # {key: value}
  31. # set # { } all unique values/no duplicates, no order... no indices, no sorting, no slicing
  32. # tuples # ( ) immutable, Python sees any x,y,z as (x,y,z) --> return a, b --> return (a, b)
  33. # range object # range()... range(0, 5) --> [0, 1, 2, 3, 4]
  34.  
  35. # Operators
  36. # = # assignment
  37. # == # equality... ASKING if these are equal
  38. # +
  39. # -
  40. # *
  41. # /
  42. # % # modulo... gives an int remainder, "How many whole things didn't fit (since the last even division)?"
  43. # // # 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 condition makes the combined condition 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 myString:
  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, 12):
  71. # for i, item in enumerate(myList):
  72. # for i in range(len(myList)): # myList[i]
  73.  
  74. # FUNCTION
  75. # defining/writing vs calling
  76. # a function has ONE particular job
  77. # parameter is a special var for the function... not like a "regular" variable
  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. #     myOther = int(input())
  88. #     myNum = someFunction(myInput, myOther)
  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. # input()
  97. # print()
  98. # range()
  99. # len()
  100. # min()
  101. # max()
  102. # sum()
  103. # enumerate()
  104. # round() # cousins math.ceil() and math.floor()
  105. # type()
  106. # sorted()
  107. # reversed()
  108. # pow() # compare to ** or math.pow()
  109. # abs() # compare to math.fabs()
  110. # int()
  111. # float()
  112. # list()
  113. # tuple()
  114. # set()
  115. # dict()
  116. # open()
  117. # help() # help(str), help(str.isspace)
  118. # dir() # print(dir(str))
  119.  
  120. # STRINGS
  121. # be able to slice
  122. # myStr = "abcdef"
  123. # revStr = myStr[::-1]
  124. # print(revStr)
  125.  
  126. # KNOW YOUR WHITESPACE
  127. # " "
  128. # a lot of spaces in Unicode
  129. # "\n"
  130. # "\t"
  131. # "\r"
  132.  
  133. # STRING METHODS
  134. # myStr.format() # "stuff I want to put together {:.2f}".format(var)
  135. # myStr.strip()
  136. # myStr.split() # returns a list of smaller strings
  137. # myStr.join() # " ".join(listOfStrings)
  138. # myStr.replace(subStr, newStr) # "remove"... myStr = myStr.replace(subStr, "")
  139. # myStr.find(subStr) # return int index, or -1
  140. # myStr.count(subStr) # return int number of occurrences
  141. # case: myStr.lower(), myStr.upper(), myStr.title(), myStr.capitalize()
  142. # is/Boolean: myStr.isupper(), .islower(), .isspace(), .isalpha(), .isnumeric(), isdigit(), isalnum()
  143. # myStr.startswith(subStr), myStr.endswith(subStr)
  144.  
  145. # LISTS
  146. # be able to use indices, slice
  147.  
  148. # LIST METHODS
  149. # # +
  150. # myList.append(item)
  151. # myList.insert(i, item)
  152. # myList.extend(anotherList)
  153. # # -
  154. # myList.pop(i) # by index, or last
  155. # myList.remove(item) # pop by index, remove by value
  156. # myList.clear()
  157. # # other
  158. # myList.count(item)
  159. # myList.sort()
  160. # myList.reverse()
  161. # myList.copy()
  162. # myList.index(item)
  163.  
  164. # DICT
  165. # use the key like an index []
  166. # myDict[key] # retrieve value for that key
  167. # myDict[key] = value # assign value to key
  168. # myDict.keys()
  169. # myDict.values()
  170. # myDict.items()
  171.  
  172. # MODULES
  173. # math and csv
  174.  
  175. # MATH MODULE
  176. # import math # FULL IMPORT
  177. # math.factorial(x)
  178. # math.ceil(x.yz)
  179. # math.floor(x.yz)
  180. # math.pow(x, y)
  181. # math.sqrt(x)
  182. # math.fabs(x) # built-in abs()
  183. # math.pi
  184. # math.e
  185. #
  186. # # PARTIAL IMPORT
  187. # from math import factorial # --> factorial(x)
  188. # from math import ceil, sqrt # --> ceil(x.yz), sqrt(x)
  189. # from math import * --> floor(x.yz)
  190. #
  191. # # ALIAS IMPORT
  192. # import math as m # --> m.floor(x.yz)
  193.  
  194.  
  195. # FILES
  196.  
  197. # READ MODE
  198. with open("test.txt", "r") as f:
  199.     # f.read() # returns entire file as one string
  200.     # f.readline # goes one line ahead and returns that line
  201.     contents = f.readlines() # list of strings... each line in the file is one string
  202. # print(contents)
  203. # for line in contents:
  204. #     line = line.strip()
  205. #     print(line)
  206.  
  207. # CSV Module
  208.  
  209. # BTW, you can use mockaroo.com to download CSV files of fake data like I'm using here
  210.  
  211. # import csv
  212. # with open("mock_data.csv", "r") as f1:
  213. #     contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t")
  214. # # print(contents)
  215. # for row in contents[0:20]:
  216. #     print(row)
  217.  
  218.  
  219. # WRITE MODE
  220. # with open("output_data20.csv", "w") as f2:
  221. #     for row in contents:
  222. #         # only write into this new file if email ends in .edu
  223. #         # email is position 3
  224. #         if row[3].endswith(".edu"):
  225. #             # write a str to file
  226. #             f2.write(",".join(row) + "\n")
  227.  
  228. # APPEND MODE
  229. # with open("append_to_this.txt", "r") as f3:
  230. #     contents = f3.readlines()
  231. # print(contents) # ['Frodo\n', 'Sam\n', 'Merry\n'] # <-- I like to check and see if the last line has a line return
  232. # with open("append_to_this.txt", "a") as f3:
  233. #     f3.write("Pippin\n")
  234.  
  235.  
  236. # Question on Lab 6.15
  237. # Since you haven't learned many string methods by Ch 6, you'd normally be doing this one with a for loop containing several if branches to sub out the characters.
  238. # But knowing the str replace() method, and that it returns a string itself, you could approach this one as a series of calls to replace()...
  239.  
  240. # user_pswd = input()
  241. #
  242. # user_pswd = user_pswd.replace("i", "1").replace("a", "@").replace("m", "M").replace("B","8").replace("s", "$")
  243. # user_pswd += "!"
  244. # print(user_pswd)
  245.  
  246. # You could, as another student noted, approach this one my making a dictionary of the characters and their substitutions
  247. # As with many problems, there are a variety of ways to approach this one!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement