Advertisement
jspill

webinar-python-exam-review-2021-08-21

Aug 21st, 2021
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.26 KB | None | 0 0
  1. # webinar review 08/21/21
  2.  
  3. # Ch 8, 9, 11 and 12 end of chapter exercises are critical practice
  4. # ... and they're structured like most exam questions! No accident!
  5.  
  6. # Be able to recognize and use common data types and modules
  7. # integers / int
  8. # float
  9. # strings / str ""
  10. # lists []
  11. # dictionaries / dict {k:v}
  12. # tuples ()
  13. # set {}
  14. # # modules
  15. # math
  16. # random
  17. # datetime
  18. # os
  19. # calendar
  20. # pandas # Ref List
  21.  
  22. # OPERATORS
  23. # +
  24. # -
  25. # *
  26. # /
  27. # % # mod
  28. # // # floor division
  29. # How many pounds and ounces is 27 oz?
  30. print(27//16) # pounds
  31. print(27%16) # oz left over
  32. # ** # raise to power
  33. # = # assignment
  34. # == # equality... ASKS is it equal? Used if/elif, while loop conditions
  35. # += # x += 1 is the same as x = x + 1
  36. # -=
  37. # != # not equal, again for comparision, for conditions
  38. # not
  39. # <
  40. # >
  41. # <=
  42. # >=
  43. # in
  44.  
  45. # CONTROL FLOW STRUCTURES
  46.  
  47. # FUNCTIONS
  48. # defining vs calling
  49. # parameter vs arguments # almost mean the same thing
  50. # def myFunction(someList):
  51. #   someList = [1, 2, 3] # don't do this! Parameters aren't assigned in the function
  52. #   # more code
  53. # myFunction([4, 5, 6]) # the CALLS provide a value as an ARGUMENT
  54. # return vs print()
  55. # methods of a type are functions
  56.  
  57. # IF... and IF/ELSE, IF/ELIF/ELSE
  58.  
  59. # LOOPS
  60. # WHILE: an IF that repeats as long the condition stays TRUE
  61. # FOR: repeat an action once for every ITEM/VALUE in a container
  62. # for ___ in ___:
  63. # for item in myList:
  64. # for char in someString:
  65. # for key in someDictionary: # value is someDictionary[key]
  66.  
  67. # IN for a "membership check", "is it in there?"
  68. myTuple = ("Gilligan", "Castaway002", "red", "crew") # things about Gilligan
  69. if "red" in myTuple:
  70.     print("yes, it's there")
  71. print("Gilligan" in myTuple) # True
  72. print("Skipper" in myTuple) # False
  73. scoobiesDCT = {
  74.     "Scooby": "a blue collar",
  75.     "Shaggy": "green",
  76.     "Velma": "orange",
  77.     "Daphne": "purple",
  78.     "Fred": "an ascot"
  79. }
  80. print("Scooby" in scoobiesDCT) # True, b/c it looks at keys
  81. print("orange" in scoobiesDCT) # False, b/c it doesn't look at values
  82.  
  83. # BUILT-IN FUNCTIONS
  84. # print()
  85. # help() # help(str), help(str.join), help(datetime.datetime.today)
  86. # dir() # good combo with help()... returns a list of just names of a type's attributes
  87. # # format()... technically true, but string.format() may be more useful
  88. # len()
  89. # sum()
  90. # min()
  91. # max()
  92. # str() # ""
  93. # list() # []
  94. # dict() # {}
  95. # set()
  96. # int()
  97. # float()
  98. # tuple() # ()
  99. # round() # the "normal" round, its cousins .ceil() and .floor() are in MATH
  100. # range() # for i in range(len(someList))
  101. # enumerate()
  102. # type()
  103.  
  104. # STRING
  105. ## know how to slice a string or list
  106. # myString = "Hello."
  107. # myString.split() # creates a list of smaller strings
  108. # " ".join(someList)
  109. # myString.format() # the WINNER way to build bigger strings
  110. # myString.isupper() # is methods return Boolean
  111. # myString.islower()
  112. # myString.upper()
  113. # myString.lower()
  114. # myString.capitalize()
  115. # myString.title()
  116. # myString.find(subString)
  117. # myString.replace(oldSubString, newSubString) # also good for removal
  118. # myString.count(subString)
  119. # myString.index(subString)
  120. # myString.strip() # has cousins lstrip(), rstrip()
  121.  
  122. # Building up larger strings...
  123. x = "Sue"
  124. greeting = "How do you do?"
  125. # CONCATENATE with +
  126. myString = "My name is " + str(x) + ". " + greeting
  127. # DATA CONVERSION SPECIFIERS or "string modulo"
  128. myString = "My name is %s. %s" % (x, greeting)
  129. # the FORMAT method
  130. myString = "My name is {}. {}".format(x, greeting)
  131. print(myString)
  132.  
  133. # LISTS
  134. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # X-Files characters
  135. # myList.append(someItem)
  136. # myList.insert(i, someItem)
  137. # myList.extend(anotherList)
  138. # myList.pop()# last one, or by index
  139. # myList.remove() # by value
  140. # myList.count(someItem)
  141. # myList.sort() # myList.sort(reverse=True)
  142. # myList.reverse()
  143. # myList.index(someItem)
  144. # myList.copy()
  145. # myList.clear()
  146.  
  147. # SLICES
  148. # Be able to slice like it's second nature
  149.  
  150. # DICTIONARIES
  151. myDict = {
  152.     "Scooby": "a blue collar",
  153.     "Shaggy": "green",
  154.     "Velma": "orange",
  155.     "Daphne": "purple",
  156.     "Fred": "an ascot"
  157. }
  158. # get the value
  159. myDict["Scooby"] # "a blue collar"
  160. myDict.get("Scooby") # "a blue collar"
  161. myDict["Scooby"] = "a red collar"
  162. myDict.update({"Scooby": "an orange collar"})
  163. # for k in myDict: # value is myDict[k]
  164. # for k, v in myDict.items() # if you really want 2 variables as you loop
  165.  
  166. # SETS
  167. mySet = {1, 2, 3}
  168. # mySet.add(newValue)
  169. # mySet.remove(aValue) # by value
  170. # mySet.discard(aValue) # by value
  171. # mySet.pop()
  172.  
  173. # TUPLES
  174. # don't worry about tuple methods
  175.  
  176. # help(set.pop)
  177. # myTuple = (1, 2, 3)
  178.  
  179. ## MODULES!
  180. # MATH
  181. import math
  182. # math.sqrt()
  183. # math.pow() # not to be confused with math.exp()
  184. # math.factorial()
  185. # math.ceil()
  186. # math.floor()
  187. # math.e
  188. # math.pi
  189.  
  190. # RANDOM
  191. import random
  192. # random.random() # returns a float b/n 0 and 1
  193. # random.choice() # random item from list
  194. # random.randint(start, stop) # randint INCLUDES the stop number
  195. # random.randrange(start, stop) # randrange EXCLUDES the stop number
  196.  
  197. # CALENDAR
  198. import calendar
  199. # calendar.weekday()
  200. # calendar.day_name[]
  201. # calendar.month_name[]
  202. # calendar.isleap(year)
  203.  
  204. # DATETIME
  205. import datetime
  206. datetime.datetime # datetime.date and datetime.time
  207. datetime.timedelta # represents a period of time
  208. dd = datetime.datetime(1978, 9, 3)
  209. dd = datetime.datetime.today()
  210. print(dd.year)
  211. print(dd.month)
  212. print(dd.day)
  213. print(dd)
  214. # timedelta
  215. td = datetime.timedelta(days=3)
  216. print(dd + td)
  217. print(td.total_seconds())
  218.  
  219. # OS
  220. import os
  221. # os.getcwd()
  222. # os.listdir()
  223. # os.path.dirname()
  224. # os.path.basename()
  225. # os.path.isfile()
  226. # os.path.isdir()
  227. # os.path.exists()
  228.  
  229. # IMPORT STATEMENTS
  230. # make sure the way the object is referenced matches the import statement
  231. # "normal" full import
  232. import datetime # datetime.date.year
  233. # PARTIAL IMPORT
  234. from os import path # path.isdir() not os.path.isdir()
  235. from math import ceil # ceil() not math.ceil()
  236. # ALIAS IMPORT
  237. import math as m # m.ceil() not math.ceil()
  238.  
  239. # help(str)
  240. # print(dir(str))
  241. # help(str.find)
  242.  
  243. # student questions
  244. # if no output from help on exam, try
  245. def myQuestionFunction(parameter1, parameter2):
  246.     h = help(list)
  247.     return h # return or print(), whichever questions says, HIT RUN
  248.     # delete or comment out above afterward
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement