Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 2022 Apr 9
- # 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)
- myTuple = ("Gilligan", "Castaway002", "red", "crew") # return x, y
- for item in myTuple:
- print(item) # print(end="\n")
- myString = "Just sit right back and you'll hear a tale."
- for char in myString:
- print(char, end="-")
- print() # if you use end param of print(), print() again to get a clean line
- # looping over dictionaries
- scoobiesDCT = { # key: value pairs
- "Scooby": "a blue collar",
- "Shaggy": "green",
- "Velma": "orange",
- "Daphne": "purple",
- "Fred": "an ascot"
- }
- # you can use the dict KEY to get its VALUE
- # myDictionary[key] # retrieve the value for that key... kinda like dict.get()
- # myDictionary[key] = value # set a new value, kinda like dict.update()
- for key in scoobiesDCT: # for loop var with dicts is the KEY
- value = scoobiesDCT[key]
- print("{} wears {}.".format(key, value))
- # loop some number of times... with range()
- # for n in 5: # can't do that! 5 is not iterable.
- for n in range(0, 5):
- print(n)
- # using range() to look at index of list items
- myList.append("Krycek")
- for i in range(len(myList)):
- print("{}: {}".format(i, myList[i]))
- for i, item in enumerate(myList):
- print("{}--> {}".format(i, item))
- # CA 6.8.2
- # Given num_rows and num_cols, print a list of all seats in a theater. Rows are numbered, columns lettered, as in 1A or 3E. Print a space after each seat.
- # Sample output with inputs: 2 3
- # 1A 1B 1C 2A 2B 2C
- num_rows = 2 # int(input())
- num_cols = 3 # int(input())
- # x += 1
- # string incrementing
- # ord("a") + 1
- # chr(ord("a") + 1) # --< "b"
- for row in range(1, num_rows+1):
- ltr = "A"
- for col in range(0, num_cols):
- print("{}{}".format(row, ltr), end=" ")
- ltr = chr(ord(ltr) + 1)
- print()
- for i in range(0, 513):
- print("{}: {}".format(i, chr(i)))
- 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
- contents = f.readlines()
- # print(contents)
- for line in contents:
- line = line.rstrip()
- # print(line)
- lineList = line.split(",")
- # print(lineList[3])
- print("{} is coming from: {}".format(lineList[3], lineList[4]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement