Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2022 Apr 30
- # LABS
- # Ch 3-13... all Labs!
- # 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!!!
- # fix your string input and output
- # 1
- # myVar = input().strip()
- # # 2
- # print("some stuff", end=" ") # if you ever override end
- # print()
- # see also: "I want the last one to be different"
- myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]
- # for item in myList:
- # print(item)
- # with a loop
- # for i in range(len(myList)):
- # if not i == len(myList)-1:
- # print(myList[i], end="-->")
- # else:
- # print(myList[i])
- # # with string methods...
- # print("-*-".join(myList))
- # 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
- # floats
- # bool (True/False)
- # str
- # lists
- # dict
- # sets # all unique values, no order/index
- # tuples # immutable... return x, y --> return (x, y)
- # operators
- # +
- # -
- # *
- # /
- # % # modulo... What is the remainder? How many WHOLE THINGS didn't fit?
- # // # floor division... x//y same as math.floor(x/y)
- # ** # raise to a power... same as math.pow()
- # <
- # >
- # <=
- # >=
- # = # assignment
- # == # equality... asking if it's equal
- # +=
- # -=
- # != # not equal
- # # keywords
- # not
- # in # if _value_ in _container_, if not _value_ in _container_
- # and
- # or
- # Comp 2 Control Flow
- # IF statements... if, if/else, if/elif, if/elif/else
- # LOOPS
- # WHILE - basically an IF that repeats
- # FOR - looping over some container, doing something a known number of times
- # for __ in __:
- # for item in myList:
- # for i in range()
- # for key in myDictionary: # myDictionary[key] # for k, v in myDictionary.items()
- # FUNCTIONS
- # defining/writing vs calling
- # parameter vs "regular" variables
- # parameters vs arguments
- # return vs print() # do what the question says
- # methods are functions that "belong" to that type/class
- # a good function has ONE JOB, modular
- # def someFunction(x, y):
- # return something
- #
- # if "__name__" == "__main__":
- # myInput = input()
- # # call our function
- # x = someFunction(myInput, myOtherInput)
- # print(x)
- # BUILT-IN FUNCTIONS
- print()
- input() # input().strip(), input().rstrip()
- len()
- max()
- min()
- sum()
- sorted()
- reversed()
- # constructor or recasting
- # int() # int(input().strip())
- # float() # float(input().strip())
- # list()
- # str()
- # dict()
- # set()
- # tuple()
- # range() # for i in range(len(someList)):
- # enumerate() # for i, item in enumerate(myList):
- # round() # not to be confused with math.ceil() and math.floor()
- # open()
- # help()
- # dir() # returns a list of "attributes"
- # type()
- # on help() and dir():
- # help() and dir()
- # help(str)
- # print(dir(str))
- # help(str.rstrip)
- # print(dir(list))
- # for item in dir(list):
- # if not item.startswith("_"):
- # print(item)
- # STRINGS
- # be able to SLICE like it's 2nd nature
- # myString = "abc"
- # # myString[start:stop:step]
- # # slice to reverse: revString = myString[::-1]
- # # STRING METHODS
- # myString.format()
- # myString.strip() # .rstrip(), lstrip()
- # myString.split() # returns a list
- # " ".join(listOfStrings)
- # myString.replace(oldSubStr, newSubStr) # remove... myString.replace(oldSubStr, "")
- # myString.count(subStr) # returns int
- # myString.find(subStr) # returns first occurrence of str, or -1 on failure
- # # # case methods: myString.upper(), myString.lower()
- # # # Boolean/is methods: myString.isupper(), myString.isdigit()
- #
- # LIST
- # # again be able to SLICE
- # # +
- # myList.append(item)
- # myList.insert(i, item)
- # myList.extend(anotherList)
- # # -
- # myList.pop() # by index
- # myList.remove() # by value
- # # other
- # myList.count(item)
- # myList.sort() # "modify in place", no return
- # myList.reverse()
- # DICT
- # use the key like an index
- # myDict[key] # retrieve the value, like myDict.get()
- # myDict[key] = new value # set a value, like myDict.update()
- # myDict.items() # for looping
- # myDict.keys() # returns a set-like object of just the keys
- # myDict.values() # returns a list-like object of just the values
- # # SETS
- # mySet.add()
- # mySet.remove(item)
- # mySet.pop() # removes a random entry
- # MODULES
- # MATH
- # import math
- # math.factorial()
- # math.floor()
- # math.ceil()
- # math.pow() # **, not same as math.exp()
- # math.sqrt()
- # math.e
- # math.pi
- # # full vs partial import
- # # import math --> math.factorial()
- # from math import factorial, sqrt --> factorial()
- # OPENING FILES
- # # good practice: Ch 12 Task 4, 7, 8
- with open("filename.txt", "r") as f:
- # # read() -> whole file as one big string
- # # readlines() -> a list of line by line strings
- myContent = f.readlines()
- # if I'm reading I like to leave the with/open() block as soon as I can
- # to free up memory.
- import csv
- with open("filename.csv", "r") as f:
- # myContent = csv.reader(f) # a list-like object of line by line lists of strings
- myContent = list(csv.reader(f))
- # write
- with open("myNewFile.txt", "w") as f:
- # if you're figuring something out (and maybe looping to create strings)
- # ... you'll probably need to stay in this with/open block as you write the file
- f.write("I am writing a string into this file.")
- # student question
- numList = [1, -5, 4, 37, -2, 512, -511, 8, -3, -55, 0]
- notNegNumList = []
- for n in numList:
- if n >= 0: # or > 0 if you don't want 0
- notNegNumList.append(n)
- # now notNegNumList has only numbers greater than 0
- print(notNegNumList)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement