Advertisement
jspill

webinar-python-exam-review-2021-03-20

Mar 20th, 2021
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.97 KB | None | 0 0
  1. # exam review March 20 2021
  2.  
  3. # pay special attention to the end of chapter exercises
  4. # Ch 8, 9, 11, 12
  5.  
  6. # be able to recognize and use common data types
  7. # integers
  8. # floats
  9. # Boolean
  10. # strings
  11. # lists
  12. # tuples
  13. # sets
  14. # dictionaries
  15. # # our modules
  16. # math
  17. # random
  18. # datetime --> datetime.datetime and datetime.timedelta
  19. # os
  20. # calendar
  21. # pandas
  22.  
  23. # functions
  24. # defining and calling
  25. # parameters vs arguments
  26. # parameters are not like "regular" variables
  27. # return vs print()
  28.  
  29. # defining the function
  30. def addThese(x, y):
  31.     # x = 3 # don't do this
  32.     # y = 4 # don't do this
  33.     return x + y
  34. # calls to function
  35. print(addThese(1, 2)) # 3 --> to see it, we'll need to print() the call itself
  36. print(addThese(3, 4)) # 7
  37.  
  38. myVar = addThese(4,5) # because this function returns, it's the same as...
  39. # myVar = 9
  40. print(addThese(4, 5))
  41.  
  42. # LOOPS repeat actions
  43. # FOR LOOPS repeat for everything in a container
  44. # WHILE LOOPS are like IF statements that keep repeating as long as TRUE
  45. # --> FOR <-- loops are probably more useful on the exam
  46.  
  47. myList = ["Gilligan", "Scooby", "Agent Scully"]
  48. for item in myList:
  49.     print(item)
  50.  
  51. scoobies = {
  52.     "Scooby": "a blue collar",
  53.     "Shaggy": "green",
  54.     "Velma": "orange",
  55.     "Daphne": "purple",
  56.     "Fred": "an ascot"
  57. }
  58. for key in scoobies:
  59.     # nameOfDictionary[key] --> retrieve or set the VALUE for that key
  60.     # Scooby wears a blue collar.
  61.     # value = scoobies[key]
  62.     print("{} wears {}.".format(key, scoobies[key]))
  63. # for key, value in scoobies.items():
  64. #     print("{} always wears {}.".format(key, value))
  65.  
  66. # IF
  67. # if some condition:
  68. # if/elif/else
  69. # if/elif/elif/else
  70.  
  71. # "MEMBERSHIP CHECK"
  72. myList = ["Gilligan", "Scooby", "Agent Scully"]
  73. if "Mulder" in myList:
  74.     print("Found Mulder in the list.")
  75. else:
  76.     print("No Mulder there.")
  77.  
  78. # Operators
  79. # +
  80. # -
  81. # *
  82. # /
  83. # # don't forget
  84. # // # floor division
  85. # % # MODULO is more important than you think: what WHOLE NUMBER is left over?
  86. # print(8/3) # 2.66666666
  87. # print(8//3) # 2
  88. # print(8 % 3) # 2
  89. # print(25%4) # 1
  90. largeOz = 38
  91. print("lbs:", largeOz // 16)
  92. print("and oz left over:", largeOz % 16)
  93.  
  94. # other operators
  95. # = # assignment
  96. # <
  97. # >
  98. # <=
  99. # >=
  100. # == # single = is assignment, double == is asking "are they equal?"
  101. # !=
  102. # += # x += 1 is the same as x = x + 1
  103. # -=
  104. # not
  105.  
  106. x = 234.134646246224
  107. # myStr = "I am specifying x to 2 decimals: %.2f" % (x)
  108. # print(myStr)
  109. myStr = "I am specifying x to 2 decimals: {:.2f}".format(x)
  110. print(myStr)
  111.  
  112. # 3 ways to round
  113. # round() # built-in functions
  114. # math.ceil()
  115. # math.floor()
  116.  
  117. # SLICING
  118. # Be able to slice strings and lists like it's second nature
  119.  
  120. # STRING methods
  121. # myString = "Hello"
  122. # myString.join() # feed it a list and get a longer string back: ",".join(someListofStrings)
  123. # myString.split() # takes your string and creates a list of substrings
  124. # myString.count() # counts a particular value
  125. # myString.replace() # 2 args: substring to look for, substring to replace it with
  126. # myString.find() # returns first index where substring starts
  127. # myString.strip() # has left and right variants: lstrip(), rstrip()
  128. # myString.lower()
  129. # myString.upper()
  130. # myString.title()
  131. # myString.capitalize()
  132. # myString.isLower()
  133. # myString.isUpper()
  134. # myString.format()
  135.  
  136. # LIST methods
  137. # myList.append()
  138. # myList.extend()
  139. # myList.insert()
  140. # myList.pop() # by index
  141. # myList.remove() # by value
  142. # myList.count()
  143. # myList.index() # if you know value but not its index: myList.index("Gilligan") --> 0
  144. # myList.sort(reverse=False)
  145. # myList.reverse()
  146.  
  147. # myList = ["Gilligan", "Scooby", "Agent Scully"]
  148. # # myList.pop(0)
  149. # myList.remove("Gilligan")
  150. # print(myList)
  151.  
  152.  
  153. # help(str)
  154. # print(dir(str))
  155.  
  156. # DATETIME module
  157. # full module import:
  158. import datetime
  159. # partial import:
  160. from datetime import timedelta
  161. # from -module- import -specific thing-
  162. # import with alias:
  163. # import datetime as dat
  164. # each way of importing changes how you refer to the thing later
  165.  
  166. print(dir(datetime.datetime))
  167. dt = datetime.datetime(2020, 12, 31)
  168. print(dt)
  169. dt = datetime.datetime.today()
  170. print(dt)
  171. dt = datetime.datetime.now()
  172. print(dt)
  173. # strftime see strftime.org
  174. print(dt.strftime("%A"))
  175. print(dt.month)
  176.  
  177.  
  178. td = datetime.timedelta(days=10)
  179. print("The delta is:", td)
  180. td.total_seconds() # a method of datetime.timedelta
  181. print(dt + td) # adding a datetime.datetime to a datetime.timedelta
  182.  
  183.  
  184. # RANDOM module
  185. import random
  186. # random.randint() # includes stop number, which is unusual in Python
  187. # random.randrange() # excludes stop number, otherwise same as randint()
  188. # random.random() # # returns a float b/n 0 and 1
  189. # random.choice() # picks random item from list or other iterable
  190.  
  191. # MATH module
  192. # math.floor()
  193. # math.ceil()
  194. # math.sqrt()
  195. # math.pow() # not to be confused with math.exp()
  196. # math.e
  197.  
  198.  
  199. # BUILT-IN FUNCTIONS
  200. # print()
  201. # list()
  202. # set()
  203. # dict()
  204. # tuples()
  205. # help()
  206. # dir()
  207. # len()
  208. # sum()
  209. # min()
  210. # max()
  211. # enumerate() # see FOR LOOPS webinar for more on enumerate() and range()
  212. # range()
  213.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement