Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2023 July 22
- # Do those LABS
- # Ch 2-14... all Labs!
- # Ch 21-32 just ADDITIONAL LABS, but important practice!
- # Prac Tests, Ch 33 and 34
- # Use Submit Mode and get them to 100%!!! And PAY ATTENTION to the unit tests!
- # Comp 1: Basic syntax and knowledge: operators, data types, etc
- # Comp 2: Control Flow
- # Comp 3: Modules and Files
- # Watch your string input and output
- # input...
- # myInput = input().strip()
- # # output/print()
- # print() # print(end="\n")
- # print("Something I'm printing", end=" ") # if we ever override end...
- # print()
- # # print("This should start on a clean new line!")
- # Comp 1: Basic syntax and knowledge: operators, data types, etc
- # Common Data Types/Classes
- # int
- # float
- # bool # True, False... x > 5... evaluates to Bool True or False
- # str # " "
- # list # [ ]
- # dict # {key: value}
- # tuple # ( ) immutable, Python sees x,y,z as (x,y,z) --> return x,y --> return (x, y)
- # set # { } all unique values/no duplicates, no order --> no indices, no slicing, no sorting
- # range # range()... range(0, 5) --> [0, 1, 2, 3, 4]
- # Operators
- # = # assigning
- # == # equality... asking "are these equal?" in a conditional expression
- # +
- # -
- # *
- # /
- # % # modulo... gives an int remainder, "How many whole things didn't fit... since the last even division?"
- # // # floor division... the last even division
- # <
- # >
- # >=
- # <=
- # += # x += 1 --> x = x + 1
- # -=
- # ** # raise to a power like math.pow()... or built-in pow()
- # !=
- # # keywords
- # in # if x in myList
- # not # if not x in myList
- # and
- # or # any one True means combined condition is True... limit OR to 2-3 conditions
- # Comp 2
- # Control Flow! The HOW stuff
- # IF statements... if, if/else, if/elif, if/elif/else
- # LOOPS
- # WHILE - an IF that repeats
- # FOR - looping over a container, or a known number of times... hence range()
- # Check out my For Loops webinar in The Gotchas
- # for ___ in _someContainer_:
- # for item in myList:
- # for char in myStr:
- # for key in myDict:
- # for num in range(0, 8):
- # for i in range(len(myList)): # myList[i]
- # for i, item in enumerate(myList):
- # FUNCTIONS
- # defining/writing vs calling
- # a function has ONE particular job
- # parameters are special variables... they don't work like regular variables
- # parameters vs arguments
- # return vs print()/output vs something else?
- #
- # def someFunction(x, y):
- # return x * y
- #
- # if __name__ == "__main__":
- # # we're solving this particular question
- # myInput = int(input())
- # myOther = int(input())
- # myNum = someFunction(myInput, myOther)
- # print(myNum)
- # See "tasks" in the last section of Ch 10, 11, 13, 14 for function writing practice
- # # CodingBat also has good function-based Python questions:
- # https://codingbat.com/python
- # BUILT-IN FUNCTIONS
- # print()
- # input()
- # len()
- # range()
- # enumerate()
- # min()
- # max()
- # sum()
- # int()
- # float()
- # list()
- # dict()
- # set()
- # type() # x = 3 # print(type(x).__name__)
- # round() # cousins are in the math module: math.ceil(), math.floor()
- # abs() # cousin math.fabs()
- # open()
- # help() # help(str), help(str.isdigit)
- # dir() # print(dir(str))
- # STRINGS
- # be able to refer to indices, and slice
- # myStr = "abcdef"
- # myRevStr = myStr[::-1]
- # print(myRevStr)
- # KNOW YOUR WHITESPACE
- # " "
- # a lot of Unicode spaces
- # "\n" # new line return
- # "\t" # tab
- # "\r" # carriage return
- # STRING METHODS
- # myStr.format() # "stuff I want to put together {}".format(var)
- # myStr.strip()
- # myStr.split() # returns a list of smaller strings
- # " ".join(listOfStrings)
- # myStr.replace(subStr, newSubStr) # "remove"... myStr = myStr.replace(subStr, "")
- # myStr.find(subStr) # returns an index where it finds it, or -1
- # myStr.count(subStr) # returns number of times that occurs
- # case: .lower(), .upper(), .title(), .capitalize()
- # is/Boolean: .isupper(), .islower(), .isspace(), .isalpha(), .isalnum(), .isdigit(), .isnumeric()
- # myStr.startswith(subStr), myStr.endswith(subStr)
- # LISTS
- # be able to use indices, to slice
- # LIST METHODS
- # +
- # myList.append(item)
- # myList.insert(i, item)
- # myList.extend(anotherList)
- # # -
- # myList.pop(i) # pop by index
- # myList.remove(item) # remove by value
- # myList.clear()
- # # other
- # myList.count(item)
- # myList.sort()
- # myList.reverse()
- # myList.copy()
- # myList.index()
- #
- # # DICT METHODS
- # # use the key like an index []... then you don't need DICT methods
- # myDict[key] # retrieves value for that key
- # myDict[key] = value # assigns a new value
- #
- # myDict.keys()
- # myDict.values()
- # myDict.items() # for key, value in myDict.items()
- # MODULES
- # math and csv
- # MATH MODULE
- # import math # FULL IMPORT
- # math.factorial(x)
- # math.ceil(x.yz)
- # math.floor(x.yz)
- # math.pow(x, y)
- # math.sqrt(x)
- # math.fabs(x) # or built-in abs()
- # math.pi
- # math.e
- # PARTIAL IMPORT
- # from math import factorial # --> factorial(x)
- # from math import ceil, floor # --> ceil(x.yz)
- # from math import * # --> sqrt(x)
- #
- # # ALIAS IMPORT
- # import math as m # m.floor(x.yz)
- # FILES
- # READ MODE
- # filename = input()
- with open("test.txt", "r") as f: # with open(filename, "r")
- contents = f.readlines()
- print(contents)
- for line in contents:
- line = line.strip()
- print(line)
- # CSV
- import csv
- with open("mock_data.csv", "r") as f1: # fake data from mockaroo.com
- contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t") for tsv files
- print(contents[:20])
- # WRITE MODE
- with open("output_data22.csv", "w") as f2:
- for row in contents:
- # only write out if email is at reference.com
- # email is position 3
- if "@reference.com" in row[3]: # if row[3].endswith("@reference.com"):
- # write() takes a single str argument
- f2.write(",".join(row) + "\n")
- # APPEND MODE
- # with open("append_to_this.txt", "r") as f3: # it's always worth taking a look at what you have!
- # print(f3.readlines()) # ['Frodo\n', 'Sam\n', 'Merry\n']
- with open("append_to_this.txt", "a") as f3:
- f3.write("Pippin\n")
- # the end
- # Best wishes to our exam takers this week!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement