Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review Nov 26 2022
- # LABS
- # Ch 2-14... all Labs!
- # Ch 21-34 just ADDITIONAL LABS, but important practice!
- # Use Submit Mode!!!
- # Watch your string input and output
- # 1
- # myVar = input().strip()
- # # 2
- # print("some stuff", end=" ") # if you ever override end...
- # print() # print(end="\n")
- # print("Clean new line!")
- # 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
- # Common Data Types
- # str # ""
- # int
- # float
- # list # [ ]
- # dict # {k:v, k:v}
- # tuple # ( ) immutable, Python sees any x,y,z as (x,y,z)... return x,y -> return (x,y)
- # set # { } no order (so no indices, etc), all unique values (no duplicates)
- # operators
- # = # assigns
- # == # "equality operator"... asking a question for a CONDITION: if/elif, while
- # +
- # -
- # *
- # /
- # % # modulo, int remainder... "how many whole things left over?"
- # // # floor division... x//y -> math.floor(x/y)
- # <
- # >
- # <=
- # >=
- # += # increment
- # -= # decrement
- # ** # raising to a power... x**y -> math.pow(x, y)
- # !=
- # # keywords used like operators
- # in # if _someValue_ in _someContainer_
- # not # if not _someValue_ in _someContainer_
- # and
- # or # ny one True means whole condition is True... limit OR to 2 conditions
- # Comp 2
- # # the HOW stuff... control flow structures
- # IF statements... if, if/else, if/elif/elif/else
- # LOOPS
- # WHILE - an IF that repeats
- # FOR - looping over a container, or a known number of times... range()
- # for _item_ in _container_:
- # for item in myList:
- # for n in range(0, 5): # [0, 1, 2, 3, 4]
- # for i in range(0, len(myList)): # myList[i]
- # for key in myDict: # myDict[key]
- # 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()... writes a file... whatever the question says
- # method are functions that belong to a particular type/class
- # def someFunction(x, y):
- # return x + y
- #
- # if __name__ == "__main__":
- # myInput = int(input())
- # myOtherInput = int(input())
- # num = someFunction(myInput, myOtherInput)
- # 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()
- # sum()
- # min()
- # max()
- # range()
- # list()
- # str()
- # int()
- # float()
- # dict()
- # set()
- # type() # print(type("3").__name__)
- # round() # buts its cousins math.ceil() and math.floor() are in the math module
- # open() # IO/file --> .read(), .readlines(), .write()
- # reversed() # returns reversed list... compare list.reverse() does not return anything
- # sorted() # returns sorted list... compare list.sort() does not return anything
- # help(str) # help(str.startswith)
- # print(dir(str))
- # 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 many Unicode spaces
- # "\n"
- # "\r"
- # "\t"
- # "\f"
- # STRING METHODS
- # "stuff I want to put together {}".format(var) # or similar f strings
- # myString.strip()
- # myString.split() # returns a list of smaller strings
- # ",".join(listOfStrings)
- # myString.replace(oldSubStr, newSubStr) # remove... myString.replace(oldSubStr, "")
- # myString.find(subStr) # return int index, or -1 if not found
- # myString.count(subStr) # return int count of number occurences
- # case: myString.lower(), myString.upper(), myString.title()...
- # is/Boolean: isupper(), islower(), isalpha(), isdigit(), isalnum(), isspace()
- # LISTS
- # again know indices and be able to slice
- # LIST METHODS
- # # +
- # myList.append(item)
- # myList.insert(i, item)
- # myList.extend(anotherList)
- # # -
- # myList.pop() # myList.pop(i)... pop() by INDEX
- # myList.remove(item) # remove() by VALUE
- # # other
- # myList.count(item) # returns int number of occurrences
- # myList.sort() # MODIFY in place... no return
- # myList.reverse() # "
- # # not as important
- # myList.clear()
- # myList.copy()
- # myList.index(item)
- # 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() # or for k, v in myDict.items()
- # # 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
- # PARTIAL IMPORT
- # from math import factorial
- # don't say math.factorial()... we didn't import math
- # factorial()
- # ALIAS IMPORT
- # import math as m --> m.floor(), etc
- # FILES!!!
- # READ MODE
- with open("test.txt", "r") as f:
- contents = f.readlines() # list of strings, line by line
- print(contents)
- # for line in contents:
- # line = line.strip()
- # print(line)
- # CSV MODULE
- import csv
- with open("mock_data.csv", "r") as f1:
- contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t")
- # contents = f.readlines() # list of strings, line by line... ['Hello.\n', ...]
- # print(contents) # [['1', 'Remington', 'Shilling', 'rshilling0@wsj.com', 'Male', '1.71.141.52']... ]
- # WRITE MODE
- with open("output_data8.csv", "w") as f2:
- for line in contents:
- if line[3].endswith(".org"): #line[3][-4:] == ".org"
- # write() method takes one single str argument
- f2.write(",".join(line)+"\n")
- # APPEND MODE
- # with open("append_to_this.txt", "r") as f3:
- # print(f3.readlines()) # --> ['Frodo\n', 'Sam\n', 'Merry\n'] # good to look at files to check last line! \n or no?
- with open("append_to_this.txt", "a") as f3:
- f3.write("Pippin\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement