Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2022 Mar 19
- # 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
- # str # input()
- # float
- # list
- # dict
- # tuple # immutable... function returning 1, 2, 3 # that's a tuple
- # set # all unique values, unordered
- # bool
- # Operators
- # +
- # -
- # *
- # /
- # % # modulo.. WHOLE NUMBER remainder... "how many things didn't fit?"
- # # even x % 2 == 0
- # # odd x % 2 == 1
- # //
- # ** # raise to a power... math.pow()
- # <
- # >
- # <=
- # >=
- # = # assignment
- # == # asking if equal? comparison for a condition
- # !=
- # +=
- # -=
- # # operator-like KEYWORDS
- # and
- # or
- # in
- # not
- myTuple = ("Gilligan", "Castaway002", "red", "crew") # things about Gilligan
- # if "Gilligan" in myTuple:
- print("Gilligan" in myTuple) # print(3 + 5)
- # print(not "Gilligan" in myTuple)
- # Comp 2 Control Flow Structures
- # Basic
- # IF statements... if, if/else, if/elif, if/elif/else
- # LOOPS...
- # WHILE - basically an IF that repeats
- # FOR - loop over a container, or a specific number of times
- # for __ in __:
- # for item in myList:
- # for i in range(len(myList)): # if interested in index as well as value, myList[i]
- # FUNCTIONS
- # defining/writing vs calling
- # parameters vs "regular" variables
- # def checkDuplicates(someList):
- # someList = # no! don't do this... the CALLS provide values
- # parameter vs argument
- # methods are functions that "belong" to a type
- # a good function has ONE JOB, modular
- # return or not (prints)
- # BUILT-IN FUNCTIONS
- # print()
- # input()
- # help()
- # dir()
- # len()
- # min()
- # max()
- # sum()
- # sorted()
- # reversed()
- # open()
- # range() # creates a container for a series of numbers
- # round() # see also... math.floor() and math.ceil()
- # # constructor / recasting functions
- # int()
- # float()
- # str()
- # dict()
- # tuple()
- # set()
- # # 2 sometimes useful functions
- # type()
- # isinstance(var, type)
- # print(type(3)) # <class 'int'>
- # print(isinstance(3, int)) # True
- # print(isinstance(3, str)) # False
- # STRINGS
- # myString = "Just sit right back and you'll hear a tale."
- # # SLICING strings and lists
- # # myString[start:stop], myString[start:stop:step]
- # # slice to reverse
- # print(myString[::-1])
- #
- # # STRING METHODS
- # myString.format() # or f strings, see Ch8.2 for special formatting instr
- # myString.rstrip()
- # myString.split() # returns a list
- # " ".join(myListOfStrings)
- # myString.replace(oldSubStr, newSubStr)# to remove.. myString.replace("abc", "")
- # myString.count(subStr) # return int
- # case methods: myString.upper(), myString.lower()
- # Boolean/is methods: myString.isupper(), myString.isdigit()
- # LIST METHODS
- # myList.append(item)
- # myList.insert(index, item)
- # myList.extend(anotherList) # myList + anotherList
- # myList.pop() # last or any index by arg
- # myList.remove() # by value
- # myList.sort() # compare to sorted()
- # myList.reverse() # compare to reversed()
- # myList.count(item) # return int
- # myList.clear()
- # myList.index(item) # a value can repeat
- #
- # # DICTIONARY
- # myDictionary[key] # retrieves value for that key, print(myDictionary[key])
- # myDictionary[key] = value # assign a value for the key
- # for _key_ in someDictionary:
- # if __ in myDictionary: # it's checking the keys
- # myDictionary.keys() # returns a set-like container of keys
- # myDictionary.values() # returns a list-like container of values
- # mydictionary.update({k:v}) # I would "update" like I did in line 135 above
- # MATH MODULE
- import math
- # math.sqrt()
- # math.pow() # raise to a power, like **
- # math.ceil()
- # math.floor()
- # math.factorial()
- # math.pi
- # math.e
- # partial import
- # from math import factorial, from math import *
- # factorial() or ceil()... not math.factorial() or math.ceil()
- # # aliased import
- # import math as m
- # m.floor(), m.e
- # # OPENING FILES
- # good practice: Ch 12 Task 4, 7, 8
- # I'll include these webinars in our follow up
- # with open("filename.txt", "r") as f:
- # f.read() # returns the whole file as one big string
- # f.readlines() # returns a list of line by line strings
- import csv
- with open("mock_data.csv", "r") as f:
- myReader = list(csv.reader(f)) # csv.reader() is a list-like object... a list of lists here
- print(myReader) # I've recast the csvreader object as a plain old list
- for line in myReader:
- # print(line[-1])
- print("{} is at {}".format(line[3], line[4])) # then we can get at positions within the line
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement