Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # WEBINAR: For Loops 2021 Aug 7
- # 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...
- # Focus on FOR LOOPS for the OA (don't worry about while loops so much)
- # Don't worry about nested loops either :), just one loop at a time
- # BASIC LOOP syntax with a list
- # for __ in __:
- # for _myVar_ in _someContainer_:
- myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # X-Files characters
- for item in myList:
- print(item)
- myString = "Just sit right back and you'll hear a tale."
- # for char in myString:
- # print(char, end=" ")
- # print("Be careful on the next call.")
- # tuple
- myTuple = ("Gilligan", "Castaway002", "red", "crew") # things about Gilligan
- for item in myTuple:
- print(item)
- # dictionaries
- scoobiesDCT = {
- "Scooby": "a blue collar",
- "Shaggy": "green",
- "Velma": "orange",
- "Daphne": "purple",
- "Fred": "an ascot"
- }
- for key in scoobiesDCT:
- # nameOfDictionary[key] = value
- print(key, scoobiesDCT[key])
- for k, v in scoobiesDCT.items():
- # print(k + " always wears " + v + ".") # string CONCATENATION
- # print("%s always wears %s" % (k, v)) # data conversion specifiers/string modulo
- print("{} always wears {}.".format(k, v)) # string class .format() method - BEST!
- for n in range(0, 5):
- print(n)
- for i in range(len(myList)):
- item = myList[i]
- # print(i, myList[i])
- print(i, item)
- for i, item in enumerate(myList):
- print(i, item)
- # break - ends the loop entirely
- # continue - end the CURRENT ITERATION of the loop
- # for n in range(0, 5):
- # if n == 3:
- # # break # 0 1 2 and we're done
- # continue # 0 1 2 4
- # print(n)
- myString = "Just sit right back and you'll hear a tale."
- # IN keyword
- if "back" in myString:
- print("It's there!")
- print(myString.find("back")) # 15
- print(myString.count("back")) # 1
- # for item in myList:
- # Ch 8 Task 10
- # Complete the function to return the number of upper case letters in the given string
- def countUpper(mystring):
- # string method isupper()
- 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
- # Question:
- # How to look up class/type methods on the exam...
- # help(str)# outputs the entire help document on something
- # help(list) # etc
- # print(dir(str)) # dir() returns a list of just attribute (method and property) names
- # exam has a Ref List for imported modules like math, random, os, datetime
- # Since dir() returns a list, you can loop over it!
- for item in dir(str):
- if not item.startswith("_"): # ignore the __methods()__ for this course
- print(item)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement