Advertisement
jspill

webinar-python-exam-review-2023-10-21

Oct 21st, 2023
2,121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.20 KB | None | 0 0
  1. # Exam Review 2023 Oct 21
  2.  
  3. # Do those LABS
  4. # Ch 2-14... all Labs!
  5. # Ch 21-32 just ADDITIONAL LABS, but important practice!
  6. # Prac Tests, Ch 33 and 34
  7.  
  8. # Use Submit Mode and get them to 100%!!! And PAY ATTENTION to the unit tests!
  9.  
  10.  
  11. # Comp 1: Basic syntax and knowledge: operators, data types, etc
  12. # Comp 2: Control Flow
  13. # Comp 3: Modules and Files
  14.  
  15.  
  16. # Comp 1: Basic syntax and knowledge: operators     , data types, etc
  17.  
  18. # Operators
  19. # = # assignment
  20. # == # asking if the values are equal
  21. # +
  22. # -
  23. # *
  24. # /
  25. # % # modulo... whole number remainder... how many whole things didn't fit since the last even division?
  26. # // # floor division... that last even division
  27. # <
  28. # >
  29. # <=
  30. # >=
  31. # += # x += 1 --> x = x + 1
  32. # -=
  33. # ** # raise to a power... pow() and math.pow()
  34. # !=
  35. # # keywords
  36. # in # if x in myList
  37. # not # if not x in myList
  38. # and
  39. # or # any one True means the combo is True... limit OR to 2-3 conditions
  40.  
  41. # Common Data Types/Classes
  42. # int
  43. # float
  44. # bool # True, False
  45. # str # "" most important data type?
  46. # list # []
  47. # dict # {key: value}... myDict[key]
  48. # set # { } no duplicates, unordered.... no index, no slicing, no sort
  49. # tuple # ( ) immutable, Python see a,b,c as (a,b,c) -> return x, y --_ return (x,y)
  50. # range() #... container of consecutive numbers range(0, 7, 1)... [0, 1, 2, 3, 4, 5, 6]
  51. # file # open()... f.read(), f.readlines(), f.write()
  52.  
  53. # Comp 2
  54. # Control Flow! The HOW stuff
  55. # IF statements... if, if/else, if/elif, if/elif/else
  56. # LOOPS
  57. # WHILE - an IF that repeats
  58. # FOR - looping over a container, or a known number of times... hence range()
  59. # Check out my For Loops webinar in The Gotchas
  60. # for ___ in _someContainer_:
  61. # for item in myList:
  62. # for char in myStr:
  63. # for key in myDict: # myDict[key]
  64. # for num in range(5):
  65. # for i in range(len(myList)): # myList[i]
  66. # for i, item in enumerate(myList):
  67.  
  68. # FUNCTIONS
  69. # defining/writing vs calling
  70. # modular... a function has ONE job
  71. # parameters are special variables for data coming into the function
  72. # parameters vs arguments
  73. # pay attention to whether the function is asked to return or print()/output
  74. #
  75. # def someFunction(x, y):
  76. #     return x - y
  77. #
  78. # if __name__ == "__main__": # is this script the one that's running now?
  79. #     # inside this block we're answering this specific question
  80. #     myInput = int(input())
  81. #     myOther = int(input())
  82. #     myNum = someFunction(myInput, myOther)
  83. #     print(myNum)
  84.  
  85. # See "tasks" in the last section of Ch 10, 11, 13, 14 for function writing practice
  86. # # CodingBat also has good function-based Python questions:
  87. # https://codingbat.com/python
  88.  
  89. # BUILT-IN FUNCTIONS
  90. # input()
  91. # print()
  92. # len()
  93. # sum()
  94. # min()
  95. # max()
  96. # list()
  97. # str()
  98. # int()
  99. # float()
  100. # range()
  101. # tuple()
  102. # dict()
  103. # type() # x = "3.14159", print(type(x).__name__)
  104. # enumerate()
  105. # open()
  106. # round() # cousins math.ceil(), math.floor()
  107. # pow()
  108. # abs() # compare to math.fabs()
  109. # help() # help(str), help(str.isspace)
  110. # dir() # print(dir(str))
  111.  
  112. # STRINGS
  113. # be able to refer to indices, and slice
  114. # myStr = "abcd"
  115. # # mySlice[start:stop:step]
  116. # revStr = myStr[::-1]
  117. # print(revStr)
  118.  
  119. # KNOW YOUR WHITESPACE
  120. " " # space from spacebar
  121. # a lot of Unicode space
  122. # \n # new line return # print() -> print(end="\n")
  123. # \r # carriage return
  124. # \t # tab
  125.  
  126. # STRING METHODS
  127. # myStr.format() # "Stuff I want to put together {}".format(var)
  128. # myStr.strip() # input().strip()
  129. # myStr.split() # return a list of smaller strings
  130. # " ".join(listOfStrings)
  131. # myStr.replace(subStr, newStr) # "remove" myStr = myStr.replace(subStr, "")
  132. # myStr.find(subStr) # returns index where found, or -1 if not there, compare to myStr.index()
  133. # myStr.count(subStr) # returns int of how many times found
  134. # case: myStr.lower(), myStr.upper(), myStr.title(), myStr.capitalize()
  135. # is/Boolean: myStr.islower(), .isupper(), isspace(), isalpha(), .isnumeric(), .isdigit(), .isalnum()
  136. # myStr.startswith(subStr), myStr.endswith(subStr)
  137.  
  138. # LISTS
  139. # be able to refer by index and to slice
  140.  
  141. # LIST METHODS
  142. # # +
  143. # myList.append(item)
  144. # myList.insert(i, item)
  145. # myList.extend(anotherList)
  146. # # -
  147. # myList.pop(i)
  148. # myList.remove(item) # pop() by index, remove() by value
  149. # myList.clear()
  150. # # others
  151. # myList.count(item)
  152. # myList.sort()
  153. # myList.reverse()
  154. # myList.copy()
  155. # myList.index(item)
  156.  
  157. # DICT
  158. # use the key like an index []... then you don't need DICT methods
  159. # myDict[key] # get the value for that key
  160. # myDict[key] = value # assign a new value to key
  161. #
  162. # # DICT METHODS
  163. # myDict.keys()
  164. # myDict.values()
  165. # myDict.items() # for k, v in myDict.items()
  166.  
  167. # if _key_ in myDict: # only looks at keys
  168.  
  169. # MODULES
  170. # math and csv
  171.  
  172. # MATH MODULE
  173. # import math # FULL IMPORT
  174. # math.factorial(x)
  175. # math.ceil(x.yz)
  176. # math.floor(x.yz)
  177. # math.pow(x, y)
  178. # math.sqrt(x)
  179. # math.fabs(x)
  180. # math.pi
  181. # math.e
  182.  
  183. # PARTIAL IMPORT
  184. from math import floor # floor(x.yz)
  185. from math import sqrt, ceil # sqrt(x), ceil(y)
  186. from math import * # sqrt(y)
  187.  
  188. # ALIAS IMPORT
  189. import math as m # m.floor(x.yz)
  190.  
  191. # FILES
  192.  
  193. # READ MODE
  194. with open("test.txt", "r") as f:
  195.     # f.read() # returns whole file as one big string
  196.     # f.readlines() # returns a list of strings, line by line
  197.     # f.write() # take one str arg and write into file
  198.     contents = f.readlines()
  199. # print(contents)
  200. # for line in contents:
  201. #     line = line.strip()
  202. #     print(line)
  203.  
  204. # CSV Module
  205. # csv.reader()
  206. import csv
  207. with open("mock_data.csv", "r") as f1: # mockaroo.com
  208.     contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t")
  209. # print(contents) # [['id', 'first_name', 'last_name', 'email', 'gender', 'ip_address'], ['1', 'Remington', 'Shilling', 'rshilling0@wsj.com', 'Male', '1.71.141.52']... ]
  210.  
  211. # WRITE MODE
  212. with open("output_data28.csv", "w") as f2:
  213.     # write out a file with every row where the last name starts with J
  214.     for row in contents:
  215.         # last name is at row[2]
  216.         if row[2][0].upper() == "J": # if row[2].startswith("j") or row[2].startswith("J"):
  217.             # we like this data, we want to write it as a string
  218.             f2.write(",".join(row) + "\n")
  219.  
  220. # APPEND MODE
  221. # with open("append_to_this.txt", "r") as f3:
  222. #     print(f3.readlines())
  223. with open("append_to_this.txt", "a") as f3:
  224.     f3.write("\nPippin")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement