# 2022 Aug 13 # WEBINAR: For Loops # We use loops to repeat actions # a WHILE loop... btw.. 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 syntax of a for loop # for ___ in __someContainer__: myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] for item in myList: print(item) myTuple = ("Gilligan", "Castaway002", "red", "crew") for item in myTuple: print(item) myString = "It was the best of times. It was the worst of times." # for char in myString: # print(char) for char in myString: print(char, end="") # default end "\n" print() # ... just to get the default "\n" print("something else I needed to print") # help(str) # print(dir(str)) # check for whitespace for char in myString: if not char.isspace(): print(char, end="") print() # for loop over a dict bestOfXF = { "1x00": "Pilot", "2x10": "Red Museum", "3x04": "Clyde Bruckman's Final Repose", "3x12": "War of the Coprophages", "3x20": "Jose Chung's From Outer Space", "4x05": "The Field Where I Died", "5x05": "The Post Modern Prometheus", "5x17": "All Souls" } for key in bestOfXF: # loop var holds KEY value = bestOfXF[key] print(f"Check out Episode {key} or '{value}'!") # value is nameOfDict[key] for k, v in bestOfXF.items(): print(f"Check out Episode {k}, which is called '{v}'!") # value is nameOfDict[key] # the RANGE object and just doing things some # of times for n in range(0, 7): # [0, 1, 2, 3, 4, 5, 6] print(n) # What I'm interested in INDEX of the VALUES in a list, etc? for i in range(len(myList)): print(f"{i} --> {myList[i]}") # for i in range(len(myList)): # print(f"{myList[i]} fought {myList[i+1]}!") # OUT OF RANGE the last time! # so... when comparing THIS position i and NEXT position i, adjust range by 1... for i in range(len(myList)-1): print(f"{myList[i]} fought {myList[i+1]}!")