Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2024 Jan 20
- # 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
- # = # assigns
- # == # asking a question? A condition
- # +
- # -
- # *
- # /
- # % # modulo... whole number remainder... "how many whole things didn't fit since the last even division?"
- # // # floor division... the last even division
- # <
- # >
- # <=
- # >=
- # += # x += 1 ... x = x + 1
- # -=
- # ** # compare to pow(), math.pow()
- # !=
- # # keyword
- # 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
- # floats
- # bool # True, False
- # str # ""
- # list # [ ]
- # dict # {key: value}
- # tuple # ( ) immutable, Python see a,b,c as (a,b,c) --> return x,y --> return (x,y)
- # set # { } no duplicates, unordered --> no index, no slicing, no sort, no reverse
- # range # range()... container of consecutive numbers --> range(start, stop, step)
- # 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, etc
- # LOOPS
- # WHILE - - an IF that repeats
- # FOR - looping over a container, or 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 n in range(5):
- # for i in range(0, len(myList)): # myList[i]
- # for i, item in enumerate(myList):
- # FUNCTIONS
- # defining/writing a function vs calling
- # modular... a function has ONE job
- # return statements
- # 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
- # 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()
- # len()
- # min()
- # max()
- # sum()
- # pow()
- # abs()
- # round() # cousins math.floor() and math.ceil()
- # enumerate()
- # int()
- # float()
- # list()
- # range()
- # str()
- # dict()
- # tuple()
- # chr()
- # ord()
- # open()
- # sorted() # different from list.sort()
- # reversed() # different from list.reverse()
- # type() # print(type(myVar).__name__)
- # help()
- # dir()
- # help(str)
- # print(dir(str))
- # help(str.isspace)
- # STRINGS
- # be able to refer to indices, and slice
- # mySlice[start:stop:step]
- # myStr = "abcd"
- # 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
- # STRING METHODS
- # myStr.format() # "Stuff I want to put together in this string like {:.2f}".format(myVar)
- # 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 where found
- # myStr.count(subStr) # return number of occurrences
- # case: myStr.lower(), myStr.upper(), myStr.capitalize(), myStr.title()
- # is/Boolean: myStr.islower(), myStr.isspace(), myStr.isalpha(), myStr.isdigit(), myStr.isnumeric(), myStr.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.sort()
- # myList.reverse()
- # myList.count(item)
- # 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 --> similar to myDict.get()
- # myDict[key] = value # assigns new value to key --> similar to myDict.update({k:v})
- # DICT METHODS
- # myDict.keys()
- # myDict.values()
- # if "this" in myDict: # membership check on a dict looks at keys
- # if "that" in myDict.values():
- # myDict.items() # for k, v in myDict.items()
- # 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 sqrt # --> sqrt()
- # from math import floor, ceil --> floor(), ceil()
- # from math import * # floor(), sqrt()
- # ALIAS IMPORT
- # import math as m # --> m.floor(), m.factorial()
- # FILES
- # modes: r, w, a
- # 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 (can't do here bc I'm in read mode)
- # contents = f.readlines()
- # for line in contents:
- # line = line.strip()
- # print(line) # print(line, end="\n")
- # print(contents)
- # CSV Module
- import csv # csv.reader()
- with open("mock_data.csv", "r") as f1: # mockaroo.com
- contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t") if a tsv
- # print(contents[0:20])
- # WRITE MODE
- # let's write out a file with just rows where the last name started with H
- # with open("output_data32.csv", 'w') as f2:
- # for row in contents:
- # # last name is position 2... row[2]
- # # lastName = row[2]
- # if row[2].startswith("H") or row[2].startswith("h"):
- # # if row[2].lower().startswith("h"):
- # # if row[2][0].lower() == "h":
- # f2.write(",".join(row) + "\n")
- # APPEND MODE
- # with open("append_to_this.txt", "r") as f3:
- # print(f3.readlines())
- # ['Frodo\n', 'Sam\n', 'Merry'] # no final line return in this file tells me how to end my file's last line
- with open("append_to_this.txt", "a") as f3:
- f3.write("\nPippin")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement