Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # # Exam Review 2022 Sept 24
- #
- # # 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 Files
- #
- # # Comp 1: Basic syntax and knowledge: operators, data types, etc
- # # operators
- # # = # assignment
- # # == # asking "are these equal?"... writing a condition
- # # +
- # # -
- # # *
- # # /
- # # % # modulo, asking "how many whole things didn't fit?" "how many left over?"
- # # // # floor division, the last even division
- # # <
- # # >
- # # <=
- # # >=
- # # += # increment
- # # -= # decrement
- # # !=
- # # ** # raising to a power, similar to math.pow()
- # # # keywords
- # # in # if _someValue_ in _someContainer_
- # # not # if not _someValue_ in _someContainer_
- # # and
- # # or # any one True means whole condition is True... limit OR to 2 conditions
- #
- # # Data Types
- # # str # ""
- # # int
- # # float
- # # list # []
- # # dict # {key: value}
- # # set # {} all unique values, no order --> no index, can't be sorted, can't be sliced
- # # tuple # () immutable... Python sees any x,y,z as (x,y,z)... return x, y -> return (x,y)
- #
- # # Comp 2
- # # the HOW stuff... control flow structures
- # # 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... (see range())
- # # for _item_ in _container_:
- # # for item in myList:
- # # for n in range(0, 5): # [0, 1, 2, 3, 4]
- # # for i in range(len(myList)): # myList[i]
- # # for key in myDictionary: # myDictionary[key]... myDictionary[key] = value
- #
- # # FUNCTIONS
- # # defining/writing vs calling
- # # parameters are special "variables"... they don't work like "regular" variables
- # # parameters vs arguments
- # # a function has ONE particular job
- # # return vs print()... write a file?... whatever the question said its job is
- # # methods are functions that belong to a particular class or data type
- #
- # # def someFunction(x, y):
- # # return x + y
- # #
- # # if __name__ == "__main__":
- # # myInput = int(input())
- # # myOther = int(input())
- # # num = someFunction(myInput, myOther)
- # # 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() # end, sep
- # # input()
- # # len()
- # # max()
- # # min()
- # # sum()
- # # type() # <class 'int'>
- # # range()
- # # list()
- # # dict()
- # # tuple()
- # # int()
- # # float()
- # # set()
- # # sorted() # compare to list.sort()
- # # reversed() # compare to list.reverse()
- # # round() # but its cousins math.ceil() and math.floor() are in the math
- # # open() # IO/file: .read(), .readlines(), .write()
- #
- # # help(str)
- # # print(dir(str))
- # # help(str.isalpha)
- #
- # # print(dir(dict))
- #
- # # STRINGS
- # # be able to slice like it's 2nd nature: myString[start:stop:step]
- # myString = "abc"
- # # revString = myString[::-1]
- # # print(revString)
- #
- # # KNOW YOUR WHITESPACE
- # # # " " # ... and a lot of other Unicode spaces
- # # "\n"
- # # "\r"
- # # "\t"
- # # "\f"
- #
- # # STRING METHODS
- # "stuff I want to put together {}".format(var) # or the similar f strings
- # # myString.strip() # input().strip()
- # # myString.split() # returns a list of smaller strings
- # # ",".join(listOfStrings)
- # # myString.replace(oldSubStr, newSubStr) # remove... myString.replace(subStr, "")
- # # myString.find(subStr) # return index, or -1
- # # myString.count(subStr) # returns number of occurences
- # # # case: lower(), upper(), capitalize(), title()
- # # # is/Boolean: isupper(), islower(), isalpha(), isdigit(), isalnum(), isspace()
- # #
- # # # LISTS
- # # # again be to slice
- # #
- # # # LIST METHODS
- # # # +
- # # myList.append(item)
- # # myList.insert(i, item)
- # # myList.extend(anotherList)
- # # # -
- # # myList.pop() # pop by index
- # # myList.remove(item) # remove by value
- # # # other modifications
- # # myList.count(item) # returns that number
- # # myList.sort()
- # # myList.reverse()
- # # # also rans
- # # myList.copy()
- # # myList.index(item)
- # # myList.clear()
- # #
- # # # DICT
- # # # use the key like an index
- # # # myDict[key] # retrieve the value for that key, so like get()
- # # # myDict[key] = value # assign (new) value for that key, so like update({k:v})
- # # myDict.keys()
- # # myDict.values()
- # # myDict.items() # for k, v in myDict.items()... x, y = [2, 8]
- # #
- # # # SETS
- # # mySet.add(item)
- # # mySet.remove(item)
- # # mySet.pop() # removes something random... crazy
- #
- # # MODULES
- # # math and csv
- # # remember there are different import styles that change how you reference them
- #
- # # MATH MODULE
- # # import math
- # # math.factorial(x)
- # # math.ceil(x.yz)
- # # math.floor(x.zy)
- # # math.pow(x, y) # similar to **, not be confused with math.exp()
- # # math.sqrt(x)
- # # math.fabs() # similar to built-in abs()
- # # math.pi
- # # math.e
- #
- # # different import types
- # # full import...
- # # import math --> math.factorial()
- #
- # # partial import
- # # from math import factorial --> factorial()
- # # from math import sqrt, floor --> sqrt(), floor()
- # # from math import * --> factorial(), sqrt(), floor()
- #
- # # alias import
- # # import math as m --> m.floor(), etc
- #
- # # OPENING FILES
- # # good practice in Ch 14 Task 4, 7, 8
- # READ MODE
- # with open("test.txt", "r") as f:
- # contents = f.readlines() # a list of line by line strings
- # # print(contents)
- # for line in contents:
- # line = line.strip()
- # print(line)
- import csv
- with open("mock_data.csv", "r") as f1:
- contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t")
- print(contents) # [['id', 'first_name', 'last_name', 'email', 'gender', 'ip_address'], ['1'...]]
- # WRITE MODE... writes over pre-existing files destructively
- with open("output_data5.csv", "w") as f2:
- for line in contents:
- if line[3][-4:] == ".edu": # if line[3].endswith(".edu"):
- # the write() method takes a SINGLE string as its arg
- f2.write(",".join(line) + "\n") # or...
- # f2.write("{}{}".format(",".join(line), "\n")) # or...
- # f2.write(f"{','.join(line)}{'\n'}")
- # APPEND MODE... puts us at the end of a pre-existing file so we can write non-destructively to it
- # let's read first...
- # with open("append_to_this.txt", "r") as f3:
- # contents = f3.readlines()
- # print(contents) # ['Frodo\n', 'Sam\n', 'Merry\n']
- with open("append_to_this.txt", "a") as f3:
- f3.write("Pippin\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement