Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2023 Sept 30
- # 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
- # Operators
- # = # assigns
- # == # asking, comparison... conditional expression
- # +
- # -
- # *
- # /
- # // # floor division... the last even division?
- # % # modulo... whole number remainder... how many whole things didn't fit since the last even division?
- # <
- # >
- # <=
- # >=
- # += # x += 1 --> x = x + 1
- # -=
- # ** # raise to a power... math.pow()... or built-in pow()
- # !=
- # # keywords
- # in # if x in myList
- # not # if not x in myList
- # and
- # or # any one True means the combo is True... limit OR to 2-3 conditions
- # Common Data Types/Classes
- # int
- # float
- # bool # True, False
- # str # " " most important type?
- # list # [ ]
- # dict # {key: value}... myDict[key]
- # tuple # ( ) immutable, Python sees x,y,z as (x,y,z) -> return a,b,c --> return (a,b,c)
- # set # { } # no duplicates, unordered --> no index, no slicing, no sorting
- # range # range()... range(0, 5, 1)... [0, 1, 2, 3, 4]
- # 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]... for k, v in myDict.items():
- # for num in range(0, 7):
- # for i in range(0, len(myList)): # myList[i]
- # for i, item in enumerate(myList):
- # FUNCTIONS
- # defining/writing vs calling
- # modular... a function has ONE job
- # parameters are special variable holding things sent into the function
- # parameters vs arguments
- #
- # def someFunction(x, y):
- # return x // y
- #
- # if __name__ == "__main__":
- # # inside this block we're answering this specific question
- # myInput = int(input())
- # myOther = int(input())
- # # myNum = someFunction(myInput, myOther)
- # # print(myNum)
- # print(someFunction(myInput, myOther))
- # 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()
- # len()
- # min()
- # max()
- # sum()
- # list()
- # int()
- # str()
- # dict()
- # range()
- # set()
- # tuple()
- # type() # print(type(x).__name__)
- # enumerate()
- # open()
- # round() # cousins math.ceil() and math.floor()
- # help() # help(str), help(str.isspace)
- # dir() # print(dir(str))
- # STRINGS
- # be able to refer to indices, and slice
- # myStr = "abc"
- # revStr = myStr[::-1] # mySlice[start:stop:step]
- # print(revStr)
- # KNOW YOUR WHITESPACE
- # " " # space from spacebar
- # # a lot of Unicode spaces
- # \n # new line return... print(end="\n")
- # \t # tab
- # \r # carriage return
- # STRING METHODS
- # myStr.format() # "stuff I want to put together {}".format(var)
- # myStr.strip() # input().strip()
- # myStr.split() # returns a list of smaller strings
- # " ".join(listOfStrings)
- # myStr.replace(subStr, newSubStr) # "remove" myStr = myStr.replace(subStr, "")
- # myStr.find(subStr) # return index (int) where found, or -1 on failure
- # myStr.count(subStr) # return int of how many times found
- # case: myStr.lower(), myStr.upper(). myStr.title(), myStr.capitalize()
- # is/Boolean: myStr.islower(), .isupper(), .isspace(), .isalpha(), .isnumeric(), .isdigit(), .isalnum()
- # myStr.startswith(subStr), myStr.endswith(subStr)
- #
- # LISTS
- # be able to ref by index, 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.count(item) # return int count
- # myList.sort() # does not return a value!
- # myList.reverse() # does not 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()
- # if var in myDict: # only looks at keys
- # MODULES
- # MATH MODULE
- # import math # FULL IMPORT
- # math.factorial(x)
- # math.ceil(x.yz)
- # math.floor(x.yz)
- # math.pow(x, y)
- # math.sqrt(x)
- # math.fabs(x) # built-in abs()
- # math.pi
- # math.e
- # PARTIAL IMPORT
- from math import floor # floor(x.yz)
- from math import sqrt, factorial # factorial(x), sqrt(x.y)
- from math import * # sqrt(x.yz)
- # ALIAS IMPORT
- import math as m # m.floor(x)
- # FILES
- # 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
- contents = f.readlines()
- # print(contents)
- # for line in contents:
- # line = line.strip()
- # print(line)
- # CSV Module
- # csv.reader()
- import csv
- with open("mock_data.csv", "r") as f1: # mockaroo.com
- contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t") for a .tsv
- # print(contents[0:20])
- # WRITE MODE
- with open("output_data27.csv", "w") as f2:
- for row in contents:
- # write out a file with every row where the last name starts with D
- # last name is at row[2]
- # if row[2].startswith("d") or row[2].startswith("D"): # if row[2][0].lower() == "d":
- if row[2].lower().startswith("d"):
- # we've found data to write
- f2.write(",".join(row) + "\n")
- # APPEND MODE
- # with open("append_to_this.txt", "r") as f3: # check to see if last line has a \n, follow suit in yours
- # print(f3.readlines())
- with open("append_to_this.txt", "a") as f3:
- addMe = "Pippin"
- f3.write(f"\n{addMe}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement