The Gotchas: For Loops
unknown
python
5 months ago
3.1 kB
33
Indexable
# WEBINAR: For Loops # We use loops to repeat actions # a WHILE loop... is an IF that repeats as long as the loop condition remains True # # FOR LOOPS are used for repeating actions for every element in a container (list, dict, tuple, str, range, etc) # Basic syntax of a for loop # for ___ in _someContainer_: # For Loop variables (the word after the for keyword) are SPECIAL VARIABLES that come along with this loop # # LIST myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"] # for item in myList: # print(item) # # TUPLE # myTuple = ("Gilligan", "Castaway002", "red", "crew") # for item in myTuple: # print(item) # # # STRING # myStr = "It was the best of times." # for char in myStr: # print(char) # LOOP A NUMBER OF TIMES # RANGE OBJECT... range() # range(start=0, stop, step=1) # range(0, 5, 1) --> range(5) --> [0, 1, 2, 3, 4] # not a list, but we'll pretend it looks like this # for n in range(5): # print(n) # Looping over an indexed container, and I need to know the INDEX # 2 choices... range() of len(), enumerate() # RANGE OF LEN # for i in range(len(myList)): # # print(i) # # print(myList[i]) # print(f"{i}: {myList[i]}") # # # ENUMERATE # for i, item in enumerate(myList): # print(f"{i}--> {item}") # DICT bestOfXF = { "1x00": "Pilot", "2x10": "Red Museum", "2x14": "Die Hand Die Verletzt", "2x20": "Humbug", "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 ___ in thisDict: # for key in bestOfXF: # # print(key) # # print(bestOfXF[key]) # # "Check out Episode ___ or '---'." # print(f"Check out Episode {key} or '{bestOfXF[key]}'.") # EXTRA... this is a general pattern you'll use a lot with all types of containers # FILL THE BASKET / FILTER # agentList = [] # start a new container # for item in myList: # # filter with an if # if "Agent" in item: # agentList.append(item) # print(agentList) # STUDENT QUESTIONS # ... we're leaving the general topic of for loops now. Lots of questions on dictionaries today. # If I know I want a certain key and its value, I don't need to loop: print(bestOfXF["3x12"]) # You can loop to find a key, and then its value, but you don't have to. # But you can get the value for a key directly if you know its there, no loop needed. for k in bestOfXF: if k == "3x12": print(k, bestOfXF[k]) if k in bestOfXF: # check to see if key is there print(bestOfXF["3x12"]) # looking for a value, grabbing key when you find it: for key in bestOfXF: # check values if bestOfXF[key] == "Jose Chung's From Outer Space": # found it! rememberThis = key print(f"I found the episode code! It's {rememberThis}") # in the above situation, checking based on value instead of key, you DO have to loop. # Remember values are not necessarily unique though. They can repeat for many keys. Keys on the other hand, can only occur once.
Editor is loading...