Advertisement
jspill

webinar-for-loops-2023-05-06

May 6th, 2023 (edited)
1,181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.72 KB | None | 0 0
  1. # 2023 May 6
  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
  9. # in a container like a list, string, tuple, etc...
  10.  
  11. # Basic syntax of a for loop
  12. # for __someVar__ in __someContainer__:
  13.  
  14. # list
  15. myList = ["Agent Scully", "Agent Mulder", "Walter Skinner", "CSM", "Mr. X"]
  16. for item in myList:
  17.     print(item)
  18.  
  19. # strings
  20. myString = "It was the best of times."
  21. # for char in myString:
  22. #     print(char, end="-")
  23. # print() # if you ever override end, make sure to get the "normal" line return back
  24. # print("This should start on a clean line by itself.") # check yourself! Delete or comment out later
  25.  
  26. # for char in myString:
  27. #     if not char.isspace():
  28. #         print(char)
  29.  
  30. # tuples
  31. myTuple = ("Gilligan", "Castaway002", "red", "crew")
  32. for item in myTuple:
  33.     print(item)
  34.  
  35. # dictionaries
  36. # myDict = {"key": "value"}
  37. # myDict[key] # retrieves the value for the key
  38. # myDict[key] = value # assign a (new) value to a key
  39. bestOfXF = {
  40.     "1x00": "Pilot",
  41.     "2x10": "Red Museum",
  42.     "2x14": "Die Hand Die Verletzt",
  43.     "3x04": "Clyde Bruckman's Final Repose",
  44.     "3x12": "War of the Coprophages",
  45.     "3x20": "Jose Chung's From Outer Space",
  46.     "4x05": "The Field Where I Died",
  47.     "5x05": "The Post Modern Prometheus",
  48.     "5x17": "All Souls"
  49. }
  50.  
  51. for key in bestOfXF: # what does the loop var hold?
  52.     # value for that key... bestOfXF[key]
  53.     # value = bestOfXF[key] # you could assign a var for that if you wanted to
  54.     print("Check out Episode {} or '{}'".format(key, bestOfXF[key]))
  55.  
  56. # range()
  57. for num in range(0, 5): # [0, 1, 2, 3, 4]
  58.     print(num)
  59.  
  60. # Interested in the index and the item/value in a list???
  61. myList.append("Queequeg")
  62. myList.append("Krycek")
  63.  
  64. for i in range(0, len(myList)):
  65.     print("{} - {}".format(i, myList[i]))
  66.  
  67. # you can use enumerate()
  68. for i, item in enumerate(myList):
  69.     print("{} > {}".format(i, item))
  70.  
  71. # Student Questions
  72. # # question on reversing (not really about For Loops...)
  73. # myStr = "Hello"
  74. # revStr = myStr[::-1] # myStr[start:stop:step]
  75.  
  76. # Lab 33.10
  77. ''' Type your code here. '''
  78. user_input = input()
  79. quitCommands = ["Done", "done", "d"] # I like to make a list of sentinel values if there's more than 1
  80.  
  81. # while user_input != "quit": # if there was only one quit command for a sentinel value
  82. # check individually as separate conditions...
  83. # while user_input != "Done" and user_input != "done" and user_input != "d":
  84. # or...
  85. while not user_input in quitCommands:
  86.     # do our stuff
  87.     print(user_input[::-1])
  88.     # at the end, get the next input for the loop's next iteration
  89.     user_input = input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement