Advertisement
jspill

webinar-for-loops-2024-06-01

Jun 1st, 2024
758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.70 KB | None | 0 0
  1. # # 2024 June 01
  2. # # WEBINAR: For Loops
  3. #
  4. # # We use loops to repeat actions
  5. # a WHILE loop... is an IF that repeats as long as the loop condition remains True
  6.  
  7. # FOR LOOPS are used for repeating actions for every element in a container (list, dict, tuple, str)
  8.  
  9. # Basic syntax of a for loop
  10. # for ___ in _someContainer__:
  11.  
  12. # # LIST
  13. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]
  14. for item in myList:
  15.     print(item)
  16.  
  17. # TUPLE
  18. myTuple = ("Gilligan", "Castaway002", "red", "crew")
  19. for item in myTuple:
  20.     print(item)
  21.  
  22. # STRING
  23. myString = "It was the best of times."
  24. for char in myString:
  25.     print(char)
  26.  
  27. # DICT
  28. # myDict = {
  29. #     key: value,
  30. #     key: value,
  31. #     key: value,
  32. # }
  33. # myDict[key] # get the value for that key
  34. # myDict[key] = value # assigns value to that key
  35.  
  36. bestOfXF = {
  37.     "1x00": "Pilot",
  38.     "2x10": "Red Museum",
  39.     "2x14": "Die Hand Die Verletzt",
  40.     "3x04": "Clyde Bruckman's Final Repose",
  41.     "3x12": "War of the Coprophages",
  42.     "3x20": "Jose Chung's From Outer Space",
  43.     "4x05": "The Field Where I Died",
  44.     "5x05": "The Post Modern Prometheus",
  45.     "5x17": "All Souls"
  46. }
  47. for key in bestOfXF:
  48.     # "Check out Episode ___ or '___'"
  49.     print(f"Check out Episode {key} or '{bestOfXF[key]}'.")
  50.  
  51. # Loop a known of number of times
  52. # RANGE
  53. # range(start=0, stop, step=1):
  54. # range(5) # range(0, 5)... [0, 1, 2, 3, 4]
  55. # for num in range(5):
  56. #     print(num)
  57.  
  58. for i in range(len(myList)):
  59.     #print(i)
  60.     print(f"{i} --> {myList[i]}")
  61.  
  62. # or loop a known number of times with enumerate()
  63. # here you get 2 variables, the index and the value at that index
  64. for i, item in enumerate(myList):
  65.     print(f"{i} ... {item}")
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement