Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # webinar review 08/28/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
- # floats
- # strings... str
- # lists
- # dictionaries... dict
- # sets
- # tuples
- # ## modules
- # math
- # random
- # datetime
- # calendar
- # pandas # Ref List
- # OPERATORS
- # +
- # -
- # *
- # /
- # **
- # % # whole number remainder... or "what didn't fit?"
- # //
- # How many pounds and ounces is 93 ounces?
- print(93//16) # pounds
- print(93%16) # oz left over
- # = # assignment
- # == # "equality operator" is asking a question
- # += # x += 1 is the same x = x + 1
- # -=
- # !=
- # <
- # >
- # <=
- # >=
- # not
- # in
- # and
- # or
- # CONTROL FLOW STRUCTURES
- # FUNCTIONS
- # defining vs calling
- # parameters/arguments vs "regular" variables
- # def myFunction(someList):
- # someList = [1, 2, 3] # don't do this!
- # myFunction([4, 5, 6]) # the call provides an ARG to give the parameter a value
- # myFunction([9, 7, 8])
- # return vs print()
- # methods are themselves functions
- # IF... and IF/ELSE, IF/ELIF/ELSE
- # LOOPS
- # WHILE - an IF that repeats
- # FOR - looping a number of times, matched to a container
- # for ___ in ___:
- # for item in myList:
- # for char in myString:
- # for key in aDictionary: # aDictionary[key] gets the value
- # value = aDictionary[key]
- # for key, value in aDictionary.items():
- for i in range(0, 5):
- print(i)
- myTuple = ("Gilligan", "Castaway002", "red", "crew") # things about Gilligan
- for item in myTuple:
- print(item)
- for i in range(len(myTuple)):
- print(i, myTuple[i])
- for i, item in enumerate(myTuple):
- print(i, item)
- # IN
- if "Gilligan" in myTuple: # x > 5
- print("Found him!")
- print("Gilligan" in myTuple) # True
- print("Skipper" in myTuple) # False
- # BUILT-IN FUNCTIONS
- # print()
- # help()# help(str)
- # dir() print(dir(str))
- # input()
- # len()
- # sum()
- # min()
- # max()
- # str()
- # list()
- # dict()
- # int()
- # float()
- # tuple()
- # range()
- # type() print(type(myTuple))
- # enumerate()
- # round() # its cousins are in math: math.floor() and math.ceil()
- # sorted()
- # reversed()
- # STRING
- ## know how to slice strings and lists
- myString = "Hello."
- # myString.split() # returns a list (of smaller strings) from this string
- # " ".join(someList)
- # myString.format() # more on that later
- # myString.find()
- # myString.replace(oldSubStr, newSubStr) # also to remove
- # myString.isupper()
- # myString.islower()
- # myString.istitle()
- # myString.isalpha()
- # myString.upper()
- # myString.lower()
- # myString.title()
- # myString.capitalize()
- # 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)
- y = 3.14159
- print("Pi to 2 decimals is {:.2f}".format(y))
- print(myString)
- # SLICES
- # Be able to slice like it's second nature
- # LISTS
- myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # X-Files characters
- # myList.append(newValue)
- # myList.insert(i, newValue)
- # myList.extend(anotherList)
- # myList.remove(aValue)
- # myList.pop() # also by index myList.pop(i)
- # myList.count(someValue)
- # myList.sort() # myList.sort(reverse=False)
- # myList.reverse()
- # myList.index()
- # myList.copy()
- # myList.clear()
- # TUPLES
- # so immutable we don't think about tuple methods
- # 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} # all unique values, no order
- # mySet.add(newValue)
- # mySet.remove(aValue)
- # mySet.discard(aValue)
- # mySet.pop() # random
- ## MODULES
- # MATH
- import math
- # math.sqrt()
- # math.factorial()
- # math.pow() # ** # not to be confused with math.exp()
- # math.floor()
- # math.ceil()
- # math.e
- # math.pi
- # RANDOM
- import random
- # random.random() # random float b/n 0 and 1
- # random.choice() # random item in a list
- # random.randint(start, stop) # INCLUDES the stop number
- # random.randrange(start,stop) # EXCLUDES the stop number
- # CALENDAR
- # import calendar
- # calendar.isleap()
- # calendar.weekday(y, m, d)
- # calendar.day_name[]
- # calendar.month_name[]
- # DATETIME
- import datetime
- print(dir(datetime))
- # focus on
- # datetime.datetime # <-- contains datetime.date and datetime.time
- # datetime.timedelta
- dd = datetime.datetime(1971, 4, 3)
- dd = datetime.datetime.today()
- print(dd)
- print(dd.year)
- print(dd.month)
- print(dd.day)
- print(dd.hour)
- # print(dir(datetime.datetime))
- # datetime.timedelta
- td = datetime.timedelta(days=10)
- print("10 days from today is {}.".format(dd + td))
- print(dir(datetime.timedelta))
- print(td.total_seconds())
- # OS
- import os
- # os.getcwd()
- # os.listdir()
- # os.path.dirname()
- # os.path.basename()
- # os.path.exists()
- # os.path.isfile()
- # os.path.isdir()
- # 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()
- def myQuestionFunction(parameter1, parameter2):
- h = help(list)
- return h # return or print(), whichever questions says, HIT RUN
- # delete or comment out above afterward
- # HTML is just strings!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement