Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Exam Review, Mar 27, 2021
- # Be able to recognize and use common data types
- # integers
- # floats
- # strings
- # lists
- # dictionary
- # sets
- # tuples
- # # and imported modules
- # math
- # random
- # datetime
- # os
- # pandas - not too much
- # calendar - not too much
- # functions
- # defining vs calling
- # parameters/arguments vs "regular variables"
- # watch the question wording: print() or return
- def addThese(x, y):
- # x = 3 # don't do that
- return x + y
- print(addThese(1, 2)) # 3
- print(addThese(3, 3)) # 6
- # repeat action with a LOOP
- # FOR loop is more useful than a WHILE loop for the OA
- myList = ["Gilligan", "Scooby", "Agent Scully"] # tv characters
- for item in myList:
- #print(item)
- print("The string '{}' has {} characters.".format(item, len(item)))
- # operators
- # +
- # -
- # /
- # *
- # // floor division
- # %
- # # Don't forget MODULO it's more important than you think
- # How many pounds is 41 ounces?
- print("lbs: ", 41 // 16)
- print("oz: ", 41 % 16)
- myString = "Hello. My name is Sue."
- # STRING methods
- # myString.split()
- # myString.join()
- # myString.find()
- # myString.replace()
- # myString.format()
- # myString.strip() # myString.lstrip(), myString.rstrip()
- # myString.count()
- # myString.isupper() # isuower()
- # myString.upper()
- # myString.lower()
- # myString.capitalize()
- # myString.title()
- # help(str)
- # print(dir(str))
- # LIST methods
- # myList.append()
- # myList.insert()
- # myList.pop() # by index
- # myList.remove() # by value
- # myList.count()
- # myList.sort(reverse=False)
- # myList.reverse()
- # myList.index()
- # if someItem in myList: # membership check
- # Complete the function to order the values in the list
- # if ascending is true then order lowest to highest
- # if ascending is false then order highest to lowest
- def sortList(mylist, ascending):
- if ascending:
- mylist.sort()
- else:
- mylist.sort(reverse=True)
- return mylist
- # expected output: [4, 12, 19, 33]
- print(sortList([19,4,33,12], True))
- # expected output: [33, 19, 12, 4]
- print(sortList([19,4,33,12], False))
- # SETS
- # mySet.add()
- # mySet.remove()
- # mySet.discard()
- # BUILT IN functions
- # print()
- # help()
- # dir()
- # len()
- # sum()
- # min()
- # max()
- # round()
- # enumerate()
- # sorted()
- # reversed()
- # int()
- # float()
- # str()
- # list()
- # set()
- # tup()
- # dict()
- # range(start, stop)
- # Know SLICING like it's second hand
- # 3 ways to round
- # round()
- # math.ceil()
- # math.floor()
- myList.append("Mulder")
- print(myList[0:2])
- for item in myList:
- print(item[0:3])
- import math
- # MATH methods
- # math.ceil()
- # math.floor()
- # math.sqrt()
- # math.pow(x, y) # x to the y power, see also **
- # math.e
- # math.exp() is just to raise math.e to a power
- print(3 * math.e)
- # RANDOM methods
- import random
- # random.randint() # i for inclusive of STOP
- # random.randrange() # like everything else is EXCLUSIVE of STOP
- # random.choice(myList)
- # random.random() # returns float between 0 and 1
- # DATETIME
- import datetime
- print(dir(datetime.datetime))
- # datetime.datetime # includes datetime.date and datetime.time
- # datetime.timedelta
- dd = datetime.datetime(2021, 1, 5)
- print(dd)
- dd = datetime.datetime.today()
- print(dd)
- td = datetime.timedelta(days=12)
- print(td.total_seconds())
- print(dir(datetime.timedelta))
- print(td.min)
- myHTMLString = "<p>Martians invade.</p>"
- # change paragraph to a level 1 heading
- myHTMLString = myHTMLString.replace("<p>", "<h1>")
- myHTMLString = myHTMLString.replace("</p>", "</h1>")
- # or to create nested parent and child tags:
- print(myList) # back to that iterable above
- myHTMLString = "<ul>\n"
- for item in myList:
- myHTMLString += "<li>{}</li>\n".format(item)
- myHTMLString += "</ul>"
- print(myHTMLString)
- # Complete the function to return the number of upper case letters in the given string
- def countUpper(mystring):
- count = 0
- for char in mystring:
- if char.isupper():
- count += 1
- return count
- print(countUpper('Welcome to WGU'))# expected output: 4
- print(countUpper('Hello Mary'))# expected output: 2
- # end of chapter exerices for Ch 8, 9, 11 and 12 are critical
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement