Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2022 Sept 17
- # LABS
- # Ch 2-14... all Labs!
- # Ch 21-33 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
- # int
- # floats
- # bool
- # str # "" maybe the most important?
- # list # [] probably the 2nd most important type
- # dict # {key: value} also important
- # set # {} all unique values, no order -> no index, can't slice
- # tuple # () immutable... Python sees any x,y,z as (x,y,z)... return x,y-> return (x,y)
- # operators
- # = # assignment
- # == # asking are these equal, comparison -> if/elif, while
- # +
- # -
- # *
- # /
- # % # whole number remainder, how many WHOLE THINGS didn't fit?
- # // # floor or integer division, last even division, int(x/y), math.floor(x/y)
- # <
- # >
- # <=
- # >=
- # += # increment
- # -= # decrement
- # !=
- # ** # raise to power... similar math.pow()
- # # keywords
- # in # if _someValue_ in _someContainer_
- # not # flip around a Boolean --> if not _value_ 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... range()
- # for _value_ in _container_:
- # for item in myList:
- # for n in range(0, 4): # [0, 1, 2, 3]
- # for i in range(len(myList)) # myList[i]
- # for key in myDictionary: # myDictionary[key]
- # FUNCTIONS
- # defining/writing vs calling
- # parameters vs "outside" or "regular" variables
- # parameters vs arguments
- # a function has ONE particular job, modular
- # return vs print()... write to a file?
- # methods are functions that belong to a particular class or data type
- # def someFunction(x, y):
- # return x + y
- #
- # if __name__ == "__main__":
- # myInput = int(input())
- # myOtherInput = int(input())
- # num = someFunction(myInput, myOtherInput)
- # 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()
- # sum()
- # min()
- # max()
- # range()
- # list()
- # dict()
- # tuple()
- # set()
- # sorted() # compare to list.sort()
- # reversed() # compare to list.reverse()
- # round() # but it's cousins math.ceil() and math.floor() are in math module
- # open() # IO/filename: .read(), .readlines(), .write()
- #
- # help()
- # dir()
- # # help(str)
- # # print(dir(str))
- # # help(str.isspace)
- #
- # for item in dir(dict):
- # if not item.startswith("__"):
- # print("{}()".format(item))
- # STRINGS
- # be able to slice like it's 2nd nature: myString[start:stop:step]
- myString = "abc"
- # revString = myString[::-1]
- # print(revString)
- # KNOW YOUR WHITESPACE
- # " " # ... and a lot of other Unicode spaces
- # \n
- # \r
- # \t
- # \f
- # STRING METHODS
- # "stuff I want to {}".format(var) # or the similar f string
- # myString.strip() # also .rstrip(), .lstrip()
- # myString.split() # returns a list of strings
- # ",".join(listOfStrings)
- # myString.replace(oldSubStr, newSubStr) # remove... myString.replace(subStr, "")
- # myString.find(subStr) # return index, or -1
- # myString.count(subStr) # return number of occurences
- # case: lower(), upper(), capitalize(), title()
- # is/Boolean: isupper(), islower(), isalpha(), isdigit(), isalnum(), isspace()
- # LISTS
- # again, be able to slice
- # LIST METHODS
- # # +
- # myList.append(item)
- # myList.insert(i, item)
- # myList.extend(anotherList)
- # # -
- # myList.pop(i) # pop by index...
- # myList.remove(item) # remove by value
- # # other
- # myList.count(item)
- # myList.sort()
- # myList.reverse()
- # # not so important
- # myList.copy()
- # myList.index(item)
- # myList.clear()
- # DICT
- # use the key like an index
- # myDict[key] # retrieve the value for that key, so like get()
- # myDict[key] = value # assign (new) value for that key, so like update()
- # myDict.items() # for k, v in myDict.items()... x, y = [2, 8]
- # myDict.keys()
- # myDict.values()
- # SETS
- # mySet.add(item)
- # mySet.remove(item) # by value
- # mySet.pop() # removes something random
- # MODULES
- # math and csv
- # MATH MODULE
- import math
- # math.factorial(x)
- # math.ceil(x.yz)
- # math.floor(x.yz)
- # math.pow(x, y) # similar to **, not to be confused with math.exp()
- # math.sqrt()
- # math.fabs() # abs()
- # math.e
- # math.pi
- # different import types
- # full import: import math
- # partial import: from math import floor, from random import seed, randint, from math import *
- # import as math as m
- # import math
- # math.factorial()
- # from math import factorial
- # --> factorial()
- # from math import *
- # --> factorial()
- # import math as m
- # --> m.factorial()
- # # OPENING FILES
- # good practice in Ch 14 Task 4, 7, 8
- # read!
- with open("test.txt", "r") as f:
- contents = f.readlines() # a list of line by line strings
- print(contents)
- for line in contents:
- line = line.strip()
- print(line)
- # read!
- import csv
- with open("mock_data.csv", "r") as f1:
- contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t")
- print(contents)
- # write!
- with open("output_data4.csv", "w") as f2:
- for line in contents:
- if line[3].endswith(".edu") or line[3].endswith(".org"): # if line[3][-3:] == "edu":
- # the write() method takes a SINGLE string as its arg
- f2.write(",".join(line) + "\n")
- # append!
- with open("append_to_this.txt", "a") as f3:
- f3.write("Pippin\n") # I had 2 line returns in the text file in Notepad !
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement