Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 2022 Dec 3
- # 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 _something_ 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. \nIt was the\t worst of times."
- for char in myString:
- print(char, end="") # print(end="\n")
- print()
- # test to see if you have a clean new line
- # print("A clean new line.")
- # dictionaries
- 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 for that key
- # myDict[key] = value # assign a value to that key
- for key in bestOfXF: # for key in nameOfDict:
- # print: Check out Episode KEY or 'VALUE'
- value = bestOfXF[key]
- print("Check out Episode {} or '{}'".format(key, value))
- # range()
- for num in range(0, 5): # [0, 1, 2, 3, 4]
- print(num)
- # range() of len()
- for i in range(len(myList)):
- print("{} - {}".format(i, myList[i]))
- # while loops
- # any variables needed in the loop condition must be created outside (they're not tied to the loop itself like a for loop var is)
- # x = int(input()) # 0
- # # if x < 3:
- # # print(x)
- # while x < 3: # has a condition, just like an if statement
- # print(x)
- # x += 1 # the loop variable (or something in the loop condition) must change with each iteration
- # nested loops
- for n in range(3):
- for m in range(4): # entire inner loop iterates for every one interation of outer loop
- print(f"{n}-{m}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement