Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2021 Nov 11/27
- # 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!
- # Comp 1 Basic syntax and knowledge: operators, data types, etc
- # DATA TYPES
- # strings / str
- # integers / int
- # floats
- # Boolean # True, False
- # list # simple array, [1, 2, 3]
- # dictionary / dict # associative array
- # tuple # immutable (1, 2, 3), functions return mult values in a tuple or list sometimes
- # set # unique values only, unordered
- # MODULES
- # math
- # csv # or standard filestream methods also tend to work
- # OPERATORS
- # +
- # -
- # *
- # /
- # % # modulo, "how many whole items are left over?"
- # // # floor division
- # How many weeks and days is 25 days?
- # print(25//7) # 3 (3 * 7 gets to 21) weeks and...
- # print(25%7) # remaining 4 days leftover
- # ** # compare math.pow()
- # = # assignment
- # == # asking, "equality operator" -- seen in conditions: if/elif, while loops
- # !=
- # <
- # >
- # <=
- # >=
- # += # increment. x += 1 is the same as x = x+1
- # -=
- # # KEYWORDS that are used like operators
- # not
- # and
- # or
- # in # if someValue in someList
- # Comp 2: Control Flow Structures
- # 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 ltr in myString:
- # for n in range(0, 27):
- myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # X-Files characters
- # for i in range(len(myList)):
- # if i == len(myList) - 1:
- # print("{} is the last one.".format(myList[i]))
- # else:
- # print("{} is in the list at position {}".format(myList[i], i))
- # FUNCTIONS
- # defining vs calling
- # parameters vs "regular" or "outside" variables
- # def doThis(x, y): # calling the function doThis(3,4), doThis(math.pi, 187)
- # # don't do this: x = 5
- # return x * y / 2
- # return vs print() # do whichever the question says
- # know that methods are themselves functions
- # ADVANCED CONTROL STRUCTURES
- # ch 10: try/except... Ch 10.3 and Lab 10.7
- # Ch 13: Classes
- # def myFunction(): # function definition is outside this IF block
- # if __name__ = "__main__": # am I running from this file directly?
- # # only happens if running this file directly
- # myVar = input()
- # print(myFunction(myVar))
- # BUILT-IN functions
- # print()
- # help()
- # dir() # returns a list of attributes (methods, properties)
- # help(str)
- # print(dir(str))
- # for attr in dir(str):
- # if not attr.startswith("_"):
- # print(attr, end=" ")
- # help(str.join)
- # range()
- # len()
- # sum()
- # min()
- # max()
- # input()
- # bin()
- # int() # int(input())
- # float()
- # list()
- # set()
- # dict()
- # tuple()
- # type() # print(type(myList))
- # STRINGS and STRING METHODS
- # Build up larger strings with str.format() or f strings
- # string.format()
- # "{} is a variable".format(myVar)
- # f string
- # f"{myVar} is a variable"
- # SLICING STRINGS (or LISTS)
- # myString[start:stop] # myString[start:stop:step]
- myString = "Agent Mulder said 'Trust No One'"
- print(myString[0:12]) # "Agent Mulder"
- # you can reverse with a slice
- revString = myString[::-1]
- print(revString)
- # STRING METHODS
- # myString.split() # returns a list of smaller strings
- # " ".join(someList)
- # myString.format()
- # myString.replace(oldSubString, newSubString) # also to remove
- # myString.upper() # lower(), capitalize(), title()
- # myString.isupper() # return Boolean
- # myString.find(subString) # returns the index , or -1 on failure
- # myString.startswith()
- # myString.strip() # also lstrip(), rstrip()
- # myString.count()
- # print(input())
- # LIST METHODS
- myList.append()
- myList.insert()
- myList.extend()
- myList.count()
- myList.pop() # myList.pop(2).. by index
- myList.remove() # by value
- myList.sort()
- myList.reverse()
- # Comp 3: modules, working with files
- # Ch 12... Task 4, 7, 8 and Ch 29 Labs, esp Lab 29.2
- f = open(filename, "r")
- contents = f.read() # whole file as string
- contents = f.readlines() # list of strings, line by line
- f.close()
- # or
- with open(filename, "r") as f:
- contents = f.readlines()
- # Question on Lab 19.2
- # Watch the spaces in those strings! This is where str.format() or f strings are helpful
- # Notice also that the 4 and 5 shown in the text are emulating the terminal inputs,
- # and don't seem to be intended as output for you to print
- user_num = int(input('Enter integer:\n'))
- print("You entered: {}".format(user_num))
- print("{} squared is {}".format(user_num, user_num*user_num))
- print("And {} cubed is {} !!".format(user_num, user_num**3))
- user_num2 = int(input("Enter another integer:\n"))
- print("{} + {} is {}".format(user_num, user_num2, user_num+user_num2))
- print("{} * {} is {}".format(user_num, user_num2, user_num*user_num
- # Pre 19 wording question
- # You don’t need to manually raise an error here necessarily. I would approach
- # this as a try with 2 excepts (a value error and a zero division error).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement