Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2023 Mar 18
- # 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() # no harm, no foul
- # 2 if you ever override end...
- # print("something", end="\n")
- # print("some stuff", end="") # but we need to get our \n back...
- # print() # print(end="\n")
- # print("Clean new line!") # safe!
- # Comp 1: Basic syntax and knowledge: operators, data types, etc -- about 50-55%
- # Comp 2: Control Flow -- about 20-25%
- # Comp 3: Modules and Files -- about 25%
- # Comp 1: Basic syntax and knowledge: operators, data types, etc
- # Operators
- # = # assignment
- # == # equality... asking, comparing... part of condition
- # +
- # -
- # *
- # /
- # % # modulo, int remainder... "how many things left things?"
- # // # 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 operators
- # in # if _someValue_ in _someContainer... if x in myList
- # not # not _someValue_ in _someContainer_
- # and
- # or # any one True means whole condition is True... limit OR to 2 conditions
- #... and + or use complete conditionss: if x > 5 and x % 2 = 0:
- # Common Data Types
- # int
- # float
- # bool
- # 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, is unordered.... no index, no slicing, no sorting
- # range() # range(0, 4) --> sorta like [0, 1, 2, 3]
- # 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 i in range(0, len(myList)): # myList[i]
- # for n in range(5): # for n in range(0, 5) # --> [0, 1, 2, 3, 4] ... just for repetition
- # for char in myStr:
- # for key in myDict: # myDict[key] is the value
- # FUNCTIONS
- # defining/writing vs calling
- # a function has ONE particular job
- # parameters are special "variables"... they dont' work like "regular" variables
- # parameter vs argument
- # 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__": # is this script I'm writing the one that's running?
- # myInput = int(input())
- # myOther = int(input())
- # # num = someFunction(myInput, myOther)
- # # print(num)
- # print(someFunction(myInput, myOther))
- # 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
- # input()
- # print()
- # len()
- # str()
- # list()
- # dict()
- # tuple()
- # float()
- # int()
- # range()
- # type() # x = [1, 2, 4] # print(type(x).__name__)
- # max()
- # min()
- # sum() # avg is.... sum(thisList) / len(thisList)
- # round() # buts its cousins math.ceil() and math.floor() are in the math module
- # sorted() # returns the sorted list... compare list.sort() does not return
- # reversed() # returns reversed list... same
- # open()# IO/file --> f.read(), f.readlines(), f.write()
- # help() # help(str), help(str.isspace)
- # dir() # 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
- # " " # ... many other Unicode spaces
- # "\n"
- # "\t"
- # "\r"
- # STRING METHODS
- # myStr.format() # "stuff I want to put together {:.2f}".format(var) # f strings
- # myStr.strip() # cousins .lstrip(), rstrip()
- # myStr.split() # returns a list of smaller strings
- # myStr.join() # " ".join(listOfStrings)
- # myStr.replace(subStr, newSub) # can use to "remove".... myStr = myStr.replace(subStr, "")
- # myStr.find(subStr) # return its index, or -1
- # myStr.count(subStr) # return number of occurences
- # case: myStr.lower(), myStr.upper(), myStr.title(), myStr.capitalize()
- # is/Boolean: myStr.isupper(), myStr.islower(), myStr.isspace(), myStr.isalpha(), myStr.isdigit(), myStr.isnumeric(), myStr.alnum()
- # LISTS
- # again know indices and be able to slice
- # LIST METHODS
- # # +
- # myList.append(item)
- # myList.insert(i, item)
- # myList.extend(anotherList)
- # # -
- # myList.pop(i) # last by default
- # myList.remove(item) # pop by index, remove by value
- # myList.clear()
- # # other...
- # myList.sort()
- # myList.reverse()
- # myList.count(item)
- # # not as important
- # myList.copy()
- # myList.index(item)
- # 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() x, y = myList # if it has 2 values in it
- # 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) # x ** y
- # math.sqrt(x)
- # math.fabs(x) # also built-in abs(x)
- # math.e
- # math.pi
- #
- # # PARTIAL IMPORT
- # from math import factorial --> factorial()
- # from math import ceil --> ceil()
- # from math import * --> but still... ceil(), floor()
- #
- # # ALIAS IMPORT
- # import math as m --> m.ceil()
- # FILES!!!
- # READ MODE
- # with open("test.txt", "r") as f:
- # contents = f.readlines() # return a list of strings... each line is a string
- # print(contents) # ['Hello.\n', 'This\n', 'is\n', 'just\n', 'a\n', 'string.\n', 'But...\n', 'with\n', 'many\n', 'line\n', 'breaks!']
- # for line in contents:
- # line = line.strip()
- # print(line) # print(line, end="\n")
- # 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[0:20])
- # # WRITE MODE
- # with open("output_data14.csv", "w") as f2:
- # for line in contents:
- # # let's say... write out file where last name in data (inner list) starts with "P"...
- # if line[2][0] == "P": # if line[2].startswith("P"):
- # # takes a single string as its argument
- # f2.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 f4:
- f4.write("Pippin\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement