Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2022 Feb 26
- # 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
- # str # input()
- # int
- # float
- # list # most common, most adaptable container
- # dict # {k:v}
- # set # {1, 2, 3} all unique values, unordered
- # tuple # (1, 2, 3) SOOOOO IMMUTABLE... 1, 2, 3
- # Operators
- # +
- # -
- # *
- # /
- # % # modulo... whole number remainder, "How many whole things are leftover?"
- # // # floor division
- # ** # raise to power, similar to math.pow()
- # <
- # >
- # <=
- # >=
- # = # assignment
- # == # equality operator is for comparision, it ASKS
- # !=
- # += # increment
- # -= # decrement
- # # operator-like KEYWORDS
- # not
- # in # if __ in ___, if not __ in __
- # and # if x > 20 and x % 2 == 0
- # or
- # Comp 2 Control Flow Structures
- # Basic
- # IF statements... if, if/else, if/elif, if/elif/else
- # LOOPS...
- # WHILE basically an IF that repeats
- # FOR - looping over a container
- # for __ in ___:
- # for item in myList:
- # for i in range(0, len(myList)): # myList[i]
- # for i, item in enumerate(myList):
- # for key in myDict: # myDict[key]... if __ in myDict -> checking keys, not values
- # for key, value in myDict.items()
- # FUNCTIONS
- # defining/writing vs calling
- # parameters vs "regular" variables
- # parameter vs argument
- # def checkDuplicates(someList):
- # dupList = []
- # for item in someList:
- # if someList.count(item) > 1:
- # dupList.append(item)
- # if len(dupList) > 0: # found duplicates!
- # return dupList
- # else:
- # return "No duplicates found."
- # checkDuplicates([1,2,3,3])
- # myList = ["Larry", "Curly", "Moe"]
- # checkDuplicates(myList)
- # a good function has ONE JOB, modular
- # return or not (prints)
- # def myFunction():
- # # do stuff
- # return 1, 2 # that's a tuple!
- # if __name__ = "__main__":
- # myVar = input()
- # call myFunction()
- # BUILT-IN FUNCTIONS
- # help()
- # dir() # returns a list of methods and other attributes
- # print()
- # input() # returns str... \n, \r, \t, \f, " "... input().rstrip()
- # int() # int(input())... int(input().rstrip())
- # len()
- # sum()
- # min()
- # max()
- # sorted() # not exactly the same as list.sort()
- # reversed() # not exactly the same as list.reverse()
- # range() # creates a special container
- # enumerate()
- # round() # and its math module cousins: math.floor() and math.ceil()
- # # creating/recasting
- # str()
- # float()
- # list()
- # dict()
- # tuple()
- # type()
- # isinstance()
- # print(type(5)) # <class 'int'>
- # print(isinstance(5, int)) # True
- # print(isinstance(5, str)) # False
- # STRINGS
- # SLICES
- myString[start:stop], myString[start:stop:step]
- # slice to reverse
- # myRevString = myString[::-1] # save your time and energy
- # STRING METHODS
- myString.format() # or f strings
- myString.rstrip() # strip, etc
- myString.split() # returns a list of smaller strings
- " ".join(myListOfStrings)
- myString.replace(oldSubStr, newSubStr) # use to remove myString.replace("abc", "")
- myString.count(subStr) # returns int
- # case methods: myString.upper(), myString.lower()
- # Boolean methods: myString.isupper(), myString.isdigit()
- # LIST methods
- # myList.append(item)
- # myList.insert(index, item)
- # myList.extend(anotherList)
- # myList.pop() # myList.pop(index)... by index
- # myList.remove() # by value
- # myList.sort()
- # myList.reverse()
- # myList.count(item)
- # myList.clear()
- # myList.index(item) # a value can repeat
- # DICTIONARY
- # myDictionary[key] # retrieves the value
- # myDictionary[key] = value # assign a value for the key
- # if __ in myDictionary: # it's checking the keys
- # myDictionary.keys() # returns container of the keys
- # myDictionary.values() # returns container of values
- # MATH
- # import math
- # math.factorial(num)
- # math.pow() # **
- # math.sqrt()
- # math.ceil()
- # math.floor()
- # math.e
- # math.pi
- # partial import
- # from math import factorial, from math import *
- # factorial() not math.factorial()
- # # aliased import
- # import math as m
- # # OPENING FILES
- # # good practice: Ch 12 Task 4, 7, 8
- # The Gotchas: Working with Files: Reading (25 min)
- # The Gotchas: Working with Files: Writing (33 min)
- with open("mock_data.csv", "r") as f:
- # read() returns the whole file as one big string
- # readlines() returns a list of line by line strings
- import csv
- with open("mock_data.csv", "r") as f:
- myReader = csv.reader(f) # recast as a list = list(csv.reader(f))
- # CSV Reader objects are like lists of lists of data so they recast easily
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement