Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # WEBINAR: for loops --- We're almost there... starting in just a moment
- # we threw in a Two Fer today and looked at FUNCTIONS + METHODS too
- # We use loops to repeat actions
- # a WHILE loop is basically an IF statement
- # that repeats as long as its condition is true
- # FOR LOOPS are used for repeating actions for every element
- # in a container like a list, string, tuple, etc...
- # BASIC FOR LOOP syntax with a list
- myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # X-Files characters
- myTuple = ("Gilligan", "Castaway002", "red", "crew") # things about Gilligan
- # for __ in __:
- for item in myList: # I use "item" as my var name with lists, sets, and tuples
- print(item)
- for item in myTuple:
- print(item)
- myString = "Just sit right back and you'll hear a tale."
- for char in myString:
- print(char, end=" ")
- # dictionary
- # myDCT{
- # key: value,
- # key1: value1
- # }
- scoobiesDCT = {
- "Scooby": "a blue collar",
- "Shaggy": "green",
- "Velma": "orange",
- "Daphne": "purple",
- "Fred": "an ascot"
- }
- # for __ in __someDictionary__: # one var?
- print() # clear out that extra " " from the last loop
- for key in scoobiesDCT:
- # someDictionary[key] --> get the value for that key
- # val = scoobiesDCT[key]
- print("{} always wears {}.".format(key, scoobiesDCT[key]))
- # print("{} always wears {}.".format(key, val))
- for k, v in scoobiesDCT.items():
- print("{} almost always wears {}.".format(k, v))
- # R A N G E
- # range() function creates an iterable sequence from a number or numbers
- for n in range(0, 5):
- print(n)
- # for item in myList
- for i in range(len(myList)):
- print(i, myList[i])
- # see also the enumerate() function
- for i, item in enumerate(myList):
- print("{} --> {}".format(i, item))
- # how many numbers b/n 1 and 100 are div by 25?
- count = 0
- for n in range(1, 101):
- if n % 25 == 0:
- count += 1
- print("How many were divisible?", count)
- # how many numbers b/n 32 and 45687987 are div by 77? # I commented this out b/c it takes a long time to run!
- # same question as above really
- # count = 0
- # for n in range(32, 45687987+1):
- # if n % 77 == 0:
- # count += 1
- # print("How many were divisible?", count)
- # Functions
- # Complete the function to PRINT all of the words in the given string
- def printWords(mystring):
- # x = 5 # don't do this to parameters
- print(mystring.split()) # correct for this question
- # return mystring.split() # would be wrong here, but would allow us to loop over the returned list
- printWords('WGU College of IT')# expected output: ['WGU', 'College', 'of', 'IT']
- printWords('Night Owls Rock') # expected output: ['Night', 'Owls', 'Rock']
- printWords('WKRP in Cincinatti') # ['WKRP', 'in', 'Cincinatti']
- # function definition, Gallant
- def squareThis(num):
- return num * num
- print(squareThis(5)) # 25
- print(squareThis(9)) # 91
- # bad function from Goofus
- def squareSomething(num):
- num = 5 # don't do this!
- return num * num
- print(squareSomething(5))
- print(squareSomething(9))
- print(squareSomething(4))
- print(1 + squareThis(8)) # 65, we can do this because the function RETURNS or EVALUATES TO an integer value
- # METHODS are functions that belong to a class or type, like list.append() or string.replace()
- # You'll spend a lot of the later chapters getting to know methods of the common types
- # I've modified this so we can leave the printWords() function above alone...
- for item in "Come and knock on our door".split(): # the str split() method returns a list, so I can use it like a list
- print(item)
- for item in "WKRP in Cincinatti".split():
- print(item)
- print(dir(dict)) # dir() returns a list...
- for item in dir(list): # ... so I can use it like a list
- if not item.startswith("_"):
- print(item)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement