Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2023 Oct 21
- # 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
- # Comp 1: Basic syntax and knowledge: operators , data types, etc
- # Operators
- # = # assignment
- # == # asking if the values are equal
- # +
- # -
- # *
- # /
- # % # modulo... whole number remainder... how many whole things didn't fit since the last even division?
- # // # floor division... that last even division
- # <
- # >
- # <=
- # >=
- # += # x += 1 --> x = x + 1
- # -=
- # ** # raise to a power... pow() and math.pow()
- # !=
- # # keywords
- # in # if x in myList
- # not # if not x in myList
- # and
- # or # any one True means the combo is True... limit OR to 2-3 conditions
- # Common Data Types/Classes
- # int
- # float
- # bool # True, False
- # str # "" most important data type?
- # list # []
- # dict # {key: value}... myDict[key]
- # set # { } no duplicates, unordered.... no index, no slicing, no sort
- # tuple # ( ) immutable, Python see a,b,c as (a,b,c) -> return x, y --_ return (x,y)
- # range() #... container of consecutive numbers range(0, 7, 1)... [0, 1, 2, 3, 4, 5, 6]
- # file # open()... f.read(), f.readlines(), f.write()
- # 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: # myDict[key]
- # for num in range(5):
- # for i in range(len(myList)): # myList[i]
- # for i, item in enumerate(myList):
- # FUNCTIONS
- # defining/writing vs calling
- # modular... a function has ONE job
- # parameters are special variables for data coming into the function
- # parameters vs arguments
- # pay attention to whether the function is asked to return or print()/output
- #
- # def someFunction(x, y):
- # return x - y
- #
- # if __name__ == "__main__": # is this script the one that's running now?
- # # inside this block we're answering this specific 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
- # input()
- # print()
- # len()
- # sum()
- # min()
- # max()
- # list()
- # str()
- # int()
- # float()
- # range()
- # tuple()
- # dict()
- # type() # x = "3.14159", print(type(x).__name__)
- # enumerate()
- # open()
- # round() # cousins math.ceil(), math.floor()
- # pow()
- # abs() # compare to math.fabs()
- # help() # help(str), help(str.isspace)
- # dir() # print(dir(str))
- # STRINGS
- # be able to refer to indices, and slice
- # myStr = "abcd"
- # # mySlice[start:stop:step]
- # revStr = myStr[::-1]
- # print(revStr)
- # KNOW YOUR WHITESPACE
- " " # space from spacebar
- # a lot of Unicode space
- # \n # new line return # print() -> print(end="\n")
- # \r # carriage return
- # \t # tab
- # STRING METHODS
- # myStr.format() # "Stuff I want to put together {}".format(var)
- # myStr.strip() # input().strip()
- # myStr.split() # return a list of smaller strings
- # " ".join(listOfStrings)
- # myStr.replace(subStr, newStr) # "remove" myStr = myStr.replace(subStr, "")
- # myStr.find(subStr) # returns index where found, or -1 if not there, compare to myStr.index()
- # myStr.count(subStr) # returns int of how many times found
- # case: myStr.lower(), myStr.upper(), myStr.title(), myStr.capitalize()
- # is/Boolean: myStr.islower(), .isupper(), isspace(), isalpha(), .isnumeric(), .isdigit(), .isalnum()
- # myStr.startswith(subStr), myStr.endswith(subStr)
- # LISTS
- # be able to refer by index and to slice
- # LIST METHODS
- # # +
- # myList.append(item)
- # myList.insert(i, item)
- # myList.extend(anotherList)
- # # -
- # myList.pop(i)
- # myList.remove(item) # pop() by index, remove() by value
- # myList.clear()
- # # others
- # myList.count(item)
- # myList.sort()
- # myList.reverse()
- # myList.copy()
- # myList.index(item)
- # DICT
- # use the key like an index []... then you don't need DICT methods
- # myDict[key] # get the value for that key
- # myDict[key] = value # assign a new value to key
- #
- # # DICT METHODS
- # myDict.keys()
- # myDict.values()
- # myDict.items() # for k, v in myDict.items()
- # if _key_ in myDict: # only looks at keys
- # 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)
- # math.pi
- # math.e
- # PARTIAL IMPORT
- from math import floor # floor(x.yz)
- from math import sqrt, ceil # sqrt(x), ceil(y)
- from math import * # sqrt(y)
- # ALIAS IMPORT
- import math as m # m.floor(x.yz)
- # FILES
- # READ MODE
- with open("test.txt", "r") as f:
- # f.read() # returns whole file as one big string
- # f.readlines() # returns a list of strings, line by line
- # f.write() # take one str arg and write into file
- contents = f.readlines()
- # print(contents)
- # for line in contents:
- # line = line.strip()
- # print(line)
- # CSV Module
- # csv.reader()
- import csv
- with open("mock_data.csv", "r") as f1: # mockaroo.com
- contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t")
- # print(contents) # [['id', 'first_name', 'last_name', 'email', 'gender', 'ip_address'], ['1', 'Remington', 'Shilling', 'rshilling0@wsj.com', 'Male', '1.71.141.52']... ]
- # WRITE MODE
- with open("output_data28.csv", "w") as f2:
- # write out a file with every row where the last name starts with J
- for row in contents:
- # last name is at row[2]
- if row[2][0].upper() == "J": # if row[2].startswith("j") or row[2].startswith("J"):
- # we like this data, we want to write it as a string
- f2.write(",".join(row) + "\n")
- # APPEND MODE
- # with open("append_to_this.txt", "r") as f3:
- # print(f3.readlines())
- with open("append_to_this.txt", "a") as f3:
- f3.write("\nPippin")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement