Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 2023 Aug 5
- # 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, str, tuples, sets, dictionary, range objects)
- # Basic syntax of a for loop
- # for ___ in _someContainer_:
- # list
- myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]
- for item in myList:
- print(item) # print(item, end="\n")
- # tuple
- myTuple = ("Gilligan", "Castaway002", "red", "crew")
- for item in myTuple:
- print(item)
- # string
- myString = "It was the best of times."
- for char in myString:
- print(char)
- # dictionaries
- # { key: value, key:value}
- # myDict[key] # retrive the value for that key
- # myDict[key] = value # assign a value to that key
- # for key in myDict: # loop var holds the keys, regardless of what I call it
- bestOfXF = {
- "1x00": "Pilot",
- "2x10": "Red Museum",
- "2x14": "Die Hand Die Verletzt",
- "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:
- # # "Check out Episode ___ or '___'"
- # val = bestOfXF[key]
- # print(f"Check out Episode {key} or '{val}'")
- # range()... just to repeat a number of times
- for num in range(0, 5): # range(start, stop, step) --> [0, 1, 2, 3, 4]
- print(num)
- # going over a sequenced container and you need the INDEX
- # 2 choices
- # range() of len()
- # for item in myList:
- for i in range(len(myList)):
- # myList[i]
- print(f"{i} - {myList[i]}")
- # enumerate()
- for i, item in enumerate(myList):
- print(f"{i} --> {item}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement