Advertisement
jspill

webinar-python-exam-review-2021-07-24

Jul 24th, 2021
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.71 KB | None | 0 0
  1. # webinar review 07/24/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
  8. # floats
  9. # strings
  10. # lists
  11. # dictionaries
  12. # sets
  13. # tuples
  14. # # modules
  15. # math
  16. # random
  17. # datetime
  18. # os
  19. # calendar
  20. # pandas
  21.  
  22. # operators
  23. # +
  24. # -
  25. # *
  26. # /
  27. # % # mod
  28. # // # floor division
  29. # How many pounds and ounces is 43 ounces?
  30. # print(43 // 16) # pounds
  31. # print(43 % 16) # leftover ounces
  32. # **
  33. # += # increment
  34. # -= # decrement
  35. # = # assignment
  36. # == # equality operator... asking if it's equal, in IF/ELIF, or WHILE
  37. # !=
  38. # <
  39. # >
  40. # <=
  41. # >=
  42. #
  43. # # keywords
  44. # not
  45. # in
  46.  
  47. # CONTROL FLOW STRUCTURES
  48.  
  49. # FUNCTIONS
  50. # defining vs calling
  51. # parameters vs arguments
  52. # x = 5 # don't do this with parameters, the CALL gives the value
  53. # return vs print()
  54. # methods of a type, mystring.replace() or mylist.append()
  55.  
  56. # IF... and IF/ELSE and IF/ELIF/ELSE...
  57.  
  58. # LOOPS
  59. # WHILE: an IF that repeats as long the condition stays TRUE
  60. # FOR: repeat an action once for every ITEM/VALUE in a container
  61. # for __ in __:
  62. # for _some_variable_ in _some_container_:
  63. # for item in mylist:
  64. # for char in mystring:
  65. # for key in mydictionary:
  66.  
  67. # IN for a "membership check"
  68. # myTuple = ("Gilligan", "Castaway002", "red", "crew") # things about Gilligan
  69. # print("Gilligan" in myTuple) # True
  70. # print("Skipper" in myTuple) # False
  71. # if "red" in myTuple:
  72. #     print("I found red!")
  73.  
  74. # BUILT IN FUNCTIONS
  75. # print()
  76. # help() # help(str), help(list), etc
  77. # dir() # returns a list of "attributes" (methods, properties, etc) of an object: print(dir(str))
  78. # len()
  79. # sum()
  80. # min()
  81. # max() # max(container, key=len)
  82. # range() # makes a container out a range of numbers, also for i in range(len(mylist)):
  83. # round() # has cousins in the math module: floor() and ceil()
  84. # enumerate() # for i, item in enumerate(mylist):
  85. # str()
  86. # list()
  87. # dict()
  88. # tuple()
  89. # set()
  90. # float()
  91. # int()
  92. # type()
  93.  
  94. # STRING
  95. # myString = "Hello."
  96. # myString.join() # " ".join(someContainer)
  97. # myString.split() # create list from string
  98. # myString.replace() # also used to remove
  99. # myString.find() # returns the index where it finds it
  100. # myString.lower()
  101. # myString.upper()
  102. # myString.title()
  103. # myString.capitalize()
  104. # myString.isupper() # many is___() methods that return Boolean
  105. # myString.strip() # has cousins lstrip() and rstrip()
  106. # myString.format() # WINNER for building larger strings
  107. # myString.count()
  108.  
  109. # Building up larger strings...
  110. x = "Sue"
  111. greeting = "How do you do?"
  112. # CONCATENATE with +
  113. myString = "My name is " + str(x) + ". " + greeting
  114. # DATA CONVERSION SPECIFIERS or "string modulo"
  115. myString = "My name is %s. %s" % (x, greeting)
  116. # the FORMAT method
  117. myString = "My name is {}. {}".format(x, greeting)
  118. print(myString)
  119.  
  120. # LISTS
  121. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # X-Files characters
  122. # myList.append()
  123. # myList.insert() # myList(0, newValue)
  124. # myList.extend()
  125. # myList.pop() # last one, or by index
  126. # myList.remove() # by value
  127. # myList.sort() # myList(reverse=True)
  128. # myList.reverse()
  129. # myList.count()
  130. # myList.index() # myList.index(someItem)
  131. # myList.copy()
  132. # myList.clear()
  133.  
  134. # SLICES
  135. # Be able to slice like it's second nature
  136.  
  137. # DICTIONARIES - do we need methods that much for dictionaries?
  138. scoobies = {
  139.     "Scooby": "a blue collar",
  140.     "Shaggy": "green",
  141.     "Velma": "orange",
  142.     "Daphne": "purple",
  143.     "Fred": "an ascot"
  144. }
  145. # nameOfDict["someKey"] --> retrieve the value of key
  146. scoobies["Scooby Dumb"] = "a pink collar"
  147. print(scoobies)
  148. for key in scoobies:
  149.     print("{} always wears {}".format(key, scoobies[key]))
  150.  
  151. # SETS # remember they have ONLY unique values and have NO ORDER
  152. # mySet.add()
  153. # mySet.remove()
  154. # mySet.discard()
  155. # mySet.pop()
  156.  
  157. # TUPLE methods
  158. # don't worry about them
  159.  
  160. # MODULES
  161.  
  162. # MATH
  163. import math
  164. # math.sqrt()
  165. # math.pow() # **, do not confuse with math.exp()
  166. # math.e # property!
  167. # math.pi # property!
  168. # math.ceil()
  169. # math.floor()
  170.  
  171. # running help
  172. # help(math)
  173. # print(dir(math))
  174. # help(math.modf)
  175.  
  176. # def myFunction(x, y):
  177. #     h = help(math)
  178. #     return h # or print, whatever question said
  179.  
  180. # RANDOM
  181. # import random
  182. # random.random() # return a float b/n 0 and 1
  183. # random.choice() # returns a random item from a list
  184. # random.randint() # random number between start number and included stop
  185. # random.randrange()# random number between start number and excluded stop
  186.  
  187. # DATETIME
  188. import datetime
  189. # print(dir(datetime))
  190. # focus on datetime.datetime and datetime.timedelta
  191. print(dir(datetime.datetime))
  192. dd = datetime.datetime(1977, 10, 31)
  193. # dd = datetime.datetime.today()
  194. # dd.day
  195. # # dd.month
  196. # # dd.year
  197. print(dd)
  198. print(dd.year)
  199. td = datetime.timedelta(weeks=4)
  200. td = datetime.timedelta(days=10)
  201. print(dd + td) # add/subtract from datetime.datetime
  202. print("How many seconds are in that delta?", td.total_seconds())
  203.  
  204. # CALENDAR
  205. import calendar
  206. # calendar.weekday()
  207. # calendar.day_name
  208. # calendar.month_name
  209.  
  210. month = calendar.month_name[dd.month]
  211. dayOfWeek = dd.strftime("%A")
  212. dayOfWeek = calendar.day_name[calendar.weekday(dd.year, dd.month, dd.day)]
  213. # %a --> Sun
  214. # %A --> Sunday
  215. # %b --> Sep
  216. # %B --> September
  217.  
  218. print(dayOfWeek)
  219.  
  220. # OS
  221. import os
  222. # os.getcwd()
  223. # os.listdir()
  224. # os.path.dirname()
  225. # os.path.basename()
  226. # os.path.isfile()
  227. # os.path.isdir()
  228.  
  229. # IMPORT STATEMENTS
  230. # make sure the way the object is referenced matches the import statement
  231. # "normal" full import
  232. import datetime
  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(), m.e ... math.e X wrong
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement