Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 2023 Feb 4
- # 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 __someVar__ in __someContainer__:
- # list
- myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]
- for item in myList:
- print(item) # print(item, end="\n")
- # tuples
- myTuple = ("Gilligan", "Castaway002", "red", "crew")
- for item in myTuple:
- print(item)
- # strings
- myString = "It was the best of times."
- for char in myString:
- print(char)
- # printing on the same line... overriding the end parameter of print()
- for char in myString:
- print(char, end="-")
- print() # get the new line back
- # print("I want this on a new line.") # good way to check yourself
- # dictionaries
- # myDict = {"key": "value"}
- 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"
- }
- # myDict[key] # retrieve the value associated with that key
- # myDict[key] = value # assign a value to that key
- for key in bestOfXF:
- # "Check out Episode --- or '---'"
- # val = bestOfXF[key]
- print("Check out Episode {} or '{}'".format(key, bestOfXF[key]))
- # range()
- for num in range(0, 5): # [0, 1, 2, 3, 4]
- print(num)
- # range() of len() of list
- myList.append("Queequeg")
- for i in range(0, len(myList)):
- print("{} - {}".format(i, myList[i]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement