Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2022 July 30
- # LABS
- # Ch 2-14... all Labs!
- # Ch 21-31 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
- # Comp 1: Basic syntax and knowledge: operators, data types, etc
- # Data Types
- # str
- # int
- # float
- # Boolean
- # list # [1, 2, 3]
- # dict # {"key": "value"}
- # tuple # (1, 2, 3) immutable, any series x, y, z -> interpreted as a tuple. return x,y -> return (x,y)
- # set # {1, 2, 3} # unordered, no repeats (all unique values)
- # operators
- # = # assign a value
- # == # comparison, asking "are these equal?"
- # +
- # -
- # *
- # /
- # % # modulo, whole number remainder, "How many whole things are left over?"
- # // # floor division, x//y -> math.floor(x/y), for positive numbers same int(x/y)
- # ** # compare to math.pow()
- # += # increment, x += 1 is same as x = x + 1
- # -=
- # <
- # >
- # <=
- # >=
- # !=
- # # keywords that we use like operators
- # in # if __ in _someContainer_
- # 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
- # IF statements... if, if/else, if/elif, if/elif/else...
- # LOOPS
- # WHILE # an IF an repeats
- # FOR # looping over a container... list, str, dict, tuple, set, others like range()
- # # for __ in __:
- # for item in myList: # list, set, tuple
- # for key in myDictionary: # key, k is a good name... use key to get val: myDict[key]
- # # for key, value in myDict.items()
- # for n in range(0, 5): # do this 5 times
- # for i in range(0, len(myList)) # use index numbers in looping over list, etc
- # FUNCTIONS
- # defining/writing vs calling
- # parameters vs outside variable or "regular" variables
- # parameters vs arguments
- # a good function has ONE job, modular
- # methods are functions that "belong" to a type/class
- def someFunction(x, y):
- return x + y
- # no other output outside of if name = main...
- if "__name__" == "__main__":
- myInput = int(input().strip())
- # call our function
- 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()
- # sum()
- # min()
- # max()
- # int()
- # float()
- # str()
- # list()
- # dict()
- # tuple()
- # set()
- # range()
- # round() # cousins math.ceil(), math.floor()
- # open() # IO/filestream: .read(), .readlines(), .write()
- # sorted() # does return the new list, compare to list.sort(), which doesn't
- # reversed() # does return the new list, compare to list.reverse(), which doesn't
- # help()
- # dir()
- # # help(list)
- # # print(dir(str))
- # for item in dir(str):
- # 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() # myStrip.rstrip()
- # myString.split() # return a list of smaller strings
- # ",".join(someListOfStrings)
- # myString.replace(oldSubStr, newSubStr) # remove... myString.replace(oldSubStr, "")
- # myString.find(subStr) # compare of myString.index(subStr)
- # myString.count(subStr) # return int number of occurrences
- # # is/Boolean methods: isupper(), islower(), isdigit(), isnumeric(), isspace(), isalphanum()
- # # case changing: upper(), lower(), title()
- # LISTS
- # be able to slice
- # 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()
- # myList.reverse()
- # # not as important
- # myList.index(item) # be careful, could be in there more than once
- # myList.copy()
- # myList.clear()
- # # # use the key like an index
- # myDict[key] # retrieve the value for that key, compare to myDict.get()
- # myDict[key] = value # compare to myDict.update()
- # myDict.keys()
- # myDict.values()
- # myDict.items() # for k, v in myDict.items():
- # MODULES
- # MATH MODULE
- # import math
- # math.factorial(x)
- # math.ceil(x.yz)
- # math.floor(x.yz)
- # math.pow(x, y)
- # math.sqrt(x)
- # math.fabs(x) # abs(x)
- # math.e
- # math.pi
- # different import types
- # full import: math.factorial()
- # partial import:
- # from math import factorial
- # --> factorial()
- # from math import factorial, sqrt
- # from math import *
- # aliased imports
- # import math as m --> m.factorial()
- # 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
- import csv
- contents = list(csv.reader(f)) # csv.reader(f, delimiter="\t")
- # does one more step for us...
- # ['1', 'Flossie', 'Levey', 'flevey0@jimdo.com', '129.134.226.30']
- print(contents)
- # WRITING to a file... you tend to stay in the with/open() block longer
- # below I'm writing out the original .csv's rows where the email ends in ".org"
- with open("mock_data2.csv", "w") as f2:
- for line in contents:
- if line[3].endswith(".org"): # if line[3][-4:] == ".org"
- # file write is NOT print(): no end or sep parameters and can't take multiple arguments
- f2.write("{}\n".format(",".join(line))) # write() takes a single string argument
- # ... don't forget that string usually needs to end in a line return
- # ... that being said you CAN also write out using print() using its file parameter
- # print("{}\n".format(",".join(line)), file=f2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement