Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2022 Dec 17
- # 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
- # # if you ever override end...
- # print("some stuff", end=" ") # set it back right...
- # 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
- # int
- # float
- # str # ""
- # list # [ ]
- # tuple # () immutable, Python sees any x,y,z as (x,y,z)... return x,y -> return (x,y)
- # set # {} all unique values (no duplication), no order (no index, no slice, no sort...)
- # dict # {k: v}
- # type() # -> type object... __name__
- # range() # -> range object
- # operators
- # = # assignment
- # == # equality... asking, comparing... part of a condition
- # +
- # -
- # *
- # /
- # % # modulo, int remainder..."how many whole things left over?"
- # // # floor division x//y -> math.floor(x/y)
- # >
- # <
- # <=
- # >=
- # += # increment x += 1 --> x = x + 1
- # -= # decrement
- # != # asking if they are no equal
- # ** # raise to a power... pow() or math.pow()
- # # keywords used like operators
- # 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
- # 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 # hence 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)): # i = index, item = myList[i]
- # for key in myDict: # key is key, value is 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()... vs write a file... whatever the question says
- # method are functions that belong to a particular class/type
- # def someFunction(x, y):
- # return x + y
- #
- # if __name__ == "__main__": # are we running from this very script I'm writing?
- # 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()
- # input() # returns a str
- # range()
- # list()
- # int()
- # float()
- # set()
- # tuple()
- # dict()
- # type() # print(type(3.14).__name__)
- # len()
- # max()
- # min()
- # sum()
- # round() # buts its cousins math.ceil() and math.floor() are in the math module
- # sorted() # returns sorted list... compare list.sort() does not return anything
- # reversed() # returns reversed list... compare list.reverse() does not return anything
- # open() # IO/file --> .read(), .readlines(), .write()
- # help() # help(str) # help(str.isspace) # help(dict.keys)
- # dir() # print(dir(str)), etc
- # STRINGS
- # be able to slice like it's 2nd nature: myString[start:stop:step]
- # myStr = "abcd"
- # revStr = myStr[::-1]
- # print(revStr)
- # KNOW YOUR WHITESPACE
- # " " #... and many Unicode spaces
- # "\n"
- # "\r"
- # "\t"
- # STRING METHODS
- # "stuff I want to put together {}".format(var) # or similar f strings
- # myStr.strip()
- # myStr.split() # returns a list of smaller strings
- # ",".join(listOfStrings)
- # myStr.replace(oldSubStr, newSubStr) # remove... myStr.replace(oldSubStr, "")
- # myStr.find(subStr) # return index, sim myStr.index(subStr)
- # myStr.count(subStr) # return int count of number occurrences
- # case: myStr.lower(), myStr.upper(), myStr.title(), myStr.capitalize()
- # is/Boolean: isupper(), islower(), isalpha(), isdigit(), isalnum(), isspace()
- # myStr = "abc"
- # upperStr = myStr.upper()
- # print(upperStr)
- # LISTS
- # again know indices and be able to slice
- myList = [1, 2, 3, 1, 2]
- # 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) # return int number of occurences
- # myList.sort()
- # 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()
- # MODULES
- # math and csv
- # MATH MODULE
- import math # <-- that's a FULL IMPORT
- # 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.e
- # math.pi
- # PARTIAL IMPORT
- # from math import factorial
- # # don't say math.factorial()... we didn't import math
- # factorial()
- # from math import * # still a partial import... factorial(), floor()
- #
- # # 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")
- # print(contents)
- with open("output_data9.csv", "w") as f2:
- for line in contents:
- if len(line[1]) > 10: # 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:
- # contents = f3.readlines()
- # print(contents)
- with open("append_to_this.txt", "a") as f4:
- f4.write("Pippin\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement