Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2022 May 21
- # 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() # myVar = input().rstrip()
- # # 2
- # print("some stuff", end=" ") # if you ever override end
- # print() # print(end="\n")
- # Comp 1: Basic syntax and knowledge: operators, data types, etc
- # Data Types
- # int
- # float
- # str
- # lists
- # dict
- # tuples # immutable, any series x, y, z -> interpreted as a tuple... return x, y -> return (x, y)
- # sets # all unique, no order -> no index, no slicing
- # operators
- # = # assignment
- # == # "equality operator"... asking a question?
- # <
- # >
- # >=
- # <=
- # !=
- # +
- # -
- # *
- # /
- # % # modulo - more USEFUL than it seems
- # // # floor division
- # # keywords used like operators
- # not
- # in # if __ in __... if myString in myList
- # and
- # or # any one True means whole condition True... limit OR to 2 conditions
- # Comp 2 Control Flow
- # IF statements... if, if/else, if/elif, if/elif/else, etc
- # LOOPS
- # WHILE - an IF that repeats
- # FOR - looping over a container, or looping a known number of times
- # for __ in __:
- # for item in myList:
- # for n in range(0, 5):
- # for i in range(len(myList):
- # for key in myDictionary:
- # FUNCTIONS
- # defining/writing vs calling
- # parameters vs outside "regular" variables
- # parameters vs arguments
- # return vs print() # do whichever the question says
- # a good function has ONE JOB, modular
- # methods are functions that "belong" to a class/type
- # if you're write a certain function
- def someFunction(x, y):
- return x + y
- if "__name__" == "__main__": # am I running from this particular file?
- myInput = input()
- # call our function
- num = someFunction(myInput, someOtherNum)
- print(num)
- # BUILT-IN FUNCTIONS
- # print()
- # input() # input().strip()
- # # constructing / recasting functions
- # int()
- # str()
- # float()
- # dict()
- # tuple()
- # set()
- # # functions that work with lists or other containers
- # len()
- # sum()
- # min()
- # max()
- # sorted() # compare to list.sort()
- # reversed() # compare to list.reverse()
- # range()
- # enumerate()
- # round() # cousins math.ceil(), math.floor()
- # open() # IO/file methods: f.write(), f.read(), f.readlines()
- # help()
- # dir()
- # type()
- # isinstance()
- # help(list)
- # print(dir(str))
- # help(str.isdigit)
- # for item in dir(str):
- # if not item.startswith("_"):
- # print(item)
- # STRINGS
- # be able to slice like it's 2nd nature
- myString = "abc"
- # myString[start:stop:step]
- myRevString = myString[::-1]
- print(myRevString)
- #
- # # STRING METHODS
- # myString.format()
- # myString.strip() # .lstrip(), .rstrip()
- # myString.split() # returns a list of smaller strings
- # " ".join(listOfStrings)
- # myString.replace(oldSubStr, newSubStr) # remove... myStr.replace(subStr, "")
- # myString.count(subStr) # returns int
- # myString.find(subStr) # returns int
- # case methods: myString.upper(), myString.lower()
- # is/Boolean methods: isupper(), islower(), isdigit()
- # know your WHITESPACE
- # " " # 20ish variations in Unicode
- # "\n"
- # "\r" # carriage return
- # "\b"
- # "\t"
- # "\f"
- # LIST
- # again be able to slice
- # LIST METHODS
- # +
- # myList.append(item)
- # myList.extend(anotherList)
- # myList.insert(i, item)
- # # -
- # myList.pop(i) # pop by index
- # myList.remove(item) # remove by value
- # # other
- # myList.count(item)
- # myList.sort() # "modify in place", no return... compare to built-in sorted()
- # myList.reverse() # "modify in place", no return... compare to built-in reversed()
- # # not as important
- # myList.index(item)
- # myList.copy()
- # myList.clear()
- #
- # # DICT
- # # use the key like an index
- # # you can use the dict KEY to get its VALUE
- # myDictionary[key] # retrieve the value for that key... kinda like dict.get()
- # # you can use the dict KEY to update/change its VALUE
- # myDictionary[key] = value # set a new value, kinda like dict.update()
- #
- # myDict.get(key)
- # myDict.update({k:v})
- # myDict.items() # for k, v in myDict.items()
- # myDict.keys() # returns a container of keys
- # myDict.values() # returns a container of values
- #
- # # SETS
- # mySet.add()
- # mySet.remove(item) # by value
- # mySet.pop() # removes a random entry
- # COMP 3 files and modules
- # MODULES
- # MATH
- # import math
- math.factorial(x)
- math.ceil(x.yz)
- math.floor(x.yz)
- math.pow(x, y) # similar to **
- math.sqrt(x)
- # full vs partial import
- # import math --> math.factorial()
- # from math import factorial --> factorial()
- # from math import * --> factorial(), sqrt(), etc
- # import math as m --> m.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()
- 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)) # delimiter="/t"
- # 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." + "\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement