Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2023 Jan 28
- # 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() # print(end="\n")
- # # if you ever override end...
- # print("some stuff", end=" ") # set it back to a clean new line
- # print() # print(end="\n") # wrap up with it!
- # 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 # [ ]
- # dict # {key:value, key:value}
- # tuple # ( ) immutable, Python sees any x,y,z as (x,y,z)... return x,y -> return (x,y)
- # set # { } all unique values (no duplicates), unordered... no index, no sorting, no slices
- # range() # -> range object
- # operators
- # = # assignment
- # == # equality... asking, comparing... part of condition
- # +
- # -
- # *
- # /
- # % # modulo, int remainder..."how many whole things left over?"
- # // # floor division... x//y -> math.floor(x/y)
- # <
- # >
- # <=
- # >=
- # += # increment x += 1 --> x = x + 1
- # -=
- # ** # raise to a power... pow() or math.pow()
- # !=
- # # keywords used like operator
- # 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 _someContainer_:
- # for item in myList:
- # for key in myDict: # key is key, value is myDict[key]
- # for n in range(0, 5): # sorta like [0, 1, 2, 3, 4]
- # for i in range(0, len(myList)):
- # # treat last one differently
- # if i == len(myList)-1:
- # # the last one
- # else:
- # # all the others
- # 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()
- # len()
- # list()
- # dict()
- # int()
- # float()
- # set()
- # tuple()
- # range()
- # sum()
- # min()
- # max()
- # type() # print(type("hi").__name__)
- # 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... same
- # open() # IO/file --> .read(), .readlines(), .write()
- # help()# help(str) or help(str.isspace)
- # dir() print(dir(str))
- # STRINGS
- # be able to slice like it's 2nd nature: myString[start:stop:step]
- # myStr = "abcdef"
- # revStr = myStr[::-1]
- # print(revStr)
- # KNOW YOUR WHITESPACE
- # " " #... and many Unicode spaces
- # "\n"
- # "\t"
- # "\r"
- # STRING METHODS
- # "stuff I want to put together {}".format(var) # or similar f strings
- # myStr.split() # returns a list of smaller strings
- # " ".join(listOfStrings)
- # myStr.strip() # lstrip(), rstrip()
- # myStr.replace(subStr, newSubStr) # "remove"... myStr = myStr.replace(subStr, "")
- # myStr.find(subStr) # return int index or -1 if not there, compare to myStr.index(subStr)
- # myStr.count(subStr) # return int count of occurrences
- # case: myStr.lower(), myStr.upper(), myStr.title(), myStr.capitalize()
- # is/Boolean: myStr.isupper(), islower(), isspace(), isalpha(), isdigit(), isnumeric()
- # 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)
- # myList.remove(item) # pop by INDEX, remove by VALUE
- # # other...
- # myList.count(item)
- # myList.sort() # modify in place, no return value
- # myList.reverse()# modify in place, no return value
- # and 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() # for k, v in myDict.items()
- # MODULES
- # math and csv
- # MATH MODULE
- # import math # <-- that's a FULL IMPORT
- # math.factorial(x)
- # math.ceil(x.yz)
- # math.floor(x.yz)
- # 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
- # from math import factorial, ceil
- # # 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()
- # FILES!!!
- # READ MODE
- # with open("test.txt", "r") as f:
- # contents = f.readlines() # list of strings, line by line
- # # print(contents)
- # # newContents = [] ... if you want a new list of stripped lines
- # for line in contents:
- # line = line.strip() # if you want to get rid of line return as you work
- # # newContents.append(line)
- # 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 = contents[0:20]
- # print(contents)
- # WRITE MODE
- with open("output_data11.csv", "w") as f2:
- for line in contents:
- if ".edu" in line[3]:
- # 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