Advertisement
jspill

webinar-for-loops-2022-09-03

Sep 3rd, 2022
1,226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.35 KB | None | 0 0
  1. # 2022 Sept 3
  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 of a for loop
  12. # for ___ in __someContainer__:
  13. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]
  14. for item in myList:
  15.     print(item) # print(item, end="\n")
  16.  
  17.  
  18. # if you override the end of print()... wrap it up after
  19. # for item in myList:
  20. #     print(item, end=" ") # print(item, end="\n")
  21. # print()
  22.  
  23. myTuple = ("Gilligan", "Castaway002", "red", "crew")
  24. for item in myTuple:
  25.     print(item)
  26.  
  27. myString = "It was the best of times. It was the worst of times."
  28. # for char in myString:
  29. #     print(char)
  30.  
  31. # a lot of string methods... isMethods()... return True or False on an entire string
  32. # isupper() # returns True only if the WHOLE STRING is uppercase
  33. # isspace() # returns True only if the WHOLE STRING is a whitespace
  34.  
  35. # know your whitespace...
  36. # " " # space from spacebar
  37. # "\n" # new line return
  38. # "\t" # tab
  39. # "\r" # carriage return
  40. # "\f" # form feed
  41.  
  42. for char in myString:
  43.     if not char.isspace():
  44.         print(char)
  45.  
  46. # "fill the basket" variation on that...
  47. newStr = ""
  48. for char in myString:
  49.     if not char.isspace():
  50.         newStr += char
  51. print(newStr)
  52.  
  53. # for loop over a dictionaries
  54. # myDict[key] # retrieve the value for that key
  55. # myDict[key] = value # assign a value to that key
  56.  
  57. bestOfXF = {
  58.     "1x00": "Pilot",
  59.     "2x10": "Red Museum",
  60.     "2x14": "Die Hand Die Verletzt",
  61.     "3x04": "Clyde Bruckman's Final Repose",
  62.     "3x12": "War of the Coprophages",
  63.     "3x20": "Jose Chung's From Outer Space",
  64.     "4x05": "The Field Where I Died",
  65.     "5x05": "The Post Modern Prometheus",
  66.     "5x17": "All Souls"
  67. }
  68. for key in bestOfXF: # loop var with a DICT holds the KEY, regardless of what we call it
  69.     # print Check out Episode KEY or 'VALUE'
  70.     print("Check out Episode {} or '{}'!".format(key, bestOfXF[key]))
  71.  
  72. for key in bestOfXF: # loop var with a DICT holds the KEY, regardless of what we call it
  73.     v = bestOfXF[key]
  74.     print("Check out Episode {} or... '{}'!".format(key, v))
  75.  
  76. for k, v in bestOfXF.items(): # 2 variables! key, value
  77.     print("Check out Episode {}, which is called '{}'!".format(k, v))
  78.     # print("Check out Episode {} or '{}'!".format(k, bestOfXF[k]))
  79.  
  80. # why does items() work like that?
  81. # print(bestOfXF.items()) #...
  82. # dict_items([('1x00', 'Pilot'), ('2x10', 'Red Museum'),
  83. #             ('2x14', 'Die Hand Die Verletzt'), ('3x04', "Clyde Bruckman's Final Repose"),
  84. #             ('3x12', 'War of the Coprophages'), ('3x20', "Jose Chung's From Outer Space"),
  85. #             ('4x05', 'The Field Where I Died'), ('5x05', 'The Post Modern Prometheus'),
  86. #             ('5x17', 'All Souls')])
  87.  
  88. a, b, c = [1, 2, 3]
  89. print(a, b, c)
  90.  
  91. # RANGE
  92. # the RANGE object and just doing things some # of times
  93. for num in range(0, 4): # sorta like [0, 1, 2, 3]
  94.     print(num)
  95.  
  96. # combine RANGE with LENGTH
  97. # in other languages for (var i=0; i < myArray.length(); i++)
  98. for i in range(len(myList)): # for i in range(0, len(myList), 1)
  99.     print(f"{i} - {myList[i]}")
  100.  
  101. # compare THIS INDEX and NEXT
  102. for i in range(len(myList)-1):
  103.     print(f"Compare {myList[i]} and {myList[i+1]}...")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement