Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2024 Mar 16
- # Do those LABS
- # Ch 2-14... all Labs!
- # Ch 21-32 just ADDITIONAL LABS, but important practice!
- # get to know the 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!
- # When you've gotten the Ch 33-34 Prac Tests to 100, go back and do each again.
- # This time, try to think of 2-3 more unit tests that could be run on each question.
- # 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
- # +
- # -
- # *
- # /
- # % # 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
- # -=
- # ** # pow(), math.pow()
- # !=
- # # keywords
- # in # membership check... if x in myList:
- # not # if not x in myList:
- # and
- # or # any one True would cause the combo is True... limit OR to 2-3 conditions
- # Data Types/Classes
- # int
- # float
- # bool
- # str # ""
- # list # [ ]
- # dict # {key: value}, review Ch 4.5
- # tuple # ( ) immutable, Python sees x,y,z as (x,y,z) -> return a,b,c --> return (a,b,c)
- # set # {x,y,z} no duplicates, no order --> no indices, can't slice it, can't sort it, can't reverse it
- # range # range()... container of consecutive numbers
- # file # open()... f.read(), f.readlines(), f.write()
- # Comp 2
- # Control Flow! The how and when of our programs
- # IF statements... if, if/else, if/elif, if/elif/else
- # LOOPS
- # WHILE - a general purpose loop, an IF that repeats
- # FOR - repeating action once for everything in a container
- # FOR - repeating actions a known number of times -> once for everything in a container
- # Check out my For Loops webinar in The Gotchas
- # for ___ in _someContainer_:
- # for item in myList:
- # for char in myStr:
- # for key in myDict: # value for that key in myDict[key]
- # for n in range(5):
- # for i in range(0, len(myList)):
- # for i, item in enumerate(myList):
- # FUNCTIONS
- # defining/writing a function vs calling it
- # modular... a function has ONE job
- # output/print vs return (or maybe something)
- # parameters are special variables for holding stuff coming into the function
- # parameters vs arguments
- #
- # def someFunction(x, y):
- # return x ** y
- #
- # if __name__ == "__main__": # is this script the one that's running now
- # input1 = int(input())
- # input2 = int(input())
- # myNum = someFunction(input1, input2)
- # print(myNum)
- # final "task" page of Ch 10, 11, 13, 14
- # CodingBat also has good function-based Python questions:
- # https://codingbat.com/python
- # BUILT-IN FUNCTIONS
- # input()
- # print()
- # range()
- # pow()
- # type() # print(type(myVar).__name__)
- # list()
- # str()
- # int()
- # float()
- # len()
- # sum()
- # min()
- # max()
- # enumerate()
- # open()
- # round() # cousins math.ceil(), math.floor()
- # abs() # math.fabs(), "magnitude"
- # help()
- # dir() # # print(dir(str))
- # help(str) # help(str.isspace)
- # STRINGS
- # be able to refer to indices, and slice
- # myStr = "abcdef"
- # # mySlice[start:stop:step]
- # revStr = myStr[::-1]
- # print(revStr)
- # KNOW YOUR WHITESPACE
- # " " # space from spacebar
- # # a lot of Unicode spaces
- # "\n" # new line return
- # "\t" # tab
- # "\r" # carriage return
- # myVar = input().strip()
- # unless otherwise stated... printed output should end a new line return... 99% of the time it does anyway
- # print("hey") # --> print("hey", end="\n")
- # we only need to change that when...
- #1 ... this specific says to do something else
- #2 ... you yourself overrode the end parameter of print() as the last thing you did... just call print() after that block
- # STRING METHODS
- # myStr.format() # "Stuff I want to put this {} and {} together in a string".format(val1, val2)
- # myStr.strip() # input().strip()
- # myStr.split() # returns a list of smaller strings
- # ",".join(listOfStrings)
- # myStr.replace(subStr, newStr) # "remove" myStr = myStr.replace(subStr, "")
- # myStr.index(subStr), myStr.find(subStr) # return index location
- # myStr.count(subStr) # returns number of times it's there
- # case: lower(), upper(), title(), capitalize()
- # is/Boolean: isspace(), isupper(), islower(), isalpha(), isnumeric(), isdigit(), isalnum()
- # myStr.startswith(subStr), 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) # last one or by index
- # myList.remove(item) # pop() by index, remove() by value
- # myList.clear()
- # # other
- # myList.sort()
- # myList.reverse()
- # myList.count(item) # returns int count of 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
- # myDict[key] = value # assigns new value to key
- #
- # # DICT METHODS
- # myDict.keys() # all dict keys in a set-like object
- # myDict.values() # all dict values in a set-like object
- # check if key in dict
- # if ___ in myDict: # checking the keys
- # MODULES
- # math and csv
- # MATH MODULE
- # import math # FULL IMPORT
- # math.factorial(x)
- # math.ceil(x)
- # math.floor(x)
- # math.sqrt(x)
- # math.pow(x, y)
- # math.fabs(x)
- # math.pi
- # math.e
- #
- # # PARTIAL IMPORT
- # from math import ceil # just ceil(), no math.
- # from math import factorial, sqrt # factorial(), sqrt()
- # from math import * # floor(), sqrt()
- #
- # # ALIAS IMPORT
- # import math as m
- # m.floor(), m.ceil()
- # FILES
- # modes: r, w, a
- # READ MODE
- # filename = input()
- with open("test.txt", "r") as f:
- # f.read() # returns whole file as one big string
- # f.readline() # singular! I stay away from this one
- # 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)
- # for line in contents:
- # line = line.strip()
- # print(line)
- # CSV module
- import csv # csv.reader()
- with open("mock_data.csv", "r") as f1: # mockaroo.com
- # contents = f1.readlines()
- contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t")
- # print(contents)
- # WRITE MODE
- # with open("output_data35.csv", "w") as f2:
- # # write out a file from contents where emails are from Yahoo
- # for row in contents:
- # # email is row[3]
- # if row[3].endswith("@yahoo.com"):
- # 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("\nPippin")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement