Advertisement
jspill

webinar-python-exam-review-v4-2021-10-23

Oct 23rd, 2021
560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.90 KB | None | 0 0
  1. # Exam Review 2021 Oct 23
  2.  
  3. # LABS
  4. # Ch 3-13
  5. # Ch 14-18 not as important
  6. # Ch 19-30 just LABS, but important practice!
  7.  
  8. # Look at ALL requirements of a question
  9. # CODE must be reusable
  10. # don't be afraid of Submit for Grading...
  11. # different TEST CASES are part of the problem
  12.  
  13. # Comp 1 Basic syntax and knowledge of operators, data types, etc
  14. # DATA TYPES
  15. # integers... int
  16. # floats
  17. # string... str
  18. # Boolean: True and False
  19. # lists
  20. # dictionaries... dict # nameOfDict[key]
  21. # sets
  22. # tuples
  23. # MODULES... not as much here in v4!
  24. # math
  25. # CSV... a trick: you can ofter use open(), read(), etc
  26.  
  27. # OPERATORS
  28. # +
  29. # -
  30. # *
  31. # /
  32. # % # modulo... whole number remainder... how many whole things didn't fit?
  33. # // # floor division... modulo and floor division useful in unit conversion
  34. # How many pounds/ounces is 47 ounces?
  35. # print(47//16) # pounds
  36. # print(47%16) # and ounces left over
  37. # ** # math.pow()... not math.exp()
  38. # += # increment
  39. # -= # decrement
  40. # = # assign
  41. # == # comparison, asking a question?
  42. # !=
  43. # not
  44. # <
  45. # >
  46. # <=
  47. # >=
  48. # and
  49. # or
  50. # in # if someItem in someList
  51.  
  52. # Comp 2: Control Flow Structures
  53. # IF... if, if/else, if/elif/else
  54. # LOOPS
  55. # WHILE -> an IF that repeats as long the condition stay true
  56. # FOR LOOP -> match for a container like a list, etc
  57. # BREAK -- ends the loop, completely
  58. # CONTINUE -- skips to the NEXT ITERATION of the same loop
  59. # for someItem in someContainer:
  60. # for item in myList:
  61. # for item in myString.split():
  62. # FUNCTIONS
  63. # defining vs calling a function
  64. # parameters vs "regular" variables # see webinar on Functions + Methods
  65. # def myFunction(x):
  66. # #     # x = "hey" # don't do it!
  67. # #     # each all will provide a value for x, not me
  68. # return vs print()
  69.  
  70. # ADVANCED CONTROL FLOW
  71. # Try/Except # Ch 10, see 10.3 and Lab 10.7
  72. # Classes # Ch 13
  73.  
  74. # BUILT-IN FUNCTIONS
  75. # print()
  76. # input() # str, print(input())
  77. # len()
  78. # range() # loop over the range() of len()
  79. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]
  80. # for item in myList:
  81. #     print(item)
  82. # for i in range(len(myList)):
  83. #     print("{}: {}".format(i, myList[i]))
  84. # for i, item in enumerate(myList):
  85. #     print(f"{i} --> {item}")
  86. # enumerate()
  87. # max()
  88. # min()
  89. # sum()
  90. # sorted() # not to be confused list.sort()
  91. # reversed() # not to be confused list.reverse()
  92. # # type()
  93. # int()
  94. # float()
  95. # list() # []
  96. # dict() # {}
  97. # str()
  98. # set()
  99. # tuple()
  100.  
  101.  
  102. # help()
  103. # dir()
  104. # help(str)
  105. # print(dir(str))
  106. # help(str.count)
  107. # for item in dir(str):
  108. #     if not item.startswith("__"):
  109. #         print(item, end=" ")
  110.  
  111. # STRINGS
  112. # 4 ways to build up strings
  113. # + # concatenation "this string" + " this string " + str(4)
  114. # string modulo # "%f" % (3.14)
  115. # "{} is a variable".format(myVar)
  116. # f"{myVar} is a variable"
  117.  
  118. # SLICES
  119. # a SLICE is an abbreviated copy
  120. myString = "hey"
  121. print(myString[::-1]) # reversed string!
  122.  
  123. # STRING METHODS (most important methods... there are many others!)
  124. # myString.split()
  125. # myString.format() # or use f strings
  126. # " ".join(someList)
  127. # myString.replace(oldSubString, newSubString)
  128. # myString.upper() or myString.lower()
  129. # myString.isupper()
  130. # myString.count()
  131.  
  132. # LISTS
  133. # myList.append()
  134. # myList.extend() # add another container's item
  135. # myList.insert(index, item)
  136. # myList.count()
  137. # myList.pop() # by index
  138. # myList.remove() # by value
  139. # myList.sort()
  140. # myList.reverse()
  141. # myList.copy()
  142. # myList.clear()
  143. # myList.index(item) # not as reliable as looping with range() or enumerate()
  144.  
  145. # SETS most important methods...
  146. # mySet.add()
  147. # mySet.remove() # by value, raise en error if not here
  148. # mySet.discard() # by value, no errors if not there
  149. # mySet.pop() # set pop() takes out a random item
  150.  
  151. # opening FILES
  152. # good practice here will be Ch 12 Task 4, 7, 8
  153. f = open(filename, "r")
  154. contents = f.read()
  155. f.close()
  156. # or
  157. with open(filename, "r"):
  158.     contents = f.read()
  159.  
  160. # Let's look at Lab 5.14 and Lab 24.3!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement