Advertisement
jspill

webinar-for-loops-2021-02-05

Feb 5th, 2022
1,634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.49 KB | None | 0 0
  1. # 2022 Feb5
  2. # WEBINAR: For Loops
  3. # We use loops to repeat actions
  4.  
  5. # a WHILE loop... btw.. is basically an IF statement
  6. # that repeats as long as its condition is true
  7.  
  8. # FOR LOOPS are used for repeating actions for every element
  9. # in a container like a list, string, tuple, etc...
  10.  
  11. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]
  12. # for __ in ___:
  13. for item in myList:
  14.     print(item)
  15.  
  16. myTuple = ("Gilligan", "Castaway002", "red", "crew")
  17. for item in myTuple:
  18.     print(item)
  19.  
  20. myString = "Just sit right back and you'll hear a tale."
  21. for char in myString:
  22.     print(char, end="-") # whenever you override end parameter of print()...
  23. # print("the next thing")
  24. print() # get a clean line... see Ch 2.9
  25. print("the next thing")
  26.  
  27. # looping over dictionaries
  28. scoobiesDCT = {
  29.     "Scooby": "a blue collar",
  30.     "Shaggy": "green",
  31.     "Velma": "orange",
  32.     "Daphne": "purple",
  33.     "Fred": "an ascot"
  34. }
  35. for key in scoobiesDCT: # for loop var with dicts is the KEY
  36.     print(f"{key} wears {scoobiesDCT[key]}.")
  37.     # f string... same as....
  38.     # print("{} wears {}.".format(key, scoobiesDCT[key]))
  39.  
  40. # myDictionary[someKey] --> retrieve the value for that key
  41. # myDictionary[someKey] = value
  42.  
  43. # range()
  44. for n in range(0, 5):
  45.     print(f"{n} hey")
  46.  
  47. # using range() to look at index of list items
  48. myList.append("Krycek")
  49. for i in range(0, len(myList)):
  50.     print("{}: {}".format(i, myList[i]))
  51.  
  52. # # could do same with enumerate()
  53. # for i, item in enumerate(myTuple):
  54. #     print("{}: {}".format(i, myTuple[i]))
  55.  
  56. numList = [0, 1, 2, 3, 4] # 0 -> 1 -> 2 -> 3 -> 4
  57. for i in range(0, len(numList)):
  58.     if i < len(numList) - 1:
  59.         print("{} ->".format(i), end=" ") # most of time, but not on the last
  60.     else:
  61.         print(i)
  62. # print() # print on else above makes this unnecessary
  63.  
  64.  
  65. # We use for loops when looping over line by line data from files...
  66.  
  67. # I have a CSV file named "mock_data.csv"
  68. with open("mock_data.csv", "r") as f:
  69.     # read() --> returns whole file as one big str
  70.     # readlines() --> returns you a line by line list of strings
  71.    
  72.     # print(f.readlines()) # you'll see all those \n returns in each string
  73.     for line in f.readlines():
  74.         # print(type(line))
  75.         # print(line)
  76.         # myInputVar = input().rstrip()
  77.         line = line.rstrip()
  78.         # print(line.split(","))
  79.         lineList = line.split(",")
  80.         # print(lineList[1]) # first names
  81.         print("{}'s email is {}.".format(lineList[1], lineList[3]))
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement