Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2023 Feb 25
- # LABS
- # Ch 2-14... all Labs!
- # Ch 21-34 just ADDITIONAL LABS, but important practice!
- # Use Submit Mode!!!
- # Watch your string input and output
- # 1
- # myVar = input().strip()
- # 2
- # print() # print(end="\n")
- # # if you ever override end...
- # print("some stuff", end="") # but we need to get back to that clean line following us
- # print()
- #
- # # print("Clean new line!")
- # Comp 1: Basic syntax and knowledge: operators, data types, etc
- # Comp 2: Control Flow
- # Comp 3: Modules and Files
- # Comp 1: Basic syntax and knowledge: operators, data types, etc
- # Common Data Types
- # int
- # float
- # bool
- # str # " "
- # list # [ ]
- # tuple # ( ) immutable, Python sees x,y,z as (x,y,z)... return x,y,z -> return (x,y,z)
- # set # { } all unique values (no repeats), is unordered... no index, no slicing, no sorting
- # dict # {key: value}
- # range() # --> range object... sorta like [0, 1, 2, 3]
- # operators
- # = # assignment
- # == # equality... asking, comparing... part of condition
- # +
- # -
- # *
- # /
- # % # modulo, int remainder... "how many whole things left over?"
- # // # floor division... x//y -> math.floor(x/y)
- # <
- # >
- # <=
- # >=
- # += # increment... x+=1 --> x = x+1
- # -= # decrement
- # ** # raise to a power... pow() or math.pow()
- # !=
- # # keywords used like operator
- # in # if _someValue_ in _someContainer_
- # not # if not _someValue_ in _someContainer_
- # and
- # or # any one True means whole condition is True... limit OR to 2 conditions
- # Comp 2
- # # the HOW stuff... control flow structures
- # 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()
- # for ___ in _someContainer_:
- # for item in myList:
- # for key in myDict: # myDict[key]
- # for n in range(5): # for n in range(0, 5) # --> [0, 1, 2, 3, 4]
- # for i in range(0, len(myList)):
- # FUNCTIONS
- # defining/writing vs calling
- # a function has ONE particular job
- # parameters are special "variables"... they dont' work like "regular" variables
- # parameters vs arguments
- # return vs print()...vs write a file... whatever the question says
- # method are functions that belong to a particular class/type
- #
- # def someFunction(x, y):
- # return x - y
- #
- # if __name__ == "__main__":
- # myInput = int(input())
- # myOther = int(input())
- # num = someFunction(myInput, myOther)
- # print(num)
- # 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()
- # range()
- # list()
- # int()
- # float()
- # str()
- # dict()
- # tuple()
- # set()
- # type() # creates a type object
- # len()
- # enumerate()
- # min()
- # max()
- # sum()
- # round() # buts its cousins math.ceil() and math.floor() are in the math module
- # sorted() # returns sorted list.. compare list.sort() does not return anything
- # reversed() # returns reversed list... same
- # open() # IO/file --> .read(), .readlines()
- # help()
- # dir()
- # help(str) # help(str.isspace)
- # print(dir(str))
- # STRINGS
- # be able to slice like it's 2nd nature: myString[start:stop:step]
- # myStr = "abcdef"
- # revStr = myStr[::-1]
- # print(revStr)
- # KNOW YOUR WHITESPACE
- # " " # ... any many Unicode spaces
- # "\n" # new line return
- # "\t" # tab
- # "\r" # carriage return
- # ... and more! There are many whitespace characters.
- # STRING METHODS #... print(dir(str))
- # "stuff I want to put together {:.2f}".format(var) # f strings
- # myStr.strip() # cousins: .lstrip(), .rstrip()
- # myStr.split() # returns a list of smaller strings
- # " ".join(listOfStrings)
- # myStr.replace(subStr, newSubStr) # "remove"... myStr = myStr.replace(subStr, "")
- # myStr.find(subStr) # return int index or -1 if not there
- # myStr.count(subStr) # return int count of occurrences
- # case: myStr.lower(), myStr.upper(), myStr.title(), myStr.capitalize()
- # is/Boolean: myStr.isupper(), islower(), isspace(), isalpha(), isdigit(), isnumeric()
- # LISTS
- # again know indices and be able to slice
- # LIST METHODS
- # +
- # myList.append(item)
- # myList.insert(i, item)
- # myList.extend(anotherList)
- # # -
- # myList.pop() # myList.pop(i)
- # myList.remove(item) # pop() by index, remove() by value
- # # other...
- # myList.sort()
- # myList.reverse()
- # myList.count(item)
- # # not as important
- # myList.clear()
- # myList.copy()
- # myList.index()
- # DICT
- # use the key like an index... myDict[key]
- # myDict[key] # retrieve the value for that key, so like get()
- # myDict[key] = value # # assign (new) value for that key, so like update({k:v})
- # myDict.keys()
- # myDict.values()
- # myDict.items() # for k, v in myDict.items()
- # MODULES
- # math and csv
- # # MATH MODULE
- # import math # that's a FULL IMPORT
- # math.factorial(x)
- # math.ceil(x.yz)
- # math.floor(x.yz)
- # math.pow(x, y)
- # math.sqrt(x)
- # math.fabs() # also built-in abs()
- # math.e
- # math.pi
- #
- # # PARTIAL IMPORT
- # from math import factorial
- # from math import factorial, ceil
- # from math import * # but still a partial import
- # factorial()
- #
- # # ALIAS IMPORT
- # import math as m
- # m.floor(x.yz)
- # FILES!!!
- # READ MODE
- with open("test.txt", "r") as f:
- contents = f.readlines() # return a list of strings... each string is a line
- print(contents)
- for line in contents:
- line = line.strip()
- print(line)
- # CSV Module
- import csv
- with open("mock_data.csv", "r") as f1:
- contents = list(csv.reader(f1)) # if a tsv... csv.reader(f1, delimiter="\t")
- # print(contents)
- print(contents[:10])
- # WRITE MODE
- with open("output_data13.csv", "w") as f2:
- for line in contents:
- if ".org" in line[3]: # if line[3].endswith(".org")
- # the file write() method takes a SINGLE STRING argument
- f2.write(",".join(line)+"\n")
- # APPEND MODE
- # with open("append_to_this.txt", "r") as f3:
- # contents = f3.readlines()
- # print(contents) # --> ["Frodo\n", "Sam\n", "Merry"]... note that the last line may or may not have a \n
- with open("append_to_this.txt", "a") as f4:
- f4.write("\nPippin\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement