Advertisement
jspill

webinar-exam-review-v4-2021-11-27

Nov 27th, 2021
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.01 KB | None | 0 0
  1. # Exam Review 2021 Nov 11/27
  2.  
  3. # LABS
  4. # Ch 3-13
  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.  
  8. # Comp 1 Basic syntax and knowledge: operators, data types, etc
  9. # DATA TYPES
  10. # strings / str
  11. # integers / int
  12. # floats
  13. # Boolean # True, False
  14. # list # simple array, [1, 2, 3]
  15. # dictionary / dict # associative array
  16. # tuple # immutable (1, 2, 3), functions return mult values in a tuple or list sometimes
  17. # set # unique values only, unordered
  18. # MODULES
  19. # math
  20. # csv # or standard filestream methods also tend to work
  21.  
  22. # OPERATORS
  23. # +
  24. # -
  25. # *
  26. # /
  27. # % # modulo, "how many whole items are left over?"
  28. # // # floor division
  29. # How many weeks and days is 25 days?
  30. # print(25//7) # 3 (3 * 7 gets to 21) weeks and...
  31. # print(25%7) # remaining 4 days leftover
  32. # ** # compare math.pow()
  33. # = # assignment
  34. # == # asking, "equality operator" -- seen in conditions: if/elif, while loops
  35. # !=
  36. # <
  37. # >
  38. # <=
  39. # >=
  40. # += # increment. x += 1 is the same as x = x+1
  41. # -=
  42. # # KEYWORDS that are used like operators
  43. # not
  44. # and
  45. # or
  46. # in # if someValue in someList
  47.  
  48. # Comp 2: Control Flow Structures
  49. # IF # if, if/else, if/elif/else
  50. # LOOPS
  51. # WHILE - like an IF that repeats as long as the condition stays True
  52. # FOR - doing a known number of times or doing some once for every ITEM in a CONTAINER
  53. # for someItem in someContainer:
  54. # for item in myList:
  55. # for ltr in myString:
  56. # for n in range(0, 27):
  57. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # X-Files characters
  58. # for i in range(len(myList)):
  59. #     if i == len(myList) - 1:
  60. #         print("{} is the last one.".format(myList[i]))
  61. #     else:
  62. #         print("{} is in the list at position {}".format(myList[i], i))
  63. # FUNCTIONS
  64. # defining vs calling
  65. # parameters vs "regular" or "outside" variables
  66. # def doThis(x, y): # calling the function doThis(3,4), doThis(math.pi, 187)
  67. #     # don't do this: x = 5
  68. #     return x * y / 2
  69. # return vs print() # do whichever the question says
  70. # know that methods are themselves functions
  71.  
  72. # ADVANCED CONTROL STRUCTURES
  73. # ch 10: try/except... Ch 10.3 and Lab 10.7
  74. # Ch 13: Classes
  75.  
  76. # def myFunction(): # function definition is outside this IF block
  77. # if __name__ = "__main__": # am I running from this file directly?
  78. #     # only happens if running this file directly
  79. #     myVar = input()
  80. #     print(myFunction(myVar))
  81.  
  82. # BUILT-IN functions
  83. # print()
  84. # help()
  85. # dir() # returns a list of attributes (methods, properties)
  86. # help(str)
  87. # print(dir(str))
  88. # for attr in dir(str):
  89. #     if not attr.startswith("_"):
  90. #         print(attr, end=" ")
  91. # help(str.join)
  92. # range()
  93. # len()
  94. # sum()
  95. # min()
  96. # max()
  97. # input()
  98. # bin()
  99. # int() # int(input())
  100. # float()
  101. # list()
  102. # set()
  103. # dict()
  104. # tuple()
  105. # type() # print(type(myList))
  106.  
  107. # STRINGS and STRING METHODS
  108. # Build up larger strings with str.format() or f strings
  109. # string.format()
  110. # "{} is a variable".format(myVar)
  111. # f string
  112. # f"{myVar} is a variable"
  113.  
  114. # SLICING STRINGS (or LISTS)
  115. # myString[start:stop] # myString[start:stop:step]
  116. myString = "Agent Mulder said 'Trust No One'"
  117. print(myString[0:12]) # "Agent Mulder"
  118. # you can reverse with a slice
  119. revString = myString[::-1]
  120. print(revString)
  121.  
  122. # STRING METHODS
  123. # myString.split() # returns a list of smaller strings
  124. # " ".join(someList)
  125. # myString.format()
  126. # myString.replace(oldSubString, newSubString) # also to remove
  127. # myString.upper() # lower(), capitalize(), title()
  128. # myString.isupper() # return Boolean
  129. # myString.find(subString) # returns the index , or -1 on failure
  130. # myString.startswith()
  131. # myString.strip() # also lstrip(), rstrip()
  132. # myString.count()
  133.  
  134. # print(input())
  135.  
  136. # LIST METHODS
  137. myList.append()
  138. myList.insert()
  139. myList.extend()
  140. myList.count()
  141. myList.pop() # myList.pop(2).. by index
  142. myList.remove() # by value
  143. myList.sort()
  144. myList.reverse()
  145.  
  146.  
  147. # Comp 3: modules, working with files
  148. # Ch 12... Task 4, 7, 8 and Ch 29 Labs, esp Lab 29.2
  149. f = open(filename, "r")
  150. contents = f.read() # whole file as string
  151. contents = f.readlines() # list of strings, line by line
  152. f.close()
  153. # or
  154. with open(filename, "r") as f:
  155.     contents = f.readlines()
  156.  
  157. # Question on Lab 19.2
  158. # Watch the spaces in those strings! This is where str.format() or f strings are helpful
  159. # Notice also that the 4 and 5 shown in the text are emulating the terminal inputs,
  160. # and don't seem to be intended as output for you to print
  161. user_num = int(input('Enter integer:\n'))
  162. print("You entered: {}".format(user_num))
  163.  
  164. print("{} squared is {}".format(user_num, user_num*user_num))
  165. print("And {} cubed is {} !!".format(user_num, user_num**3))
  166.  
  167. user_num2 = int(input("Enter another integer:\n"))
  168. print("{} + {} is {}".format(user_num, user_num2, user_num+user_num2))
  169. print("{} * {} is {}".format(user_num, user_num2, user_num*user_num
  170.  
  171. # Pre 19 wording question
  172. # You don’t need to manually raise an error here necessarily. I would approach
  173. # this as a try with 2 excepts (a value error and a zero division error).
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement