Advertisement
jspill

webinar-python-exam-review-2022-04-30

Apr 30th, 2022
1,379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.67 KB | None | 0 0
  1. # Exam Review 2022 Apr 30
  2.  
  3. # LABS
  4. # Ch 3-13... all Labs!
  5. # Ch 14-18 not as important, other than some good labs in Ch 15
  6. # Ch 19-30 just LABS, but important practice!
  7. # Use Submit Mode!!!
  8.  
  9. # fix your string input and output
  10. # 1
  11. # myVar = input().strip()
  12. # # 2
  13. # print("some stuff", end=" ") # if you ever override end
  14. # print()
  15.  
  16. # see also: "I want the last one to be different"
  17. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]
  18. # for item in myList:
  19. #     print(item)
  20. # with a loop
  21. # for i in range(len(myList)):
  22. #     if not i == len(myList)-1:
  23. #         print(myList[i], end="-->")
  24. #     else:
  25. #         print(myList[i])
  26. # # with string methods...
  27. # print("-*-".join(myList))
  28.  
  29.  
  30. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  31. # Comp 2: Control Flow Structures
  32. # Basic: if statements, loops, functions
  33. # Adv: try/except and raising errors, classes
  34. # Comp 3: modules, working with files
  35.  
  36. # Comp 1 Basic syntax and knowledge: operators, data types, etc
  37. # Data Types
  38. # int
  39. # floats
  40. # bool (True/False)
  41. # str
  42. # lists
  43. # dict
  44. # sets # all unique values, no order/index
  45. # tuples # immutable... return x, y --> return (x, y)
  46.  
  47. # operators
  48. # +
  49. # -
  50. # *
  51. # /
  52. # % # modulo... What is the remainder? How many WHOLE THINGS didn't fit?
  53. # // # floor division... x//y same as math.floor(x/y)
  54. # ** # raise to a power... same as math.pow()
  55. # <
  56. # >
  57. # <=
  58. # >=
  59. # = # assignment
  60. # == # equality... asking if it's equal
  61. # +=
  62. # -=
  63. # != # not equal
  64. # # keywords
  65. # not
  66. # in # if _value_ in _container_, if not _value_ in _container_
  67. # and
  68. # or
  69.  
  70. # Comp 2 Control Flow
  71. # IF statements... if, if/else, if/elif, if/elif/else
  72. # LOOPS
  73. # WHILE - basically an IF that repeats
  74. # FOR - looping over some container, doing something a known number of times
  75. # for __ in __:
  76. # for item in myList:
  77. # for i in range()
  78. # for key in myDictionary: # myDictionary[key] # for k, v in myDictionary.items()
  79.  
  80. # FUNCTIONS
  81. # defining/writing vs calling
  82. # parameter vs "regular" variables
  83. # parameters vs arguments
  84. # return vs print() # do what the question says
  85. # methods are functions that "belong" to that type/class
  86. # a good function has ONE JOB, modular
  87.  
  88. # def someFunction(x, y):
  89. #     return something
  90. #
  91. # if "__name__" == "__main__":
  92. #     myInput = input()
  93. #     # call our function
  94. #     x = someFunction(myInput, myOtherInput)
  95. #     print(x)
  96.  
  97. # BUILT-IN FUNCTIONS
  98. print()
  99. input() # input().strip(), input().rstrip()
  100. len()
  101. max()
  102. min()
  103. sum()
  104. sorted()
  105. reversed()
  106. # constructor or recasting
  107. # int() # int(input().strip())
  108. # float() # float(input().strip())
  109. # list()
  110. # str()
  111. # dict()
  112. # set()
  113. # tuple()
  114. # range() # for i in range(len(someList)):
  115. # enumerate() # for i, item in enumerate(myList):
  116. # round() # not to be confused with math.ceil() and math.floor()
  117. # open()
  118. # help()
  119. # dir() # returns a list of "attributes"
  120. # type()
  121.  
  122. # on help() and dir():
  123. # help() and dir()
  124. # help(str)
  125.  
  126. # print(dir(str))
  127. # help(str.rstrip)
  128.  
  129. # print(dir(list))
  130. # for item in dir(list):
  131. #     if not item.startswith("_"):
  132. #         print(item)
  133.  
  134. # STRINGS
  135. # be able to SLICE like it's 2nd nature
  136. # myString = "abc"
  137. # # myString[start:stop:step]
  138. # # slice to reverse:  revString = myString[::-1]
  139. # # STRING METHODS
  140. # myString.format()
  141. # myString.strip() # .rstrip(), lstrip()
  142. # myString.split() # returns a list
  143. # " ".join(listOfStrings)
  144. # myString.replace(oldSubStr, newSubStr) # remove... myString.replace(oldSubStr, "")
  145. # myString.count(subStr) # returns int
  146. # myString.find(subStr) # returns first occurrence of str, or -1 on failure
  147. # # # case methods: myString.upper(), myString.lower()
  148. # # # Boolean/is methods: myString.isupper(), myString.isdigit()
  149. #
  150. # LIST
  151. # # again be able to SLICE
  152. # # +
  153. # myList.append(item)
  154. # myList.insert(i, item)
  155. # myList.extend(anotherList)
  156. # # -
  157. # myList.pop() # by index
  158. # myList.remove() # by value
  159. # # other
  160. # myList.count(item)
  161. # myList.sort() # "modify in place", no return
  162. # myList.reverse()
  163.  
  164. # DICT
  165. # use the key like an index
  166. # myDict[key] # retrieve the value, like myDict.get()
  167. # myDict[key] = new value # set a value, like myDict.update()
  168. # myDict.items() # for looping
  169. # myDict.keys() # returns a set-like object of just the keys
  170. # myDict.values() # returns a list-like object of just the values
  171.  
  172. # # SETS
  173. # mySet.add()
  174. # mySet.remove(item)
  175. # mySet.pop() # removes a random entry
  176.  
  177. # MODULES
  178. # MATH
  179. # import math
  180. # math.factorial()
  181. # math.floor()
  182. # math.ceil()
  183. # math.pow() # **, not same as math.exp()
  184. # math.sqrt()
  185. # math.e
  186. # math.pi
  187.  
  188. # # full vs partial import
  189. # # import math --> math.factorial()
  190. # from math import factorial, sqrt --> factorial()
  191.  
  192. # OPENING FILES
  193. # # good practice: Ch 12 Task 4, 7, 8
  194.  
  195. with open("filename.txt", "r") as f:
  196.     # # read() -> whole file as one big string
  197.     # # readlines() -> a list of line by line strings
  198.     myContent = f.readlines()
  199. # if I'm reading I like to leave the with/open() block as soon as I can
  200. # to free up memory.
  201.  
  202. import csv
  203. with open("filename.csv", "r") as f:
  204.     # myContent = csv.reader(f) # a list-like object of line by line lists of strings
  205.     myContent = list(csv.reader(f))
  206.  
  207.  
  208. # write
  209. with open("myNewFile.txt", "w") as f:
  210.     # if you're figuring something out (and maybe looping to create strings)
  211.     # ... you'll probably need to stay in this with/open block as you write the file
  212.     f.write("I am writing a string into this file.")
  213.  
  214.  
  215. # student question
  216. numList = [1, -5, 4, 37, -2, 512, -511, 8, -3, -55, 0]
  217. notNegNumList = []
  218. for n in numList:
  219.     if n >= 0: # or > 0 if you don't want 0
  220.         notNegNumList.append(n)
  221. # now notNegNumList has only numbers greater than 0
  222. print(notNegNumList)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement