Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # WEBINAR: for loops
- # 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 LOOP syntax with a list
- myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # X-Files characters
- for item in myList:
- print(item)
- myTuple = ("Gilligan", "Castaway002", "red", "crew") # things about Gilligan
- for item in myTuple:
- print(item)
- myString = "Just sit right back and you'll hear a tale."
- # for char in myString:
- # print(char)
- scoobies = {
- "Scooby": "a blue collar",
- "Shaggy": "green",
- "Velma": "orange",
- "Daphne": "purple",
- "Fred": "an ascot"
- }
- for key in scoobies: # loop var for a dictionary is the KEY
- # someDictionary[someKey] --> retrieve the value for the key
- print(key, "-", scoobies[key])
- # if you REALLY want a var for key and value
- for k, v in scoobies.items():
- print(k, "-->", v)
- print(k, "------>", scoobies[k])
- # if you're as interested in the INDEX position as you the ITEM/VALUE
- for i, item in enumerate(myList):
- # print(i, item)
- print(i, myList[i])
- for n in range(0, 5):
- print(n)
- for i in range(len(myString)):
- print(i, myString[i])
- # Complete the function to return the number of upper case letters in the given string
- def countUpper(mystring):
- # string class method: .isupper()
- count = 0
- for char in mystring:
- if char.isupper():
- count += 1
- return count
- print(countUpper('Welcome to WGU')) # 4
- print(countUpper('Hello Mary')) # 2
- print("--- 25's in 1 to 100:")
- count = 0 # if you're "accumulating" or "filling the basket" create those vars BEFORE the loop...
- numList = []
- for num in range(1, 101):
- if num % 25 == 0:
- print(num)
- numList.append(num) # ... then add to them within the loop...
- count += 1
- print(count) # and remember they're not done until the loop is over (back one level of indentation)
- print(numList)
- print("--- 37's in 1 to 10000:")
- for num in range(1, 10001):
- if num % 43 == 0:
- print(num)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement