Advertisement
jspill

webinar-python-exam-review-2021-03-27

Mar 27th, 2021
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.09 KB | None | 0 0
  1. # Exam Review, Mar 27, 2021
  2.  
  3. # Be able to recognize and use common data types
  4. # integers
  5. # floats
  6. # strings
  7. # lists
  8. # dictionary
  9. # sets
  10. # tuples
  11. # # and imported modules
  12. # math
  13. # random
  14. # datetime
  15. # os
  16. # pandas - not too much
  17. # calendar - not too much
  18.  
  19. # functions
  20. # defining vs calling
  21. # parameters/arguments vs "regular variables"
  22. # watch the question wording: print() or return
  23.  
  24. def addThese(x, y):
  25.     # x = 3 # don't do that
  26.     return x + y
  27.  
  28. print(addThese(1, 2)) # 3
  29. print(addThese(3, 3)) # 6
  30.  
  31. # repeat action with a LOOP
  32. # FOR loop is more useful than a WHILE loop for the OA
  33.  
  34. myList = ["Gilligan", "Scooby", "Agent Scully"] # tv characters
  35. for item in myList:
  36.     #print(item)
  37.     print("The string '{}' has {} characters.".format(item, len(item)))
  38.  
  39. # operators
  40. # +
  41. # -
  42. # /
  43. # *
  44. # // floor division
  45. # %
  46. # # Don't forget MODULO it's more important than you think
  47. # How many pounds is 41 ounces?
  48. print("lbs: ", 41 // 16)
  49. print("oz: ", 41 % 16)
  50.  
  51. myString = "Hello. My name is Sue."
  52. # STRING methods
  53. # myString.split()
  54. # myString.join()
  55. # myString.find()
  56. # myString.replace()
  57. # myString.format()
  58. # myString.strip() # myString.lstrip(), myString.rstrip()
  59. # myString.count()
  60. # myString.isupper() # isuower()
  61. # myString.upper()
  62. # myString.lower()
  63. # myString.capitalize()
  64. # myString.title()
  65.  
  66. # help(str)
  67. # print(dir(str))
  68.  
  69. # LIST methods
  70. # myList.append()
  71. # myList.insert()
  72. # myList.pop() # by index
  73. # myList.remove() # by value
  74. # myList.count()
  75. # myList.sort(reverse=False)
  76. # myList.reverse()
  77. # myList.index()
  78.  
  79. # if someItem in myList: # membership check
  80.  
  81. # Complete the function to order the values in the list
  82. # if ascending is true then order lowest to highest
  83. # if ascending is false then order highest to lowest
  84. def sortList(mylist, ascending):
  85.     if ascending:
  86.         mylist.sort()
  87.     else:
  88.         mylist.sort(reverse=True)
  89.     return mylist
  90.  
  91. # expected output: [4, 12, 19, 33]
  92. print(sortList([19,4,33,12], True))
  93. # expected output: [33, 19, 12, 4]
  94. print(sortList([19,4,33,12], False))
  95.  
  96.  
  97. # SETS
  98. # mySet.add()
  99. # mySet.remove()
  100. # mySet.discard()
  101.  
  102. # BUILT IN functions
  103. # print()
  104. # help()
  105. # dir()
  106. # len()
  107. # sum()
  108. # min()
  109. # max()
  110. # round()
  111. # enumerate()
  112. # sorted()
  113. # reversed()
  114. # int()
  115. # float()
  116. # str()
  117. # list()
  118. # set()
  119. # tup()
  120. # dict()
  121. # range(start, stop)
  122.  
  123. # Know SLICING like it's second hand
  124.  
  125. # 3 ways to round
  126. # round()
  127. # math.ceil()
  128. # math.floor()
  129.  
  130. myList.append("Mulder")
  131. print(myList[0:2])
  132. for item in myList:
  133.     print(item[0:3])
  134.  
  135. import math
  136. # MATH methods
  137. # math.ceil()
  138. # math.floor()
  139. # math.sqrt()
  140. # math.pow(x, y) # x to the y power, see also **
  141. # math.e
  142. # math.exp() is just to raise math.e to a power
  143.  
  144. print(3 * math.e)
  145.  
  146. # RANDOM methods
  147. import random
  148. # random.randint() # i for inclusive of STOP
  149. # random.randrange() # like everything else is EXCLUSIVE of STOP
  150. # random.choice(myList)
  151. # random.random() # returns float between 0 and 1
  152.  
  153. # DATETIME
  154. import datetime
  155. print(dir(datetime.datetime))
  156. # datetime.datetime # includes datetime.date and datetime.time
  157. # datetime.timedelta
  158.  
  159. dd = datetime.datetime(2021, 1, 5)
  160. print(dd)
  161. dd = datetime.datetime.today()
  162. print(dd)
  163.  
  164. td = datetime.timedelta(days=12)
  165. print(td.total_seconds())
  166.  
  167. print(dir(datetime.timedelta))
  168. print(td.min)
  169.  
  170. myHTMLString = "<p>Martians invade.</p>"
  171. # change paragraph to a level 1 heading
  172. myHTMLString = myHTMLString.replace("<p>", "<h1>")
  173. myHTMLString = myHTMLString.replace("</p>", "</h1>")
  174.  
  175. # or to create nested parent and child tags:
  176. print(myList) # back to that iterable above
  177. myHTMLString = "<ul>\n"
  178. for item in myList:
  179.     myHTMLString += "<li>{}</li>\n".format(item)
  180. myHTMLString += "</ul>"
  181. print(myHTMLString)
  182.  
  183. # Complete the function to return the number of upper case letters in the given string
  184. def countUpper(mystring):
  185.     count = 0
  186.     for char in mystring:
  187.         if char.isupper():
  188.             count += 1
  189.     return count
  190.  
  191.  
  192. print(countUpper('Welcome to WGU'))# expected output: 4
  193. print(countUpper('Hello Mary'))# expected output: 2
  194.  
  195. # end of chapter exerices for Ch 8, 9, 11 and 12 are critical
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement