Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2023 Sept 23
- # Do those LABS
- # Ch 2-14... all Labs!
- # Ch 21-32 just ADDITIONAL LABS, but important practice!
- # Prac Tests, Ch 33 and 34
- # Use Submit Mode and get them to 100%!!! And PAY ATTENTION to the unit tests!
- # Comp 1: Basic syntax and knowledge: operators, data types, etc
- # Comp 2: Control Flow
- # Comp 3: Modules and Files
- # Comp 1: Basic syntax and knowledge: operators, data types, etc
- # Common Data Types/Classes
- # int
- # float
- # bool # True, False
- # str # "" most important type?
- # list # [ ]
- # dict # {key:value}
- # tuple # ( ) immutable, Python sees any a,b,c as (a,b,c)... return x,y --> return (x,y)
- # set # { } all unique/ no duplicate values, no order... no indices, no slicing, no sorting
- # range # range()... range(0, 4) --> [0, 1, 2, 3]
- # Operators
- # = # assigns
- # == # asking if they're equal... a conditional expression
- # +
- # -
- # *
- # /
- # % # modulo (remainder... whole number)... how many whole things didn't fit since the last even division?
- # // # floor division... the last even division
- # <
- # > # x > 5... print(x>5)
- # <=
- # >=
- # += # x = x+1
- # -=
- # ** # raise to a power... math.pow(x, y)... or built-in pow(x, y)
- # != # not equals... not x == y
- # # keywords
- # in # if x in myList (with a dictionary in only looks at the keys)
- # not # if not x in myList
- # and
- # or # any one True make the combo True... limit OR to 2-3 conditions
- # Comp 2
- # Control Flow! The HOW stuff
- # IF statements... if, if/else, if/elif/else,
- # LOOPS
- # WHILE - an IF that repeats
- # FOR - looping over a container, or a known number of times... hence range()
- # for ___ in _someContainer_:
- # for item in myList:
- # for char in myStr:
- # for key in myDict: # getting the value... myDict[key]
- # for num in range(0, 8):
- # for i in range(len(myList)): # myList[i]
- # for i, item in enumerate(myList):
- # FUNCTIONS
- # defining/writing vs calling
- # a function has ONE particular job
- # parameters (vs arguments)
- # def someFunction(x, y):
- # return x+y
- # # print("hey") # will cause failure
- # if __name__ == "__main__": # is this the script that was run?
- # myInput = int(input())
- # myOther = int(input())
- # myNum = someFunction(myInput, myOther)
- # print(myNum)
- #
- # See "tasks" in the last section of Ch 10, 11, 13, 14 for function writing practice
- # # CodingBat also has good function-based Python questions:
- # https://codingbat.com/python
- # BUILT-IN FUNCTIONS
- # input()
- # print()
- # range()
- # list()
- # set()
- # tuple()
- # dict()
- # str()
- # int()
- # float()
- # min()
- # max()
- # sum()
- # len()
- # enumerate()
- # type() # type(x).__name__
- # round() # cousins math.floor() and math.ceil()
- # open() # create a file/IO object
- # help() # help(str) vs help(str.isspace)
- # dir() # print(dir(str)), print(dir(list))
- # STRINGS
- # be able to refer by index, and slice
- # myStr = "xyz"
- # revStr = myStr[::-1] # mySlice[start:stop:step]
- # print(revStr)
- # KNOW YOUR WHITESPACE
- # " " # space from spacebar
- # # a lot of other spaces in Unicode
- # \n # new line return
- # \t # tab
- # \r # carriage return
- # see Ch 2.9 expectation printed output ends with a new line return, unless otherwise stated
- print() # print(end="\n")
- # STRING METHODS
- # myStr.format() # "stuff I want to put together {}".format(var)
- # myStr.strip()
- # myStr.split() # returns a list of smaller strings
- # " ".join(listOfStrings)
- # myStr.replace(subStr, newStr) # "remove" myStr = myStr.replace(subStr, "")
- # myStr.find(subStr) # return index where found, similar myStr.index()
- # myStr.count(subStr) # return int count of occurrences
- # case: myStr.lower(), myStr.upper(), myStr.title(), myStr.capitalize()
- # is/Boolean: myStr.islower(), myStr.isupper(), myStr.isspace(), .isalpha(), .isalnum(), .isnumeric(), .isdigit()
- # myStr.startswith(subStr), myStr.endswith(subStr)
- # LISTS
- # be able to slice, refer by index
- # LIST METHODS
- # # +
- # myList.append(item)
- # myList.insert(i, item)
- # myList.extend(anotherList)
- # # -
- # myList.pop(i) # or last item
- # myList.remove(item) # pop by index, remove by value
- # myList.clear()
- # # others
- # myList.count(item)
- # myList.sort() # does not in return a value!
- # myList.reverse() # does not in return a value!
- # myList.copy()
- # myList.index(item)
- # DICT
- # use the key like an index []... then you don't need DICT methods
- # myDict[key] # retrieve the value for that key
- # myDict[key] = value # assign a new value to that key
- #
- # # DICT METHODS
- # myDict.keys()
- # myDict.values()
- # myDict.items() # for key, value in myDict.items()
- # MODULES
- # MATH MODULE
- # import math # FULL IMPORT
- # math.factorial(x)
- # math.ceil(x.yz)
- # math.floor(x.yz)
- # math.pow(x, y) # not math.exp()
- # math.sqrt(x)
- # math.fabs(x) # built-in abs()
- # math.pi
- # math.e
- # PARTIAL IMPORT
- from math import ceil # ceil(x.yz)
- from math import factorial, floor # factorial(x), floor(x), sqrt(x)
- from math import * # ceil(y.z)
- # ALIAS IMPORT
- import math as m # m.floor(x.yz)
- # FILES
- # READ MODE
- with open("test.txt", "r") as f:
- # file objects have file methods
- # f.read() # returns whole files as one str
- # f.readlines() # returns a list of strings, line by line
- # f.write() # writes to the file, in write or append modes
- contents = f.readlines()
- # print(contents)
- # CSV Module
- import csv
- with open("mock_data.csv", "r") as f1: # mockaroo.com
- contents = list(csv.reader(f1)) # contents = list(csv.reader(f1, delimiter="\t"))
- # print(contents)
- # WRITE MODE
- with open("output_data26.csv", "w") as f2:
- for row in contents:
- # only write line to new file if last name begins with P
- # last name is pos 2 of each inner list, which is row
- if row[2][0] == "P": # if row[2].startswith("P"):
- # one single str argument
- f2.write(",".join(row) + "\n")
- # APPEND MODE
- # with open("append_to_this.txt", "r") as f3:
- # print(f3.readlines())
- with open("append_to_this.txt", "a") as f3:
- f3.write("Pippin\n") # take note of whether last line of provided file has a \n, and adjust as needed!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement