Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 2022 Mar 12
- # 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 and structure and a FOR LOOP:
- # for __ in _someContainer_:
- # for _item_ in _someContainer_:
- myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]
- for item in myList:
- print(item)
- myTuple = ("Gilligan", "Castaway002", "red", "crew")
- for item in myTuple:
- print(item)
- myString = "Just sit right back and you'll hear a tale."
- for char in myString:
- # print(char) # print(char, end="\n")
- print(char, end=" ")
- print() # always follow up overriding end with a "regular" print() call
- print("hey")
- # looping over dictionaries
- scoobiesDCT = {
- "Scooby": "a blue collar",
- "Shaggy": "green",
- "Velma": "orange",
- "Daphne": "purple",
- "Fred": "an ascot"
- }
- # myDictionary[someKey] --> retrieve the value for that key
- # myDictionary[someKey] = value
- for key in scoobiesDCT: # key, k... those are good for loop vars for a dictionary
- # value = scoobiesDCT[key]
- print("{} wears {}.".format(key, scoobiesDCT[key]))
- # myVar = input().rstrip()
- # if myVar in myDictionary: # is this key even in this dictionary?
- for k, v in scoobiesDCT.items(): # key, value
- # print("{} always wears {}.".format(k, v))
- print(scoobiesDCT[k]) # print(v)
- print(scoobiesDCT.values())
- # How do we repeat just "some number" of times?
- # for n in 5: # can't do this! Integers are not iterable
- for n in range(0, 5):
- print(n)
- # looping over a list or other container and you want the INDEX
- # loop over the range() of the len()
- for i in range(0, len(myList)):
- print("{}: {}".format(i, myList[i]))
- for i in range(0, len(myList)):
- if i < len(myList) - 1: # not the last one
- print("{} ->".format(myList[i]), end=" ")
- else: # so... the last one
- print(myList[i])
- # we could use str join() to do the same thing
- print("-->".join(myList))
- # question on delimiter strings... could be literal or variable
- myDelim = " "
- print(myDelim.join(myList))
- # Advanced... Ch 13 and after
- # We use for loops when looping over line by line data from files...
- # I have a CSV file named "mock_data.csv"
- with open("mock_data.csv", "r") as f:
- # read() --> returns whole file as one big str
- # readlines() --> returns you a line by line list of strings
- # for line in f: # but I like known container types...
- contents = f.readlines()
- # print(contents)
- for line in contents:
- line = line.rstrip()
- print(line) # end="\n"
- lineList = line.split(",")
- print(lineList)
- print("{} is using IP {}".format(lineList[3], lineList[4]))
- # help() and dir() are always your friends, and work great in the exam:
- help(str)
- help(list)
- print(dir(dict))
- help(dict.get) # use dir() to zero in on a specific method or property
- # question on whitespace characters
- # use strip() or rstrip() to remove from input strings
- myVar = input().rstrip() # " ", "\n", "\r", "\f", "\t", plus other Unicode space variations
- myVar = int(input().rstrip())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement