Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2023 Nov 18
- # Do those LABS
- # Ch 2-14... all Labs!
- # Ch 21-32 just ADDITIONAL LABS, but important practice!
- # Prac Tests, Ch 33 and 34... more than the Pre
- # Use Submit Mode and get them to 100%!!!
- # PAY ATTENTION to the unit tests!
- # ... then UNIT TEST more! Unit test, unit test, unit test!
- # 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
- # Operators
- # = # assignment
- # == # asking a question? A condition
- # +
- # -
- # *
- # /
- # % # modulo... whole number remainder... "how many whole things didn't fit since the last even division?"
- # // # floor division... last even division
- # <
- # >
- # <=
- # >=
- # += # x+=1 --> x = x + 1
- # -=
- # ** # compare to pow(), math.pow()
- # !=
- # # keywords
- # in # if x in myList, if something in myDict
- # not # if not x in myList
- # and
- # or # any one True means the combo is True... limit OR to 2-3 conditions
- # Data Types/Classes
- # int
- # float
- # bool # True, False
- # str # "" most important data type
- # list # [ ]
- # dict # {key: value}
- # tuple # ( ) immutable, Python sees a,b,c as (a,b,c) --> return x,y --> return (x,y)
- # set # no duplicates, unordered -> no index, no slicing, no sorting
- # range # range()... container of consecutive numbers --> range(start, stop, step)
- # file # open()... f.read(), f.readlines(), f.write()
- # Comp 2
- # Control Flow! The HOW stuff
- # IF statements... if, if/else, if/elif, if/elif/else
- # LOOPS
- # WHILE - an IF that repeats
- # FOR - looping over a container, or a known number of times... hence range()
- # # Check out my For Loops webinar in The Gotchas
- # for ___ in _someContainer__:
- # for item in myList:
- # for char in myStr:
- # for key in myDict: # myDict[key] or use for k, v in myDict.items(): --> x, y = [2, 3]
- # for n in range(5):
- # for i in range(len(myList)): # myList[i]
- # for i, item in enumerate(myList):
- # FUNCTIONS
- # defining/writing vs calling
- # modular... a function has ONE job
- # parameters are special variables for holding stuff coming into the function
- # parameters vs arguments
- # pay attention to whether the function is asked to return or print()/output
- # def someFunction(x, y):
- # return x * y
- #
- # if __name__ == "__main__": # is this script the one that's running now
- # # inside this block we're answering this specific question
- # input1 = int(input())
- # input2 = int(input())
- # # myNum = someFunction(input1, input2)
- # # print(myNum)
- # print(someFunction(input1, input2))
- # 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()
- # enumerate()
- # min()
- # max()
- # sum()
- # len()
- # open()
- # int()
- # float()
- # str()
- # list()
- # dict()
- # set()
- # tuple()
- # type() # x = "5.426353 print(type(x).__name__) --> str
- # round() # cousins math.ceil(), math.floor()
- # pow() # compare to math.pow()
- # abs() # math.fabs()
- # help() # help(str), help(str.isspace)
- # dir() # print(dir(str))
- # STRINGS
- # be able to refer to indices, and slice
- # myStr = "abcd"
- # # mySlice[start:stop:step]
- # revStr = myStr[::-1]
- # print(revStr)
- # KNOW YOUR WHITESPACE
- # " " # space from the spacebar
- # # a lot of Unicode space
- # \n # new line return
- # \r # carriage return
- # \t # tab
- # print() # --> print(end="\n")
- # STRING METHODS
- # myStr.format() # "Stuff I want to put together {:.2f}".format(myVar)
- # myStr.strip() # input().strip()
- # myStr.split() # returns a list of smaller strings
- # ",".join(listOfStrings)
- # myStr.replace(subStr, newSubStr) # "remove" myStr = myStr.replace(subStr, "")
- # myStr.index(subStr), myStr.find(subStr)
- # myStr.count(subStr) # return number of times found
- # case: .lower(), .upper(), .title(), capitalize()
- # is/Boolean: .islower(), .isupper(), .isspace(), .isalpha(), .isnumeric(), .isdigit(), .isalnum()
- # myStr.startswith(subStr), myStr.endswith(subStr)
- # LISTS
- # be able to refer by index and to slice
- # LIST METHODS
- # # +
- # myList.append(item)
- # myList.insert(i, item)
- # myList.extend(anotherList)
- # # -
- # myList.pop(i)
- # myList.remove(item) # pop() by index, remove() by value
- # myList.clear()
- # # other
- # myList.sort()
- # myList.reverse()
- # myList.count(item)
- # myList.copy()
- # myList.index(item)
- # DICT
- # use the key like an index []... then you don't really need DICT methods
- # myDict[key] # get the value for that key --> similar to myDict.get()
- # myDict[key] = value # assigns new value to key --> similar to myDict.update({k:v})
- # DICT METHODS
- # myDict.keys()
- # myDict.values()
- # myDict.items() # for k, v in myDict.items()
- # membership check is on keys
- # if _key_ in myDict: # looks at keys, not values
- # MODULES
- # math and csv
- # MATH MODULE
- # import math # FULL IMPORT
- # math.factorial(x)
- # math.ceil(x)
- # math.floor(x)
- # math.pow(x, y)
- # math.sqrt(x)
- # math.fabs(x)
- # math.pi
- # math.e
- # PARTIAL IMPORT
- # from math import floor # --> floor(x)
- # from math import ceil, sqrt # --> sqrt(x), ceil(x)
- # from math import * # floor(x), sqrt(x)
- #
- # # ALIAS IMPORT
- # import math as m
- # math.floor(x) # can't do!
- # FILES
- # modes: r, w, a
- # READ MODE
- with open("test.txt", "r") as f:
- # f.read() # returns whole file as one big string
- # f.readlines() # returns a list of strings, line by line
- # f.write() # take one str arg and write into file (can't do here bc I'm in read mode)
- contents = f.readlines()
- # print(contents) # ['Hello.\n', 'This\n', 'is\n', 'just\n', 'a\n', 'string.\n', 'But...\n', 'with\n', 'many\n', 'line\n', 'breaks!']
- # for line in contents:
- # line = line.strip()
- # print(line) # print(line, end="\n")
- # CSV Module
- import csv # csv.reader()
- with open("mock_data.csv", "r") as f1: # mockaroo.com
- contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t") for .tsv
- # print(contents)
- # for row in contents[:20]:
- # print(row)
- # WRITE MODE
- with open("output_data30.csv", "w") as f2:
- # write rows into new file where firstName begins with "Re"
- for row in contents:
- # firstname is a position 1
- # if row[1][:2] == "Re":
- if row[1].startswith("Re"):
- f2.write(",".join(row) + "\n") # file write() takes one str arg
- # f2.write(f'{",".join(row)}\n')
- # f2.write('{}\n'.format(",".join(row)))
- # 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("\nPippin")
- # Final advice:
- # Unit test, unit test, unit test!
- # < >, check numbers on either side
- # doing math? check 0, check really big numbers
- # check leading O's in numbers when converting type:
- # input of 007 -> that's "007" -> int("007") -> 7
- # check possible keys from input to see if in dict
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement