Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2022 June 25
- # LABS
- # Ch 2-14... all Labs!
- # Ch 21-31 just ADDITIONAL LABS, but important practice!
- # Use Submit Mode!!!
- # Watch your string input and output
- # # 1
- # myVar = input().strip() # myVar = input().rstrip()
- # # 2
- # print("some stuff", end=" ") # if you ever override end
- # print() # print(end="\n")
- # 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
- # Data Types
- # str
- # int
- # float
- # Boolean
- # list
- # dict
- # set # all unique values, no order
- # tuple # immutable, any series x, y, z -> interpreted as a tuple, return x,y -> return (x,y)
- # operators
- # = # assigns a value
- # == # equality, asking a question
- # +
- # -
- # *
- # /
- # % # modulo... whole number remainder, "How many whole things are left over?"
- # // # floor division... math.floor(x/y), int(x/y)
- # += # increment
- # -= # decrement
- # <
- # >
- # <=
- # >=
- # !=
- # ** # math.pow(), pow()
- # # # # keywords used like operators
- # in # if _value_ in _container_
- # not # if not _value_ in _container_
- # and
- # or # any one True makes the combined condition True... limit OR to 2 conditions
- # Comp 2
- # Basic control structures
- # IF statements... if, if/else, if/elif, if/elif/else, etc
- # LOOPS
- # WHILE # an IF that repeats
- # FOR # looping over some container, or a known number of times... range()
- # # for __ in __:
- # for item in myList:
- # for n in range(0, 5):
- # for i in range(len(myList)):
- # for key in myDictionary:
- # FUNCTIONS
- # defining/writing vs calling
- # parameters vs outside, or "regular" variables
- # parameters vs arguments
- # a good function has ONE job, modular
- # return vs print() vs... whatever, write a file # do what the questions asked!
- # methods are functions that "belong" to a type/class
- # def someFunction(x, y):
- # return x + y
- #
- # if "__name__" == "__main__": # am I running from this file I'm writing
- # myInput = int(input().strip())
- # # call our function
- # num = someFunction(myInput, someOtherNum)
- # 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
- # don't do this
- # count = 0
- # def upCount():
- # count += 1
- # BUILT-IN FUNCTIONS
- # print()
- # input()
- # len()
- # sum()
- # min()
- # max()
- # range()
- # str()
- # list()
- # dict()
- # tuple()
- # set()
- # round() # cousins math.ceil(), math.floor()
- # open() # IO/file methods: .read(), .readlines(), .write()
- # sorted() # does return the new list, compare to list.sort(), which doesn't
- # reversed()# does return the new list, compare to list.reverse(), which doesn't
- #
- # help()
- # dir()
- # # help(list)
- # # print(dir(str))
- # for item in dir(str):
- # if not item.startswith("__"):
- # print("{}()".format(item))
- # STRINGS
- # be able to slice like it's second nature... myString[start:stop:step]
- # myString = "abcd"
- # myRevString = myString[::-1]
- # print(myRevString)
- # KNOW YOUR WHITESPACE
- # # " " # ... and a lot of other Unicode spaces
- # # "\n"
- # # "\r"
- # # "\b"
- # # "\t"
- # # "\f"
- # # STRING METHODS
- # myString.format()
- # myString.strip() # myStrip.rstrip()
- # myString.split() # return a list of smaller strings
- # ",".join(someListOfStrings)
- # myString.replace(oldSubStr, newSubStr) # remove... myString.replace(oldSubStr, "")
- # myString.find(subStr) # compare of myString.index(subStr)
- # myString.count(subStr) # return int number of occurrences
- # # is/Boolean methods: isupper(), islower(), isdigit(), isnumeric(), isspace(), isalphanum()
- # # case changing: upper(), lower(), title()
- # LISTS
- # 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.count(item)
- # myList.sort()
- # myList.reverse()
- # # not as important
- # myList.index(item) # be careful, could be in there more than once
- # myList.copy()
- # myList.clear()
- # DICT
- # # # use the key like an index
- # myDict[key] # retrieve the value for that key, compare to myDict.get()
- # myDict[key] = value # compare to myDict.update()
- # myDict.keys()
- # myDict.values()
- # myDict.items() # for k, v in myDict.items():
- # SETS
- # mySet.add(item)
- # mySet.remove(item)
- # mySet.pop() # remove a random item
- # MODULES
- # MATH MODULE
- # import math
- # math.factorial(x)
- # math.ceil(x.yz)
- # math.floor(x.yz)
- # math.pow(x, y)
- # math.sqrt(x)
- # math.fabs(x) # abs(x)
- # math.e
- # math.pi
- # different import types
- # full import: math.factorial()
- # partial import:
- # from math import factorial
- # --> factorial()
- # from math import factorial, sqrt
- # from math import *
- # aliased imports
- # import math as m --> m.factorial()
- # # OPENING FILES
- # good practice in Ch 14 Task 4, 7, 8
- # READING files
- # with open("filename.txt", "r") as f:
- # # when reading, I grab contents and get out of the open block
- # contents = f.read() # whole file as one big string
- # contents = f.readlines() # a list of line by line strings, WILL have \n
- # with open("test.txt", "r") as f:
- # contents = f.readlines()
- # print(contents)
- # for line in contents:
- # line = line.strip()
- # print(line)
- with open("mock_data.csv", "r") as f:
- # contents = f.readlines() # ["1,Flossie,Levey...\n"...]
- # or...
- import csv
- contents = list(csv.reader(f)) # csv.reader(f, delimiter="\t")
- # does one more step for us...
- # ['1', 'Flossie', 'Levey', 'flevey0@jimdo.com', '129.134.226.30']
- print(contents)
- # WRITING to a file... you tend to stay in the with/open() block longer
- # below I'm writing out the original .csv's rows where the email ends in ".org"
- with open("mock_data2.csv", "w") as f2:
- for line in contents:
- if line[3].endswith(".org"): # if line[3][-4] == ".org"
- # file write is NOT print(): no end or sep parameters and can't take multiple arguments
- f2.write("{}\n".format(",".join(line))) # write() takes a single string argument
- # ... don't forget that string usually needs to end in a line return
- # ... that being said you CAN also write out using print() using its file parameter
- # print("{}\n".format(",".join(line)), file=f2)
- # the end!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement