Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2024 Apr 20
- # 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
- # Data Types/Classes
- # int
- # float
- # bool # True, False
- # str # ""
- # list # [ ]
- # dict # {key:value}
- # tuple # ( ) immutable, Python sees a,b,c as (a,b,c) --> return x,y,z --> return (x,y,z)
- # set # { } no duplicate values, no order --> no indices, can't slice it, can't sort it or reverse
- # range # range()... container of consecutive numbers
- # file # open()... f.read(), f.readlines(), f.write()
- # Operators
- # = # assigns a value
- # == # asking a question... result in Boolean value
- # +
- # -
- # *
- # /
- # % # modulo... whole number remainder... "how many things didn't fit since the last even division?"
- # // # floor division... the last even division
- # <
- # >
- # >=
- # <=
- # += # x += 1 --> x = x + 1
- # -=
- # ** # similar to pow() and 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 to be True... limit OR to 2-3 conditions
- # 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 actions a known number of times --> once for everything in the container
- # FOR in Python is explicitly about a container: list, etc
- # Check out my For Loops webinar in The Gotchas
- # for ___ in _someContainer_:
- # for item in myList:
- # for char in myStr:
- # for key in myDict: # for key, value in myDict.items()... or if k is the key, the value for that key is myDict[k]
- # for n in range(0, someNum):
- # for i in range(0, len(myList)):
- # for i, item in enumerate(myList):
- # FUNCTIONS
- # defining/writing a function vs calling
- # modular... a function has ONE specific job
- # print/output or return (or maybe something else)
- # 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?
- # input1 = int(input())
- # input2 = int(input())
- # myNum = someFunction(input1, input2)
- # 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()
- # float()
- # int()
- # str()
- # range()
- # len()
- # sum()
- # min()
- # max()
- # type() # myVar = "hi", print(type(myVar).__name__)
- # enumerate()
- # pow()
- # abs() # cousin math.fabs()
- # round() cousins math.ceil(), math.floor()
- # open()
- # help()# help(str), help(str.isspace)
- # dir() # print(dir(str))
- # STRINGS
- # be able to refer to indices, and to slice
- # myStr = "abcd"
- # # mySlice[start:stop:step]
- # revStr = myStr[::-1]
- # print(revStr)
- # KNOW YOUR WHITESPACE
- " " # space from spacebar
- # a lot of other spaces
- "\n" # new line return
- "\t" # tab
- "\r" # carriage return
- # unless otherwise stated... printed output should end a new line return...
- # 99% of the time it already does
- # 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() again after the loop
- # STRING METHODS
- # myStr.format() # "Stuff I want to put {:formatting instr} and {:.2f} into my string".format(var1, var2)
- # myStr.strip()
- # myStr.split() # returns a list of smaller strings
- # ",".join(listOfStrings)
- # myStr.replace(subStr, newStr) # "remove" myStr = myStr.replace(subStr, "")
- # myStr.index(subStr) # return int index where found, or raise error
- # myStr.find(subStr) # return int index where found, or -1
- # myStr.count(subStr) # return int count of how many times it's there
- # 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) # pop() by index
- # myList.remove(item) # remove() by value
- # myList.clear()
- # # other
- # myList.sort()
- # myList.reverse()
- # myList.count(item) # returns num of times it's there
- # myList.index(item)
- # myList.copy()
- # DICT
- # use the key like an index []... then you don't really need DICT methods
- # myDict[key] # get the value of that key
- # myDict[key] = value # assign a new value to key
- #
- # # membership check
- # # if ___ in myDict: # looking at keys
- #
- # # DICT METHODS
- # myDict.keys() # all dict keys in a set-like object
- # myDict.values() # all dict values in a set-like object
- # MODULES
- # math and csv
- # MATH MODULES
- # 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() not math.ceil()
- # from math import sqrt, floor # sqrt(), floor()
- # from math import * # factorial(), floor(), but no math
- #
- # # ALIAS IMPORT
- # import math as m # m.floor(), m.ceil()
- # FILES
- # modes: r, w, a
- # READ MODE
- # filename = input()
- # with open(filename, "r") as f:
- # with open("plain_text_file.txt", "r") as f:
- # f.read() # returns the whole file as one big string
- # f.readlines() # returns a list of strings, each line is one string
- # f.write(someStr) # writes a single str arg to file
- # with open("plain_text_file.txt", "r") as f:
- # # contents = f.read()
- # contents = f.readlines()
- # # print(contents)
- # for line in contents:
- # line = line.strip()
- # print(line)
- # print(contents)
- import csv
- # csv.reader()
- with open("mock_data.csv", "r") as f1: # mockaroo.com
- # contents = f1.readlines()
- # for row in csv.reader(f1): # csv.reader(f1, delimiter="\t") for tsv files
- # print(row)
- contents = list(csv.reader(f1))
- # print(contents)
- # for row in contents[:10]:
- # print(row)
- # WRITE MODE
- # write out a new file where all the last names begin with "P"
- # with open("output_data37.csv", "w") as f2:
- # for row in contents:
- # # # ['1', 'Remington', 'Shilling', 'rshilling0@wsj.com', 'Male', '1.71.141.52']
- # # last name is row[2]
- # # if row[2].startswith("P") or row[2].startswith("p"):
- # # if row[2][0].lower() == "p":
- # if row[2].lower().startswith("p"):
- # f2.write(",".join(row) + "\n")
- # APPEND MODE
- # reading it to see...
- # with open("append_to_this.txt") as f3:
- # print(f3.readlines())
- with open("append_to_this.txt", "a") as f3:
- f3.write("\nPippin")
- with open("append_to_this.txt") as f3:
- print(f3.readlines())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement