Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2022 July 23
- # 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 Files
- # Comp 1: Basic syntax and knowledge: operators, data types, etc
- # Data Type
- # int
- # float
- # Boolean
- # str
- # list
- # dictionary
- # sets # unordered and no duplicate values
- # tuples # IMMUTABLE, any comma separated series x, y, z... return x, y same as return (x, y)
- # Modules
- # math
- # csv
- # random # not needed but ... random.choice(), random.randint()/random.randrange()
- # OPERATORS
- # = # assigns
- # == ## comparison, asking "are these equal?"
- # +
- # -
- # *
- # /
- # // # floor division... math.floor(x/y), for positive numbers int(x/y)
- # % # modulo... whole number remainder, "how many didn't fit?"
- # ** # raises to a power, similar to math.pow()
- # <
- # >
- # <=
- # >=
- # += # increment x += 1 same as x = x + 1
- # -= # decrement
- # !=
- # # # # # keywords used like operators
- # not # if not _value_ in _container_
- # in # if _value_ in _container_
- # and
- # or # any one True makes the combined condition True.. limit OR to 2 conditions
- # Comp 2
- # Basic control structures
- # IF statements... if, if/elif, if/else, if/elif/else, etc
- # LOOPS
- # WHILE # an IF that repeats
- # FOR # looping over a container
- # for __ in __:
- # for item in myList:
- # for key in myDictionary: # or k... value for a key myDictionary[key]
- # for num in range(0, 5): # or range(5) # args are just like slice: start, stop, step
- # for i in range(len(myList)): # when you're interested in the index of a value, myList[i]
- # FUNCTIONS
- # defining/writing vs calling
- # parameters vs outside, or "regular" variables
- # parameters vs arguments
- # a good function has ONE job, modular
- # return vs print() vs... whatever, write a file # do what the questions asked!
- # def someFunction(x, y):
- # return x + y
- # print(someFunction(1, 2)) # see the function printed? Then the function should return
- # # vs...
- # someFunction(3,4) # function called directly for output? Then it must print() instead
- #
- # # that if main thing...
- # def someFunctionWithIfName(x, y):
- # return x + y
- #
- # if "__name__" == "__main__": # am I running directly from the file I'm writing?
- # myInput = int(input().strip())
- # # call our function
- # # num = someFunctionWithIfName(myInput, someOtherNum)
- # # print(num)
- # BUILT-IN FUNCTIONS
- # print()
- # input()
- # len()
- # sum()
- # min()
- # max()
- # type()
- # # construct or recast a type
- # int()
- # float()
- # list()
- # tuple()
- # set()
- # dict()
- # str()
- # abs()
- # enumerate()
- # range()
- # round() # regular round, cousins math.ceil() and math.floor()
- # sorted() # returns new list, sorted
- # reversed() # returns new list, reversed
- # open() # IO/filestream methods: read(), readlines(), write()
- # help()
- # dir()
- #
- # # help() and dir()
- # # help(str)
- # # dir() returns a list of class "attributes"
- # # print(dir(str)) # can help me zero in...
- # # help(str.split)
- #
- # print(dir(dict))
- # 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() # myString.lstrip(), myString.rstrip()
- # myString.split() # returns a list of smaller strings
- # ",".join(listOfStrings)
- # myString.replace(oldSubStr, newSubStr) # yString.replace(oldSubStr, "")
- # myString.find(subStr) # return the index, or -1
- # myString.count(subStr) #return int count
- # # # is/Boolean methods: isupper(), islower(), isdigit(), isnumeric(), isspace(), isalphanum()
- # # case changing: upper(), lower(), title()
- # LISTS
- # be able is 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 imp
- # myList.index(item) # be careful, could be in there more than once
- # myList.copy()
- # myList.clear()
- # DICT
- # # # 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)
- # 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()
- # # OPENING FILES
- # good practice in Ch 14 Task 4, 7, 8
- # 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
- # with open("mock_data.csv", "r") as f:
- # contents = f.readlines()
- # print(contents)
- with open("mock_data.csv", "r") as f:
- import csv
- contents = list(csv.reader(f))
- 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"):
- f2.write("{}\n".format(",".join(line)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement