Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2021 Oct 31
- # LABS
- # Ch 3-13
- # Ch 14-18 not as important
- # Ch 19-30 just LABS, but important practice!
- # Look at ALL requirements of a question
- # CODE must be reusable
- # don't be afraid of Submit for Grading...
- # different TEST CASES are part of the problem
- # Comp 1 Basic syntax and knowledge: operators, data types, etc
- # DATA TYPES
- # numeric types: int and float
- # strings... str
- # lists
- # dictionaries... dict
- # set... no order (which means no indices, no slices), all unique values
- # tuple... immutable
- # MODULES
- # math
- # OPERATORS
- # +
- # -
- # *
- # /
- # % # modulo... gets the INT remainder, how many whole things didn't fit?
- # // # floor division
- # How many weeks and days is 41 days?
- # print(41//7) # gets the weeks
- # print(41%7) # gets the days leftover
- # ** # can also do with math.pow()
- # = # assignment
- # == # comparison, the "equality operator"
- # !=
- # <
- # >
- # <=
- # >=
- # +=
- # -=
- # KEYWORDS that are used like OPERATORS
- # not
- # in # if someItem in someList, if not someItem in someList
- # and
- # or
- # Comp 2: Control Flow Structurs
- # IF... if, if/else, if/elif/else
- # LOOPS...
- # WHILE - like an IF that repeats as long as the condition stays True
- # FOR - doing a known number of times or doing some once for every ITEM in a CONTAINER
- # for someItem in someContainer:
- # for item in myList:
- # for char in myString:
- # for i in range(someNum):
- # for i in range(len(someList)): # i for the index, someList[i] for the value
- # FUNCTIONS
- # defining vs calling a function
- # parameters vs "regular" or "outside" variables
- # def myFunction(x, end="\n"):
- # return vs print(), etc
- # understand that methods are themselves functions
- # ADVANCED CONTROL FLOW
- # Ch 10: try/except
- # Ch 13: classes
- # How many lines or separate strings are in the data input window?
- # print(input())
- # print(input())
- # BUILT-IN FUNCTIONS
- # help()
- # dir() # print(dir(str)), print(dir(list))
- # input()
- # print()
- # range()
- # enumerate()
- # len()
- # sum()
- # min()
- # max()
- # sorted() # compare to list.sort()
- # reversed() # compare to list.reverse()
- # int()
- # float()
- # list() # []
- # set()
- # tuple() # ()
- # dict() # {}
- # type() # print(type(5)) --> <class 'int'>
- # Use help() and dir()!
- # help(list)
- # help(str)
- # print(dir(list))
- # import math
- # for item in dir(math):
- # if not item.startswith("_"):
- # print(item)
- scoobiesDCT = {
- "Scooby": "a blue collar",
- "Shaggy": "green",
- "Velma": "orange",
- "Daphne": "purple",
- "Fred": "an ascot"
- }
- # DICTIONARY looping
- # for key in scoobiesDCT:
- # # key: value
- # print("{}: {}".format(key, scoobiesDCT[key])) # dictName[someKey]
- # for key, value in scoobiesDCT.items():
- # print("{}--> {}".format(key, value))
- # STRINGS
- # 4 ways to build strings
- # + # concatenation "this string" + " this string " + str(4)
- # string modulo # "%f" % (3.14)
- # "{} is a variable".format(myVar)
- # f"{myVar} is a variable"
- # import math
- # print("Pi to 5 places is {:.5f}".format(math.pi)) # <-- float precision
- # SLICES
- myString = "hey there"
- # # myString[start:stop:step]
- # print(myString[::-1]) # yeh
- # STRING METHODS
- # myString.split() # myString.split("\n")
- # " ".join(someList)
- # myString.format()
- # myString.replace(oldSubStr, newSubStr)
- # myString.upper() # myString.lower() and other case methods
- # myString.isupper() # and other is? methods, that return a Boolean
- # myString.count()
- # LISTS
- # myList.append()
- # myList.extend(anotherList)
- # myList.insert(index, item)
- # myList.count()
- # myList.pop() # by index
- # myList.remove() # by value
- # myList.sort() # myList(reverse=True)
- # myList.reverse()
- # SETS
- # mySet.add()
- # mySet.remove() # by value
- # mySet.discard() # by value
- # mySet.pop() # set pop() takes out a random value
- # Comp 3
- # opening FILES
- # good practice here will be Ch 12 Task 4, 7, 8
- f = open(filename, "r")
- contents = f.read()
- f.close()
- # or
- with open(filename, "r"):
- contents = f.read()
- # Lab 20.2, 23.6, 12.8
- # Lab 20.2 unit conversion by 3's: hours, minutes, and seconds
- # whew 3 units instead of 2!
- # remember // to get the larger unit, % to get the smaller (original unit) left over
- # focus on the largest unit and the smallest, then figure out the middle
- total = int(input()) # 4000, 0, etc
- hours = total // (60*60) # round down to get last full hour value, after (60*60) or 3600 seconds
- min= (total % 3600)//60 # do this last
- sec= total % 60 # smallest unit num left almost always simple mod operation
- print("Hours: {}".format(hours))
- print("Minutes: {}".format(min))
- print("Seconds: {}".format(sec))
- # Lab 23.6... we don't know when the negative number is coming. So how
- # many calls to input()? Use a loop:
- value = 0
- numsList = []
- while value >= 0: # this will keep us going until we see the -1
- value = int(input()) # if we're still going, I'm still grabbing inputs
- numsList.append(value)
- print(max(numsList))
- # Lab 12.8 with a word list file
- filename = input()
- startWord = input()
- stopWord = input()
- with open(filename, "r") as f:
- outputList = f.readlines()
- for item in outputList:
- item = item.strip("\n")
- if item >= startWord and item <= stopWord:
- print(item)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement