Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2021 Oct 23
- # 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 of operators, data types, etc
- # DATA TYPES
- # integers... int
- # floats
- # string... str
- # Boolean: True and False
- # lists
- # dictionaries... dict # nameOfDict[key]
- # sets
- # tuples
- # MODULES... not as much here in v4!
- # math
- # CSV... a trick: you can ofter use open(), read(), etc
- # OPERATORS
- # +
- # -
- # *
- # /
- # % # modulo... whole number remainder... how many whole things didn't fit?
- # // # floor division... modulo and floor division useful in unit conversion
- # How many pounds/ounces is 47 ounces?
- # print(47//16) # pounds
- # print(47%16) # and ounces left over
- # ** # math.pow()... not math.exp()
- # += # increment
- # -= # decrement
- # = # assign
- # == # comparison, asking a question?
- # !=
- # not
- # <
- # >
- # <=
- # >=
- # and
- # or
- # in # if someItem in someList
- # Comp 2: Control Flow Structures
- # IF... if, if/else, if/elif/else
- # LOOPS
- # WHILE -> an IF that repeats as long the condition stay true
- # FOR LOOP -> match for a container like a list, etc
- # BREAK -- ends the loop, completely
- # CONTINUE -- skips to the NEXT ITERATION of the same loop
- # for someItem in someContainer:
- # for item in myList:
- # for item in myString.split():
- # FUNCTIONS
- # defining vs calling a function
- # parameters vs "regular" variables # see webinar on Functions + Methods
- # def myFunction(x):
- # # # x = "hey" # don't do it!
- # # # each all will provide a value for x, not me
- # return vs print()
- # ADVANCED CONTROL FLOW
- # Try/Except # Ch 10, see 10.3 and Lab 10.7
- # Classes # Ch 13
- # BUILT-IN FUNCTIONS
- # print()
- # input() # str, print(input())
- # len()
- # range() # loop over the range() of len()
- myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]
- # for item in myList:
- # print(item)
- # for i in range(len(myList)):
- # print("{}: {}".format(i, myList[i]))
- # for i, item in enumerate(myList):
- # print(f"{i} --> {item}")
- # enumerate()
- # max()
- # min()
- # sum()
- # sorted() # not to be confused list.sort()
- # reversed() # not to be confused list.reverse()
- # # type()
- # int()
- # float()
- # list() # []
- # dict() # {}
- # str()
- # set()
- # tuple()
- # help()
- # dir()
- # help(str)
- # print(dir(str))
- # help(str.count)
- # for item in dir(str):
- # if not item.startswith("__"):
- # print(item, end=" ")
- # STRINGS
- # 4 ways to build up strings
- # + # concatenation "this string" + " this string " + str(4)
- # string modulo # "%f" % (3.14)
- # "{} is a variable".format(myVar)
- # f"{myVar} is a variable"
- # SLICES
- # a SLICE is an abbreviated copy
- myString = "hey"
- print(myString[::-1]) # reversed string!
- # STRING METHODS (most important methods... there are many others!)
- # myString.split()
- # myString.format() # or use f strings
- # " ".join(someList)
- # myString.replace(oldSubString, newSubString)
- # myString.upper() or myString.lower()
- # myString.isupper()
- # myString.count()
- # LISTS
- # myList.append()
- # myList.extend() # add another container's item
- # myList.insert(index, item)
- # myList.count()
- # myList.pop() # by index
- # myList.remove() # by value
- # myList.sort()
- # myList.reverse()
- # myList.copy()
- # myList.clear()
- # myList.index(item) # not as reliable as looping with range() or enumerate()
- # SETS most important methods...
- # mySet.add()
- # mySet.remove() # by value, raise en error if not here
- # mySet.discard() # by value, no errors if not there
- # mySet.pop() # set pop() takes out a random item
- # 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()
- # Let's look at Lab 5.14 and Lab 24.3!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement