Advertisement
jspill

webinar-for-loops-2022-03-12

Mar 12th, 2022
1,478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.19 KB | None | 0 0
  1. # 2022 Mar 12
  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. # Basic syntax and structure and a FOR LOOP:
  12. # for __ in _someContainer_:
  13. # for _item_ in _someContainer_:
  14. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]
  15. for item in myList:
  16.     print(item)
  17.  
  18. myTuple = ("Gilligan", "Castaway002", "red", "crew")
  19. for item in myTuple:
  20.     print(item)
  21.  
  22. myString = "Just sit right back and you'll hear a tale."
  23. for char in myString:
  24.     # print(char) # print(char, end="\n")
  25.     print(char, end=" ")
  26. print() # always follow up overriding end with a "regular" print() call
  27. print("hey")
  28.  
  29. # looping over dictionaries
  30. scoobiesDCT = {
  31.     "Scooby": "a blue collar",
  32.     "Shaggy": "green",
  33.     "Velma": "orange",
  34.     "Daphne": "purple",
  35.     "Fred": "an ascot"
  36. }
  37. # myDictionary[someKey] --> retrieve the value for that key
  38. # myDictionary[someKey] = value
  39. for key in scoobiesDCT: # key, k... those are good for loop vars for a dictionary
  40.     # value = scoobiesDCT[key]
  41.     print("{} wears {}.".format(key, scoobiesDCT[key]))
  42.  
  43. # myVar = input().rstrip()
  44. # if myVar in myDictionary: # is this key even in this dictionary?
  45.  
  46. for k, v in scoobiesDCT.items(): # key, value
  47.     # print("{} always wears {}.".format(k, v))
  48.     print(scoobiesDCT[k]) # print(v)
  49.  
  50. print(scoobiesDCT.values())
  51.  
  52. # How do we repeat just "some number" of times?
  53. # for n in 5: # can't do this! Integers are not iterable
  54. for n in range(0, 5):
  55.     print(n)
  56.  
  57. # looping over a list or other container and you want the INDEX
  58. # loop over the range() of the len()
  59. for i in range(0, len(myList)):
  60.     print("{}: {}".format(i, myList[i]))
  61. for i in range(0, len(myList)):
  62.     if i < len(myList) - 1: # not the last one
  63.         print("{} ->".format(myList[i]), end=" ")
  64.     else: # so... the last one
  65.         print(myList[i])
  66. # we could use str join() to do the same thing
  67. print("-->".join(myList))
  68.  
  69. # question on delimiter strings... could be literal or variable
  70. myDelim = "   "
  71. print(myDelim.join(myList))
  72.  
  73. # Advanced... Ch 13 and after
  74. # We use for loops when looping over line by line data from files...
  75. # I have a CSV file named "mock_data.csv"
  76. with open("mock_data.csv", "r") as f:
  77.     # read() --> returns whole file as one big str
  78.     # readlines() --> returns you a line by line list of strings
  79.     # for line in f: # but I like known container types...
  80.     contents = f.readlines()
  81. # print(contents)
  82. for line in contents:
  83.     line = line.rstrip()
  84.     print(line) # end="\n"
  85.     lineList = line.split(",")
  86.     print(lineList)
  87.     print("{} is using IP {}".format(lineList[3], lineList[4]))
  88.  
  89. # help() and dir() are always your friends, and work great in the exam:
  90. help(str)
  91. help(list)
  92. print(dir(dict))
  93. help(dict.get) # use dir() to zero in on a specific method or property
  94.  
  95. # question on whitespace characters
  96. # use strip() or rstrip() to remove from input strings
  97. myVar = input().rstrip() # " ", "\n", "\r", "\f", "\t", plus other Unicode space variations
  98. myVar = int(input().rstrip())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement