Advertisement
jspill

webinar-for-loops-2023-05-03

Jun 3rd, 2023
1,015
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.37 KB | None | 0 0
  1. # 2023 June 3
  2. # WEBINAR: For Loops
  3.  
  4. # We use loops to repeat actions
  5.  
  6. # a WHILE loop... is an IF that repeats as long as the loop condition remains True
  7.  
  8. # FOR LOOPS are used for repeating actions for every element in a container (list, str, tuples, sets, dictionary, range objects)
  9.  
  10. # Basic syntax of a for loop
  11. # for ___ in _someContainer_:
  12.  
  13. # list
  14. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]
  15. for item in myList:
  16.     print(item) # print(item, end="\n")
  17.  
  18. # tuples
  19. myTuple = ("Gilligan", "Castaway002", "red", "crew")
  20. for item in myTuple:
  21.     print(item)
  22.  
  23. # strings
  24. myString = "It was the best of times."
  25. # for char in myString:
  26. #     print(char)
  27.  
  28.  
  29. # print(char)
  30. # print(char, end="\n")
  31. for char in myString:
  32.     #print(char) # print(char, end="\n")
  33.     print(char, end="->")
  34. print() # I add this!
  35. print("I wish this was on a clean new line by itself.")
  36.  
  37. # Ch 2.9 and Lab 2.12
  38.  
  39. # dictionaries
  40. # myDict = {"key": "value"}
  41. #
  42. # for key in myDict: # loop var in dict for loop holds the KEY
  43. #     myDict[key] # retrieves value for that key
  44. #     myDict[key] = value # assign a (new) value to key
  45. bestOfXF = {
  46.     "1x00": "Pilot",
  47.     "2x10": "Red Museum",
  48.     "2x14": "Die Hand Die Verletzt",
  49.     "3x04": "Clyde Bruckman's Final Repose",
  50.     "3x12": "War of the Coprophages",
  51.     "3x20": "Jose Chung's From Outer Space",
  52.     "4x05": "The Field Where I Died",
  53.     "5x05": "The Post Modern Prometheus",
  54.     "5x17": "All Souls"
  55. }
  56. for key in bestOfXF:
  57.     # "Check out Episode ___ or '___'"
  58.     print("Check out Episode {} or '{}'".format(key, bestOfXF[key])) # value is bestOfXF[key]... myList[0]
  59.  
  60. # help(str.format)
  61.  
  62. # range objects... to just to do something X number of times
  63. for num in range(0, 5): # [0, 1, 2, 3, 4]
  64.     print(num)
  65.  
  66. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]
  67. # if I need to know the index, then
  68. # for item in myList: # doesn't help!
  69. for i in range(0, len(myList)):
  70.     print("{}: {}".format(i, myList[i]))
  71.  
  72. # could also use ENUMERATE
  73. for i, item in enumerate(myList):
  74.     print("{} - {}".format(i, item))
  75.  
  76. for i in range(0, len(myList)):
  77.     if i < len(myList) - 1:
  78.         print("{} vs ".format(myList[i]), end="")
  79.     else: # must be the last one!
  80.         print(myList[i])
  81.  
  82. print("I wish this was on a clean new line by itself.")
  83.  
  84. print(" vs ".join(myList))
  85.  
  86.  
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement