Advertisement
jspill

webinar-python-exam-review-2021-08-28

Aug 28th, 2021
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.94 KB | None | 0 0
  1. # webinar review 08/28/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. # floats
  9. # strings... str
  10. # lists
  11. # dictionaries... dict
  12. # sets
  13. # tuples
  14. # ## modules
  15. # math
  16. # random
  17. # datetime
  18. # calendar
  19. # pandas # Ref List
  20.  
  21. # OPERATORS
  22. # +
  23. # -
  24. # *
  25. # /
  26. # **
  27. # % # whole number remainder... or "what didn't fit?"
  28. # //
  29. # How many pounds and ounces is 93 ounces?
  30. print(93//16) # pounds
  31. print(93%16) # oz left over
  32. # = # assignment
  33. # == # "equality operator" is asking a question
  34. # += # x += 1 is the same x = x + 1
  35. # -=
  36. # !=
  37. # <
  38. # >
  39. # <=
  40. # >=
  41. # not
  42. # in
  43. # and
  44. # or
  45.  
  46. # CONTROL FLOW STRUCTURES
  47.  
  48. # FUNCTIONS
  49. # defining vs calling
  50. # parameters/arguments vs "regular" variables
  51. # def myFunction(someList):
  52. #   someList = [1, 2, 3] # don't do this!
  53. # myFunction([4, 5, 6]) # the call provides an ARG to give the parameter a value
  54. # myFunction([9, 7, 8])
  55. # return vs print()
  56. # methods are themselves functions
  57.  
  58. # IF... and IF/ELSE, IF/ELIF/ELSE
  59.  
  60. # LOOPS
  61. # WHILE - an IF that repeats
  62. # FOR - looping a number of times, matched to a container
  63. # for ___ in ___:
  64. # for item in myList:
  65. # for char in myString:
  66. # for key in aDictionary: # aDictionary[key] gets the value
  67. #   value = aDictionary[key]
  68. # for key, value in aDictionary.items():
  69. for i in range(0, 5):
  70.     print(i)
  71. myTuple = ("Gilligan", "Castaway002", "red", "crew") # things about Gilligan
  72. for item in myTuple:
  73.     print(item)
  74. for i in range(len(myTuple)):
  75.     print(i, myTuple[i])
  76. for i, item in enumerate(myTuple):
  77.     print(i, item)
  78.  
  79. # IN
  80. if "Gilligan" in myTuple: # x > 5
  81.     print("Found him!")
  82. print("Gilligan" in myTuple) # True
  83. print("Skipper" in myTuple) # False
  84.  
  85. # BUILT-IN FUNCTIONS
  86. # print()
  87. # help()# help(str)
  88. # dir() print(dir(str))
  89. # input()
  90. # len()
  91. # sum()
  92. # min()
  93. # max()
  94. # str()
  95. # list()
  96. # dict()
  97. # int()
  98. # float()
  99. # tuple()
  100. # range()
  101. # type()    print(type(myTuple))
  102. # enumerate()
  103. # round() # its cousins are in math: math.floor() and math.ceil()
  104. # sorted()
  105. # reversed()
  106.  
  107. # STRING
  108. ## know how to slice strings and lists
  109. myString = "Hello."
  110. # myString.split() # returns a list (of smaller strings) from this string
  111. # " ".join(someList)
  112. # myString.format() # more on that later
  113. # myString.find()
  114. # myString.replace(oldSubStr, newSubStr) # also to remove
  115. # myString.isupper()
  116. # myString.islower()
  117. # myString.istitle()
  118. # myString.isalpha()
  119. # myString.upper()
  120. # myString.lower()
  121. # myString.title()
  122. # myString.capitalize()
  123. # myString.count(subString)
  124. # myString.index(subString)
  125. # myString.strip() # has cousins lstrip(), rstrip()
  126.  
  127. # Building up larger strings...
  128. x = "Sue"
  129. greeting = "How do you do?"
  130. # CONCATENATE with +
  131. myString = "My name is " + str(x) + ". " + greeting
  132. # DATA CONVERSION SPECIFIERS or "string modulo"
  133. myString = "My name is %s. %s" % (x, greeting)
  134. # the FORMAT method
  135. myString = "My name is {}. {}".format(x, greeting)
  136.  
  137. y = 3.14159
  138. print("Pi to 2 decimals is {:.2f}".format(y))
  139. print(myString)
  140.  
  141. # SLICES
  142. # Be able to slice like it's second nature
  143.  
  144. # LISTS
  145. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # X-Files characters
  146. # myList.append(newValue)
  147. # myList.insert(i, newValue)
  148. # myList.extend(anotherList)
  149. # myList.remove(aValue)
  150. # myList.pop() # also by index myList.pop(i)
  151. # myList.count(someValue)
  152. # myList.sort() # myList.sort(reverse=False)
  153. # myList.reverse()
  154. # myList.index()
  155. # myList.copy()
  156. # myList.clear()
  157.  
  158. # TUPLES
  159. # so immutable we don't think about tuple methods
  160.  
  161. # DICTIONARIES
  162. myDict = {
  163.     "Scooby": "a blue collar",
  164.     "Shaggy": "green",
  165.     "Velma": "orange",
  166.     "Daphne": "purple",
  167.     "Fred": "an ascot"
  168. }
  169. # get the value
  170. myDict["Scooby"] # "a blue collar"
  171. myDict.get("Scooby") # "a blue collar"
  172. myDict["Scooby"] = "a red collar"
  173. myDict.update({"Scooby": "an orange collar"})
  174. # for k in myDict: # value is myDict[k]
  175. # for k, v in myDict.items() # if you really want 2 variables as you loop
  176.  
  177. # SETS
  178. # mySet = {1, 2, 3} # all unique values, no order
  179. # mySet.add(newValue)
  180. # mySet.remove(aValue)
  181. # mySet.discard(aValue)
  182. # mySet.pop() # random
  183.  
  184.  
  185. ## MODULES
  186. # MATH
  187. import math
  188. # math.sqrt()
  189. # math.factorial()
  190. # math.pow() # ** # not to be confused with math.exp()
  191. # math.floor()
  192. # math.ceil()
  193. # math.e
  194. # math.pi
  195.  
  196. # RANDOM
  197. import random
  198. # random.random() # random float b/n 0 and 1
  199. # random.choice() # random item in a list
  200. # random.randint(start, stop) # INCLUDES the stop number
  201. # random.randrange(start,stop) # EXCLUDES the stop number
  202.  
  203. # CALENDAR
  204. # import calendar
  205. # calendar.isleap()
  206. # calendar.weekday(y, m, d)
  207. # calendar.day_name[]
  208. # calendar.month_name[]
  209.  
  210. # DATETIME
  211. import datetime
  212. print(dir(datetime))
  213. # focus on
  214. # datetime.datetime # <-- contains datetime.date and datetime.time
  215. # datetime.timedelta
  216. dd = datetime.datetime(1971, 4, 3)
  217. dd = datetime.datetime.today()
  218. print(dd)
  219. print(dd.year)
  220. print(dd.month)
  221. print(dd.day)
  222. print(dd.hour)
  223. # print(dir(datetime.datetime))
  224.  
  225. # datetime.timedelta
  226. td = datetime.timedelta(days=10)
  227. print("10 days from today is {}.".format(dd + td))
  228. print(dir(datetime.timedelta))
  229. print(td.total_seconds())
  230.  
  231. # OS
  232. import os
  233. # os.getcwd()
  234. # os.listdir()
  235. # os.path.dirname()
  236. # os.path.basename()
  237. # os.path.exists()
  238. # os.path.isfile()
  239. # os.path.isdir()
  240.  
  241. # IMPORT STATEMENTS
  242. # make sure the way the object is referenced matches the import statement
  243. # "normal" full import
  244. import datetime # datetime.date.year
  245. # PARTIAL IMPORT
  246. from os import path # path.isdir() not os.path.isdir()
  247. from math import ceil # ceil() not math.ceil()
  248. # ALIAS IMPORT
  249. import math as m # m.ceil() not math.ceil()
  250.  
  251.  
  252. def myQuestionFunction(parameter1, parameter2):
  253.     h = help(list)
  254.     return h # return or print(), whichever questions says, HIT RUN
  255.     # delete or comment out above afterward
  256.  
  257.  
  258. # HTML is just strings!
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement