Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2022 Aug 20
- # LABS
- # Ch 2-14... all Labs!
- # Ch 21-33 just ADDITIONAL LABS, but important practice!
- # Use Submit Mode!!!
- # Watch your string input and output
- # # 1
- # myVar = input().strip() # myVar = input().rstrip()
- # # 2
- # print("some stuff", end=" ") # if you ever override end
- # print() # print(end="\n")
- # Comp 1: Basic syntax and knowledge: operators, data types, etc
- # Comp 2: Control Flow
- # Comp 3: Modules and File Reading/Writing
- # Comp 1: Basic syntax and knowledge: operators, data types, etc
- # Data Types
- # str
- # int
- # float
- # bool
- # list # [1, 2, 3]
- # dict # {"key": "value"}
- # tuple # (1, 2, 3) immutable, any series x, y, z -> Python sees this as a tuple
- # set # {1, 2, 3} unique, has no order (means index positions, no slices)
- # Operators
- # = # assigns a value
- # == # comparison, asking "are these equal?"
- # +
- # -
- # *
- # /
- # % # modulo, get the WHOLE NUMBER remainder, asking "how many didn't fit?"
- # // # last even division, rounded down division... math.floor(x/y), sorta like int(x/y)
- # ** # math.pow(x, y)
- # += # increment
- # -= # decrement
- # <
- # >
- # <=
- # >=
- # !=
- # # # keywords that we use like operators
- # in # if __ in _someContainer_... if myNum in myList
- # not # if not __ in _someContainer_
- # and
- # or # any one True makes the combined condition True... # limit OR to 2 conditions
- # Comp 2
- # Basic control structures
- # CONDITIONAL statements: if, if/else, if/elif, if/elif/else...
- # LOOPS
- # WHILE # an IF that repeats
- # FOR # iterating over a container, or a known # of times
- # for __ in _someContainer_:
- # for item in myList:
- # for char in myString:
- # for key in myDictionary:
- # for n in range(0, 5):
- # for i in range(0, len(myList)): # myList[i]
- # SLICE... an abbreviated copy of str, list, tuple
- # myList = [1, 2, 3, 4, 5]
- # myRevList = myList[::-1]
- # print(myRevList)
- # FUNCTIONS
- # defining/writing vs calling
- # parameters vs outside or "regular" variables
- # parameters vs arguments
- # a good function has ONE job, modular
- # methods are functions that "belong" to that type/class
- def someFunction(x, y):
- return x + y
- # no other output outside of if name = main...
- if __name__ == "__main__":
- myInput = int(input().strip())
- num = someFunction(myInput, someOtherNum)
- print(num)
- # 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
- # print()
- # input()
- # len()
- # range()
- # min()
- # max()
- # sum()
- # # creating or recasting
- # str()
- # int()
- # float()
- # list()
- # tuple()
- # set()
- # dict()
- # sorted() # does return a sorted list (list.sort() doesn't return a value)
- # reversed() # same
- # round() # cousins math.ceil(), math.floor()
- # open() # IO/filestream: .read(), .readlines(), .write()
- #
- # help() # help(str)
- # # help(str)
- # # help(str.strip)
- #
- # # print(dir(str))
- # for item in dir(dict):
- # if not item.startswith("_"):
- # print("{}()".format(item))
- # STRINGS
- # be able to slice like it's second nature... myString[start:stop:step]
- # myString = "abcd"
- # myRevString = myString[::-1]
- # print(myRevString)
- # KNOW YOUR WHITESPACE
- # # " " # ... and a lot of other Unicode spaces
- # "\n"
- # "\r"
- # "\b"
- # "\t"
- # "\f"
- # # STRING METHODS
- # myString.format()
- # myString.strip() # myString.rstrip()
- # myString.split() # return a list of smaller strings
- # ", ".join(listOfStrings)
- # myString.replace(subStr, newSubStr) # remove... myString.replace(subStr, "")
- # myString.find(subStr) # return index where it starts, or -1 if not there
- # myString.count(subStr) # return int of number of occurences
- # # is/Boolean methods: isupper(), islower(), isdigit(), isspace(), isalpha
- # case changing: upper(), lower(), title()
- # LISTS
- # be able to slice!
- # LIST METHODS
- # +
- # myList.append(item)
- # myList.insert(i, item)
- # myList.extend(anotherList)
- # # -
- # myList.pop(index)
- # myList.remove(value/item)
- # # other
- # myList.count(item) # returns number of occurences
- # myList.sort() # no return, modifies in place
- # myList.reverse() # same
- # # not as important
- # myList.clear()
- # myList.copy()
- # myList.index(item)
- # DICTIONARIES
- # # # use the key like an index
- # myDict[key] # retrieves the value for that key, myDict.get()
- # myDict[key] = value # compare to myDict.update()
- # myDict.keys()
- # myDict.values()
- # myDict.items() # for k, v in myDict.items()
- #
- # # IN keyword with a dictionary is looking at KEYS, not values
- #
- # # MODULES
- # # MATH MODULE
- # import math
- # math.factorial()
- # math.ceil()
- # math.floor()
- # math.pow(x, y) # **, not to be confused with math.exp()
- # math.sqrt()
- # math.fabs() # abs()
- # math.pi
- # math.e
- #
- # # different import types
- # # full import: math.factorial()
- # # partial import
- # # from math import factorial
- # # --> factorial()
- # # from math import *
- # # still --> factorial(), sqrt()
- # # # aliased imports
- # # import math as m --> m.factorial()
- #
- # # RANDOM
- # # import random
- # # random.choice(myList) # returns a random item from a list
- # # random.randint(x, y) # INCLUDES the stop y
- # # random.randrange(x, y) # exclude the stop number y
- #
- # # CSV
- # csv.reader() # reads a .csv or .tsv and returns list-like object
- #
- # # READING files
- # with open("filename.txt", "r") as f:
- # # when reading, I grab contents and get out of the open block
- # # contents = f.read() # whole file as one big string
- # contents = f.readlines() # a list of line by line strings, WILL have \n
- # # get off with/open quickly when reading
- import csv
- with open("mock_data.csv", "r") as f:
- contents = list(csv.reader(f)) # a list of lists of strings, individual cell values
- # contents = list(csv.reader(f, delimiter="\t")) # to get a TSV's data
- # print(contents)
- with open("mock_data_new.csv", "w") as f2:
- for line in contents:
- if line[3].endswith(".org"): # if line[3][-4:] == ".org"
- # f2.write(",".join(line) + "\n") # takes a single string argument
- # or
- # f2.write("{}\n".format(",".join(line)))
- # or
- f2.write(f"{','.join(line)}\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement