Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2022 Jan 29
- # 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
- # Comp 2: Control Flow Structures
- # Basic: if statements, loops, functions
- # Adv: try/except and raising errors, classes
- # Comp 3: modules, working with files
- # Comp 1 Basic syntax and knowledge: operators, data types, etc
- # DATA TYPES
- # int
- # float
- # str # ""
- # list # [ ]
- # dict # {key: value}
- # tuple # ( ) # function return "more than one value"
- # set # { }
- # # MODULES
- # math
- # csv # often basic filestream methods read(), readlines(), write()
- # OPERATORS
- # +
- # -
- # *
- # /
- # % # modulo - whole number remainder, "how many whole things didn't fit?"
- # // # floor division, truncates division down to whole number
- # = # assignment
- # == # equality operator, asking/comparing as a condition: if, elif, while
- # += # increment
- # -= # decrement
- # >
- # <
- # >=
- # <=
- # ** # raise to power, compare to math.pow()
- # !=
- # keywords we use like operators
- # NOT # w IN... if myString not in myList, if not myString in myList
- # IN
- # AND
- # OR
- # DEF
- # DEL
- # Comp 2: Control Flow Structures
- # Basic...
- # IF statements... if, if/else, if/elif/else
- # LOOPS...
- # WHILE LOOP - if that repeats, more "general"
- # FOR LOOP - looping over a CONTAINER
- # for __ in __:
- # for item in myList:
- # for i in range(len(myList)): # myList[i]
- # for key in myDictionary: # myDictionary[key]
- # myDictionary[key] = value
- # if key in myDictionary:
- # FUNCTIONS
- # defining/writing vs calling
- # parameter/arguments vs "regular"/"outside" variables... miles_per_gallon, mpg
- # return vs print() # do whatever it asks...
- # a function has A JOB
- # understand built-in functions and class methods...
- # are functions: str .split(), list .append(), print(), sum()
- # ADV control structures
- # Ch 10: try/except... read 10.3 and Lab 10.7
- # Ch 13: classes... Lab 13.13
- # first in Ch 7.18... and after Ch 11
- # def someFunction():
- # do some stuff
- # if =__name__ "__main__":
- # myVar = input()
- # myOutput = someFunction(myVar)
- # print(myOutput)
- # BUILT-IN FUNCTIONS
- # print()
- # input() # returns a str
- # int()
- # float()
- # str()
- # list()
- # tuple()
- # set()
- # dict()
- # len() # very useful!
- # sum()
- # min()
- # max()
- # round() # cousins in the math module: math.floor() and math.ceil()
- # range()
- # help()
- # dir()
- # type(myString) # <class str>
- # isinstance(object, type) # isinstance(myString, str)... True
- # Using help() and dir()... yes you can in OA!
- # help(str)
- # print(dir(str))
- # for item in dir(str):
- # if not item.startswith("_"):
- # print(item)
- # help(str.count)
- # STRINGS
- # SLICES for strings and list
- # myString[start:stop] # myString[start:stop:step]
- # Build up larger strings with str.format() or f strings
- # other ways that aren't as good
- # 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"
- # STRING METHODS
- # myString.format()
- # myString.split() # makes a list of strings out of a longer string
- # " ".join(myListOfStrings)
- # myString.strip() and myString.rstrip()
- # myString.replace(someStr, newStr) # myString.replace(someStr, "")
- # myString.upper() # .lower(), .title(), .capitalize()
- # myString.isupper() # is___() returns Boolean
- # myString.count(someStr)
- # 2 main sources of print output problems
- # userInput = input().rstrip() # get rid of extra whitespace: " ", "\n", "\r", "\t"
- # read() or readlines()
- # DON'T LEAVE THE CURSOR HANGING ON A LINE
- # if you ever use the end parameter of print()...
- # for item in myList:
- # print(item, end=" ")
- # print()
- #
- # # LIST METHODS
- # # myList.append(item)
- # # myList.insert(i, item)
- # # myList.extend(anotherList) # merges another list, item by item
- # # myList.pop() # by index, by default -1
- # # myList.remove(item) # by value
- # # myList.sort() # myList.sort(reverse=True)
- # # myList.reverse() # compare to built-ins reversed() and sorted()
- # # myList.count(item)
- #
- # # DICTIONARY
- # # myDictionary[key] # retrieves the value
- # # myDictionary[key] = value # assign a value for the key
- # # if __ in myDictionary: # it's checking the keys
- #
- # # MATH
- # # import math # full import... math.ceil()
- # # # from math import ceil # partial import... ceil() not math.ceil()
- # # math.factorial()
- # # math.pow() # same as **
- # # math.sqrt()
- # # math.floor()
- # # math.ceil() # regular round() is a built-in
- # # math.e
- # # math.pi
- #
- # # 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()
- # # # aliased import
- # # import wholeModule as mm
- # # mm.thisMethod()
- #
- # # OPENING FILES
- # # good practice: Ch 12 Task 4, 7, 8
- #
- # with open(filename, "r") as f:
- # contents = f.read() # whole file as a big string
- # contents = f.readlines() # returns a list of line by line strings
- #
- # # # write back out
- # with open(filename, "w") as f:
- # f.write("my string")
- # mock_data.csv
- # import csv # I kinda ignore this....
- with open("mock_data.csv", "r") as f:
- # print(f.read())
- # print(f.readlines())
- for line in f.readlines(): # line is holding a string
- line = line.strip()
- # print(line)# print(line, end="\n")
- lineList = line.split(",")
- print(lineList[4])
- # question on Lab 26.14 and output formatting
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement