Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2023 Apr 22
- # 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%!!!
- # Comp 1: Basic syntax and knowledge: operators, data types, etc
- # Comp 2: Control Flow
- # Comp 3: Modules and Files
- # Watch your string input and output
- # input...
- # myInput = input().strip()
- # output/print
- # print() is the same print(end="\n")
- # print("some stuff", end=" ") # if I ever override that \n from end...
- # print()
- # print("Clean new line!")
- # Comp 1: Basic syntax and knowledge: operators, data types, etc
- # Common Data Types
- # int
- # float
- # bool # True, False... if x > 5
- # str # " "
- # list # [ ]
- # dict # {key: value}
- # tuple # ( ) immutable, Python sees any a,b,c as (a,b,c)... return a, b -> return (a, b)
- # set # { } no order... no index, no sorting, no slicing; all unique values
- # range # range() --> range object # range(5) or range(0, 5) --> # [0, 1, 2, 3, 4]
- # operators
- # = # assigns
- # == # equality... asking a question, evaluation, used in a condition
- # +
- # -
- # *
- # /
- # % # modulo, gives us the int remainder, "how many whole things didn't fit?"
- # // # floor division... the last even division
- # <
- # >
- # <=
- # >=
- # += # x += 1 is the same x = x + 1
- # -=
- # ** # raise to a power... pow() or math.pow()
- # !=
- # # keywords used like operators
- # in # if value in container
- # not # if not value in container
- # and
- # or # any one True means whole condition is True... limit OR to 2 conditions
- # Comp 2
- # the HOW stuff... control flow structures
- # 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()
- # for ___ in _container_:
- # for item in myList:
- # for char in myStr:
- # for key in myDict:
- # for n in range(0, 12):
- # for i in range(len(myList)): # enumerate()
- # FUNCTIONS
- # defining/writing vs calling
- # a function has ONE, PARTICULAR job
- # parameters are special variables... they don't work like "regular" variables
- # parameters vs arguments
- # return vs print()/output... vs other... do whatever the question says
- # def someFunction(x, y): # don't use any parameters unless the question says to
- # return x ** y
- # # print("hey")
- #
- # if __name__ == "__main__": # are we running from this very script I'm writing?
- # myInput = int(input())
- # myOther = int(input())
- # myNum = someFunction(myInput, myOther)
- # print(myNum)
- # print(__name__) # __main__
- # 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()
- # len()
- # sum()
- # min()
- # max()
- # int()
- # float()
- # list()
- # str()
- # tuple()
- # set()
- # enumerate() # for i, item in enumerate(myList)
- # open() # opens a file and creates a file/IO stream object
- # type() # print(type(myVar).__name__)
- # round() # cousins math.ceil() and math.floor() are in math module
- # sorted() # returns sorted list.. compare list.sort() does not return anything
- # reversed() # returns reversed list... same
- # help() # help(str), help(str.isspace)
- # dir() # print(dir(str))
- # STRINGS
- # be able to slice strings like it's second nature
- # [start:stop:step]
- myStr = "abcd"
- revStr = myStr[::-1]
- print(revStr)
- # KNOW YOUR WHITESPACE
- # # " "
- # # many other Unicode spaces
- # "\n"
- # "\r"
- # "\t"
- # STRING METHODS
- # myStr.format() # "stuff I want to put together {}".format(var)... compare to similar to f strings
- # myStr.split() # return a list of smaller strings
- # myStr.join() # " ".join(listOfStrings)
- # myStr.strip() # lstrip(), rstrip()
- # myStr.replace(subStr, newStr) # "remove"... myStr = myStr.replace(subStr, "")
- # myStr.find(subStr) # return int index of where found, or -1 if not there
- # myStr.count(subStr) # return int count of occurrences
- # case: myStr.lower(), myStr.upper()... title(), capitalize()
- # is/Boolean: myStr.isupper(), myStr.islower(), myStr.isspace(), myStr.isalpha(), myStr.isdigit(), myStr.isnumeric(), myStr.isalnum()
- # LISTS
- # be able to use indices
- # be able 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.count(item)
- # myList.sort()
- # myList.reverse()
- # myList.copy()
- # myList.index(item)
- # DICT
- # use the key like an index
- # myDict[key] # retrieve value for that key
- # myDict[key] = value # assign value to key
- # myDict.keys()
- # myDict.values()
- # dataList = [ " Agent Scully", "Queequeg", "Fred", " Dino", "Thor ", "Frog Thor"]
- # pets = {}
- # # pets[key.strip()] = value.strip()
- # for i in range(0, len(dataList), 2):
- # pets[dataList[i].strip()] = dataList[i+1].strip()
- # print(pets)
- # MODULES
- # math and csv
- # MATH MODULE
- import math # full import
- # math.factorial(x)
- # math.ceil(x.yz)
- # math.floor(x.yz)
- # math.pow(x, y) # x**y
- # math.sqrt(x)
- # math.fabs(x) # abs(x)
- # math.pi
- # math.e
- # # PARTIAL IMPORT
- # from math import factorial
- # from math import ceil, fabs
- # factorial(x) # not math.factorial(x)
- #
- # # ALIAS IMPORT
- # import math as m
- # m.floor(x.yz) # not math.floor(x.yz)
- # FILES
- # READ MODE
- # with open("test.txt", "r") as f:
- # contents = f.readlines() # like a list of strings of split()
- # print(contents)
- #
- # for line in contents:
- # line = line.strip() # if I want to get rid of final \n
- # print(line)
- # CSV MODULE
- import csv # csv.reader()
- with open("mock_data.csv", "r") as f1:
- contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t")
- print(contents[0:20])
- # WRITE MODE
- with open("output_data16.csv", "w") as f2:
- for row in contents:
- # email = row[3]
- if row[3].endswith(".us"):
- f2.write(",".join(row)+"\n")
- # APPEND MODE
- with open("output_data16.csv", "a") as f3:
- # ['3', 'Enoch', 'Keady', 'ekeady2@hud.gov', 'Male', '216.15.244.138']
- f3.write("1001, Barney, Fife, thelmasman@mayberry.us, Male, 157.108.244.162\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement