Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # webinar review 08/21/21
- # Ch 8, 9, 11 and 12 end of chapter exercises are critical practice
- # ... and they're structured like most exam questions! No accident!
- # Be able to recognize and use common data types and modules
- # integers / int
- # float
- # strings / str ""
- # lists []
- # dictionaries / dict {k:v}
- # tuples ()
- # set {}
- # # modules
- # math
- # random
- # datetime
- # os
- # calendar
- # pandas # Ref List
- # OPERATORS
- # +
- # -
- # *
- # /
- # % # mod
- # // # floor division
- # How many pounds and ounces is 27 oz?
- print(27//16) # pounds
- print(27%16) # oz left over
- # ** # raise to power
- # = # assignment
- # == # equality... ASKS is it equal? Used if/elif, while loop conditions
- # += # x += 1 is the same as x = x + 1
- # -=
- # != # not equal, again for comparision, for conditions
- # not
- # <
- # >
- # <=
- # >=
- # in
- # CONTROL FLOW STRUCTURES
- # FUNCTIONS
- # defining vs calling
- # parameter vs arguments # almost mean the same thing
- # def myFunction(someList):
- # someList = [1, 2, 3] # don't do this! Parameters aren't assigned in the function
- # # more code
- # myFunction([4, 5, 6]) # the CALLS provide a value as an ARGUMENT
- # return vs print()
- # methods of a type are functions
- # IF... and IF/ELSE, IF/ELIF/ELSE
- # LOOPS
- # WHILE: an IF that repeats as long the condition stays TRUE
- # FOR: repeat an action once for every ITEM/VALUE in a container
- # for ___ in ___:
- # for item in myList:
- # for char in someString:
- # for key in someDictionary: # value is someDictionary[key]
- # IN for a "membership check", "is it in there?"
- myTuple = ("Gilligan", "Castaway002", "red", "crew") # things about Gilligan
- if "red" in myTuple:
- print("yes, it's there")
- print("Gilligan" in myTuple) # True
- print("Skipper" in myTuple) # False
- scoobiesDCT = {
- "Scooby": "a blue collar",
- "Shaggy": "green",
- "Velma": "orange",
- "Daphne": "purple",
- "Fred": "an ascot"
- }
- print("Scooby" in scoobiesDCT) # True, b/c it looks at keys
- print("orange" in scoobiesDCT) # False, b/c it doesn't look at values
- # BUILT-IN FUNCTIONS
- # print()
- # help() # help(str), help(str.join), help(datetime.datetime.today)
- # dir() # good combo with help()... returns a list of just names of a type's attributes
- # # format()... technically true, but string.format() may be more useful
- # len()
- # sum()
- # min()
- # max()
- # str() # ""
- # list() # []
- # dict() # {}
- # set()
- # int()
- # float()
- # tuple() # ()
- # round() # the "normal" round, its cousins .ceil() and .floor() are in MATH
- # range() # for i in range(len(someList))
- # enumerate()
- # type()
- # STRING
- ## know how to slice a string or list
- # myString = "Hello."
- # myString.split() # creates a list of smaller strings
- # " ".join(someList)
- # myString.format() # the WINNER way to build bigger strings
- # myString.isupper() # is methods return Boolean
- # myString.islower()
- # myString.upper()
- # myString.lower()
- # myString.capitalize()
- # myString.title()
- # myString.find(subString)
- # myString.replace(oldSubString, newSubString) # also good for removal
- # myString.count(subString)
- # myString.index(subString)
- # myString.strip() # has cousins lstrip(), rstrip()
- # Building up larger strings...
- x = "Sue"
- greeting = "How do you do?"
- # CONCATENATE with +
- myString = "My name is " + str(x) + ". " + greeting
- # DATA CONVERSION SPECIFIERS or "string modulo"
- myString = "My name is %s. %s" % (x, greeting)
- # the FORMAT method
- myString = "My name is {}. {}".format(x, greeting)
- print(myString)
- # LISTS
- myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # X-Files characters
- # myList.append(someItem)
- # myList.insert(i, someItem)
- # myList.extend(anotherList)
- # myList.pop()# last one, or by index
- # myList.remove() # by value
- # myList.count(someItem)
- # myList.sort() # myList.sort(reverse=True)
- # myList.reverse()
- # myList.index(someItem)
- # myList.copy()
- # myList.clear()
- # SLICES
- # Be able to slice like it's second nature
- # DICTIONARIES
- myDict = {
- "Scooby": "a blue collar",
- "Shaggy": "green",
- "Velma": "orange",
- "Daphne": "purple",
- "Fred": "an ascot"
- }
- # get the value
- myDict["Scooby"] # "a blue collar"
- myDict.get("Scooby") # "a blue collar"
- myDict["Scooby"] = "a red collar"
- myDict.update({"Scooby": "an orange collar"})
- # for k in myDict: # value is myDict[k]
- # for k, v in myDict.items() # if you really want 2 variables as you loop
- # SETS
- mySet = {1, 2, 3}
- # mySet.add(newValue)
- # mySet.remove(aValue) # by value
- # mySet.discard(aValue) # by value
- # mySet.pop()
- # TUPLES
- # don't worry about tuple methods
- # help(set.pop)
- # myTuple = (1, 2, 3)
- ## MODULES!
- # MATH
- import math
- # math.sqrt()
- # math.pow() # not to be confused with math.exp()
- # math.factorial()
- # math.ceil()
- # math.floor()
- # math.e
- # math.pi
- # RANDOM
- import random
- # random.random() # returns a float b/n 0 and 1
- # random.choice() # random item from list
- # random.randint(start, stop) # randint INCLUDES the stop number
- # random.randrange(start, stop) # randrange EXCLUDES the stop number
- # CALENDAR
- import calendar
- # calendar.weekday()
- # calendar.day_name[]
- # calendar.month_name[]
- # calendar.isleap(year)
- # DATETIME
- import datetime
- datetime.datetime # datetime.date and datetime.time
- datetime.timedelta # represents a period of time
- dd = datetime.datetime(1978, 9, 3)
- dd = datetime.datetime.today()
- print(dd.year)
- print(dd.month)
- print(dd.day)
- print(dd)
- # timedelta
- td = datetime.timedelta(days=3)
- print(dd + td)
- print(td.total_seconds())
- # OS
- import os
- # os.getcwd()
- # os.listdir()
- # os.path.dirname()
- # os.path.basename()
- # os.path.isfile()
- # os.path.isdir()
- # os.path.exists()
- # IMPORT STATEMENTS
- # make sure the way the object is referenced matches the import statement
- # "normal" full import
- import datetime # datetime.date.year
- # PARTIAL IMPORT
- from os import path # path.isdir() not os.path.isdir()
- from math import ceil # ceil() not math.ceil()
- # ALIAS IMPORT
- import math as m # m.ceil() not math.ceil()
- # help(str)
- # print(dir(str))
- # help(str.find)
- # student questions
- # if no output from help on exam, try
- def myQuestionFunction(parameter1, parameter2):
- h = help(list)
- return h # return or print(), whichever questions says, HIT RUN
- # delete or comment out above afterward
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement