Advertisement
jspill

webinar-for-loops-2023-11-04

Nov 4th, 2023
910
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. # 2023 Nov 4
  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)
  17.  
  18. # tuple
  19. myTuple = ("Gilligan", "Castaway002", "red", "crew")
  20. for item in myTuple:
  21.     print(item)
  22.  
  23. # string
  24. myString = "It was the best of times."
  25. for char in myString:
  26.     print(char)
  27.  
  28. for char in myString:
  29.     # if not char.isspace():
  30.     if char.isalpha():
  31.         print(char)
  32.  
  33. # for key in myDict:
  34. #     # myDict[key]
  35. #     # myDict[key] = value
  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 k in bestOfXF:
  48.     # "Check out Episode ___ or '___'"
  49.     print(f"Check out Episode {k} or '{bestOfXF[k]}'")
  50.  
  51. # membership check... in
  52. print("5x05" in bestOfXF) # True... b/c key is in dict
  53. print("6x01" in bestOfXF) # False
  54. print("All Souls" in bestOfXF) # False... that is not KEY in this dict
  55.  
  56. # if I wanted to check if something is present as a dict VALUE
  57. # myDict.values() - returns a special container type with just the values
  58. print("All Souls" in bestOfXF.values()) # True
  59.  
  60. # what is the VALUE for this KEY
  61. # we can get that directly: myDict[key]
  62. print(bestOfXF["5x05"])
  63.  
  64. # what is the KEY for this VALUE
  65. # loop through dict and check each value, remember the key where found
  66. # say I got "All Souls" from input(), is it a value and what's the key?
  67. for key in bestOfXF:
  68.     if bestOfXF[key] == "All Souls":
  69.         print(key)
  70. # if I make it this far without finding it, I can say it's not there
  71.  
  72. # range()... a range object, which we use to loop a number of times
  73. # range(0, 5) # [0, 1, 2, 3, 4]
  74. for num in range(5):
  75.     print(num)
  76.  
  77. # using range() to correspond to index
  78. for i in range(len(myList)):
  79.     # print(myList[i])
  80.     print(f"Position {i} is {myList[i]}")
  81.  
  82. # or... use enumerate() to get index and value
  83. for i, item in enumerate(myList):
  84.     print(f"Position {i} is {item}")
  85.  
  86.  
  87.  
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement