Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2022 May 28
- # LABS
- # Ch 3-13... all Labs!
- # Ch 14-18 not as important, other than some good labs in Ch 15
- # Ch 19-30 just LABS, but important practice!
- # Use Submit Mode!!!
- # fix 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 Types
- # int
- # float
- # str
- # lists
- # dict
- # tuples # () immutable, any series x, y, z -> interpreted as a tuple, return x, y, return (x,y)
- # sets # all unique/no dupes, no order --> no index, no slicing
- # operators
- # = # assigning a value
- # == # asking, "equality operator"... use in conditions
- # +
- # -
- # *
- # /
- # % # modulo... whole number remainder, "How many whole things are left over?"
- # // # floor division... int(x/y) or really more like math.floor(x/y)
- # <
- # >
- # <=
- # <=
- # !=
- # += # increment... x += 1 same as x = x + 1
- # -= # decrement
- # # keywords used like operators
- # in # if __ in _someContainer_
- # not # if not __ in __
- # and
- # or # any one True means whole condition True... limit OR to 2 conditions
- # Comp 2 Control Flow
- # Basic
- # IF statements... if, if/else, if/elif, if/elif/else, etc
- # LOOPS
- # WHILE - an IF that repeats
- # FOR - looping over some container, or looping a known number of times (range())
- # for __ in __:
- # for item in myList:
- # for n in range(0, 5):
- # for i in range(len(myList)):
- # for key in myDictionary:
- # 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 write a file # do whichever the question says
- # methods are functions that belong to a type/class
- def someFunction(x, y):
- return x + y
- if "__name__" == "__main__": # am I running from this particular file?
- myInput = input().strip()
- # call your function
- num = someFunction(myInput, someOtherNum)
- print(num)
- # BUILT-IN FUNCTIONS
- # print()
- # input() # input().strip()
- # # # constructing / recasting functions
- # int()
- # float()
- # str()
- # list()
- # dict()
- # set()
- # tuple()
- # range()
- # # # functions that work with lists or other containers
- # len()
- # sum()
- # min()
- # max()
- # sorted() # compare to list.sort()
- # reversed() # compare to list.reverse()
- # enumerate() # gives you an index and item/value variable to loop over container
- #
- # round() # cousins math.ceil(), math.floor()
- # open() # IO/file methods: .read(), .readlines(), .write()
- # type() # <class int>
- # isinstance() # returns True or False
- #
- # help()
- # dir()
- # # help(str)
- # # print(dir(str))
- # for item in dir(str):
- # if not item.startswith("_"):
- # print("{}()".format(item))
- #
- # help(str.find)
- # STRINGS
- # be able to slice like it's 2nd nature... myString[start:stop:step]
- # myString = "abc"
- # myRevString = myString[::-1]
- # myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]
- # print(myList[::-1])
- #
- # # know your WHITESPACE
- # # " " # ... and a lot of other Unicode spaces
- # # "\n"
- # # "\r"
- # # "\b"
- # # "\t"
- # # "\f"
- #
- # # STRING METHODS
- # myString.format()
- # myString.strip() # myString.rstrip()... input().strip()
- # myString.split() # returns a list of smaller string
- # " ".join(someListOfStrings)
- # myString.replace(oldSubStr, newSubStr) # remove... myString.replace(oldSubStr, "")
- # myString.find(subStr) # compare myString.index()
- # myString.count(subStr) # return int of num occurrences
- # # case methods: myString.upper(), lower(), title(), capitalize()
- # # is/Boolean methods: isupper(), islower(), isdigit(), isnumeric(), isalphanum()
- #
- # # LISTS
- # # again be able to slice
- #
- # # LIST METHODS
- # # +
- # myList.append(item)
- # myList.insert(i, item)
- # myList.extend(anotherList)
- # # -
- # myList.pop(i) # pop by index, default is last
- # myList.remove(item) # remove by value
- # # others
- # 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()
- #
- # # DICT
- # # # use the key like an index
- # # # you can use the dict KEY to get its VALUE
- # myDictionary[key] # retrieve the value for that key... myDict.get()
- # myDictionary[key] = value # so kinda like myDict.update()
- # myDictionary.items() # for k, v in myDict.items()
- # myDictionary.keys() # returns a container of the keys
- # myDictionary.values() # returns a container of values
- #
- #
- # # SETS
- # mySet.add()
- # mySet.remove(item) # by value
- # mySet.pop() # removes a random item
- #
- # # MODULES
- # # MATH MODULE
- # import math
- # math.factorial(x)
- # math.ceil(x.yz)
- # math.floor(x.yz)
- # math.pow(x, y) # similar to **
- # math.sqrt(x)
- # math.fabs() # abs()
- # math.e
- # math.pi
- #
- # # full vs partial import
- # # import math --> math.factorial()
- # # from math import factorial --> factorial()
- # # from math import * --> factorial(), sqrt()
- # # # aliased imports
- # # import math as m --> m.factorial()
- #
- # # OPENING FILES
- # # # good practice: Ch 12 Task 4, 7, 8
- # with open("filename.txt", "r") as f:
- # # # f.read() -> whole file as one big string
- # # # f.readlines() -> a list of line by line strings
- # myContent = f.readlines()
- #
- import csv
- # with open("filename.csv", "r") as f:
- # # csv.reader(f) # a list-like object of line by line lists of strings
- # myContent = list(csv.reader(f))
- #
- # # writing to a file
- # with open("myNewFile.txt", "w") as f:
- # # if you're figuring something out (and maybe looping to create strings)
- # # ... you'll probably need to stay in this with/open block as you write the file
- # f.write("I am writing into this file.\n")
- with open("mock_data.csv", "r") as f:
- contents = list(csv.reader(f))
- print(contents)
- with open("mock_data2.csv", "w") as f2:
- for line in contents:
- if line[3].endswith(".com"): # if line[3][-4] == ".com":
- # f2.write(",".join(line) + "\n")
- # f2.write(f"{",".join(line)}\n")
- f2.write("{}\n".format(",".join(line)))
- # try:
- # # as if it all works
- # except:
- # # as if an error got raised
- # Question on 28.1
- # Type your code here.
- # try: # as if it all works
- # # the idea here is that an error might occur when grabbing these inputs...
- # count = 0
- # v1 = int(input().strip())
- # count += 1
- # v2 = int(input().strip())
- # count += 1
- # v3 = int(input().strip())
- # count += 1
- #
- # theMax = max([v1, v2, v3])
- # print(theMax)
- #
- # except EOFError: # if it fails
- # # ... so we'll use the count var to see how many inputs we did get
- # print("{} input(s) read:".format(count))
- # if count == 0:
- # print("No max")
- # elif count == 1 or v1 >= v2:
- # print("Max is {}".format(v1))
- # else:
- # print("Max is {}".format(v2))
- # Question on Lab 12.8 Words in a Range
- # Remember you can use comparison operators here
- # print("a" < "b") # True
- # print("b" < "a") # False
- # grab the 3 input strings, maybe call them...
- filename = input().strip()
- startWord = input().strip()
- stopWord = input().strip()
- # with open the file:
- # grab contents with readlines
- # loop over those contents:
- # reassign the loop var to the current line stripped (there will be a "\n" on the end)
- # use an IF to check if startWord <= loop var <= stopWord: # or do as 2 conditions
- # if yes, print it
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement