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... 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 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)
- # 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) # print(char, end="\n")
- # 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] # retrieves the value associated with the key
- # bestOfXF["1x00"] # "Pilot"
- # # myDict[key] = value # assign a value to that key
- for key in bestOfXF:
- 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()
- myList.append("Queequeg")
- for i in range(0, len(myList)):
- print("{} - {}".format(i, myList[i]))
- # Student Questions
- # nested loops
- # inner loop cycles completes in entirety once PER every cylce of outer loop, like multiplication...
- # one of these for every one of those
- rows = [1, 2, 3]
- cols = ["a", "b", "c", "d", "e"]
- for r in rows:
- for c in cols:
- print("{}-{}".format(r, c))
- # 1-a
- # 1-b
- # 1-c
- # 1-d
- # 1-e
- # 2-a
- # 2-b
- # 2-c
- # 2-d
- # 2-e
- # 3-a
- # 3-b
- # 3-c
- # 3-d
- # 3-e
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement