Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Webinar: Exam Review 7/31/21
- # Ch 8, 9, 11 and 12 end of chapter exercises are critical.
- # And structured a lot like the Pre and OA questions
- # Be able to recognize your common data types and modules
- # int
- # float
- # str
- # list
- # dictionaries
- # tuples
- # sets
- ## modules # --> Ref List
- # math
- # random
- # datetime
- # os
- # calendar
- # pandas
- # Built-In Functions
- # operators
- # +
- # -
- # *
- # /
- # % # whole number remainder, "what's left over" or "how many didn't fit?"
- # // # floor division
- # how many pounds and ounces is 53 ounces?
- print(53 // 16) # pounds, and...
- print(53 % 16) # ounces left over
- # **
- # = # assignment
- # == # comparison, asking if they're equal: if/elif, while
- # += # incrementing, x += 1 is the same x = x + 1
- # -=
- # !=
- # <
- # >
- # <=
- # >=
- # not
- # and
- # or
- # in
- # CONTROL STRUCTURES
- # FUNCTIONS
- # defining vs calling
- # parameters vs arguments (vs "regular variables")
- # return vs print()
- # methods of a type are themselves functions
- # IF and other conditional statements: IF, IF/ELSE, IF/ELIF/ELSE
- # LOOPS
- # WHILE LOOPS - an if that repeats as long as the condition stays true
- # FOR LOOPS- tied to a container, do something once for everything in that list, etc
- # for _someVar_ in _someContainer_:
- # for item in myList:
- # for char in myString:
- # for key in myDictionary: # myDictionary[key]
- # for n in range(0, 5):
- # for i in range(0, len(myList)):
- # IN... the "membership check"
- myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # X-Files characters
- print("Agent Scully" in myList)
- # print("Krychek" in myList)
- # if "Agent Mulder" in myList:
- # print("Mulder is in the list.")
- # SLICING
- print(myList[0:2])
- print(myList[-2:])
- # STRINGS
- # Building up longer strings
- x = "Sue"
- greeting = "How do you do?"
- # CONCATENATION - easy to do, but easy to mess up
- myString = "My name is " + str(x) + ". " + greeting
- # DATA CONVERSION SPEC or STRING MODULO
- myString = "My name is %s. %s" % (x, greeting)
- # STRING CLASS FORMAT() METHOD ---> THE BESTEST WAY
- myString = "My name is {}. {}".format(x, greeting)
- print(myString)
- # F strings do not work on the exam
- # decimal precision with string .format()
- import math
- print("Pi to 5 places is {:.5f}".format(math.pi))
- # STRING METHODS # --> use help(str)
- # help(str)
- print(dir(str))
- help(str.rstrip)
- # def myFunction(x,y):
- # h = help(list)
- # return h
- # OK... so what are the big string methods
- # myString.split()
- # ", ".join(someList)
- # myString.format()
- # myString.find()
- # myString.replace() # also used remove
- # myString.lower()
- # myString.upper()
- # myString.title()
- # myString.capitalize()
- # myString.isupper() # many isXYZ() methods that return Boolean
- # myString.startswith()
- # myString.count()
- # myString.strip() # don't forget its cousins: lstrip(), rstrip()
- # LIST methods
- # print(dir(list))
- # myList.append()
- # myList.insert()
- # myList.extend()
- # myList.pop() # last or index number
- # myList.remove() # by value
- # myList.sort() # myList(reverse=True)
- # myList.reverse()
- # myList.count()
- # myList.index()
- # myString.copy()
- # myString.clear()
- myOtherList = ["Cunningham", "Fonzie", "Chaci", "Arnold"]
- # myList.append(myOtherList)
- # myList.extend(myOtherList)
- # help(list.extend)
- print(myList)
- # SETS {}
- # mySet.add()
- # mySet.update()
- # mySet.pop() # but random!
- # TUPLES ()
- # are so immutable they hardly have methods
- # DICTIONARIES {k:v}
- # someDictionary[key] # retrieves value... so same as someDictionary.get(key)
- # someDictionary[key] = someNewValue # similar to dictionary update()
- # someDictionary.update({aKey:value})
- scoobies = {
- "Scooby": "a blue collar",
- "Shaggy": "green",
- "Velma": "orange",
- "Daphne": "purple",
- "Fred": "an ascot"
- }
- scoobies["Scooby Dumb"] = "a red collar"
- scoobies.update({"Scrappy Doo": "a blue collar"})
- for key in scoobies:
- print('{} always wears {}.'.format(key, scoobies[key]))
- # MODULES
- # MATH
- # import math
- # math.pow() # **, not to be confused with math.exp()
- # math.e # Euler's number
- # math.pi # 3.14159...
- # math.sqrt()
- # math.factorial()
- # math.ceil() # always rounds up
- # math.floor() # always round down
- # RANDOM
- # import random
- # random.random() # returns a float b/n 0 and 1
- # random.choice() # random item from a list
- # random.randrange() # EXCLUDES the stop
- # random.randint() # INCLUDES the stop
- # CALENDAR
- # import calendar
- # calendar.isleap()
- # calendar.day_name
- # calendar.month_name
- # calendar.weekday(y, m, d)
- # DATETIME
- import datetime
- # datetime.datetime # representing a point in time, combo of datetime.date + datetime.time
- dd = datetime.datetime(2021, 7, 31)
- dd = datetime.datetime.today()
- print(dd)
- print(dd.year)
- print(dd.month)
- print(dd.day)
- print(dd.strftime("%b")) # www.strftime.org
- # datetime.timedelta # a length or delta of time
- td = datetime.timedelta(weeks=2, days=1, hours=3)
- print(td.total_seconds())
- print(dd - td)
- # OS
- import os # full import
- # os.getcwd()
- # os.listdir()
- # os.path.basename()
- # os.path.dirname()
- # os.path.isfile()
- # os.path.isdir()
- # IMPORT STATEMENTS
- import os # full import
- from datetime import timedelta # partial import
- #... so now timedelta(days=9) NOT datetime.timedelta(days=9)
- from math import ceil
- #... so ceil(x) instead of math.ceil(x)
- import datetime as d # alias import
- #... so now d.timedelta(days=3) or d.datetime.today()
- # BUILT-IN FUNCTIONS
- # print()
- # help()
- # dir()
- # range()
- # len()
- # sum()
- # max()
- # min()
- # round() # regular round() is not in math
- # int()
- # float()
- # list()
- # set()
- # dict()
- # tuple()
- # str()
- # enumerate()
- # input() # get terminal input from user
- # type()
- print(type(myList))
- # HTML is just strings
- # def myFunction(someDictionary):
- # do something with a dictionary
- # myFunction({"Toyota": "Camry", "Chevy": "Silverado"})
- # myFunction(scoobies)
- # merging 2 lists into a dictionary as keys and values
- myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # X-Files characters
- actorList = ["Gillian Anderson", "David Duchovny", "Mitch Pileggi", "Bob", "Joe"]
- # My apologies to William B Davis and Steven Williams, who played CSM and Mr. X respectively on The X-Files.
- actorList[-1] = "Steven Williams"
- actorList[-2] = "William B Davis"
- def makeDict(someList, anotherList):
- newDict = {}
- #for item in myList:
- for i in range(len(someList)): # <-- I forgot to change this in the webinar
- # newDict[actorList[i]] = myList[i]
- newDict[someList[i]] = anotherList[i]
- return newDict
- print(makeDict(actorList, myList))
- print(makeDict(["a", "b", "c"], ["Ernie", "Bert", "Cookie Monster"]))
Add Comment
Please, Sign In to add comment