Advertisement
jspill

webinar-python-exam-review-2021-06-26

Jun 26th, 2021
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.08 KB | None | 0 0
  1. # webinar review 06/26/21
  2.  
  3. # Good group! We'll get started in a moment...
  4.  
  5. # Ch 8, 9, 11 and 12 end of chapter exercises are critical practice
  6. # ... and they're structured like most exam questions! No accident!
  7.  
  8. # Be able to recognize and use common data types and modules
  9. # integers # int
  10. # strings # str
  11. # floats
  12. # lists
  13. # dictionaries # dict
  14. # sets
  15. # tuples
  16. # # modules
  17. # math
  18. # random
  19. # calendar
  20. # datetime
  21. # os
  22. # pandas
  23.  
  24. # operators
  25. # = # assignment operator
  26. # +
  27. # -
  28. # *
  29. # /
  30. # % # full number remainder, how many are left over?
  31. # // # floor division
  32. # # how many pounds and ounces is 41 ounces?
  33. # # print(41 // 16, "pounds and")
  34. # # print(41 % 16, "ounces")
  35. # ** # raise to a power
  36. # == # you're asking if it's equal... if/elif or while
  37. # += # incrementing, x += 1 is the same as x = x + 1
  38. # -=
  39. # != # asking, not equal?
  40. # >
  41. # <
  42. # >=
  43. # <=
  44. # not
  45. # in # "the membership check" if __ in __
  46. # and
  47. # or
  48.  
  49. # CONTROL STRUCTURES
  50.  
  51. # # FUNCTIONS
  52. # # defining vs calling
  53. # # parameters vs arguments
  54. # # parameters are not like other variables
  55. # def myFunction(x):
  56. #   # don't do this! x = 5 # don't hard code your parameters
  57. # myFunction(5)
  58. # myFunction(27)
  59. # return vs print()
  60. # know your useful built-in functions
  61. # understand class methods are themselves functions: str class split(), join(), etc, list append(), pop()
  62.  
  63. # IF and IF/ELSE and IF/ELIF/ELSE
  64.  
  65. # LOOPS
  66. # WHILE - pretty much an IF that repeats
  67. # FOR - for repeating a number of times, or most commonly ONCE per value in a CONTAINER
  68. # for __ in __:
  69. # for item in myList:
  70. # for char in myString:
  71. # for key in myDictionary: # myDictionary[key] --> accesses the value for that key
  72.  
  73. # in for "membership check"
  74. myTuple = ("Gilligan", "Castaway002", "red", "crew")
  75. # item __ in __
  76. if "red" in myTuple:
  77.     print("I found red!")
  78. print("red" in myTuple)
  79. print("blue" in myTuple)
  80.  
  81. # STRING METHODS
  82. # # help(str)
  83. # print(dir(str))
  84. # myString.count()
  85. # myString.split() # returns a list from the string
  86. # " ".join(someList) # returns a long str from a list plus a seperator string
  87. # myString.find()
  88. # myString.replace()
  89. # myString.index(someValue)
  90. # myString.isupper()
  91. # myString.islower()
  92. # myString.istitle()
  93. # myString.upper()
  94. # myString.lower()
  95. # myString.title()
  96. # myString.capitalize()
  97. # myString.format()
  98. # myString.strip() # plus lstrip() and rstrip()
  99.  
  100. # Building up larger strings
  101. x = "Sue"
  102. greeting = "How do you do?"
  103. # I could concatenate:
  104. # myString = "My name is " + x + ". " + greeting
  105. # myString = "My name is %s. %s" % (x, greeting)
  106. myString = "My name is {}. {}".format(x, greeting)
  107. print(myString)
  108.  
  109. # Be able to SLICE like it's second nature.
  110.  
  111. # LIST METHODS
  112. myList = ["Loki", "Sylvie", "Agent Mobius"]
  113. # myList.append(someItem)
  114. # myList.extend(someOtherList)
  115. # myList.insert(i, someItem)
  116. # myList.pop() # last item, or by index
  117. # myList.remove() # by value
  118. # myList.index()
  119. # myList.sort() # myList.sort(reverse=True)
  120. # myList.reverse()
  121. # myList.count(someItem)
  122. # myList.copy()
  123. # myList.clear()
  124. # print(myList)
  125.  
  126. # DICTIONARIES
  127. # someDictionary[someKey] # gets the value for that key, myDictionary.get()
  128. # someDictionary[someKey] = someValue # sets a value, replaces myDictionary.update()
  129. scoobies = {
  130.     "Scooby": "a blue collar",
  131.     "Shaggy": "green",
  132.     "Velma": "orange",
  133.     "Daphne": "purple",
  134.     "Fred": "an ascot"
  135. }
  136. scoobies["Scooby Dumb"] = "a red collar"
  137. for key in scoobies:
  138. # for key, value in scoobies.items():
  139.     print("{} always wears {}.".format(key, scoobies[key]))
  140. # myDictionary.get()
  141. # myDictionary.items()
  142. # myDictionary.update()
  143. # # myDictionary.keys()
  144. # # myDictionary.values()
  145.  
  146. # SETS - remembers sets have ONLY UNIQUE values and HAVE NO ORDER
  147. # mySet.add()
  148. # mySet.remove()
  149. # mySet.discard()
  150.  
  151. # TUPLES
  152. # no methods are important here
  153.  
  154. # MODULES
  155. import math
  156. # math.sqrt()
  157. # math.pow() # **
  158. # math.ceil()
  159. # math.floor()
  160. # math.exp() # don't confuse with math.pos()
  161. # math.fsum()
  162. # math.factorial()
  163. # math.e
  164. # math.pi
  165.  
  166. # RANDOM
  167. import random
  168. # random.random()
  169. # random.choice()
  170. # random.randint(start, stop) # INCLUDES the stop
  171. # random.randrange(start, stop) # EXCLUDES the stop
  172.  
  173. # DATETIME
  174. import datetime
  175. #print(dir(datetime))
  176. # focus on:
  177. # datetime.datetime
  178. # datetime.datetime(year, month, day)
  179. # datetime.datetime.today()
  180. dd = datetime.datetime.today()
  181. print(dd.day)
  182. # datetime.datetime.day
  183. # datetime.datetime.month
  184. # datetime.datetime.year
  185. # datetime.datetime.hour
  186. print(dd)
  187. # datetime.timedelta
  188. # datetime.timedelta(days=90)
  189. td = datetime.timedelta(weeks=6)
  190. print(td)
  191. print(dd + td)
  192. print(dd + datetime.timedelta(days=1))
  193. print(td.total_seconds())
  194.  
  195. # os
  196. import os
  197. # os.getcwd()
  198. # os.listdir()
  199. # os.path.basename()
  200. # os.path.dirname()
  201. # os.path.isfile()
  202. # os.path.isdir()
  203. # os.path.exists()
  204.  
  205. # Complete the function to return FILE if the given path is a file
  206. # or return DIRECTORY if the given path is a directory
  207. # or return NEITHER is it's not a file or directory
  208. def whatIsIt(somePath):
  209.     if os.path.isdir(somePath):
  210.         return "DIRECTORY"
  211.     elif os.path.isfile(somePath):
  212.         return "FILE"
  213.     else:
  214.         return "NEITHER"
  215. print(whatIsIt(os.getcwd())) # DIRECTORY
  216. print(whatIsIt(os.listdir(os.getcwd())[1])) # FILE or DIRECTORY
  217. print(whatIsIt('apple.pie.123.txt')) # NEITHER
  218.  
  219. # TYPES OF IMPORT STATEMENT
  220. # regular...
  221. # import os # os.getcwd()
  222. # import math # math.e
  223. # partial...
  224. # from math import e # print(e) not print(math.e)
  225. # from os import path # path not os.path
  226. # alias
  227. # import datetime as dt # dt.datetime.today()
  228. # improt math as m # m.e, m.pow() etc
  229.  
  230. # HTML is just STRINGS
  231.  
  232. # BUILT IN FUNCTIONS
  233. # help()
  234. # dir()
  235. # print()
  236. # str()
  237. # int()
  238. # float()
  239. # list()
  240. # tuple()
  241. # dict()
  242. # enumerate()
  243. # range()
  244. # len()
  245. # sum()
  246. # min()
  247. # max()
  248. # round()
  249. # type()
  250.  
  251. for i in range(len(myList)):
  252.     print("{} --> {}".format(i, myList[i]))
  253.  
  254. myString = [1, 2, 3] # wrong! this isn't a string!
  255. print(type(myString)) # <class 'list'> #... thank you type()!
  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.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement