Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Webinar: Exam Review
- # 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
- # integers
- # floats
- # strings # str
- # lists
- # dictionaries
- # tuples
- # sets
- # # modules
- # math
- # random
- # datetime
- # os
- # calendar
- # pandas
- # operators
- # +
- # -
- # *
- # /
- # //
- # % # modulo asks "what's the whole number remainder?" or "how many didn't fit?"
- print(51 // 16, "pounds and")
- print(51 % 16, "ounces")
- # **
- # = # assignment operator, setting something to equal
- # == # equality operator, asking if they're equal, as a condition for if/elif, or a while loop
- # !=
- # <
- # >
- # <=
- # >=
- # not
- # is
- # and
- # or
- # += # x += 1 is the same as x = x + 1
- # -=
- # FUNCTIONS
- # defining vs calling
- # parameters vs arguments
- # return vs print()
- # methods of a data type are themselves functions
- # IF and IF/ELSE and IF/ELIF/ELSE
- # LOOPS
- # WHILE LOOP - like an IF that repeats if its condition remains True
- # FOR LOOP - tied to a container, like list, string, etc
- # for __ in _iterable_:
- # for item in myList:
- # for char in myString:
- # for key in myDictionary: # myDictionary[key]
- # for i in range(0, 7):
- # for i in range(len(myList)):
- # "membership check"
- myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # X-Files characters
- print("Agent Scully" in myList) # True
- print("Scooby" in myList) # False
- # Adebayo asks how to get the longest string in a list
- # print(max(myList))
- # help(max)
- # for item in myList:
- # longest = ""
- # if len(item) > len(longest):
- # longest = item
- # print("The longest item is {}, with a length of {}.".format(longest, len(longest)))
- # Be able to SLICE
- print(myList[0:3])
- print(myList[-2:])
- # Building up 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 in exam environment
- # learn those METHODS of each type
- # STRING
- # myString.join() # starts with separator string ", ".join(someIterable)
- # myString.split() # turns a string into a list, splitting on a separator string
- # myString.count() # looks for occurence of some substring
- # myString.strip() # has cousins: lstrip(), rstrip()
- # myString.replace() # replace a substring with a new substring
- # myString.format()
- # myString.isupper() # plus other is---() methods
- # myString.lower() # return a copy forced lowercase
- # myString.upper()
- # myString.title()
- # myString.capitalize()
- # myString.find() # finds a substring and returns the index, or -1 on failure
- # myString.startswith()
- #
- # # LISTS []
- # myList.append()
- # myList.pop()
- # myList.remove()
- # myList.insert()
- # myList.sort() # has a keyword arg of reverse: myList.sort(reverse=True)
- # myList.extend() # add another list, item by item
- # myList.reverse()
- # myList.count() # count occurrences of an item/value in the list
- # myList.index() # myList.index(item)
- # myList.clear()
- # myList.copy()
- for item in myList:
- print(item)
- print(myList[-1])
- # for item in myList:
- for i in range(len(myList)): # for i in enumerate(myList):
- if i == len(myList) - 1:
- print("{} is the last person in the list.".format(myList[i]))
- else:
- print(myList[i])
- # DICTIONARIES {k:v}
- # someDictionary[key] --> retrieves value for that key
- # someDictionary[key] = new value
- scoobies = {
- "Scooby": "a blue collar",
- "Shaggy": "green",
- "Velma": "orange",
- "Daphne": "purple",
- "Fred": "an ascot"
- }
- scoobies["Scooby Dumb"] = "a red collar"
- for key in scoobies:
- print(key, "=", scoobies[key])
- # SETS {}
- # mySet.add()
- # mySet.update()
- # mySet.pop()
- # mySet.remove()
- # TUPLES ()
- # are immutable
- # MODULES
- # 3 types of import statement
- import math # import the whole thing, math.pow()
- # PARTIAL IMPORT
- # from math import pow # now I write pow()
- # ALIAS IMPORT
- # import math as m # now it's m.pow(), etc
- # MATH
- # math.pow() # raise a number to a power, not to be confused with math.exp()
- # math.sqrt()
- # math.e
- # math.pi
- # math.floor() # always rounds down
- # math.ceil() # always rounds up
- # RANDOM
- # random.random() # gets a float 0-1
- # random.choice() # gets a random value from a list
- # random.randInt() # INCLUSIVE of the stop number
- # random.randRange() # EXCLUSIVE of the stop number
- # DATETIME
- # datetime.datetime # copy of datetime.date and datetime.time
- # datetime.timedelta # a duration of time
- import datetime
- td = datetime.timedelta(weeks=4)
- td.total_seconds()
- dd = datetime.datetime(2021, 5, 22)
- dd = datetime.datetime.today()
- print(dd)
- print(dd.year)
- print(dd.hour)
- # OS
- # import os
- # os.getcwd()
- # os.listdir()
- # os.path.basename()
- # os.path.dirname()
- # os.path.isfile()
- # os.path.isdir()
- # BUILT IN FUNCTIONS
- # print()
- # dir()
- # help()
- # type()
- # enumerate()
- # input()
- # str()
- # list()
- # tuple()
- # dict()
- # int()
- # float()
- # range()
- # round()
- # len()
- # max()
- # min()
- # sum()
- # HTML is just strings
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement