Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # exam review March 20 2021
- # pay special attention to the end of chapter exercises
- # Ch 8, 9, 11, 12
- # be able to recognize and use common data types
- # integers
- # floats
- # Boolean
- # strings
- # lists
- # tuples
- # sets
- # dictionaries
- # # our modules
- # math
- # random
- # datetime --> datetime.datetime and datetime.timedelta
- # os
- # calendar
- # pandas
- # functions
- # defining and calling
- # parameters vs arguments
- # parameters are not like "regular" variables
- # return vs print()
- # defining the function
- def addThese(x, y):
- # x = 3 # don't do this
- # y = 4 # don't do this
- return x + y
- # calls to function
- print(addThese(1, 2)) # 3 --> to see it, we'll need to print() the call itself
- print(addThese(3, 4)) # 7
- myVar = addThese(4,5) # because this function returns, it's the same as...
- # myVar = 9
- print(addThese(4, 5))
- # LOOPS repeat actions
- # FOR LOOPS repeat for everything in a container
- # WHILE LOOPS are like IF statements that keep repeating as long as TRUE
- # --> FOR <-- loops are probably more useful on the exam
- myList = ["Gilligan", "Scooby", "Agent Scully"]
- for item in myList:
- print(item)
- scoobies = {
- "Scooby": "a blue collar",
- "Shaggy": "green",
- "Velma": "orange",
- "Daphne": "purple",
- "Fred": "an ascot"
- }
- for key in scoobies:
- # nameOfDictionary[key] --> retrieve or set the VALUE for that key
- # Scooby wears a blue collar.
- # value = scoobies[key]
- print("{} wears {}.".format(key, scoobies[key]))
- # for key, value in scoobies.items():
- # print("{} always wears {}.".format(key, value))
- # IF
- # if some condition:
- # if/elif/else
- # if/elif/elif/else
- # "MEMBERSHIP CHECK"
- myList = ["Gilligan", "Scooby", "Agent Scully"]
- if "Mulder" in myList:
- print("Found Mulder in the list.")
- else:
- print("No Mulder there.")
- # Operators
- # +
- # -
- # *
- # /
- # # don't forget
- # // # floor division
- # % # MODULO is more important than you think: what WHOLE NUMBER is left over?
- # print(8/3) # 2.66666666
- # print(8//3) # 2
- # print(8 % 3) # 2
- # print(25%4) # 1
- largeOz = 38
- print("lbs:", largeOz // 16)
- print("and oz left over:", largeOz % 16)
- # other operators
- # = # assignment
- # <
- # >
- # <=
- # >=
- # == # single = is assignment, double == is asking "are they equal?"
- # !=
- # += # x += 1 is the same as x = x + 1
- # -=
- # not
- x = 234.134646246224
- # myStr = "I am specifying x to 2 decimals: %.2f" % (x)
- # print(myStr)
- myStr = "I am specifying x to 2 decimals: {:.2f}".format(x)
- print(myStr)
- # 3 ways to round
- # round() # built-in functions
- # math.ceil()
- # math.floor()
- # SLICING
- # Be able to slice strings and lists like it's second nature
- # STRING methods
- # myString = "Hello"
- # myString.join() # feed it a list and get a longer string back: ",".join(someListofStrings)
- # myString.split() # takes your string and creates a list of substrings
- # myString.count() # counts a particular value
- # myString.replace() # 2 args: substring to look for, substring to replace it with
- # myString.find() # returns first index where substring starts
- # myString.strip() # has left and right variants: lstrip(), rstrip()
- # myString.lower()
- # myString.upper()
- # myString.title()
- # myString.capitalize()
- # myString.isLower()
- # myString.isUpper()
- # myString.format()
- # LIST methods
- # myList.append()
- # myList.extend()
- # myList.insert()
- # myList.pop() # by index
- # myList.remove() # by value
- # myList.count()
- # myList.index() # if you know value but not its index: myList.index("Gilligan") --> 0
- # myList.sort(reverse=False)
- # myList.reverse()
- # myList = ["Gilligan", "Scooby", "Agent Scully"]
- # # myList.pop(0)
- # myList.remove("Gilligan")
- # print(myList)
- # help(str)
- # print(dir(str))
- # DATETIME module
- # full module import:
- import datetime
- # partial import:
- from datetime import timedelta
- # from -module- import -specific thing-
- # import with alias:
- # import datetime as dat
- # each way of importing changes how you refer to the thing later
- print(dir(datetime.datetime))
- dt = datetime.datetime(2020, 12, 31)
- print(dt)
- dt = datetime.datetime.today()
- print(dt)
- dt = datetime.datetime.now()
- print(dt)
- # strftime see strftime.org
- print(dt.strftime("%A"))
- print(dt.month)
- td = datetime.timedelta(days=10)
- print("The delta is:", td)
- td.total_seconds() # a method of datetime.timedelta
- print(dt + td) # adding a datetime.datetime to a datetime.timedelta
- # RANDOM module
- import random
- # random.randint() # includes stop number, which is unusual in Python
- # random.randrange() # excludes stop number, otherwise same as randint()
- # random.random() # # returns a float b/n 0 and 1
- # random.choice() # picks random item from list or other iterable
- # MATH module
- # math.floor()
- # math.ceil()
- # math.sqrt()
- # math.pow() # not to be confused with math.exp()
- # math.e
- # BUILT-IN FUNCTIONS
- # print()
- # list()
- # set()
- # dict()
- # tuples()
- # help()
- # dir()
- # len()
- # sum()
- # min()
- # max()
- # enumerate() # see FOR LOOPS webinar for more on enumerate() and range()
- # range()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement