Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 2022 Sept 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 ___ in __someContainer__:
- myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]
- for item in myList:
- print(item) # print(item, end="\n")
- # if you override the end of print()... wrap it up after
- # for item in myList:
- # print(item, end=" ") # print(item, end="\n")
- # print()
- myTuple = ("Gilligan", "Castaway002", "red", "crew")
- for item in myTuple:
- print(item)
- myString = "It was the best of times. It was the worst of times."
- # for char in myString:
- # print(char)
- # a lot of string methods... isMethods()... return True or False on an entire string
- # isupper() # returns True only if the WHOLE STRING is uppercase
- # isspace() # returns True only if the WHOLE STRING is a whitespace
- # know your whitespace...
- # " " # space from spacebar
- # "\n" # new line return
- # "\t" # tab
- # "\r" # carriage return
- # "\f" # form feed
- for char in myString:
- if not char.isspace():
- print(char)
- # "fill the basket" variation on that...
- newStr = ""
- for char in myString:
- if not char.isspace():
- newStr += char
- print(newStr)
- # for loop over a dictionaries
- # myDict[key] # retrieve the value for that key
- # myDict[key] = value # assign a value to that key
- 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: # loop var with a DICT holds the KEY, regardless of what we call it
- # print Check out Episode KEY or 'VALUE'
- print("Check out Episode {} or '{}'!".format(key, bestOfXF[key]))
- for key in bestOfXF: # loop var with a DICT holds the KEY, regardless of what we call it
- v = bestOfXF[key]
- print("Check out Episode {} or... '{}'!".format(key, v))
- for k, v in bestOfXF.items(): # 2 variables! key, value
- print("Check out Episode {}, which is called '{}'!".format(k, v))
- # print("Check out Episode {} or '{}'!".format(k, bestOfXF[k]))
- # why does items() work like that?
- # print(bestOfXF.items()) #...
- # dict_items([('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')])
- a, b, c = [1, 2, 3]
- print(a, b, c)
- # RANGE
- # the RANGE object and just doing things some # of times
- for num in range(0, 4): # sorta like [0, 1, 2, 3]
- print(num)
- # combine RANGE with LENGTH
- # in other languages for (var i=0; i < myArray.length(); i++)
- for i in range(len(myList)): # for i in range(0, len(myList), 1)
- print(f"{i} - {myList[i]}")
- # compare THIS INDEX and NEXT
- for i in range(len(myList)-1):
- print(f"Compare {myList[i]} and {myList[i+1]}...")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement