Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review 2022 Jan 22
- # LABS
- # Ch 3-13
- # Ch 14-18 not as important, other than some good labs in Ch 15
- # Ch 19-30 just LABS, but important practice!
- # Use Submit Mode!!!
- # Comp 1 Basic syntax and knowledge: operators, data types, etc
- # Comp 2: Control Flow Structures
- # Basic: if statements, loops, functions
- # Adv: try/except and raising errors, classes
- # Comp 3: modules, working with files
- # Comp 1 Basic syntax and knowledge: operators, data types, etc
- # OPERATORS
- # +
- # -
- # /
- # *
- # //
- # %
- # = # assignment
- # == # "equality operator" - asking, condition
- # !=
- # +=
- # -=
- # <
- # >
- # <=
- # >=
- # ** # raise to a power, similar math.pow() or pow()
- # # KEYWORDS used like OPERATORS
- # not
- # in # membership check... if myVar in myList
- # and # all must be True
- # or # any one makes the combo True
- # DATA TYPES
- # int
- # float
- # str
- # Boolean
- # dict
- # list
- # tuple
- # set # no order, all unique values
- # # MODULES
- # math
- # csv # you don't necessarily need it... read() and readlines() often work just as well
- # Comp 2: Control Flow Structures
- # Basic
- # IF statements... if, if/else, if/elif/else
- # LOOPS: WHILE and FOR
- # WHILE is basically an IF that repeats until it's no longer True
- # FOR loops for going over every value in a container
- # for __ in ___:
- # for item in myList:
- # FUNCTIONS
- # defining/writing vs calling
- # parameters/arguments vs "regular" or outside variables
- # x = 5 # don't do this to parameters
- # return vs print()
- # functions are modular - they have ONE job
- # class methods like str.split() or list.append() are functions
- # if name = main
- # define functions
- # if __name__ = "__main__":
- # myVar = input()
- # myOutput = myFunction(myVar)
- # print(myOutput)
- # help() and dir() in Labs and OA!!!
- # print('Enter your program here')
- # help(str)
- # dir() -- Use dir() to zero in
- # print(dir(str))
- # Call help more specifically
- # help(str.rstrip)
- # # myString.rstrip() # rstrip() by default!
- #
- # # BUILT IN FUNCTIONS
- # print()
- # input()
- # help()
- # dir()
- # min()
- # max()
- # len()
- # sum()
- # range()
- # int()
- # float()
- # str() # ""
- # dict() # {}
- # list() # [ ]
- # set()
- # tuple() # ()
- # round() # cousins: math.floor(), math.ceil()
- # reversed() # compare to list.reverse()
- # sorted() # compare to list.sort()
- # STRINGS
- # Build up larger strings with str.format() or f strings
- # CONCATENATION... "this string" + "another string"
- # STRING MODULO... print("%s added in this string" % myString)
- # string.format()
- # "{} is a variable".format(myVar)
- # f string
- # f"{myVar} is a variable"
- # SLICES for strings and list
- # myString[start:stop] # myString[start:stop:step]
- # myString[::-1]
- # STRING METHODS
- # myString.format()
- # myString.split() # returns a list of smaller strings
- # " ".join(myList)
- # myString.replace(someSubStr, newSubStr)
- # myString.upper() # and other case methods
- # myString.isupper() # and other Boolean methods
- # myString.strip() # cousins: myString.lstrip() and myString.rstrip()
- # myString.count(subStr)
- # LIST METHODS
- # myList.append(item)
- # myList.insert(index, item)
- # myList.extend(anotherList)
- # myList.pop() # optional arg: index
- # myList.remove() # remove by value
- # myList.count(item)
- # myList.sort() # myList.sort(reverse=True)
- # myList.reverse() # compare to sorted() and reversed()
- # DICT
- # myDictionary[key] # retrieves the value
- # myDictionary[key] = value # assign a value for the key
- # if __ in myDictionary: # checking the KEYS, NOT the values
- # if __ in myDictionary.keys(): # same as above
- # SETS
- # mySet.add()
- # mySet.pop() # random!
- # mySet.remove() # remove by value
- # MATH
- # import math
- # math.pow(x, y) # do not confuse with math.exp()
- # math.e # logarithmic constant
- # math.pi
- # math.sqrt()
- # math.floor()
- # math.ceil()
- # math.factorial()
- # What's in the data window for input()?
- # a string
- # another string
- # another string
- # TEST IT OUT
- # print(input())
- # Ask you to grab inputs until you see -1
- # myVar = input()
- # while myVar != "-1":
- # # do stuff
- # myVar = input()
- # Comp 3: modules, working with files
- # full, normal import
- # import wholeModule # import math, import csv... math.floor()
- # PARTIAL IMPORT
- #from module import thisMethod... from math import floor... floor()
- # # ALIASED import
- # import wholeModule as mm
- # mm.thisMethod()
- # OPENING FILES
- # good practice: Ch 12 Task 4, 7, 8
- # f = open(filename, "r") # "w" for write, "a" for append
- # contents = f.read() # get the whole file one big string
- # or
- # contents = f.readlines() # list of strings, line by line
- # f.close()
- # with open(filename, "r") as f:
- # contents = f.readlines()
- #
- # # write back out
- # with open(filename, "w") as f:
- # f.write("my string")
- # Fibonacci
- # 0 1 1 2 3 5 8 13 21 34
- def fibonacci(n):
- # return that index of the sequence
- if n < 0:
- return -1
- elif n <= 1:
- return n
- else:
- fibList = [0, 1]
- while len(fibList) < n + 1:
- fibList.append(fibList[-1] + fibList[-2])
- return fibList[n]
- print(fibonacci(9)) # 34
- print(fibonacci(8)) # 21
- print(fibonacci(3)) # 2
- # Another cause of bad string output:
- print(myVar, end=" ") # don't leave the cursor hanging
- print() # give a blank print() so you get a clean line
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement