Advertisement
jspill

webinar-for-loops-2024-04-06

Apr 6th, 2024
721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.71 KB | None | 0 0
  1. # # 2024 Apr 06
  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. # # STR
  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. # looping a known number of times
  52. # RANGE
  53. # range(start=0, stop, step=1)
  54. # for num in range(5): # [0, 1, 2, 3, 4]
  55. #     print(num)
  56.  
  57. # if we're interested in the INDEX, we can loop over the RANGE of the LENGTH
  58. for i in range(0, len(myList), 1):
  59.     print(f"{i}: {myList[i]}")
  60.  
  61. # another way to loop focused on the INDEX is using ENUMERATE
  62. # for i, item in enumerate(myList):
  63. #     print(f"{i}--> {item}")
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement