Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2023 Oct 28
- # 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
- # +
- # -
- # *
- # /
- # // # floor division... last even division
- # % # modulo... whole number remainder... "how many whole things didn't fit since the last even division?"
- # <
- # >
- # <=
- # >=
- # += # x = x + 1
- # -=
- # ** # compare to pow(), math.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
- # Data Types/Classes
- # int
- # float
- # bool # True, False
- # str # "" most important data type
- # list # [ ]
- # dict # {key:value}
- # tuple # ( ) immutable, Python sees x,y,z as (a,b,c) --> return a,b --> return (a,b)
- # set # no duplicates, no order... no index, no slicing, no sort
- # range # range()... container of consecutive numbers --> 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 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: # value is myDict[key]
- # for num 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
- # myInput = int(input())
- # myOther = int(input())
- # myNum = someFunction(myInput, myOther)
- # 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()
- # len()
- # min()
- # max()
- # sum()
- # range()
- # int()
- # list()
- # float()
- # set()
- # tuple()
- # dict()
- # type() # print(type(myVar).__name__)
- # enumerate()
- # open()
- # round() # cousins math.ceil(), math.floor()
- # pow()
- # abs()
- # help()
- # dir() # print(dir(str))
- # help(str) # help(str.isspace)
- # STRINGS
- # be able to refer to indices, and slice
- # myStr = "abc"
- # # 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
- # STRING METHODS
- # myStr.format() # "Stuff I want to put together {}".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.find(subStr) # return index where it's found, or -1 on failure
- # myStr.count(subStr) # return int of number found of that thing
- # 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.count(item)
- # myList.sort()
- # myList.reverse()
- # 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, get()
- # myDict[key] = value # assigns a new value to key, takes the place update({key:value})
- # DICT METHODS
- # myDict.keys()
- # myDict.values()
- # myDict.items() # for k, v in myDict.items()
- # if _key_ in myDict:
- # MODULES
- # math and csv
- # MATH MODULE
- # import math # FULL IMPORT
- # math.factorial(x)
- # math.ceil(x)
- # math.floor(y)
- # 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)
- #
- # # 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
- import csv
- with open("mock_data.csv", "r") as f1: # mockaroo.com
- contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t")
- print(contents[0:20])
- # WRITE MODE
- with open("output_data29.csv", "w") as f2:
- # write rows into new file where email address is Australian
- for row in contents:
- # email address is at row[3]
- if row[3].endswith(".au"): # or slice, etc
- 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")
- # I'll end with this:
- # Unit test, unit test, unit test!
- #
- # < >, check numbers on either side
- # math: check 0, check a really big numbers
- # check leading O's in numbers when converting type:
- # input of 011 -> int("011") -> 11
- # check possible keys from input to see if in dict (what happens if you input something that isn't in there?)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement