Advertisement
jspill

webinar-python-exam-review-2022-09-17

Sep 17th, 2022
1,213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.89 KB | None | 0 0
  1. # Exam Review 2022 Sept 17
  2.  
  3. # LABS
  4. # Ch 2-14... all Labs!
  5. # Ch 21-33 just ADDITIONAL LABS, but important practice!
  6. # Use Submit Mode!!!
  7.  
  8. # Watch your string input and output
  9. # # 1
  10. # myVar = input().strip() # myVar = input().rstrip()
  11. # # 2
  12. # print("some stuff", end=" ") # if you ever override end
  13. # print() # print(end="\n")
  14.  
  15.  
  16. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  17. # Comp 2: Control Flow
  18. # Comp 3: Modules and Files
  19.  
  20. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  21. # Data Types
  22. # int
  23. # floats
  24. # bool
  25. # str # "" maybe the most important?
  26. # list # [] probably the 2nd most important type
  27. # dict # {key: value} also important
  28. # set # {} all unique values, no order -> no index, can't slice
  29. # tuple # () immutable... Python sees any x,y,z as (x,y,z)... return x,y-> return (x,y)
  30.  
  31. # operators
  32. # = # assignment
  33. # == # asking are these equal, comparison -> if/elif, while
  34. # +
  35. # -
  36. # *
  37. # /
  38. # % # whole number remainder, how many WHOLE THINGS didn't fit?
  39. # // # floor or integer division, last even division, int(x/y), math.floor(x/y)
  40. # <
  41. # >
  42. # <=
  43. # >=
  44. # += # increment
  45. # -= # decrement
  46. # !=
  47. # ** # raise to power... similar math.pow()
  48. # # keywords
  49. # in # if _someValue_ in _someContainer_
  50. # not # flip around a Boolean --> if not _value_ in _someContainer_
  51. # and
  52. # or # any one True means whole condition is True... limit OR to 2 conditions
  53.  
  54. # Comp 2
  55. # the HOW stuff... control flow structures
  56. # IF statements... if, if/else, if/elif, if/elif/else...
  57. # LOOPS
  58. # WHILE - an IF that repeats
  59. # FOR - looping over a container, or a known number of times... range()
  60. # for _value_ in _container_:
  61. # for item in myList:
  62. # for n in range(0, 4): # [0, 1, 2, 3]
  63. # for i in range(len(myList)) # myList[i]
  64. # for key in myDictionary: # myDictionary[key]
  65.  
  66. # FUNCTIONS
  67. # defining/writing vs calling
  68. # parameters vs "outside" or "regular" variables
  69. # parameters vs arguments
  70. # a function has ONE particular job, modular
  71. # return vs print()... write to a file?
  72. # methods are functions that belong to a particular class or data type
  73.  
  74.  
  75. # def someFunction(x, y):
  76. #     return x + y
  77. #
  78. # if __name__ == "__main__":
  79. #     myInput = int(input())
  80. #     myOtherInput = int(input())
  81. #     num = someFunction(myInput, myOtherInput)
  82. #     print(num)
  83.  
  84. # See "tasks" in the last section of Ch 10, 11, 13, 14 for function writing practice
  85. # CodingBat also has good function-based Python questions:
  86. # https://codingbat.com/python
  87.  
  88. # BUILT-IN FUNCTIONS
  89. # print()
  90. # input()
  91. # len()
  92. # sum()
  93. # min()
  94. # max()
  95. # range()
  96. # list()
  97. # dict()
  98. # tuple()
  99. # set()
  100. # sorted() # compare to list.sort()
  101. # reversed() # compare to list.reverse()
  102. # round() # but it's cousins math.ceil() and math.floor() are in math module
  103. # open() # IO/filename: .read(), .readlines(), .write()
  104. #
  105. # help()
  106. # dir()
  107. # # help(str)
  108. # # print(dir(str))
  109. # # help(str.isspace)
  110. #
  111. # for item in dir(dict):
  112. #     if not item.startswith("__"):
  113. #         print("{}()".format(item))
  114.  
  115. # STRINGS
  116. # be able to slice like it's 2nd nature: myString[start:stop:step]
  117. myString = "abc"
  118. # revString = myString[::-1]
  119. # print(revString)
  120.  
  121. # KNOW YOUR WHITESPACE
  122. # " " # ... and a lot of other Unicode spaces
  123. # \n
  124. # \r
  125. # \t
  126. # \f
  127.  
  128. # STRING METHODS
  129. # "stuff I want to {}".format(var) # or the similar f string
  130. # myString.strip() # also .rstrip(), .lstrip()
  131. # myString.split() # returns a list of strings
  132. # ",".join(listOfStrings)
  133. # myString.replace(oldSubStr, newSubStr) # remove... myString.replace(subStr, "")
  134. # myString.find(subStr) # return index, or -1
  135. # myString.count(subStr) # return number of occurences
  136. # case: lower(), upper(), capitalize(), title()
  137. # is/Boolean: isupper(), islower(), isalpha(), isdigit(), isalnum(), isspace()
  138.  
  139. # LISTS
  140. # again, be able to slice
  141.  
  142. # LIST METHODS
  143. # # +
  144. # myList.append(item)
  145. # myList.insert(i, item)
  146. # myList.extend(anotherList)
  147. # # -
  148. # myList.pop(i) # pop by index...
  149. # myList.remove(item) # remove by value
  150. # # other
  151. # myList.count(item)
  152. # myList.sort()
  153. # myList.reverse()
  154. # # not so important
  155. # myList.copy()
  156. # myList.index(item)
  157. # myList.clear()
  158.  
  159. # DICT
  160. # use the key like an index
  161. # myDict[key] # retrieve the value for that key, so like get()
  162. # myDict[key] = value # assign (new) value for that key, so like update()
  163. # myDict.items() # for k, v in myDict.items()... x, y = [2, 8]
  164. # myDict.keys()
  165. # myDict.values()
  166.  
  167. # SETS
  168. # mySet.add(item)
  169. # mySet.remove(item) # by value
  170. # mySet.pop() # removes something random
  171.  
  172. # MODULES
  173. # math and csv
  174.  
  175. # MATH MODULE
  176. import math
  177. # math.factorial(x)
  178. # math.ceil(x.yz)
  179. # math.floor(x.yz)
  180. # math.pow(x, y) # similar to **, not to be confused with math.exp()
  181. # math.sqrt()
  182. # math.fabs() # abs()
  183. # math.e
  184. # math.pi
  185.  
  186. # different import types
  187. # full import: import math
  188. # partial import: from math import floor, from random import seed, randint, from math import *
  189. # import as math as m
  190.  
  191. # import math
  192. # math.factorial()
  193.  
  194. # from math import factorial
  195. # --> factorial()
  196.  
  197. # from math import *
  198. # --> factorial()
  199.  
  200. # import math as m
  201. # --> m.factorial()
  202.  
  203. # # OPENING FILES
  204. #  good practice in Ch 14 Task 4, 7, 8
  205.  
  206. # read!
  207. with open("test.txt", "r") as f:
  208.     contents = f.readlines() # a list of line by line strings
  209. print(contents)
  210. for line in contents:
  211.     line = line.strip()
  212.     print(line)
  213.  
  214. # read!
  215. import csv
  216. with open("mock_data.csv", "r") as f1:
  217.     contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t")
  218.  
  219. print(contents)
  220.  
  221. # write!
  222. with open("output_data4.csv", "w") as f2:
  223.     for line in contents:
  224.         if line[3].endswith(".edu") or line[3].endswith(".org"): # if line[3][-3:] == "edu":
  225.             # the write() method takes a SINGLE string as its arg
  226.             f2.write(",".join(line) + "\n")
  227.  
  228. # append!
  229. with open("append_to_this.txt", "a") as f3:
  230.     f3.write("Pippin\n") # I had 2 line returns in the text file in Notepad !
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement