jspill

webinar-python-exam-review-2021-07-31

Jul 31st, 2021 (edited)
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.65 KB | None | 0 0
  1. # Webinar: Exam Review 7/31/21
  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. # int
  8. # float
  9. # str
  10. # list
  11. # dictionaries
  12. # tuples
  13. # sets
  14. ## modules # --> Ref List
  15. # math
  16. # random
  17. # datetime
  18. # os
  19. # calendar
  20. # pandas
  21.  
  22. # Built-In Functions
  23.  
  24. # operators
  25. # +
  26. # -
  27. # *
  28. # /
  29. # % # whole number remainder, "what's left over" or "how many didn't fit?"
  30. # // # floor division
  31. # how many pounds and ounces is 53 ounces?
  32. print(53 // 16) # pounds, and...
  33. print(53 % 16) # ounces left over
  34. # **
  35. # = # assignment
  36. # == # comparison, asking if they're equal: if/elif, while
  37. # += # incrementing, x += 1 is the same x = x + 1
  38. # -=
  39. # !=
  40. # <
  41. # >
  42. # <=
  43. # >=
  44. # not
  45. # and
  46. # or
  47. # in
  48.  
  49. # CONTROL STRUCTURES
  50.  
  51. # FUNCTIONS
  52. # defining vs calling
  53. # parameters vs arguments (vs "regular variables")
  54. # return vs print()
  55. # methods of a type are themselves functions
  56.  
  57. # IF and other conditional statements: IF, IF/ELSE, IF/ELIF/ELSE
  58.  
  59. # LOOPS
  60. # WHILE LOOPS - an if that repeats as long as the condition stays true
  61. # FOR LOOPS- tied to a container, do something once for everything in that list, etc
  62. # for _someVar_ in _someContainer_:
  63. # for item in myList:
  64. # for char in myString:
  65. # for key in myDictionary: # myDictionary[key]
  66. # for n in range(0, 5):
  67. # for i in range(0, len(myList)):
  68.  
  69. # IN... the "membership check"
  70. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # X-Files characters
  71.  
  72. print("Agent Scully" in myList)
  73. # print("Krychek" in myList)
  74. # if "Agent Mulder" in myList:
  75. #     print("Mulder is in the list.")
  76.  
  77.  
  78. # SLICING
  79. print(myList[0:2])
  80. print(myList[-2:])
  81.  
  82. # STRINGS
  83. # Building up longer 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 on the exam
  94.  
  95. # decimal precision with string .format()
  96. import math
  97. print("Pi to 5 places is {:.5f}".format(math.pi))
  98.  
  99. # STRING METHODS # --> use help(str)
  100. # help(str)
  101. print(dir(str))
  102. help(str.rstrip)
  103. # def myFunction(x,y):
  104. #     h = help(list)
  105. #     return h
  106. # OK... so what are the big string methods
  107. # myString.split()
  108. # ", ".join(someList)
  109. # myString.format()
  110. # myString.find()
  111. # myString.replace() # also used remove
  112. # myString.lower()
  113. # myString.upper()
  114. # myString.title()
  115. # myString.capitalize()
  116. # myString.isupper() # many isXYZ() methods that return Boolean
  117. # myString.startswith()
  118. # myString.count()
  119. # myString.strip() # don't forget its cousins: lstrip(), rstrip()
  120.  
  121. # LIST methods
  122. # print(dir(list))
  123. # myList.append()
  124. # myList.insert()
  125. # myList.extend()
  126. # myList.pop() # last or index number
  127. # myList.remove() # by value
  128. # myList.sort() # myList(reverse=True)
  129. # myList.reverse()
  130. # myList.count()
  131. # myList.index()
  132. # myString.copy()
  133. # myString.clear()
  134. myOtherList = ["Cunningham", "Fonzie", "Chaci", "Arnold"]
  135. # myList.append(myOtherList)
  136. # myList.extend(myOtherList)
  137. # help(list.extend)
  138. print(myList)
  139.  
  140. # SETS {}
  141. # mySet.add()
  142. # mySet.update()
  143. # mySet.pop() # but random!
  144.  
  145. # TUPLES ()
  146. # are so immutable they hardly have methods
  147.  
  148. # DICTIONARIES {k:v}
  149. # someDictionary[key] # retrieves value... so same as someDictionary.get(key)
  150. # someDictionary[key] = someNewValue # similar to dictionary update()
  151. # someDictionary.update({aKey:value})
  152. scoobies = {
  153.     "Scooby": "a blue collar",
  154.     "Shaggy": "green",
  155.     "Velma": "orange",
  156.     "Daphne": "purple",
  157.     "Fred": "an ascot"
  158. }
  159. scoobies["Scooby Dumb"] = "a red collar"
  160. scoobies.update({"Scrappy Doo": "a blue collar"})
  161. for key in scoobies:
  162.     print('{} always wears {}.'.format(key, scoobies[key]))
  163.  
  164. # MODULES
  165.  
  166. # MATH
  167. # import math
  168. # math.pow() # **, not to be confused with math.exp()
  169. # math.e # Euler's number
  170. # math.pi # 3.14159...
  171. # math.sqrt()
  172. # math.factorial()
  173. # math.ceil() # always rounds up
  174. # math.floor() # always round down
  175.  
  176. # RANDOM
  177. # import random
  178. # random.random() # returns a float b/n 0 and 1
  179. # random.choice() # random item from a list
  180. # random.randrange() # EXCLUDES the stop
  181. # random.randint() # INCLUDES the stop
  182.  
  183. # CALENDAR
  184. # import calendar
  185. # calendar.isleap()
  186. # calendar.day_name
  187. # calendar.month_name
  188. # calendar.weekday(y, m, d)
  189.  
  190. # DATETIME
  191. import datetime
  192. # datetime.datetime # representing a point in time, combo of datetime.date + datetime.time
  193. dd = datetime.datetime(2021, 7, 31)
  194. dd = datetime.datetime.today()
  195. print(dd)
  196. print(dd.year)
  197. print(dd.month)
  198. print(dd.day)
  199. print(dd.strftime("%b")) # www.strftime.org
  200.  
  201. # datetime.timedelta # a length or delta of time
  202. td = datetime.timedelta(weeks=2, days=1, hours=3)
  203.  
  204. print(td.total_seconds())
  205. print(dd - td)
  206.  
  207. # OS
  208. import os # full import
  209. # os.getcwd()
  210. # os.listdir()
  211. # os.path.basename()
  212. # os.path.dirname()
  213. # os.path.isfile()
  214. # os.path.isdir()
  215.  
  216. # IMPORT STATEMENTS
  217. import os # full import
  218. from datetime import timedelta # partial import
  219. #... so now timedelta(days=9) NOT datetime.timedelta(days=9)
  220. from math import ceil
  221. #... so ceil(x) instead of math.ceil(x)
  222.  
  223. import datetime as d # alias import
  224. #... so now d.timedelta(days=3) or d.datetime.today()
  225.  
  226. # BUILT-IN FUNCTIONS
  227. # print()
  228. # help()
  229. # dir()
  230. # range()
  231. # len()
  232. # sum()
  233. # max()
  234. # min()
  235. # round() # regular round() is not in math
  236. # int()
  237. # float()
  238. # list()
  239. # set()
  240. # dict()
  241. # tuple()
  242. # str()
  243. # enumerate()
  244. # input() # get terminal input from user
  245. # type()
  246.  
  247. print(type(myList))
  248.  
  249. # HTML is just strings
  250.  
  251. # def myFunction(someDictionary):
  252.     # do something with a dictionary
  253.  
  254. # myFunction({"Toyota": "Camry", "Chevy": "Silverado"})
  255. # myFunction(scoobies)
  256.  
  257.  
  258. # merging 2 lists into a dictionary as keys and values
  259. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]  # X-Files characters
  260. actorList = ["Gillian Anderson", "David Duchovny", "Mitch Pileggi", "Bob", "Joe"]
  261.  
  262. # My apologies to William B Davis and Steven Williams, who played CSM and Mr. X respectively on The X-Files.
  263. actorList[-1] = "Steven Williams"
  264. actorList[-2] = "William B Davis"
  265.  
  266. def makeDict(someList, anotherList):
  267.     newDict = {}
  268.     #for item in myList:
  269.     for i in range(len(someList)): # <-- I forgot to change this in the webinar
  270.         # newDict[actorList[i]] = myList[i]
  271.         newDict[someList[i]] = anotherList[i]
  272.     return newDict
  273. print(makeDict(actorList, myList))
  274. print(makeDict(["a", "b", "c"], ["Ernie", "Bert", "Cookie Monster"]))
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
Add Comment
Please, Sign In to add comment