Advertisement
jspill

webinar-python-exam-review-2021-04-24

Apr 24th, 2021
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.64 KB | None | 0 0
  1. # webinar review 04/22/21
  2.  
  3. # Ch 8, 9, 11 and 12 end of chapter are critical
  4.  
  5. # Be able to recognize and use common data types and modules
  6. # integers
  7. # floats
  8. # strings
  9. # lists
  10. # dictionaries
  11. # sets
  12. # tuples
  13. # # modules
  14. # math
  15. # random
  16. # datetime
  17. # os
  18. # calendar
  19. # pandas
  20.  
  21. # operators
  22. # = # assignment
  23. # == # equality - asking if it's equal
  24. # +
  25. # -
  26. # *
  27. # /
  28. # // # floor division
  29. # % # modulo - gives the (whole number) REMAINDER, how many didn't fit?
  30. # print(41 // 16, "pounds and")
  31. # print(41 % 16, "ounces")
  32. # **
  33. # !=
  34. # <
  35. # >
  36. # <=
  37. # >=
  38. # not
  39. # += # x += 1 is the same as x = x + 1
  40. # -=
  41.  
  42. # FUNCTIONS
  43. # defining vs calling
  44. # parameters vs arguments
  45. # x = 5 # don't do that with parameters
  46. # return vs print()
  47. # methods of a data type are themselves functions
  48.  
  49. # IF and IF/ELSE and IF/ELIF/ELSE
  50.  
  51. # LOOPS are for repeating action
  52. # WHILE LOOP is an IF that repeats as long as the condition is TRUE
  53. # FOR LOOP is tied to a container
  54. # for __ in __:
  55. # for item in myList:
  56. # for i in range(0, 5):
  57.  
  58. # "membership check"
  59. myTuple = ("Gilligan", "Castaway002", "red", "crew")
  60. # item __ in __
  61. print("Gilligan" in myTuple) # True
  62. print("Skipper" in myTuple) # False
  63. if "red" in myTuple:
  64.     print("I found red")
  65.  
  66.  
  67. # print(dir(str))
  68. # STRINGS methods
  69. # myString.format()
  70. # myString.join()
  71. # myString.split()
  72. # myString.find()
  73. # myString.replace() # used to "remove"
  74. # myString.isUpper() # and .isLower()
  75. # myString.upper()
  76. # myString.lower()
  77. # myString.title()
  78. # myString.capitalize()
  79. # myString.strip() # .lstrip() and rstrip()
  80. # myString.count()
  81.  
  82. # Be able to SLICE like it's second nature
  83.  
  84. # BUILDING UP LARGER STRINGS
  85. x = "Sue"
  86. greeting = "How do you do?"
  87. # myString = "My name is " + x + ". " + greeting
  88. # myString = "My name is %s. %s" % (x, greeting)
  89. myString = "My name is {}. {}".format(x, greeting)
  90. print(myString)
  91.  
  92. # LISTS
  93. myList = ["Sam", "Bucky", "Sharon", "Bad Cap"]
  94. # myList.append()
  95. # myList.insert()
  96. # myList.pop() # by index
  97. # myList.remove() # by value
  98. # myList.count()
  99. # myList.sort(reverse=False)
  100. # myList.reverse()
  101. # myList.index()
  102. # myList.copy()
  103. # myList.clear()
  104.  
  105. # DICTIONARIES
  106. scoobies = {
  107.     "Scooby": "a blue collar",
  108.     "Shaggy": "green",
  109.     "Velma": "orange",
  110.     "Daphne": "purple",
  111.     "Fred": "an ascot"
  112. }
  113. # scoobies["Scooby Dumb"] = "red"
  114. # for key in scoobies:
  115. #     # nameOfDict[key] --> get value for key
  116. #     print("{} always wears {}.".format(key, scoobies[key]))
  117.  
  118. # SETS - remember sets have no order, no duplicates (all unique)
  119. # mySet.add()
  120. # mySet.remove()
  121. # mySet.discard()
  122.  
  123. # MODULES
  124. # MATH
  125. # math.e
  126. # math.sqrt()
  127. # math.pow() # raise x to y power
  128. # math.floor() # always rounds down
  129. # math.ceil() # always rounds up
  130. # math.exp() # don't confuse with .pow(), this raise math.e to a power
  131.  
  132. # RANDOM
  133. # random.random() # returns float between 0 and 1
  134. # random.choice() # gets random value from a list
  135. # random.randint() # randInt is INCLUSIVE of the stop number
  136. # random.randrange() # randrangE EXCLUDES the stop, like normal
  137.  
  138. # DATETIME module
  139. import datetime
  140. # print(dir(datetime))
  141. # focus on
  142. # datetime.datetime # represents a point in time
  143. # datetime.timedelta # represents a period of time: a difference or delta
  144. dt = datetime.datetime(2021, 1, 1)
  145. print(dt)
  146. dt = datetime.datetime.today()
  147. print(dt)
  148. print(dt.month)
  149. print(dt.hour)
  150. td = datetime.timedelta(days=7)
  151. print(dt + td)
  152. print(td.total_seconds())
  153. print(dir(datetime.timedelta))
  154.  
  155. # OS
  156. # os.getcwd()
  157. # os.listdir()
  158. # os.path.isdir()
  159.  
  160. # BUILT IN FUNCTIONS
  161. # print()
  162. # help()
  163. # dir()
  164. # len()
  165. # min()
  166. # max()
  167. # sum()
  168. # range()
  169. # str()
  170. # list()
  171. # dict()
  172. # tuple()
  173. # set()
  174. # int()
  175. # float()
  176. # enumerate()
  177. # round()
  178.  
  179. for i, item in enumerate(myList):
  180.     print(i, item)
  181. for i in range(len(myList)):
  182.     print(i, "->", myList[i])
  183.  
  184. # HTML is just strings
  185.  
  186. # IMPORT statements
  187. import math # import it all
  188. from datetime import date # partial --> not datetime.date.today() but date.today()
  189. import random as rr # rr.randrange()
  190. print(round(math.pi, 10))
  191.  
  192. def addToPi(someNum):
  193.     import math
  194.     return math.pi + someNum
  195.  
  196. print(addToPi(0))
  197. print(addToPi(1))
  198. print(addToPi(5))
  199. print(addToPi(math.e))
  200.  
  201. def addToList(someList, someValue):
  202.     if someValue in someList:
  203.         print(someList)
  204.     else:
  205.         someList.append(someValue)
  206.         print(someList)
  207.  
  208. addToList(myList, "Wanda")
  209. addToList([1, 2, math.pi], math.pow(math.e, 3))
  210. addToList([1, 2, math.pi], math.exp(3))
  211. addToList([1, 2, math.pi], round(math.exp(3), 2))
  212. # strings and float precision
  213. print("%.2f" % math.e)
  214. print("{:.2f}".format(math.e))
  215. #
  216. # print(dir(math))
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement