Advertisement
jspill

webinar-python-exam-review-2021-05-22

May 22nd, 2021
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.21 KB | None | 0 0
  1. # Webinar: Exam Review
  2.  
  3. # Ch 8, 9, 11 and 12 end of chapter exercises are critical.
  4. # And structured a lot like the Pre and OA questions
  5.  
  6. # Be able to recognize your common data types and modules
  7. # integers
  8. # floats
  9. # strings # str
  10. # lists
  11. # dictionaries
  12. # tuples
  13. # sets
  14. # # modules
  15. # math
  16. # random
  17. # datetime
  18. # os
  19. # calendar
  20. # pandas
  21.  
  22. # operators
  23. # +
  24. # -
  25. # *
  26. # /
  27. # //
  28. # % # modulo asks "what's the whole number remainder?" or "how many didn't fit?"
  29. print(51 // 16, "pounds and")
  30. print(51 % 16, "ounces")
  31. # **
  32. # = # assignment operator, setting something to equal
  33. # == # equality operator, asking if they're equal, as a condition for if/elif, or a while loop
  34. # !=
  35. # <
  36. # >
  37. # <=
  38. # >=
  39. # not
  40. # is
  41. # and
  42. # or
  43. # += # x += 1 is the same as x = x + 1
  44. # -=
  45.  
  46. # FUNCTIONS
  47. # defining vs calling
  48. # parameters vs arguments
  49. # return vs print()
  50. # methods of a data type are themselves functions
  51.  
  52. # IF and IF/ELSE and IF/ELIF/ELSE
  53.  
  54. # LOOPS
  55. # WHILE LOOP - like an IF that repeats if its condition remains True
  56. # FOR LOOP - tied to a container, like list, string, etc
  57. # for __ in _iterable_:
  58. # for item in myList:
  59. # for char in myString:
  60. # for key in myDictionary: # myDictionary[key]
  61. # for i in range(0, 7):
  62. # for i in range(len(myList)):
  63.  
  64. # "membership check"
  65. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # X-Files characters
  66. print("Agent Scully" in myList) # True
  67. print("Scooby" in myList) # False
  68.  
  69. # Adebayo asks how to get the longest string in a list
  70. # print(max(myList))
  71. # help(max)
  72.  
  73. # for item in myList:
  74. #     longest = ""
  75. #     if len(item) > len(longest):
  76. #         longest = item
  77. # print("The longest item is {}, with a length of {}.".format(longest, len(longest)))
  78.  
  79. # Be able to SLICE
  80. print(myList[0:3])
  81. print(myList[-2:])
  82.  
  83. # Building up STRINGS
  84. x = "Sue"
  85. greeting = "How do you do?"
  86. # CONCATENATION - easy to do, but easy to mess up
  87. myString = "My name is "  + str(x) + ". " + greeting
  88. # DATA CONVERSION SPEC or STRING MODULO
  89. myString = "My name is %s. %s" % (x, greeting)
  90. # STRING CLASS FORMAT() METHOD ---> THE BESTEST WAY
  91. myString = "My name is {}. {}".format(x, greeting)
  92. print(myString)
  93. # F strings DO NOT WORK in exam environment
  94.  
  95. # learn those METHODS of each type
  96. # STRING
  97. # myString.join() # starts with separator string ", ".join(someIterable)
  98. # myString.split() # turns a string into a list, splitting on a separator string
  99. # myString.count() # looks for occurence of some substring
  100. # myString.strip() # has cousins: lstrip(), rstrip()
  101. # myString.replace() # replace a substring with a new substring
  102. # myString.format()
  103. # myString.isupper() # plus other is---() methods
  104. # myString.lower() # return a copy forced lowercase
  105. # myString.upper()
  106. # myString.title()
  107. # myString.capitalize()
  108. # myString.find() # finds a substring and returns the index, or -1 on failure
  109. # myString.startswith()
  110. #
  111. # # LISTS []
  112. # myList.append()
  113. # myList.pop()
  114. # myList.remove()
  115. # myList.insert()
  116. # myList.sort() # has a keyword arg of reverse: myList.sort(reverse=True)
  117. # myList.extend() # add another list, item by item
  118. # myList.reverse()
  119. # myList.count() # count occurrences of an item/value in the list
  120. # myList.index() # myList.index(item)
  121. # myList.clear()
  122. # myList.copy()
  123.  
  124. for item in myList:
  125.     print(item)
  126. print(myList[-1])
  127.  
  128. # for item in myList:
  129. for i in range(len(myList)): # for i in enumerate(myList):
  130.     if i == len(myList) - 1:
  131.         print("{} is the last person in the list.".format(myList[i]))
  132.     else:
  133.         print(myList[i])
  134.  
  135. # DICTIONARIES {k:v}
  136. # someDictionary[key] --> retrieves value for that key
  137. # someDictionary[key] = new value
  138. scoobies = {
  139.     "Scooby": "a blue collar",
  140.     "Shaggy": "green",
  141.     "Velma": "orange",
  142.     "Daphne": "purple",
  143.     "Fred": "an ascot"
  144. }
  145. scoobies["Scooby Dumb"] = "a red collar"
  146. for key in scoobies:
  147.     print(key, "=", scoobies[key])
  148.  
  149. # SETS {}
  150. # mySet.add()
  151. # mySet.update()
  152. # mySet.pop()
  153. # mySet.remove()
  154.  
  155. # TUPLES ()
  156. # are immutable
  157.  
  158. # MODULES
  159.  
  160. # 3 types of import statement
  161. import math # import the whole thing, math.pow()
  162. # PARTIAL IMPORT
  163. # from math import pow # now I write pow()
  164. # ALIAS IMPORT
  165. # import math as m # now it's m.pow(), etc
  166.  
  167. # MATH
  168. # math.pow() # raise a number to a power, not to be confused with math.exp()
  169. # math.sqrt()
  170. # math.e
  171. # math.pi
  172. # math.floor() # always rounds down
  173. # math.ceil() # always rounds up
  174.  
  175. # RANDOM
  176. # random.random() # gets a float 0-1
  177. # random.choice() # gets a random value from a list
  178. # random.randInt() # INCLUSIVE of the stop number
  179. # random.randRange() # EXCLUSIVE of the stop number
  180.  
  181. # DATETIME
  182. # datetime.datetime # copy of datetime.date and datetime.time
  183. # datetime.timedelta # a duration of time
  184. import datetime
  185. td = datetime.timedelta(weeks=4)
  186. td.total_seconds()
  187. dd = datetime.datetime(2021, 5, 22)
  188. dd = datetime.datetime.today()
  189. print(dd)
  190. print(dd.year)
  191. print(dd.hour)
  192.  
  193. # OS
  194. # import os
  195. # os.getcwd()
  196. # os.listdir()
  197. # os.path.basename()
  198. # os.path.dirname()
  199. # os.path.isfile()
  200. # os.path.isdir()
  201.  
  202. # BUILT IN FUNCTIONS
  203. # print()
  204. # dir()
  205. # help()
  206. # type()
  207. # enumerate()
  208. # input()
  209. # str()
  210. # list()
  211. # tuple()
  212. # dict()
  213. # int()
  214. # float()
  215. # range()
  216. # round()
  217. # len()
  218. # max()
  219. # min()
  220. # sum()
  221.  
  222. # HTML is just strings
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement