Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2021 Nov 12/18
- # LABS
- # Ch 3-13
- # 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!!!
- # Comp 1 Basic syntax and knowledge: operators, data types, etc
- # DATA TYPES
- # str
- # int
- # floats
- # Boolean
- # list
- # dict # keys, value pairs
- # tuple
- # set # no order, and are all unique values
- # MODULES
- # math
- # csv # often basic filestream methods read(), readlines(), write()
- # OPERATORS
- # +
- # -
- # *
- # /
- # % # modulo - whole number remainder, "how many whole things didn't fit?"
- # // # floor division - truncated integer division
- # = # assignment operator, setting something equal
- # == # equality operator, asking if it's equal, part of a condition: if, while
- # !=
- # += # incrementing, x += 1 is the same x = x + 1
- # -= # decrementing
- # <
- # >
- # <=
- # >=
- # # KEYWORDS used like OPERATORS
- # not # sometimes we're looking not, but there's no equals, not if __ in __
- # in # if __ in __, for __ in ___
- # and # if something and something
- # or # if something or something
- # def
- # del
- # Comp 2: Control Flow Structures
- # Basic
- # IF statements... if, if/else, if/elif/else
- # LOOPS
- # WHILE - more general, like an if that repeats
- # FOR - doing a known number of times or doing some once for every ITEM in a CONTAINER
- # for __ in __:
- # for item in myList:
- # for char in myString:
- # for key in myDictionary: # myDictionary[key]
- # for n in range(0, 5): # do this 5 times
- # for i in range(len(myList)): # if you need the index as much as the item/value at that index
- # myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # X-Files characters
- # if "Agent Scully" in myList:
- # print("Found her.")
- # if "Godzilla" in myList:
- # print("Found him.")
- # else:
- # print("No Godzilla.")
- # print("Godzilla" in myList)
- # FUNCTIONS
- # defining vs calling
- # parameter vs "regular"/"outside" variable - the function call gives the parameter a value
- # # def myFunction(x):
- # # # never say x = something
- # # return x//16
- # return vs print() # do whichever one the question
- # understand that methods are just functions
- # Advanced Control Structures
- # Ch 10: try/except... read 10.3 then Lab 10.7
- # Ch 13: classes... Lab 13.13
- # IF NAME == MAIN
- # import myModule
- # a bunch of function definitions
- # if __name__ = "__main__": # is this file the file I'm running from? Or was it imported?
- # call some functions
- # do some other stuff... but only myModule is the actual file I'm running
- # define functions
- # if __name__ = "__main__":
- # myVar = input()
- # myOutput = myFunction(myVar)
- # print(myOutput)
- # BUILT IN FUNCTIONS
- # print()
- # input() # str... int(input()), float(input()), input().split()
- # help()
- # dir()
- # round()
- # len()
- # max()
- # min()
- # sum()
- # range()
- # int()
- # float()
- # str()
- # list() # []
- # tuple() # ()
- # set()
- # dict() # {}
- # type() # print(type([1, 2, 3]))
- # STRINGS
- # Build up larger strings with str.format() or f strings
- # CONCATENATION... "this string" + "another string"
- # STRING MODULO... print("%s added in this string" % myString)
- # string.format()
- # "{} is a variable".format(myVar)
- # f string
- # f"{myVar} is a variable"
- # SLICES for strings and list
- # myString[start:stop] # myString[start:stop:step]
- # myString = "Just sit right back and you'll hear a tale."
- # print(myString[0:8])
- # print(myString[::-1])
- # print(myString)
- #
- # # STRING METHODS
- # myString.format()
- # myString.split() # myString.split(","), myString.split("\n")
- # " ".join(myList)
- # myString.replace(someSubStr, newSubStr) # also to remove myString.replace(someSubStr, "")
- # myString.upper() # and other case related methods
- # myString.isupper() # isSomething? return Boolean
- # myString.count() # myString.count(someSubStr)
- #
- # # LIST METHODS
- # myList.append(item)
- # myList.extend(anotherList) # merging a second list item by item
- # myList.insert(i, item)
- # myList.remove(item) # by value
- # myList.pop(i) # by index
- # myList.sort() # myList.sort(reverse=True)
- # myList.reverse() # compare to sorted() and reversed()
- #
- # # DICTIONARY
- # myDictionary[key] # retrieves the value
- # myDictionary[key] = value # assign a value for the key
- # # if __ in myDictionary: # it's checking the keys
- # # if __ in myDictionary.keys(): # don't really need keys() here
- #
- # # MATH
- # math.pow(x, y) # same as **, do not confuse math.pow() with math.exp()
- # math.e
- # math.pi
- # math.sqrt()
- # math.ceil()
- # math.floor() # and the built in round()
- # math.factorial()
- # What's in the data window for input()?
- # a string
- # another string
- # another string
- # TEST IT OUT
- # print(input())
- # Ask you to grab inputs until you see -1
- # myVar = input()
- # while myVar != -1:
- # # do our stuff
- # myVar = input()
- # Comp 3: modules, working with files
- # full, normal import
- # import wholeModule
- # wholeModule.thisMethod()
- # math.floor()
- # # partial import
- # from wholeModule import thisMethod
- # thisMethod() # not wholeModule.thisMethod()
- # floor() # not math.floor()
- # # aliased import
- # import wholeModule as mm
- # mm.thisMethod()
- # OPENING FILES
- # good practice: Ch 12 Task 4, 7, 8
- # f = open(filename, "r") # "w" for write, "a" for append
- # contents = f.read() # get the whole file one big string
- # or
- # contents = f.readlines() # list of strings, line by line
- # f.close()
- # with open(filename, "r") as f:
- # contents = f.readlines()
- #
- # # write back out
- # with open(filename, "w") as f:
- # f.write("my string")
- # TAKE THE PRACTICE TEST... CH 31
- myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # X-Files characters
- for i in range(len(myList)):
- print("{}: {}".format(i, myList[i]))
- # for __, __ in enumerate(myList):
- for i, item in enumerate(myList):
- print("{} --> {}".format(i, item))
- # Fibonacci
- # 0 1 1 2 3 5 8 13 21 34
- def fibonacci(n):
- if n < 0:
- return -1
- elif n <= 1:
- return n
- else:
- fibList = [0, 1] # [0, 1, n]
- while len(fibList) < n + 1:
- # fibList[n-1] + fibList[n-2]
- fibList.append(fibList[-1] + fibList[-2])
- return fibList[n]
- print(fibonacci(9)) # 34
- print(fibonacci(8)) # 21
- print(fibonacci(3)) # 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement