# Exam Review 2023 Feb 18 # LABS # Ch 2-14... all Labs! # Ch 21-34 just ADDITIONAL LABS, but important practice! # Use Submit Mode!!! # 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 # strip() # myVar = input().strip() # myNum = int(input().strip()) # print() --> print(end="\n") # if you ever override the end parameter of print()... # print("some stuff", end=" ") # print() # print("Clean new line!") # Comp 1: Basic syntax and knowledge: operators, data types, etc # Common Data Types # str # "" # float # int # bool # True, False # list # [] # dict # {key:value} # set # {}, all unique values, no order --> no index, no sorting, no slices # tuple # (), immutable... Python sees x,y,z as (x,y,z)... return x, y --> return (x, y) # range # range() creates a series of sequenced numbers # operators # = # assignment # == # equality... asking, comparing... part of condition # + # - # * # / # % # modulo, int remainder..."how many whole things left over?" # // # floor division... "the last even division before this"... x//y -> math.floor(x/y) # < # > # <= # >= # ** # raise to power... similar to math.pow() # != # += # x += 1 --> x = x+1 # -= # # keywords used like operators # 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 # Control Flow Structures # the HOW stuff # IF statements... if, if/else, if/elif, if/elif/else # LOOPS... ## WHILE... an IF statement that repeats ## FOR... for looping a container, or a known number of times (i.e., range()) # for _someVar_ in _someContainer_: # for item in myList: # for key in myDict: # for k, v in myDict.items() # for n in range(0, 5): # [0, 1, 2, 3, 4] # for i in range(len(myList)): # if I'm interested in the index as much as the value/item # FUNCTIONS # defining/writing a function vs calling # special variable: parameter(s) to a function... they don't work like "regular" variables # parameters vs argument # a function has ONE particular job # return vs print()... write a file... whatever the question says # the FUNCTION will be tested for doing ITS JOB alone # method are functions that belong to a particular class/type # def someFunction(x, y): # return x + y # # if __name__ == "__main__": # are we running from this very script I'm writing? # 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() # len() # str() # int() # float() # list() # dict() # range() # set() # tuple() # sum() # min() # max() # type() # print(type(x).__name__) # round() # sorted() # returns sorted list.. compare list.sort() does not return anything # reversed() # returns reversed list... same # open() # IO/file --> .read(), .readlines(), .write() # help(str) # help(str.isspace) # dir() # print(dir(str)) # STRINGS # be able to slice like it's 2nd nature: myString[start:stop:step] myStr = "abcdefg" revStr = myStr[::-1] print(revStr) # KNOW YOUR WHITESPACE # " " # ... and many Unicode spaces # "\n" # "\t" # "\r" # STRING METHODS # "stuff I want to put together {:.2f}".format(var) # f"stuff I want to put together{var:.2f}" # myStr.strip() # lstrip(), rstrip() # myStr.split() # returns a list of smaller strings # " ".join(listOfStrings) # myStr.replace(subStr, newSubStr) # "remove"... myStr = myStr.replace(subStr, "") # myStr.find(subStr) # returns int index of where it starts, or -1 # myStr.count(subStr) # return int count # case: myStr.lower(), myStr.upper(), myStr.title(), myStr.capitalize() # is/Boolean: myStr.isupper(), islower(), isspace(), isalpha(), isalnum(), isnumeric(), isdigit() # LISTS # again know indices and be able to slice # LIST METHODS # # + # myList.append(item) # myList.insert(i, item) # myList.extend(anotherList) # # - # myList.pop(i) # or pop() no arg takes out last one # myList.remove(item) # pop() by index, remove() by value # # other # myList.count(item) # myList.sort() # myList.reverse() # # not as imp # myList.clear() # myList.copy() # myList.index(item) # DICT # use the key like an index # myDict[key] # retrieves the value for that key # myDict[key] = value # assign (new) value for that key # 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() # abs() is a built-in # math.e # math.pi # PARTIAL IMPORT # from math import factorial # from math import ceil, fabs # # don't say math.factorial() # factorial() # floor() # # from math import * # still a partial import... ceil(), sqrt() # # # ALIAS IMPORT # import math as m # m.floor() # m.factorial() # FILES!!! # READ # with open("test.txt", "r") as f: # contents = f.readlines()# list of strings, line by line # print(contents) # for line in contents: # line = line.strip() # print(line) # print(line, end="\n") # # # CSV Module # import csv # with open("mock_data.csv", "r") as f: # contents = list(csv.reader(f)) # print(contents[0:10]) # WRITE MODE # with open("output_data12.csv", "w") as f1: # for line in contents: # if ".edu" in line[3]: # if line[3][-4:] # if line[3].endswith(".edu") # # write() takes a single string arg # f1.write(",".join(line)+"\n") # APPEND MODE # with open("append_to_this.txt", "r") as f3: # contents = f3.readlines() # print(contents) with open("append_to_this.txt", "a") as f3: f3.write("Pippin\n")